NAME
Hardware::Vhdl::Lexer - Split VHDL code into lexical tokens
SYNOPSIS
use Hardware::Vhdl::Lexer;
use Hardware::Vhdl::PreProcess;
my $fh;
open $fh, '<', 'device_behav.vhd' || die $!
my $lexer = Hardware::Vhdl::Lexer->new(linesource => $fh);
my ($token, $type);
while( (($token, $type) = $lexer->get_next_token) && defined $token) {
print "# type = '$type' token='$token'\n";
}
# use a class to supply the source code lines, and ask the lexer to remember the last 5 code tokens.
my $lexer = Hardware::Vhdl::Lexer->new(
linesource => Hardware::Vhdl::PreProcess->new(sourcefile => 'device_behav.vhd'),
nhistory => 5
);
DESCRIPTION
Hardware::Vhdl::Lexer
splits VHDL code into lexical tokens. To use it, you need to first create a lexer object, passing in an object with a get_next_line
method which will supply lines of VHDL code to the lexer. Repeated calls to the get_next_token
method of the lexer will then return VHDL tokens (in scalar context) or a token type code and the token (in list context). get_next_token
returns undef when there are no more tokens to be read.
CONSTRUCTOR
- new(linesource => $object_with_get_next_line_method [, nhistory => N])
- new(linesource => $filehandle_reference [, nhistory => N])
- new(linesource => \@array_of_lines [, nhistory => N])
- new(linesource => \$scalar_containing_vhdl [, nhistory => N])
- new(linesource => \&subroutine_that_returns_lines [, nhistory => N])
-
The linesource argument is required: it can be either an object with a
get_next_line
method, or a filehandle reference, in which casereadline
will be used on it. The linesource is expected to return undef when there are no more lines to read. The optional nhistory argument sets how many "code" tokens (see below) will be remembered for access by thehistory
method.
METHODS
- linesource()
-
Returns the linesource argument passed into the constructor
get_next_token()
-
In scalar context, returns the next VHDL token.
In list context, returns a token type code and the token
Nothing is removed from the source code: if you concatenate all the tokens returned by
get_next_token()
, you will get the same result as if you concatenate all the strings returned by the linesource object.The token type codes are 1 or 2-character strings. When the codes are 2 characters, the first character gives the general class of the token and the second indicates its type more specifically. The first character will be 'w' for whitespace, 'r' for comments (remarks) or 'c' for code. It should be possible to remove all comment tokens, and change whitespace tokens for different whitespace, and always end up with functionally equivalent code.
The token type codes are:
- wn
-
Whitespace:Newline. This could be any of \012, \015, \015\012 or \012\015.
- ws
-
Whitespace:Spaces. A group of whitespace characters which match the /s regexp pattern but which do not include any carriage-return or linefeed characters.
- r
-
Remark. The token will start with two dashes and include the remainder of the source code line, not including any newline characters. The next token will either be a newline or undef.
- cs
-
Code:String literal. The lexer accepts multi-line strings, even though the VHDL specification does not allow them.
- cc
-
Code:Character literal.
- cb
-
Code:Bit_vector literal. For example,
B"001_1010"
orO"7720"
orH"A7_DEAD"
. - cn
-
Code:Numeric literal. This could be a specified-base literal like
8#7720#
or a simple integer or floating-point value. - ci
-
Code:Identifier or keyword. For example,
package
ormy_signal_23
or/extended identifier$%!/
.. - cp
-
Code:Punctuation. A group of punctuation symbols which cannot be part of an extended identifier, and are not separated by whitespace.
- history(N)
-
Returns previous code tokens. N must not be larger than the nhistory argument passed to the constructor.
history(0)
will return the text of the last token returned byget_next_token
whose type started with a 'c',history(1)
will return the code token before that, and so on.
AUTHOR
Michael Attenborough, <michael.attenborough at physics.org>
BUGS
Please report any bugs or feature requests to bug-hardware-vhdl-lexer at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hardware-Vhdl-Lexer. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Hardware::Vhdl::Lexer
You can also look for information at:
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hardware-Vhdl-Lexer
Search CPAN
ACKNOWLEDGEMENTS
COPYRIGHT & LICENSE
Copyright 2006 Michael Attenborough, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.