Class AParsedLanguageParser

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type AParsedLanguageParser = class(AParser)

Description

This class represents a parser that is used to process source code of some kind using a parsed language definition. It extends AParser to support processing source using a language definition provided by an instance of AParsedLanguage and provides several utility routines that are useful for parsers that process source using a language definition.

Instead of directly creating an instance of this class, you will probably derive a class from it and then create an instance of that, since this method does not implement any new parsing behavior.

When constructing an instance of AParsedLanguageParser, you are expected to provide:

  • a language definition

as well as the items required by AParser before calling parse.

Hierarchy

Overview

Fields

Protected MyLanguage: AParsedLanguage;

Methods

Public constructor forSource(const ThisParent: AParser); overload; override;
Public function init: boolean; override;
Public destructor destroy; override;
Public function readyToParse: boolean; override;
Public function parseUntil(const thisRule: TSortKey): int64; overload; virtual;
Public procedure resyncTo(const rule: TSortKey); virtual; overload;
Public procedure skipTo(const rule: TSortKey; const silenceInterveningTokens: boolean = true); overload; virtual;
Public procedure skipOver(const rule: TSortKey; const silenceInterveningTokens: boolean = true); overload; virtual;
Public function Language: AParsedLanguage; virtual;

Description

Fields

Protected MyLanguage: AParsedLanguage;

Refers to the language used to tokenize the source

Methods

Public constructor forSource(const ThisParent: AParser); overload; override;

Construct a new parser that will inherit the properties of its parent parser.

This method builds on the behavior inherited from AParser.forSource: it ensures that the new parser also inherits the value of it ThisParent's AParsedLanguageParser.Language.

Public function init: boolean; override;

Initializer

Public destructor destroy; override;

Destroy the parser.

This method frees AParsedLanguageParser.Language and then calls the inherited routine.

Public function readyToParse: boolean; override;

Initialize the parser prior to parsing the source.

This method builds on the behavior inherited from AParser.readyToParse. It calls the inherited routine and, if that routine returns True to indicate all is well, it checks to determine whether Self.Language is Nil, indicating that a descendant class has not yet assigned a language to the parser. If not, the method will attempt to borrow the reference from a parent. In any case, if Self.Language is Nil, this method will return False.

Returns

True if the parser is ready to parse; False if not.

Public function parseUntil(const thisRule: TSortKey): int64; overload; virtual;

Parse tokens or statements until one of the tokens in the specified rule is encountered.

Before this method is called, the return value of AParsedLanguageParser.readyToParse should be checked; if that method returns False, the parser is not ready to parse and this method should not be called.

This method looks up the rule identified by thisRule by calling AParsedLanguage.SyntaxRule on Self.Language. If the specified rule is found, then this method loops, calling Self.parse repeatedly until a token from the specified rule is encountered.

Each time through the loop, after Self.parse returns, this method checks to see if the end of the stream has been reached. If it has, and TOKCAT_EOS is not part of thisRule, then this method will raise a fatal error to indicate that the end of the stream was not supposed to be encountered. Otherwise, the routine calls Self.resyncTo, passing RULE_END_STATEMENT to ensure that the end of the statement has actually been reached. It then calls Self.NextToken and loops again.

When this method exits, AParsedLanguageStatementBlock.CurrentToken should refer to a token from thisRule or the end of the stream.

Returns

The total number of errors that occurred while parsing.

Exceptions raised
AParserFatalError
if the end of the source stream is encountered before a token from thisRule is found – unless TOKCAT_EOS is part of thisRule.
Public procedure resyncTo(const rule: TSortKey); virtual; overload;

Resynchronize the parser to one of the tokens in the specified rule.

This method is designed to allow the parser to recover after a syntax error. It logs a syntax error to indicate that the current token is not expected, then looks up the specified rule in the list of syntax rules specified by AParsedLanguageParser.Language. If the rule is found, it calls AParser.resyncTo, passing the rule reference to that routine.

If the specified rule is not found, then this routine does nothing.

Public procedure skipTo(const rule: TSortKey; const silenceInterveningTokens: boolean = true); overload; virtual;

Skip tokens until one of the tokens in the specified rule is encountered.

This method looks up the specified rule in the list of syntax rules specified by AParsedLanguageParser.Language. If the rule is found, it calls AParser.skipTo, passing the rule reference to that routine. If the specified rule is not found, then this routine does nothing.

If silenceInterveningTokens is True, then all tokens encountered before opcode will have their silenced property set by a call to AToken.setSilenced. Although AParsedLanguageParser does not make use of this behavior, descendant classes may prevent silenced tokens from being written to their intermediate code streams.

Public procedure skipOver(const rule: TSortKey; const silenceInterveningTokens: boolean = true); overload; virtual;

Skip over tokens until one is encountered that is not a part of the specified rule.

This method looks up the specified rule in the list of syntax rules specified by AParsedLanguageParser.Language. If the rule is found, it calls AParser.skipOver, passing the rule reference to that routine. If the specified rule is not found, then this routine does nothing.

If silenceInterveningTokens is True, then all tokens encountered before opcode will have their silenced property set by a call to AToken.setSilenced. Although AParsedLanguageParser does not make use of this behavior, descendant classes may prevent silenced tokens from being written to their intermediate code streams.

Public function Language: AParsedLanguage; virtual;

Retrieve a reference to the language used to parse the source.

The caller should NOT free the reference returned by this routine. That will be done by the parser itself when it is freed.


Generated by PasDoc 0.13.0 on 2015-06-25 11:12:03