Struct AlangCursor
Represents a cursor for parsing Alang expressions from an input string.
Inherited Members
Namespace: Automata.Core.Alang
Assembly: Automata.Core.dll
Syntax
public ref struct AlangCursor
Remarks
A cursor is a lightweight struct that consumes characters from the left of a string input. The input string is never modified. Instead, the cursor maintains a lightweight span of the remaining input. The parser in the Automata.Core.Alang namespace only needs to create a single cursor instance that is passed along during the parse process. The contract of the AlangCursor is that it always points to a non-white-space character or EOI if the input is empty.
Consequently:
- All methods in AlangCursor that move the cursor must ensure on exit that leading whitespace is trimmed.
- All methods in AlangCursor can assume on entry that the input is trimmed of leading whitespace.
Constructors
| Edit this page View SourceAlangCursor(string)
Represents a cursor for parsing Alang expressions from an input string.
Declaration
public AlangCursor(string input)
Parameters
Type | Name | Description |
---|---|---|
string | input | The input string to parse. |
Remarks
A cursor is a lightweight struct that consumes characters from the left of a string input. The input string is never modified. Instead, the cursor maintains a lightweight span of the remaining input. The parser in the Automata.Core.Alang namespace only needs to create a single cursor instance that is passed along during the parse process. The contract of the AlangCursor is that it always points to a non-white-space character or EOI if the input is empty.
Consequently:
- All methods in AlangCursor that move the cursor must ensure on exit that leading whitespace is trimmed.
- All methods in AlangCursor can assume on entry that the input is trimmed of leading whitespace.
Properties
| Edit this page View SourceCursorIndex
Current position of the cursor in the original input string.
Declaration
public readonly int CursorIndex { get; }
Property Value
Type | Description |
---|---|
int |
IsEmpty
Indicates whether the cursor has reached the end of the input.
Declaration
public readonly bool IsEmpty { get; }
Property Value
Type | Description |
---|---|
bool |
IsExpressionStart
Indicates whether the current character indicates the start of an expression. false if the input is empty.
Declaration
public readonly bool IsExpressionStart { get; }
Property Value
Type | Description |
---|---|
bool |
NextAsString
String representation of the next character in the input, or "End-Of-Input" if at the end of the input.
Declaration
public readonly string NextAsString { get; }
Property Value
Type | Description |
---|---|
string |
Methods
| Edit this page View SourceConsumeAtom()
Consumes an Atom from the input.
Declaration
public Atom ConsumeAtom()
Returns
Type | Description |
---|---|
Atom | The consumed Atom. |
Remarks
This method will return an empty (invalid) Atom if no characters could be consumed. It is up to the calling code to handle this.
ConsumeRightParen()
Consumes a right parenthesis (RightParen) from the input, advancing the cursor.
Declaration
public void ConsumeRightParen()
Exceptions
Type | Condition |
---|---|
AlangFormatException | Thrown when the next character is not a right parenthesis. |
Is(char)
Indicates whether the first character in the remaining input is the specified character.
Declaration
public readonly bool Is(char c)
Parameters
Type | Name | Description |
---|---|---|
char | c | The character to check. |
Returns
Type | Description |
---|---|
bool | true
|
ShouldBeRightParen()
Validates that the current character is a right parenthesis.
Declaration
public readonly void ShouldBeRightParen()
Exceptions
Type | Condition |
---|---|
AlangFormatException | Thrown when the current character is not a right parenthesis ')'. |
ShouldNotBeEmpty()
Validates that the cursor is not at the end of input.
Declaration
public readonly void ShouldNotBeEmpty()
Remarks
This method enforces the rule that empty input is not valid in Alang expressions. To represent an empty set, use parentheses '()' instead.
Exceptions
Type | Condition |
---|---|
AlangFormatException | Thrown when the cursor is at the end of input. |
ShouldNotBeOperator()
Validates that the current character is not an operator.
Declaration
public readonly void ShouldNotBeOperator()
Remarks
This method ensures expressions do not start with an operator.
Exceptions
Type | Condition |
---|---|
AlangFormatException | Thrown when the current character is an operator. |
ShouldNotBeRightParen()
Validates that the current character is not a right parenthesis.
Declaration
public readonly void ShouldNotBeRightParen()
Exceptions
Type | Condition |
---|---|
AlangFormatException | Thrown when the current character is a right parenthesis ')'. |
ToString()
String representation of the remaining input.
Declaration
public override readonly string ToString()
Returns
Type | Description |
---|---|
string | A string that represents the remaining input. |
Overrides
| Edit this page View SourceTryConsume(char)
Tries to consume the specified character from the input and advances the cursor if successful.
Declaration
public bool TryConsume(char c)
Parameters
Type | Name | Description |
---|---|---|
char | c | The character to attempt to consume. |
Returns
Type | Description |
---|---|
bool | true
|
TryConsumeAny(params char[])
Tries to consume one of the specified characters from the input and advances the cursor if successful.
Declaration
public char TryConsumeAny(params char[] chars)
Parameters
Type | Name | Description |
---|---|---|
char[] | chars | An array of characters to attempt to consume. |
Returns
Type | Description |
---|---|
char | The character that was consumed if successful; otherwise, Invalid. |
Remarks
This method supports inclusion of EOI to also match against End-Of-Input.
TryConsumeDifferenceOperator()
Tries to consume a difference operator (Difference) from the input and advances the cursor if successful.
Declaration
public bool TryConsumeDifferenceOperator()
Returns
Type | Description |
---|---|
bool | true
|
TryConsumeIntersectionOperator()
Tries to consume an intersection operator (Intersection) from the input and advances the cursor if successful.
Declaration
public bool TryConsumeIntersectionOperator()
Returns
Type | Description |
---|---|
bool | true
|
TryConsumeLeftParen()
Tries to consume a left parenthesis (LeftParen) from the input and advances the cursor if successful.
Declaration
public bool TryConsumeLeftParen()
Returns
Type | Description |
---|---|
bool | true
|
TryConsumeUnionOperator()
Tries to consume a union operator (Union) from the input and advances the cursor if successful.
Declaration
public bool TryConsumeUnionOperator()
Returns
Type | Description |
---|---|
bool | true
|