<commit_before>#$Revision: 1.39 $$Date: 2004/12/04 20:13:28 $$Author: wsnyder $
######################################################################
#
# Copyright 2001-2004 by <NAME>.  This program is free software;
# you can redistribute it and/or modify it under the terms of either the GNU
# General Public License or the Perl Artistic License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
######################################################################

package Verilog::Preproc;
use Carp;
use Verilog::Getopt;

require DynaLoader;
@ISA = qw(DynaLoader);

use strict;
use vars qw($VERSION);

$VERSION = '2.303';

######################################################################
#### Configuration Section

bootstrap Verilog::Preproc;

######################################################################
#### Accessors

sub new {
    my $class = shift;  $class = ref $class if ref $class;
    my $self = {keep_comments=>1,
		line_directives=>1,
		pedantic=>0,
		options=>Verilog::Getopt->new(),	# If the user didn't give one, still work!
		@_};
    bless $self, $class;
    # Sets $self->{_cthis}
    $self->{keep_comments} = 2 if ($self->{keep_comments} eq 'sub');
    $self->_new(
		$self,
		$self->{keep_comments},
		$self->{line_directives},
		$self->{pedantic},
		);
    #use Data::Dumper; print Dumper($self);
    return $self;
}

sub open {
    my $self = shift;
    my $filename = shift;
    # Allow user to put `defined names on the command line instead of filenames,
    # then convert them properly.
    $filename = $self->remove_defines($filename);
    printf ("Perl open $filename\n") if $self->{debug};
    $filename = $self->{options}->file_path($filename);
    printf ("Perl openfp $filename\n") if $self->{debug};
    if (!-r $filename) {
	$self->error("Cannot open $filename");
    } else {
	$self->_open($filename);
    }
}

sub debug {
    my $self = shift;
    my $level = shift;
    $self->{debug} = $level;
    $self->_debug($level);
}

#In Preproc.xs:
# sub _new (class, keepcmt, linedir, pedantic)
# sub _open (class)
# sub getline (class)
# sub eof (class)
# sub filename (class)
# sub lineno (class)
# sub unreadback (class, text)

######################################################################
#### Utilities

