Goldie: class Parser

Goldie
Parsing System (v0.4)
Goldie Home (v0.4)
Getting Started

- Requirements
- Download / Install
- Never Used a Parsing Tool?
- How To Use Goldie
- Grammar Definition Language
- Message Board
- Ticket System

Goldie's Tools

- DumpCGT
- GenDocs
  - Template Format
- GRMC: Grammar Compiler
- JsonViewer
  - Parse Tree Mode
- Parse
- StaticLang
- Other Tools

GoldieLib Overview

- Static And Dynamic Styles
- Ambiguous Symbols

GoldieLib Reference

- Base
- Exception
- class Language
- class Lexer
- class Parser
- struct Symbol
- class Token
- Ver

GoldieLib Sample Apps

- Calculator Dynamic
- Calculator Static
- Parse Anything

CGT File Format
FAQ
Change Log
If You Use Trunk
Future Work
Developers

- Library Source and StaticLang

Goldie Home (v0.4) -> GoldieLib Reference -> class Parser

# class Parser

- class Parser
  - class Parser_{languageName}: Parser

module goldie.parser

class 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
Token[] tokenStack
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.

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

class Parser_{languageName} : 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_{languageName}!{symbol} 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_{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!"); }