NAME

Hardware::Vhdl::Lexer - Split VHDL code into lexical tokens

SYNOPSIS

# Example 1: Just dump all the tokens in a VHDL file
use Hardware::Vhdl::Lexer;
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";
}

# Example 2: use a class (not shown) to supply the source code
#  lines, and ask the lexer to remember the last 5 code tokens.
use Hardware::Vhdl::PreProcess;
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 something 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 => <source> [, nhistory => N])

The linesource argument is required: it defines where the VHDL source code will be taken from (see below). The optional nhistory argument sets how many "code" tokens (see the get_next_token method) will be remembered for access by the history method.

new(linesource => $filehandle_reference [, nhistory => N])

To read from a file, pass in the filehandle reference like this:

use Hardware::Vhdl::Lexer;
my $fh;
open $fh, '<', $filename || die $!
my $lexer = Hardware::Vhdl::Lexer->new(linesource => $fh);
new(linesource => \@array_of_lines [, nhistory => N])
new(linesource => \$scalar_containing_vhdl [, nhistory => N])

To read VHDL source that is already in program memory, the linesource argument can be a reference to either an array of lines or a single string which can have embedded newlines.

new(linesource => $object_with_get_next_line_method [, nhistory => N])

The linesource argument can be an object with a get_next_line method. This method must return undef when there are no more lines to read.

new(linesource => \&subroutine_that_returns_lines [, nhistory => N])

If none of the above input methods suits your needs, you can give a subroutine reference and wrap whatever code you need to get the VHDL source. When called, this subroutine must return each line of source code in turn, and then return undef when there are no more lines.

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" or O"7720" or H"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 or my_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 by get_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:

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.