[$name|APIOver$]
[$title|GoldieLib Overview$]
[$frame|HTMLPage.tmpl$]
<a name="Overview" />

<p>
[$ident|GoldieLib$] is Goldie's
[$link| http://www.digitalmars.com/d/2.0/ | D v2.x $]
API. Using GoldieLib, your D programs
can parse text according to any GOLD-compatible grammar and access
some of Goldie's other capabilities.
</p>

<a name="Import" />
<h2>Importing</h2>

<p>Importing is simple:</p>

[$code|highlight|
[:import goldie.all;
:]$]

<h2>Conventions Used By GoldieLib</h2>

<a name="LineColumn" />
<h3>Line and Column Numbers</h3>
<p>
All line numbers and column numbers are internally stored and treated
by the API as zero-indexed and displayed to the user as one-indexed.
</p>

<p>
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:
</p>
<ul>
<li>
	Tab-size is a matter of presentation (For better or worse, depending on one's perspective).
</li>
<li>
	Non-printing characters may exist in a source to be parsed.
</li>
<li>
	The increasingly popular concept of
	[$link|http://nickgravgaard.com/elastictabstops/|elastic tabstops$]
	makes proportional fonts practical for source code, which renders
	the traditional concept of "column number" meaningless.
</li>
</ul>

<a name="TokenSymbol" />
<h3>
	Tokens, Symbols, and Symbol Types:<br />
	<span style="font-size: 0.9em">The Front-End's Building Blocks</span>
</h3>
<p>
A [$typeName|Token$] can be thought of as an instance of a [$typeName|Symbol$].
A [$typeName|Token$] is part of the parsed source, and a [$typeName|Symbol$]
is part of the grammar.
</p>
<p>
For example, consider this grammar:

[$code|plain|
[:Word = {Letter}+
&lt;Sentence&gt; ::= &lt;Sentence&gt; Word
:]$]

And this source:

[$code|plain|
[:Hello world
:]$]
</p>
<p>
These are the Tokens and Symbols:
</p>
<table class="grid-table">
<tr><td>[$ident|Word$]</td><td>[$typeName|Symbol$]</td><td>This Symbol's type is [$typeName|SymbolType$][$ident|.Terminal$].</td></tr>
<tr><td>[$ident|&lt;Sentence&gt;$]</td><td>[$typeName|Symbol$]</td><td>This Symbol's type is [$typeName|SymbolType$][$ident|.NonTerminal$].</td></tr>
<tr><td>[$ident|Hello$]</td><td>[$typeName|Token$]</td><td>This Token's Symbol is [$ident|Word$].</td></tr>
<tr><td>[$ident|world$]</td><td>[$typeName|Token$]</td><td>This Token's Symbol is [$ident|Word$].</td></tr>
<tr><td>[$ident|Hello world$]</td><td>[$typeName|Token$]</td><td>This Token's Symbol is [$ident|&lt;Sentence&gt;$].</td></tr>
</table>
<p>
Note that Goldie defines more symbol types
than just [$ident|Terminal$] and [$ident|NonTerminal$] (see the
[$typeName|SymbolType$] documentation).
So if you want to check if a Symbol or Token is a [$ident|SymbolType.NonTerminal$]
do NOT do it by comparing the type with [$ident|SymbolType.Terminal$]. Just because
something isn't a [$ident|SymbolType.Terminal$] does NOT imply that
it's a [$ident|SymbolType.NonTerminal$].
</p>

<a name="EmptyRule" />
<h2>Referring to a rule that has no subtokens</h2>
<p>
Just use [$ident|null$] for the subtokens.
</p>
<p>
Example:
</p>
<p>
Suppose you have this in your grammar for language "myLang":
[$code|plain|
[:&lt;Optional Fred&gt; ::= 'fred'
                  | :]
$]
</p>
<p>
The nonterminal [$ident|&lt;Optional Fred&gt;$] can be created from either the
text [$ident|fred$] or from nothing at all. To check if a token matches the
first case, ie. the [$ident|fred$] rule, you use this:
[$code|highlight|
[:// static-style: 
if( auto typedTok = cast(Token_myLang!("&lt;Optional Fred&gt;", "fred"))token )
{
	// use typedTok
}

// dynamic-style: 
if( token.matches("&lt;Optional Fred&gt;", "fred") )
{
	// use token
}:]
$]
</p>
<p>
To check for the empty rule, you just use [$ident|null$]:
[$code|highlight|
[:// static-style: 
if( auto typedTok = cast(Token_myLang!("&lt;Optional Fred&gt;", null))token )
{
	// use typedTok
}

// dynamic-style: 
if( token.matches("&lt;Optional Fred&gt;", null) )
{
	// use token
}:]
$]
</p>

<h2>Simple Examples</h2>
<p>
For simple examples of how to use GoldieLib, see the [$pageTitle|SampleApps$].
</p>

<h3 class="list-heading">See Also:</h3>
<ul>
<li>[$pageLink|APIRef| GoldieLib API Reference $]</li>
</ul>
