Class AScanner
Unit
parsing
Declaration
type AScanner = class(AnIterator)
Description
This class represents a scanner that is used to return tokens from a stream.
The base class, AScanner, is meant to be used with files that contained parsed intermediate code. The first descendant of this class, ASourceScanner, is designed to be used with files that contain raw source.
Scanners are constructed as iterators that iterate over the tokens in an arbitrary stream. Thus, they can be used by a parser within an iterator loop:
type MyParser = ASourceParser
public
function parse: integer; override;
end;
function MyParser.parse: integer;
begin
result := -1;
if MySource = nil then
exit;
MyScanner := ASourceScanner(MySource.iterator);
if MyScanner = nil then
exit;
result := 0;
while MyScanner.continues do
begin
case MyScanner.CurrentToken.opcode of:
end;
MyScanner.next;
end;
MyScanner.free;
result := MyLog.Counter('errors').value;
end;
Hierarchy
Overview
Fields
Methods
Description
Fields
|
MySource: AStream; |
Refers to the stream being scanned for tokens.
|
|
myLineNumber: longword; |
Stores the current line number in the source.
|
|
MyCurrentToken: AToken; |
Refers to the most recent token retrieved from the source stream
|
Methods
|
constructor forSource(const ThisSource: AStream); virtual; overload; |
Construct a scanner that will iterate over the given source stream.
Scanning begins at the current position within ThisSource .
|
|
function init: boolean; override; |
Initializer
|
|
procedure returnToken(const ThisToken: AToken); virtual; |
Return the specified token to the source stream.
This method can be used by parsers to "peek" at a token and then return it to the source for further processing.
ThisToken is freed by this routine and should not be used after the routine returns.
This method calls AToken.returnTo.
|
|
procedure rewind; virtual; |
Return the token most recently read from the source back to the source.
This method can be used by parsers to "peek" at a token and then return it to the source for further processing. The current token is freed by this routine and should not be used after the routine returns.
This method calls AToken.returnTo.
|
|
procedure next; override; |
Retrieve the next token from the source.
This method always succeeds; however, if the end of the source has been reached, the token created will be an instance of AStreamEndingToken. If AScanner.CurrentToken already refers to such an instance, then this routine does nothing.
|
|
function Peek: AToken; virtual; |
Peek at the next token from the source without advancing the scanner.
This method reads a token from the source and returns it, then rewinds the source position so that the token will be read again.
The caller should free the token reference returned by this routine when it is no longer needed. The scanner does NOT free the reference.
|
|
function continues: boolean; override; |
Determine whether or not there are more tokens to retrieve from the source.
If the source indicates that its end has been reached, this method will return False ; otherwise, it will return True , because the last token to retrieve from the source will always be an instance of AStreamEndingToken.
|
|
function Source: AStream; |
Retrieve a reference to the source stream being scanned for tokens.
This reference should NOT be freed before the scanner itself is freed, otherwise, undefined things (program crashes) will happen.
|
|
function lineNumber: longword; |
Retrieve the current line number from the source.
This function is primarily used by the parser to output error and status messages. The line number is updated whenever an instance of ALineEndingToken is encountered by the scanner.
|
|
function CurrentToken: AToken; |
Retrieve a reference to the most recent token read from the stream.
The caller should free this reference. This is NOT done by the scanner, not even when a new token is read from the source. This behavior is by design, since AParser and its descendants organize tokens into instances of ATokenList, which will free all of its token references when the list itself is destroyed.
|
|
function tokenBehaviors: TScannerTokenBehaviors; virtual; |
Retrieve the behaviors defined for the scanner when certain tokens are encountered. For more information on the available behaviors, see TScannerTokenBehavior.
|
Generated by PasDoc 0.13.0 on 2015-01-10 17:13:18
|