Goldie: struct Symbol
Parsing System (v0.9)
Goldie Home (v0.9) -> GoldieLib Reference -> struct Symbol

struct Symbol

See also the explanation of Tokens vs Symbols.

module goldie.base

NonTerminal
Terminal
Whitespace
EOF
CommentStart
CommentEnd
CommentLine
Error

EOF and Error symbols (there is exactly one of each in every language) are automatically created when a grammar is compiled. A Whitespace symbol is automatically defined if the grammar doesn't already define one.

Symbols of the other SymbolType values are defined by a grammar.

All SymbolType values except for NonTerminal are technically considered to be terminals (and tokens of these symbols are created by the Lexer instead of the Parser), although the actual SymbolType.Terminal value is only used for "normal" terminals (ie., ones that don't match any of the other SymbolType values).

module goldie.base

string name

The name of the symbol, as defined in a language's grammar. For nonterminals, this includes the surrounding < and >.

For example, in a grammar, <If Statement> ::= 'if' would define a nonterminal symbol with a name of <If Statement>.

The type of symbol.
int id
The ID of the symbol. This is an index into Language.symbolTable.
string toString()
The result of this is valid D code representing a struct literal. For example:
auto sym = Symbol("foo", SymbolType.Terminal, 7); assert(sym.toString() == `Symbol("foo",SymbolType.Terminal,7)`);

module goldie.base

Example:
string str = symbolTypeToString(SymbolType.NonTerminal); assert(str == "NonTerminal");
string fullSymbolTypeToString(SymbolType type)
Example:
string str = fullSymbolTypeToString(SymbolType.NonTerminal); assert(str == "SymbolType.NonTerminal");
string symbolTypesToString(SymbolType[] types)
Example:
auto symTypes = [SymbolType.Terminal, SymbolType.NonTerminal, SymbolType.Terminal]; string str = symbolTypesToString(symTypes); assert(str == "Terminal, NonTerminal, Terminal");
string[] symbolTypesToStrings(SymbolType[] types)
Example:
auto symTypes = [SymbolType.Terminal, SymbolType.NonTerminal, SymbolType.Terminal]; auto strings = symbolTypesToStrings(symTypes); assert(strings.length == 3); assert(strings[0] == "Terminal"); assert(strings[1] == "NonTerminal"); assert(strings[2] == "Terminal");