Parsing System (v0.8)
|
class Parser
module goldie.parser
The root token in the parse tree of the last source parsed.
The Lexer that was used to lex the source before parsing.
The Language that was used to lex and parse the source.
string filename
The filename (if available) of the last source parsed.
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. 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.
Token_ !parseTree
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_ and not a !Token_. 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!");
}
|