sub remove_defines {
    my $self = shift;
    my $sym = shift;
    my $val = "x";
    while (defined $val) {
	last if $sym eq $val;
	(my $xsym = $sym) =~ s/^\`//;
	$val = $self->{options}->defvalue_nowarn($xsym);  #Undef if not found
	$sym = $val if defined $val;
    }
    return $sym;
}

######################################################################
#### Called by the parser

sub error {
    my ($self,$text,$token)=@_;
    my $fileline = $self->filename.":".$self->lineno;
    croak ("%Error: $fileline: $text\n"
	   ."Stopped");
}

sub comment {}

sub include {
    my ($self,$filename)=@_;
    print "INCLUDE $filename\n" if $self->{debug};
    $self->open($filename);
}

# Note rather then overriding these, a derived Verilog::Getopt class can
# accomplish the same thing.

sub undef {
    my $self = shift;
    $self->{options}->undef(@_);
}
sub define {
    my $self = shift;
    #print "DEFINE @_\n";
    $self->{options}->fileline($self->filename.":".$self->lineno);
    $self->{options}->define(@_);
}
sub def_params {
    # Return define parameters
    my $self = shift;
    my $val = $self->{options}->defparams(@_);
    #printf "DEFEXISTS @_ -> %s\n", $val if $self->{debug};
    $val = "" if !defined $val;
    return $val;
}
sub def_value {
    # Return value
    my $self = shift;
    #printf "DEFVALUE @_ -> %s\n", $self->{options}->defvalue_nowarn(@_);
    return $self->{options}->defvalue(@_);
}

######################################################################
#### Package return
1;
__END__

=pod

=head1 NAME

Verilog::Preproc - Preprocess Verilog files Files

=head1 SYNOPSIS

    use Verilog::Getopt;

    my $vp = Verilog::Preproc->new(I<parameters>);
    $vp->open("verilog_file.v");
    my $line = $vp->getline();

=head1 EXAMPLE

    # This is a complete verilog pre-parser!
    # For a command line version, see vppp
    use Verilog::Getopt;
    use Verilog::Preproc;

    my $opt = new Verilog::Getopt;
    @ARGV = $opt->parameter(@ARGV);

    my $vp = Verilog::Preproc->new(options=>$opt,);
    $vp->open("verilog_file.v");
    while (defined (my $line = $vp->getline())) {
       print $line;
    }

=head1 DESCRIPTION

L<Verilog::Preproc> reads Verilog files, and preprocesses them according
to the Verilog 2001 specification.  Programs can be easily converted from<commit_msg>Fix typo<commit_after>#$Revision: 1.40 $$Date: 2004/12/08 22:20:01 $$Author: wsnyder $
######################################################################
#
# Copyright 2001-2004 by <NAME>.  This program is free software;
# you can redistribute it and/or modify it under the terms of either the GNU
# General Public License or the Perl Artistic License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
######################################################################

package Verilog::Preproc;
use Carp;
use Verilog::Getopt;

require DynaLoader;
@ISA = qw(DynaLoader);

use strict;
use vars qw($VERSION);

$VERSION = '2.303';

######################################################################
#### Configuration Section

bootstrap Verilog::Preproc;

######################################################################
#### Accessors

sub new {
    my $class = shift;  $class = ref $class if ref $class;
    my $self = {keep_comments=>1,
		line_directives=>1,
		pedantic=>0,
		options=>Verilog::Getopt->new(),	# If the user didn't give one, still work!
		@_};
    bless $self, $class;
    # Sets $self->{_cthis}
    $self->{keep_comments} = 2 if ($self->{keep_comments} eq 'sub');
    $self->_new(
		$self,
		$self->{keep_comments},
		$self->{line_directives},
		$self->{pedantic},
		);
    #use Data::Dumper; print Dumper($self);
    return $self;
}

sub open {
    my $self = shift;
    my $filename = shift;
    # Allow user to put `defined names on the command line instead of filenames,
    # then convert them properly.
    $filename = $self->remove_defines($filename);
    printf ("Perl open $filename\n") if $self->{debug};
    $filename = $self->{options}->file_path($filename);
    printf ("Perl openfp $filename\n") if $self->{debug};
    if (!-r $filename) {
	$self->error("Cannot open $filename");
    } else {
	$self->_open($filename);
    }
}

sub debug {
    my $self = shift;
    my $level = shift;
    $self->{debug} = $level;
    $self->_debug($level);
}

#In Preproc.xs:
# sub _new (class, keepcmt, linedir, pedantic)
# sub _open (class)
# sub getline (class)
# sub eof (class)
# sub filename (class)
# sub lineno (class)
# sub unreadback (class, text)

######################################################################
#### Utilities

sub remove_defines {
    my $self = shift;
    my $sym = shift;
    my $val = "x";
    while (defined $val) {
	last if $sym eq $val;
	(my $xsym = $sym) =~ s/^\`//;
	$val = $self->{options}->defvalue_nowarn($xsym);  #Undef if not found
	$sym = $val if defined $val;
    }
    return $sym;
}

######################################################################
#### Called by the parser

sub error {
    my ($self,$text,$token)=@_;
    my $fileline = $self->filename.":".$self->lineno;
    croak ("%Error: $fileline: $text\n"
	   ."Stopped");
}

sub comment {}

sub include {
    my ($self,$filename)=@_;
    print "INCLUDE $filename\n" if $self->{debug};
    $self->open($filename);
}

# Note rather then overriding these, a derived Verilog::Getopt class can
# accomplish the same thing.

sub undef {
    my $self = shift;
    $self->{options}->undef(@_);
}
sub define {
    my $self = shift;
    #print "DEFINE @_\n";
    $self->{options}->fileline($self->filename.":".$self->lineno);
    $self->{options}->define(@_);
}
sub def_params {
    # Return define parameters
    my $self = shift;
    my $val = $self->{options}->defparams(@_);
    #printf "DEFEXISTS @_ -> %s\n", $val if $self->{debug};
    $val = "" if !defined $val;
    return $val;
}
sub def_value {
    # Return value
    my $self = shift;
    #printf "DEFVALUE @_ -> %s\n", $self->{options}->defvalue_nowarn(@_);
    return $self->{options}->defvalue(@_);
}

######################################################################
#### Package return
1;
__END__

=pod

=head1 NAME

Verilog::Preproc - Preprocess Verilog files

=head1 SYNOPSIS

    use Verilog::Getopt;

    my $vp = Verilog::Preproc->new(I<parameters>);
    $vp->open("verilog_file.v");
    my $line = $vp->getline();

=head1 EXAMPLE

    # This is a complete verilog pre-parser!
    # For a command line version, see vppp
    use Verilog::Getopt;
    use Verilog::Preproc;

    my $opt = new Verilog::Getopt;
    @ARGV = $opt->parameter(@ARGV);

    my $vp = Verilog::Preproc->new(options=>$opt,);
    $vp->open("verilog_file.v");
    while (defined (my $line = $vp->getline())) {
       print $line;
    }

=head1 DESCRIPTION

L<Verilog::Preproc> reads Verilog files, and preprocesses them according
to the Verilog 2001 specification.  Programs can be easily converted from
{'fraction_non_alphanumeric': 0.24825284239073744, 'fraction_numerical': 0.00876186502555544, 'mean_word_length': 3.9499225606608155, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '186594', 'n_tokens_mistral': 3232, 'n_tokens_neox': 3067, 'n_words': 885}