Class Grammar
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
DfaOnChar
The Grammar's Dfa<TChar> on char, if it exists.
public Dfa<char>? DfaOnChar { get; }
Property Value
GrammarInfo
General information about this Grammar.
public GrammarInfo GrammarInfo { get; }
Property Value
Groups
public GroupCollection Groups { get; }
Property Value
HasUnknownData
Whether the Grammar contains data that are not recognized by this version of Farkle.
public bool HasUnknownData { get; }
Property Value
LrStateMachine
The Grammar's LrStateMachine, if it exists.
public LrStateMachine? LrStateMachine { get; }
Property Value
Nonterminals
A collection of the Grammar's Nonterminals.
public NonterminalCollection Nonterminals { get; }
Property Value
Productions
A collection of the Grammar's Productions.
public ProductionCollection Productions { get; }
Property Value
SpecialNameDefinitions
A collection of the Grammar's SpecialNameDefinitions.
public SpecialNameDefinitionCollection SpecialNameDefinitions { get; }
Property Value
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
TokenSymbols
A collection of the Grammar's TokenSymbols.
public TokenSymbolCollection TokenSymbols { get; }
Property Value
Methods
ConvertFromGoldParser(Stream)
Converts a grammar file produced by GOLD Parser into a Grammar.
public static Grammar ConvertFromGoldParser(Stream grammarFile)
Parameters
Returns
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
stringThe path to the grammar file.
Returns
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
NonterminalHandleA handle to the nonterminal.
Returns
Exceptions
- ArgumentNullException
- 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
ProductionHandleA handle to the production.
Returns
Exceptions
- ArgumentNullException
- 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
stringThe symbol's special name.
throwIfNotFound
boolWhether 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
TokenSymbolHandleA handle to the token symbol.
Returns
Exceptions
- ArgumentNullException
- 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
TokenSymbolHandleThe token symbol handle to check.
Returns
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
Exceptions
- ArgumentNullException
- 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
stringThe path to the file.
Returns
Exceptions
- ArgumentNullException
path
is null.- NotSupportedException
The data format is unsupported.
- InvalidDataException
The grammar contains invalid data.