Parsing System (v0.8)
|
GoldieLib OverviewGoldieLib is Goldie's D API. Using GoldieLib, your D programs can parse text according to any GOLD-compatible grammar and access some of Goldie's other capabilities. ImportingImporting is simple: import goldie.all;
When you compile, make sure to tell the compiler where to find Goldie and SemiTwist D Tools: >rdmd --build-only -I{path to SemiTwistDTools}/src -I{path to Goldie}/src myMain.d
Conventions Used By GoldieLibLine and Column NumbersAll line numbers and column numbers are internally stored and treated by the API as zero-indexed and displayed to the user as one-indexed. When Goldie refers to a "column number", it really means "the number of characters (ie, UTF code-points) from the start of the line". This behavior is more reliable and more useful than a true "column number" because:
Tokens, Symbols, and Symbol Types:
|
Word | Symbol | This Symbol's type is SymbolType.Terminal. |
<Sentence> | Symbol | This Symbol's type is SymbolType.NonTerminal. |
Hello | Token | This Token's Symbol is Word. |
world | Token | This Token's Symbol is Word. |
Hello world | Token | This Token's Symbol is <Sentence>. |
Note that Goldie defines more symbol types than just Terminal and NonTerminal (see the SymbolType documentation). So if you want to check if a Symbol or Token is a SymbolType.NonTerminal do NOT do it by comparing the type with SymbolType.Terminal. Just because something isn't a SymbolType.Terminal does NOT imply that it's a SymbolType.NonTerminal.
Just use null for the subtokens.
Example:
Suppose you have this in your grammar for language "myLang":
The nonterminal <Optional Fred> can be created from either the text fred or from nothing at all. To check if a token matches the first case, ie. the fred rule, you use this:
To check for the empty rule, you just use null:
For simple examples of how to use GoldieLib, see the GoldieLib Sample Apps.