Goldie: class Parser
Parsing System (v0.9)
Goldie Home (v0.9) -> GoldieLib Reference -> class Parser

class Parser

module goldie.parser

Token parseTreeX
The root token in the parse tree of the last source parsed.
Lexer lexer
The Lexer that was used to lex the source before parsing.
Language lang
The Language that was used to lex and parse the source.
string filename
The filename (if available) of the last source parsed.
int[] rulesUsed
Stack!Token tokenStack
Stack!int stateStack
Token currTok
int tokIndex
int state

Internal parser state. Access is provided to these for diagnostic purposes. If an exception is thrown during parsing, then these can be inspected to check the parser's internal state at the time of the exception. This is probably only useful for debugging Goldie.

The templated type Stack!T is defined in the module semitwist.util.container from SemiTwist D Tools.

Token process(Token[] tokens, Language lang, string filename="", Lexer lexer=null)

Parses a series of tokens. Throws a ParseException if the source contains an error.

Normally, you would use one of the Language.parse functions instead of calling this directly, but this direct access is provided in case you'd rather re-use the same parser instead of instantiating a new one every time.

The filename from which the source originated can be provided so error messages can report the appropriate filename.

The Lexer that was used to lex the source can be provided so that, upon any error, the ParseException can provide access to all the lexing information.

Returns parseTreeX.

module {user-specified package}.parser

{languageName} = Name of static-style language

This is the static-style counterpart to Parser, and is generated by the StaticLang tool.

If the name of a language is, for example, foo, then the name of this class will be Parser_foo.

Type-safe static-style counterpart to Parser.parseTreeX.

Since the actual rule used for the root can only be known at runtime (because it depends on the actual source parsed), this is a Token_{languageName}!{symbol} and not a Token_{languageName}!{rule}. So in order to access the subtokens, you will need to determine which rule was used, like this:

// Assume the language "calc": // "Start Symbol" = <Add Exp> // <Add Exp> ::= <Add Exp> '+' <Mult Exp> // | <Add Exp> '-' <Mult Exp> // | <Mult Exp> void foo(string src) { Parser_calc parserUsed = language_calc.parseCode(src); Token_calc!"<Add Exp>" rootUnknownRule = parserUsed.parseTree; // This is the static-style equivilent to using Token.matches() if( auto root = cast(Token_calc!("<Add Exp>", "<Mult Exp>"))rootUnknownRule ) { // Use root } else if( auto root = cast(Token_calc!("<Add Exp>", "<Add Exp>", "+", "<Mult Exp>"))rootUnknownRule ) { // Use root } else if( auto root = cast(Token_calc!("<Add Exp>", "<Add Exp>", "-", "<Mult Exp>"))rootUnknownRule ) { // Use root } else throw new Exception("Forgot to handle some other rule!"); }