Class GrammarSymbolExtensions
Contains extension methods that to set configuration options on IGrammarSymbol and IGrammarSymbol<T>.
public static class GrammarSymbolExtensions
- Inheritance
-
GrammarSymbolExtensions
Remarks
These methods apply to specific symbols within the grammar and do not modify the object they are called on. Instead, they return a new object with the requested configuration option changed.
Changing configuration on a symbol does not change the symbol's identity and the changed symbol instance will be treated as the same symbol as before when building the grammar. Consult the documentation of each method for information on what happens when a symbol exists in a grammar with varying configuration options.
Methods
AddSpecialName(IGrammarSymbol, string)
Adds a special name to the symbol, which is used to uniquely identify it within the grammar.
public static IGrammarSymbol AddSpecialName(this IGrammarSymbol symbol, string name)
Parameters
symbol
IGrammarSymbolname
string
Returns
Remarks
If a grammar has multiple symbols with the same special name, building will fail.
Calling this method multiple times will add multiple special names to the symbol.
- See Also
AddSpecialName<T>(IGrammarSymbol<T>, string)
Adds a special name to the symbol, which is used to uniquely identify it within the grammar.
public static IGrammarSymbol<T> AddSpecialName<T>(this IGrammarSymbol<T> symbol, string name)
Parameters
symbol
IGrammarSymbol<T>name
string
Returns
Type Parameters
T
Remarks
If a grammar has multiple symbols with the same special name, building will fail.
Calling this method multiple times will add multiple special names to the symbol.
- See Also
Cast(IGrammarSymbol)
Changes the type of IGrammarSymbol to a generic IGrammarSymbol<T> of type object, forcing it to return a value.
public static IGrammarSymbol<object?> Cast(this IGrammarSymbol symbol)
Parameters
symbol
IGrammarSymbolThe grammar symbol.
Returns
- IGrammarSymbol<object>
An IGrammarSymbol<T> that returns the object
symbol
would return. Ifsymbol
had been created with the untyped API, the returned object will be null.
Many<T, TCollection>(IGrammarSymbol<T>, bool)
Creates a symbol that can match the given symbol multiple times.
public static IGrammarSymbol<TCollection> Many<T, TCollection>(this IGrammarSymbol<T> symbol, bool atLeastOnce = false) where TCollection : ICollection<T>, new()
Parameters
symbol
IGrammarSymbol<T>The symbol to match multiple times.
atLeastOnce
boolWhether
symbol
must be matched at least once. Optional, defaults to false
Returns
- IGrammarSymbol<TCollection>
Type Parameters
T
The type of values the symbol returns.
TCollection
The type of collection to place the values of the symbol in. Must implement ICollection<T>.
Nullable<T>(IGrammarSymbol<T>)
Creates a symbol that can match either the given symbol once, or nothing. In the latter case it returns default.
public static IGrammarSymbol<T?> Nullable<T>(this IGrammarSymbol<T> symbol) where T : struct
Parameters
symbol
IGrammarSymbol<T>The symbol to make nullable.
Returns
- IGrammarSymbol<T?>
Type Parameters
T
The type of values the symbol returns.
- See Also
Optional<T>(IGrammarSymbol<T>)
Creates a symbol that can match either the given symbol once, or nothing. In the latter case it returns null.
public static IGrammarSymbol<T?> Optional<T>(this IGrammarSymbol<T> symbol)
Parameters
symbol
IGrammarSymbol<T>
Returns
Type Parameters
T
- See Also
Select<T, TNew>(IGrammarSymbol<T>, Func<T, TNew>, string?)
Creates a symbol that matches the given symbol and then applies a transformation to its returning value.
public static IGrammarSymbol<TNew> Select<T, TNew>(this IGrammarSymbol<T> symbol, Func<T, TNew> selector, string? name = null)
Parameters
symbol
IGrammarSymbol<T>The symbol to transform.
selector
Func<T, TNew>The transformation to apply to the value of
symbol
.name
stringThe name of the new symbol. If not provided, a default name will be generated.
Returns
- IGrammarSymbol<TNew>
Type Parameters
T
The type of values the symbol returns.
TNew
The type of values the new symbol will return.
SeparatedBy<T, TCollection>(IGrammarSymbol<T>, IGrammarSymbol, bool)
Creates a symbol that can match the given symbol multiple times, separated by another symbol.
public static IGrammarSymbol<TCollection> SeparatedBy<T, TCollection>(this IGrammarSymbol<T> symbol, IGrammarSymbol separator, bool atLeastOnce = false) where TCollection : ICollection<T>, new()
Parameters
symbol
IGrammarSymbol<T>The symbol to match multiple times.
separator
IGrammarSymbolThe symbol to match between the instances of
symbol
.atLeastOnce
boolWhether
symbol
must be matched at least once. Optional, defaults to false
Returns
- IGrammarSymbol<TCollection>
Type Parameters
T
The type of values the symbol returns.
TCollection
The type of collection to place the values of the symbol in. Must implement ICollection<T>.