?? parser.pm
字號:
# Verilog - Verilog Perl Interface# See copyright, etc in below POD section.######################################################################package Verilog::Parser;use Carp;use Verilog::Getopt;use Verilog::Language;require DynaLoader;use base qw(DynaLoader);use strict;use vars qw($VERSION $Debug);$VERSION = '3.120';#$Debug sets the default value for debug. You're better off with the object method though.########################################################################## Configuration Sectionbootstrap Verilog::Parser;#In Parser.xs:# sub _new (class, sigparser)# sub _open (class)# sub _debug (class, level)# sub parse (class)# sub eof (class)# sub filename (class, [setit])# sub lineno (class, [setit])# sub unreadback (class, [setit])# sub unreadbackCat (class, add)########################################################################## Constructorssub new { my $class = shift; $class = ref $class if ref $class; my $self = {_sigparser=>0, use_unreadback => 1, # Backward compatibility @_}; bless $self, $class; # Sets $self->{_cthis} $self->_new($self, # Options go here $self->{_sigparser}, $self->{use_unreadback}, ); $self->language(Verilog::Language::language_standard()); $self->debug($Debug) if $Debug; return $self;}########################################################################## Accessorssub debug { my $self = shift; my $level = shift; $self->_debug($level) if defined $level;}sub fileline { my $self = shift; return ($self->filename||"").":".($self->lineno||"");}sub line { return lineno(@_); } # Old, now undocumented########################################################################### Called by the parsersub reset { my $self = shift;}sub parse_file { # Read a file and parse @_ == 2 or croak 'usage: $parser->parse_file($filename)'; my $self = shift; my $filename = shift; my $fh = new IO::File; $fh->open($filename) or croak "%Error: $! $filename"; $self->reset(); $self->filename($filename); $self->lineno(1); while (defined(my $line = $fh->getline())) { $self->parse ($line); } $self->eof; $fh->close; return $self;}sub parse_preproc_file { # Read a preprocess file and parse @_ == 2 or croak 'usage: $parser->parse_file(Verilog::Preproc_object_ref)'; my $self = shift; my $pp = shift; ref($pp) or croak "%Error: not passed a Verilog::Preproc object"; $self->reset(); while (defined(my $line = $pp->getline())) { $self->parse ($line); } $self->eof; return $self;}########################################################################## Called by the parsersub error { my ($self,$text,$token)=@_; my $fileline = $self->filename.":".$self->lineno; croak ("%Error: $fileline: $text\n" ."Stopped");}sub attribute { # Default Internal callback my $self = shift; # Parser invoked my $token = shift; # What token was parsed $self->unreadbackCat($token);}sub comment { # Default Internal callback my $self = shift; # Parser invoked my $token = shift; # What token was parsed $self->unreadbackCat($token);}sub string { # Default Internal callback my $self = shift; # Parser invoked my $token = shift; # What token was parsed $self->unreadbackCat($token);}sub keyword { # Default Internal callback my $self = shift; # Parser invoked my $token = shift; # What token was parsed $self->unreadbackCat($token);}sub symbol { # Default Internal callback my $self = shift; # Parser invoked my $token = shift; # What token was parsed $self->unreadbackCat($token);}sub operator { # Default Internal callback my $self = shift; # Parser invoked my $token = shift; # What token was parsed $self->unreadbackCat($token);}sub preproc { # Default Internal callback my $self = shift; # Parser invoked my $token = shift; # What token was parsed if (Verilog::Language::is_keyword($token)) { $self->keyword($token); # Do this for backward compatibility with Version 2.* } else { $self->symbol($token); # Do this for backward compatibility with Version 2.* }}sub number { # Default Internal callback my $self = shift; # Parser invoked my $token = shift; # What token was parsed $self->unreadbackCat($token);}sub sysfunc { # Default Internal callback - note the default action my $self = shift; # Parser invoked my $token = shift; # What token was parsed $self->symbol($token); # Do this for backward compatibility with Version 2.*}sub endparse { # Default Internal callback my $self = shift; # Parser invoked my $token = shift; # What token was parsed $self->unreadbackCat($token);}########################################################################## Package return1;__END__=pod=head1 NAMEVerilog::Parser - Parse Verilog language files=head1 SYNOPSIS use Verilog::Parser; my $parser = new Verilog::Parser; $string = $parser->unreadback (); $line = $parser->lineno (); $parser->parse ($text) $parser->parse_file ($filename)=head1 DESCRIPTIONVerilog::Parser will tokenize a Verilog file when the parse() method iscalled and invoke various callback methods. This is useful for extractinginformation and editing files while retaining all context. For netlistlike extractions, see L<Verilog::Netlist>.See the "Which Package" section of L<Verilog::Language> if you are unsurewhich parsing package to use for a new application.=head1 METHODS=over 4=item $parser = Verilog::Parser->new (args...)Create a new Parser. Passing the named argument "use_unreadback => 0" willdisable later use of the unreadback method, which may improve performance.=item $parser->eof ()Indicate the end of the input stream. All incomplete tokens will be parsedand all remaining callbacks completed.=item $parser->filename ($set)Return (if $set is undefined) or set current filename.=item $parser->lineno ($set)Return (if $set is undefined) or set current line number.=item $parser->parse ($string)Parse the $string as verilog text. Can be called multiple times. Note notall callbacks may be invoked until the eof method is called.=item $parser->parse_file ($filename);This method can be called to parse text from a file. The argument canbe a filename or an already opened file handle. The return value fromparse_file() is a reference to the parser object.=item $parser->parse_preproc_file ($preproc);This method can be called to parse preprocessed text from a predeclaredVerilog::Preproc object.=item $parser->unreadback ($string)Return any input string from the file that has not been sent to thecallback. This will include whitespace and tokens which did not have acallback. (For example comments, if there is no comment callback.) Thisis useful for recording the entire contents of the input, forpreprocessors, pretty-printers, and such.With the optional argument, set the text to be returned with the nextunreadback call. See also unreadbackCat, which is much faster.To use this option, "use_unreadback => 1" must have been passed to theconstructor.=item $parser->unreadbackCat ($text)Add text to be returned with the next unreadback call. This is much fasterthan using "$parser->unreadback($parser->unreadback . $text)".=back=head1 CALLBACKSIn order to make the parser do anything interesting, you must make asubclass where you override one or more of the following callback methodsas appropriate:=over 4=item $self->attribute ( $token )This method is called when any text in (* *) are recognized. The firstargument, $token, is the contents of the attribute including the delimiters.=item $self->comment ( $token )This method is called when any text in // or /**/ comments are recognized.The first argument, $token, is the contents of the comment including thecomment delimiters.=item $self->endparse ( $token )This method is called when the file has been completely parsed, at theEnd-Of-File of the parsed file. It is useful for writing clean uproutines.=item $self->keyword ( $token )This method is called when any Verilog keyword is recognized.The first argument, $token, is the keyword.=item $self->number ( $token )This method is called when any number is recognized. The first argument,$token, is the number. The Verilog::Language::number_value function may beuseful for converting a Verilog value to a Perl integer.=item $self->operator ( $token )This method is called when any symbolic operator (+, -, etc) is recognized.The first argument, $token, is the operator.=item $self->preproc ( $token )This method is called when any Verilog preprocessor `command is recognized.Most of these are handled by the preprocessor, however any unrecognized`defines are passed through. For backward compatibility, if not definedthis function will call the symbol callback.=item $self->string ( $token )This method is called when any text in double quotes are recognized, or onthe text of protected regions. The first argument, $token, is the contentsof the string including the quotes.=item $self->symbol ( $token )This method is called when any Verilog symbol is recognized. A symbol isconsidered a non-keyword bare-word. The first argument, $token, is thesymbol.=item $self->sysfunc ( $token )This method is called when any Verilog $syscall is recognized. The firstargument, $token, is the symbol. For backward compatibility, if notdefined this function will call the symbol callback.=back=head1 EXAMPLEHere's a simple example which will print every symbol in a verilogfile. package MyParser; use Verilog::Parser; @ISA = qw(Verilog::Parser); # parse, parse_file, etc are inherited from Verilog::Parser sub new { my $class = shift; #print "Class $class\n"; my $self = $class->SUPER::new(); bless $self, $class; return $self; } sub symbol { my $self = shift; my $token = shift; $self->{symbols}{$token}++; } sub report { my $self = shift; foreach my $sym (sort keys %{$self->{symbols}}) { printf "Symbol %-30s occurs %4d times\n", $sym, $self->{symbols}{$sym}; } } package main; my $parser = MyParser->new(); $parser->parse_file (shift); $parser->report();=head1 BUGSThis is being distributed as a baseline for future contributions. Don'texpect a lot, the Parser is still naive, and there are many awkward casesthat aren't covered.The parser currently assumes the string it is passed ends on a newlineboundary. It should be changed to allow arbitrary chunks.Cell instantiations without any arguments are not supported, an empty setof parenthesis are required. (Use "cell cell();", not "cell cell;".)=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::Preproc>,L<Verilog::SigParser>,L<Verilog::Language>,L<Verilog::Netlist>,L<Verilog::Getopt>,L<vrename>,L<vpassert>L<vppreproc>=cut
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -