📄 preproc.pm
字号:
# Verilog - Verilog Perl Interface# See copyright, etc in below POD section.######################################################################package Verilog::Preproc;use Carp;use Verilog::Getopt;require DynaLoader;use base qw(DynaLoader);use strict;use vars qw($VERSION);$VERSION = '3.120';########################################################################## Configuration Sectionbootstrap Verilog::Preproc;#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)########################################################################## Accessorssub new { my $class = shift; $class = ref $class if ref $class; my $self = {keep_comments=>1, keep_whitespace=>1, line_directives=>1, pedantic=>0, options=>Verilog::Getopt->new(), # If the user didn't give one, still work! #include_open_nonfatal=>0, @_}; bless $self, $class; # Sets $self->{_cthis} $self->{keep_comments} = 2 if ($self->{keep_comments} eq 'sub'); $self->{keep_comments} = 3 if ($self->{keep_comments} eq 'expand'); #TBD $self->_new($self, $self->{keep_comments}, $self->{keep_whitespace}, $self->{line_directives}, $self->{pedantic}, ); #use Data::Dumper; print Dumper($self); return $self;}sub open { my $self = shift; my %params = ( # filename => # open_nonfatal => 0, ); if ($#_ > 0) { %params=(@_); } else { $params{filename}=shift; } # We allow either open(name) or open(filename=>name); # Allow user to put `defined names on the command line instead of filenames, # then convert them properly. my $filename = $params{filename}; $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) { if (!$params{open_nonfatal}) { $self->error("Cannot open $filename"); } return undef; } else { $self->_open($filename); } return $self;}sub debug { my $self = shift; my $level = shift; $self->{debug} = $level; $self->_debug($level);}########################################################################## Utilitiessub 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;}sub fileline { my $self = shift; return ($self->filename||"").":".($self->lineno||"");}########################################################################## Called by the parsersub 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 => $filename, open_nonfatal => $self->{include_open_nonfatal}, );}# Note rather than 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 return1;__END__=pod=head1 NAMEVerilog::Preproc - Preprocess Verilog files=head1 SYNOPSIS use Verilog::Getopt; my $vp = Verilog::Preproc->new(I<parameters>); $vp->open(filename=>"verilog_file.v"); my $line = $vp->getline();=head1 EXAMPLE # This is a complete verilog pre-parser! # For a command line version, see vppreproc use Verilog::Getopt; use Verilog::Preproc; my $opt = new Verilog::Getopt; @ARGV = $opt->parameter(@ARGV); my $vp = Verilog::Preproc->new(options=>$opt,); $vp->open(filename=>"verilog_file.v"); while (defined (my $line = $vp->getline())) { print $line; }=head1 DESCRIPTIONVerilog::Preproc reads Verilog files, and preprocesses them according tothe SystemVerilog 2005 specification. Programs can be easily convertedfrom reading a IO::File into reading preprocessed output fromVerilog::Preproc.See the "Which Package" section of L<Verilog::Language> if you are unsurewhich parsing package to use for a new application.=head1 MEMBER FUNCTIONS=over 4=item $self->eof()Returns true at the end of the file.=item $self->filename()Returns the filename of the most recently returned getline(). May not matchthe filename passed on the command line, as `line directives are honored.=item $self->getline()Return the next line of text. Returns undef at EOF. (Just likeIO::File->getline().)=item $self->lineno()Returns the line number of the last getline(). Note that the line numbermay change several times between getline(), for example when traversingmultiple include files.=item $self->new(I<parameters>)Creates a new preprocessor. See the PARAMETERS section for the optionsthat may be passed to new.=item $self->open(filename=>I<filename>)Opens the specified file. If called before a file is completely parsed,the new file will be parsed completely before returning to the previouslyopen file. (As if it was an include file.)Open may also be called without named parameters, in which case the onlyargument is the filename.=item $self->unreadback(I<text>)Insert text into the input stream at the given point. The text will notbe parsed, just returned to the application. This lets comment() callbacksinsert special code into the output stream.=back=head1 PARAMETERSThe following named parameters may be passed to the new constructor.=over 4=item include_open_nonfatal=>1With include_open_nonfatal set to one, ignore any include files that donot exist.=item keep_comments=>0With keep_comments set to zero, strip all comments. When set to one (thedefault), insert comments in output streams. When set to 'sub', call thecomment() function so that meta-comments can be processed outside of theoutput stream. Note that some programs use meta-comments to embed usefulinformation (synthesis and lint), so use this with caution if feeding totools other than your own. Defaults to 1.=item keep_whitespace=>0With keep_whitespace set to zero, compress all whitespace to a single spaceor newline. When set to one (the default), retain whitespace. Defaults to1.=item line_directives=>0With line_directives set to zero, suppress "`line" comments which indicatefilename and line number changes. Use the lineno() and filename() methodsinstead to retrieve this information. Defaults true.=item options=>Verilog::Getopt objectSpecifies the object to be used for resolving filenames and defines. Otherclasses may be used, as long as their interface matches that of Getopt.=item pedantic=>1With pedantic set, rigorously obey the Verilog pedantic. This disablesthe `__FILE__ and `__LINE__ features, and may disable other features thatare not specified in the language pedantic. Defaults false.=back=head1 CALLBACKSDefault callbacks are implemented that are suitable for most applications.Derived classes may override these callbacks as needed.=over 4=item $self->comment(I<comment>)Called with each comment, when keep_comments=>'sub' is used. Defaults todo nothing.=item $self->undef(I<defname>)Called with each `undef. Defaults to use options object.=item $self->define(I<defname>, I<value>, I<params>)Called with each `define. Defaults to use options object.=item $self->def_exists(I<defname>)Called to determine if the define exists. Return true if the defineexists, or argument list with leading parenthesis if the define hasarguments. Defaults to use options object.=item $self->def_value(I<defname>)Called to return value to substitute for specified define. Defaults to useoptions object.=item $self->error(I<message>)Called on errors, with the error message as an argument. Defaultsto die.=item $self->include(I<filename>)Specifies a include file has been found. Defaults to call $self->openafter resolving the filename with the options parameter.=back=head1 COMPLIANCEThe preprocessor supports the constructs defined in the Verilog 2005 andSystemVerilog 2005 standards.Verilog::Preproc adds the following features (unless the pedantic parameteris set.):=over 4=item `__FILE__`__FILE__ will be replaced by the current filename. (Like C++ __FILE__.)=item `__LINE__`__LINE__ will be replaced by the current line number. (Like C++ __LINE__.)=item `error I<"string">`error will be reported whenever it is encountered. (Like C++ #error.)These are useful for error macros, similar to assert() in C++.=back=head1 DISTRIBUTIONVerilog-Perl is part of the L<http://www.veripool.org/> free Verilog EDAsoftware tool suite. The latest version is available from CPAN and fromL<http://www.veripool.org/verilog-perl>.Copyright 2000-2009 by Wilson Snyder. This package is free software; youcan redistribute it and/or modify it under the terms of either the GNULesser General Public License or the Perl Artistic License.=head1 AUTHORSWilson Snyder <wsnyder@wsnyder.org>=head1 SEE ALSOL<Verilog-Perl>,L<Verilog::Language>, L<Verilog::Getopt>L<IO::File>This package is layered on a C++ interface which may be found in the kit.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -