Table of Contents

Class Grammar

Namespace
Farkle.Grammars
Assembly
Farkle.dll

Provides information about a context-free grammar.

public abstract class Grammar : IGrammarProvider
Inheritance
Grammar
Implements
Extension Methods

Remarks

The grammar's data is internally stored in a binary format described in https://github.com/teo-tsirpanis/Farkle/blob/mainstream/designs/7.0/grammar-file-format-spec.md

Properties

Data

A read-only buffer to the Grammar's binary data.

public ReadOnlySpan<byte> Data { get; }

Property Value

ReadOnlySpan<byte>

DfaOnChar

The Grammar's Dfa<TChar> on char, if it exists.

public Dfa<char>? DfaOnChar { get; }

Property Value

Dfa<char>

GrammarInfo

General information about this Grammar.

public GrammarInfo GrammarInfo { get; }

Property Value

GrammarInfo

Groups

A collection of the Grammar's Groups.

public GroupCollection Groups { get; }

Property Value

GroupCollection

HasUnknownData

Whether the Grammar contains data that are not recognized by this version of Farkle.

public bool HasUnknownData { get; }

Property Value

bool

LrStateMachine

The Grammar's LrStateMachine, if it exists.

public LrStateMachine? LrStateMachine { get; }

Property Value

LrStateMachine

Nonterminals

A collection of the Grammar's Nonterminals.

public NonterminalCollection Nonterminals { get; }

Property Value

NonterminalCollection

Productions

A collection of the Grammar's Productions.

public ProductionCollection Productions { get; }

Property Value

ProductionCollection

SpecialNameDefinitions

A collection of the Grammar's SpecialNameDefinitions.

public SpecialNameDefinitionCollection SpecialNameDefinitions { get; }

Property Value

SpecialNameDefinitionCollection

Remarks

This type is intended to be used for presentation purposes only. For maximum performance, parsers are strongly recommended to use GetSymbolFromSpecialName(string, bool), or one of the extension methods in GrammarExtensions instead.

See Also

Terminals

A collection of the Grammar's TokenSymbols that have the Terminal flag set.

public TokenSymbolCollection Terminals { get; }

Property Value

TokenSymbolCollection

TokenSymbols

A collection of the Grammar's TokenSymbols.

public TokenSymbolCollection TokenSymbols { get; }

Property Value

TokenSymbolCollection

Methods

ConvertFromGoldParser(Stream)

Converts a grammar file produced by GOLD Parser into a Grammar.

public static Grammar ConvertFromGoldParser(Stream grammarFile)

Parameters

grammarFile Stream

A Stream containing the GOLD Parser grammar file.

Returns

Grammar

Remarks

Both Enhanced Grammar Tables (EGT) and Compiled Grammar Tables (CGT) files are supported.

Exceptions

NotSupportedException

The data format is unsupported.

InvalidDataException

The grammar contains invalid data.

ConvertFromGoldParser(string)

Converts a grammar file produced by GOLD Parser into a Grammar.

public static Grammar ConvertFromGoldParser(string path)

Parameters

path string

The path to the grammar file.

Returns

Grammar

Remarks

Both Enhanced Grammar Tables (EGT) and Compiled Grammar Tables (CGT) files are supported.

Exceptions

NotSupportedException

The data format is unsupported.

InvalidDataException

The grammar contains invalid data.

GetNonterminal(NonterminalHandle)

Gets the Nonterminal pointed by the given NonterminalHandle.

public Nonterminal GetNonterminal(NonterminalHandle handle)

Parameters

handle NonterminalHandle

A handle to the nonterminal.

Returns

Nonterminal

Exceptions

ArgumentNullException

handle's HasValue property is false.

ArgumentOutOfRangeException

handle points to a nonterminal that does not exist.

GetProduction(ProductionHandle)

Gets the Production pointed by the given ProductionHandle.

public Production GetProduction(ProductionHandle handle)

Parameters

handle ProductionHandle

A handle to the production.

Returns

Production

Exceptions

ArgumentNullException

handle's HasValue property is false.

ArgumentOutOfRangeException

handle points to a production that does not exist.

GetSymbolFromSpecialName(string, bool)

Looks up a token symbol or nonterminal with the specified special name.

public EntityHandle GetSymbolFromSpecialName(string specialName, bool throwIfNotFound = false)

Parameters

specialName string

The symbol's special name.

throwIfNotFound bool

Whether to throw an exception if the symbol was not found. Defaults to .

Returns

EntityHandle

An EntityHandle containing either a TokenSymbolHandle or a NonterminalHandle pointing to the symbol with the specified special name, or pointing to nothing if the symbol was not found and throwIfNotFound has a value of false.

Remarks

Special names are intended to be used on token symbols that will be emitted by custom tokenizers. Because symbol names are not guaranteed to be unique, a special name provides a guaranteed way to retrieve the handle for a specific symbol.

Exceptions

ArgumentNullException

specialName is null.

KeyNotFoundException

The symbol was not found and throwIfNotFound had a value of true.

GetTokenSymbol(TokenSymbolHandle)

Gets the TokenSymbol pointed by the given TokenSymbolHandle.

public TokenSymbol GetTokenSymbol(TokenSymbolHandle handle)

Parameters

handle TokenSymbolHandle

A handle to the token symbol.

Returns

TokenSymbol

Exceptions

ArgumentNullException

handle's HasValue property is false.

ArgumentOutOfRangeException

handle points to a token symbol that does not exist.

IsTerminal(TokenSymbolHandle)

Checks whether the given TokenSymbolHandle points to a token symbol with the Terminal flag set.

public bool IsTerminal(TokenSymbolHandle handle)

Parameters

handle TokenSymbolHandle

The token symbol handle to check.

Returns

bool

Load(ImmutableArray<byte>)

Creates a Grammar from an immutable byte array.

public static Grammar Load(ImmutableArray<byte> grammarData)

Parameters

grammarData ImmutableArray<byte>

An ImmutableArray<T> containing the grammar's data.

Returns

Grammar

Exceptions

ArgumentNullException

grammarData has its IsDefault property set to true.

NotSupportedException

The data format is unsupported.

InvalidDataException

The grammar contains invalid data.

Load(string)

Creates a Grammar from a file. The entire file is read in memory.

public static Grammar Load(string path)

Parameters

path string

The path to the file.

Returns

Grammar

Exceptions

ArgumentNullException

path is null.

NotSupportedException

The data format is unsupported.

InvalidDataException

The grammar contains invalid data.