Grammar Class
Represents a grammar that can be used with a recursive descent parser.
public class Grammar : IKeyedObject
- Inheritance:
- System.Object Object
- Implements:
- IKeyedObject
Constructors
Grammar(String)
Initializes a new instance of the Grammar
class.
public Grammar(string key)
Parameter | Type | Description |
---|---|---|
key | System.String | A string that identifies the grammar, generally the related language name. |
Properties
IsCompiled
Gets whether the grammar and its elements are compiled, via a call to its Compile() method.
public bool IsCompiled { get; }
Property Value
- System.Boolean:
true
if the grammar and its elements are compiled; otherwise,false
.
Key
Gets the string that identifies the grammar, generally the related language name.
public string Key { get; }
Property Value
- System.String:
The string that identifies the grammar, generally the related language name.
NonTerminals
Gets a dictionary of all non-terminals defined in the grammar, with the key being the non-terminal string key.
public IDictionary<string, NonTerminal> NonTerminals { get; }
Property Value
- System.Collections.Generic.IDictionary<System.String, NonTerminal>:
A dictionary of all non-terminals defined in the grammar, with the key being the non-terminal string key.
Remarks
This property returns null
until the grammar has been compiled.
Root
Gets or sets the root NonTerminal of the grammar, meaning the first non-terminal that is attempted to be matched when parsing begins.
public NonTerminal Root { get; set; }
Property Value
- NonTerminal:
The root NonTerminal of the grammar.
Remarks
Once the grammar has been compiled, the setter of this property will throw an exception if called.
Terminals
Gets a dictionary of all terminals defined in the grammar, with the key being the terminal string key.
public IDictionary<string, Terminal> Terminals { get; }
Property Value
- System.Collections.Generic.IDictionary<System.String, Terminal>:
A dictionary of all terminals defined in the grammar, with the key being the terminal string key.
Remarks
This property returns null
until the grammar has been compiled.
Methods
Ast(String, ITreeConstructionNode[])
Creates an ITreeConstructionNode that generates an IAstNode with the specified value and optional child nodes.
public static ITreeConstructionNode Ast(string value, params ITreeConstructionNode[] childNodes)
Parameter | Type | Description |
---|---|---|
value | System.String | The value to assign to the generated IAstNode. |
childNodes | ITreeConstructionNode[] | The optional child ITreeConstructionNode objects. |
Returns
- ITreeConstructionNode:
The ITreeConstructionNode that was created.
Ast<TAstNode>()
Creates an ITypedTreeConstructionNode<TAstNode> that generates an IAstNode of the specified type.
public static ITypedTreeConstructionNode<TAstNode> Ast<TAstNode>()
where TAstNode : class, IAstNode, new()
- Type Parameters:
-
TAstNode
-The type of IAstNode-based class to create.
Returns
- ITypedTreeConstructionNode<TAstNode>:
The ITypedTreeConstructionNode<TAstNode> that was created.
AstChildFrom(String)
Creates an ITreeConstructionNode that generates an IAstNode from the first child of an IAstNodeMatch that has the specified label.
public static ITreeConstructionNode AstChildFrom(string label)
Parameter | Type | Description |
---|---|---|
label | System.String | The IAstNodeMatch for which to search. |
Returns
- ITreeConstructionNode:
The ITreeConstructionNode that was created.
AstChildFrom(String, Int32)
Creates an ITreeConstructionNode that generates an IAstNode from the specified child of an IAstNodeMatch that has the specified label.
public static ITreeConstructionNode AstChildFrom(string label, int childIndex)
Parameter | Type | Description |
---|---|---|
label | System.String | The IAstNodeMatch for which to search. |
childIndex | System.Int32 | The index of the child. |
Returns
- ITreeConstructionNode:
The ITreeConstructionNode that was created.
AstChildrenFrom(String)
Creates an ITreeConstructionNode that generates an IAstNode from the children of an IAstNodeMatch that has the specified label.
public static ITreeConstructionNode AstChildrenFrom(string label)
Parameter | Type | Description |
---|---|---|
label | System.String | The IAstNodeMatch for which to search. |
Returns
- ITreeConstructionNode:
The ITreeConstructionNode that was created.
AstChildrenFrom(String, Int32)
Creates an ITreeConstructionNode that generates an IAstNode from the descendants at the specified nesting level of an IAstNodeMatch that has the specified label, useful for gathering desired nodes two or more levels deep.
public static ITreeConstructionNode AstChildrenFrom(string label, int nestingLevel)
Parameter | Type | Description |
---|---|---|
label | System.String | The IAstNodeMatch for which to search. |
nestingLevel | System.Int32 | The zero-based target nesting level, where |
Returns
- ITreeConstructionNode:
The ITreeConstructionNode that was created.
AstConditional(String, ITreeConstructionNode[])
Creates an ITreeConstructionNode that generates an IAstNode with the specified value and optional child nodes, but only if there is more than one child available. If one child is available, it is directly returned.
public static ITreeConstructionNode AstConditional(string value, params ITreeConstructionNode[] childNodes)
Parameter | Type | Description |
---|---|---|
value | System.String | The value to assign to the generated IAstNode. |
childNodes | ITreeConstructionNode[] | The optional child ITreeConstructionNode objects. |
Returns
- ITreeConstructionNode:
The ITreeConstructionNode that was created.
AstConditional<TAstNode>(ITreeConstructionNode[])
Creates an ITypedTreeConstructionNode<TAstNode> that generates an IAstNode of the specified type, but only if there is more than one child available. If one child is available, it is directly returned.
public static ITypedTreeConstructionNode<TAstNode> AstConditional<TAstNode>(params ITreeConstructionNode[] childNodes)
where TAstNode : class, IAstNode, new()
- Type Parameters:
-
TAstNode
-
Parameter | Type | Description |
---|---|---|
childNodes | ITreeConstructionNode[] | The optional child ITreeConstructionNode objects. |
Returns
- ITypedTreeConstructionNode<TAstNode>:
The ITypedTreeConstructionNode<TAstNode> that was created.
AstConditionalFrom(String, String)
Creates an ITreeConstructionNode that generates an IAstNode with the specified value and child nodes from the children of an IAstNodeMatch that has the specified label, but only if there is at least one child available.
public static ITreeConstructionNode AstConditionalFrom(string value, string label)
Parameter | Type | Description |
---|---|---|
value | System.String | The value to assign to the generated IAstNode. |
label | System.String | The IAstNodeMatch for which to search. |
Returns
- ITreeConstructionNode:
The ITreeConstructionNode that was created.
AstCount(ITreeConstructionNode[])
Creates an ITreeConstructionNode that generates an IAstNode whose value is the count of IAstNode objects generated by contained nodes.
public static ITreeConstructionNode AstCount(params ITreeConstructionNode[] countNodes)
Parameter | Type | Description |
---|---|---|
countNodes | ITreeConstructionNode[] | The optional child ITreeConstructionNode objects. |
Returns
- ITreeConstructionNode:
The ITreeConstructionNode that was created.
AstFrom(String)
Creates an ITreeConstructionNode that generates an IAstNode from an IAstNodeMatch that has the specified label.
public static ITreeConstructionNode AstFrom(string label)
Parameter | Type | Description |
---|---|---|
label | System.String | The IAstNodeMatch for which to search. |
Returns
- ITreeConstructionNode:
The ITreeConstructionNode that was created.
AstFrom<TAstNode>(String)
Creates an ITypedTreeConstructionNode<TAstNode> that generates an IAstNode of the specified type from an IAstNodeMatch that has the specified label.
public static ITypedTreeConstructionNode<TAstNode> AstFrom<TAstNode>(string label)
where TAstNode : class, IAstNode
- Type Parameters:
-
TAstNode
-The type of IAstNode-based class to create.
Parameter | Type | Description |
---|---|---|
label | System.String | The IAstNodeMatch for which to search. |
Returns
- ITypedTreeConstructionNode<TAstNode>:
The ITypedTreeConstructionNode<TAstNode> that was created.
AstLeftAssociativity<TAstNode, TDerivedAstNode>(ITreeConstructionNode, Expression<Func<TDerivedAstNode, TAstNode>>, Expression<Func<TDerivedAstNode, TAstNode>>, Func<TDerivedAstNode, TDerivedAstNode, Boolean>)
Creates an ITypedTreeConstructionNode<TAstNode> that attempts to make a child node left associative instead of right associative, useful for scenarios such as binary operators.
public static ITypedTreeConstructionNode<TAstNode> AstLeftAssociativity<TAstNode, TDerivedAstNode>(ITreeConstructionNode childNode, Expression<Func<TDerivedAstNode, TAstNode>> leftPropExpression, Expression<Func<TDerivedAstNode, TAstNode>> rightPropExpression, Func<TDerivedAstNode, TDerivedAstNode, bool> condition)
where TAstNode : class, IAstNode where TDerivedAstNode : class, TAstNode
- Type Parameters:
-
TAstNode
-The type of IAstNode-based class to return.
TDerivedAstNode
-The type of IAstNode (that must inherit
TAstNode
) that supports associativity.
Parameter | Type | Description |
---|---|---|
childNode | ITreeConstructionNode | The child ITreeConstructionNode object. |
leftPropExpression | System.Linq.Expressions.Expression<System.Func<TDerivedAstNode, TAstNode>> | The lambda expression indicating the property for the left side branch. |
rightPropExpression | System.Linq.Expressions.Expression<System.Func<TDerivedAstNode, TAstNode>> | The lambda expression indicating the property for the right side branch. |
condition | System.Func<TDerivedAstNode, TDerivedAstNode, System.Boolean> | The optional lambda expression that is passed both the root and child nodes and returns |
Returns
- ITypedTreeConstructionNode<TAstNode>:
The ITypedTreeConstructionNode<TAstNode> that was created.
AstValueOf(ITreeConstructionNode, ITreeConstructionNode[])
Creates an ITreeConstructionNode that generates an IAstNode with the value of the specified ITreeConstructionNode's constructed node, and optional child nodes.
public static ITreeConstructionNode AstValueOf(ITreeConstructionNode valueOfNode, params ITreeConstructionNode[] childNodes)
Parameter | Type | Description |
---|---|---|
valueOfNode | ITreeConstructionNode | The ITreeConstructionNode whose constructed IAstNode value will be used. |
childNodes | ITreeConstructionNode[] | The optional child ITreeConstructionNode objects. |
Returns
- ITreeConstructionNode:
The ITreeConstructionNode that was created.
AstValueOfConditional(ITreeConstructionNode, ITreeConstructionNode[])
Creates an ITreeConstructionNode that generates an IAstNode with the value of the specified ITreeConstructionNode's constructed node, and optional child nodes, but only if there is more than one child available. If one child is available, it is directly returned.
public static ITreeConstructionNode AstValueOfConditional(ITreeConstructionNode valueOfNode, params ITreeConstructionNode[] childNodes)
Parameter | Type | Description |
---|---|---|
valueOfNode | ITreeConstructionNode | The ITreeConstructionNode whose constructed IAstNode value will be used. |
childNodes | ITreeConstructionNode[] | The optional child ITreeConstructionNode objects. |
Returns
- ITreeConstructionNode:
The ITreeConstructionNode that was created.
CanAlwaysMatch(IParserState)
Implements a ParserCanMatchCallback that always returns true
, and is useful for assigning to non-terminals that are called
from the Root non-terminal for a Grammar.
public static bool CanAlwaysMatch(IParserState state)
Parameter | Type | Description |
---|---|---|
state | IParserState | A IParserState that provides information about the parser's current state. |
Returns
- System.Boolean:
Always
true
.
Compile()
Compiles the grammar, which prepares the grammar and its elements for parsing usage.
public void Compile()
Remarks
Compiling the grammar also means that all elements of the language will be locked and no longer able to be modified.
ToEbnfString()
Outputs the contents of the grammar in EBNF form.
public string ToEbnfString()
Returns
- System.String:
A string containing the EBNF form of the grammar.
Validate()
Validates that the grammar and its elements are configured correctly and ready to be compiled.
public void Validate()
Remarks
An exception will be thrown if validation fails.
Inherited Members
- System.Object.ToString()
- System.Object.Equals(System.Object)
- System.Object.Equals(System.Object, System.Object)
- System.Object.ReferenceEquals(System.Object, System.Object)
- System.Object.GetHashCode()
- System.Object.GetType()
- System.Object.MemberwiseClone()