|
Language::Basic::Token - Module to handle lexing BASIC statements. |
Language::Basic::Token - Module to handle lexing BASIC statements.
See the Language::Basic manpage for the overview of how the Language::Basic module works. This pod page is more technical.
# lex a line of basic into a bunch of tokens.
my $token_group = new Language::Basic::Token::Group;
$token_group->lex('PRINT "YES","NO" : A=A+1');
# Look at tokens
my $tok = $token_group->lookahead && print $tok->text;
# Eat expected tokens
my $tok = $token_group->eat_if_string(",");
my $tok = $token_group->eat_if_class("Keyword");
BASIC tokens are pretty simple. They include Keywords, Identifiers (Variable or Function names), String and Numeric Constants, and a few one- or two-character operators, like ':' and '<='. Tokens aren't very ambiguous, so for example, you don't need to know what type of Statement you're looking at in order to lex a line of BASIC. (The only remotely ambiguous thing is that '=' can be either a Relational Operator or an Assignment statement.)
The subclasses of LB::Token represent the various sorts of tokens. The Token::Group class isn't really a subclass at all; it's a group of tokens. See Language::Basic::Token::Group for more info.
The ``text'' method returns the text that makes up the token. Note that text is stored in upper case (except for string constants, which are stored exactly as entered).
This important class handles a group of tokens. Text from the BASIC program is lexed and turned into LB::Tokens which are stored in a Token::Group. Any access to these Tokens (including creating them) is through the Token::Group methods. Other classes' parse methods will usually eat their way through the tokens in the Token::Group until it's empty.
Note that the string to match should be upper case, since all \w tokens are stored as uppercase.
The other subclasses are actually kinds of Tokens, unlike Token::Group. There are no ``new'' methods for these classes. Creation of Tokens is done by Token::Group::lex. In fact, these classes don't have any public methods. They're mostly there to use ``isa'' on.
|
Language::Basic::Token - Module to handle lexing BASIC statements. |