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 calling AParsedLanguageParser.parse on a direct instance of this class does not do much that is useful.

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 parse: int64; 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 parse: int64; override;

Parse the source.

This method builds on the behavior inherited from AParser.parse. It calls the inherited routine and, if that routine returns zero (0) to indicate all is well, it checks to determine whether AParsedLanguageParser.Language is Nil.

For classes that override this method, the generally-accepted rule is that, although Self.CurrentToken should refer to the token which ended the given statement when this method exits, the actual process of resynchronizing the parser to ensure that is done by AParsedLanguageParser.parseUntil. If you would rather do so in this method, you will need to override AParsedLanguageParser.parseUntil, if you use it, as you will otherwise the confuse the parser.

Returns

A negative value if an error has occurred before any parsing was done. This will most likely occur if AParser.Source, AParser.Log, or AParsedLanguageParser.Language are invalid (Nil) pointers. A positive value indicates the number of non-fatal errors that occurred while parsing. A value of zero (0) indicates all is well.

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

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

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-01-10 17:13:18