Struct AlangCursor
Represents a cursor for parsing Alang regex strings.
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 the input regexString
as it is parsed.
For performance, regexString
is never split, copied or modified. Instead, the cursor maintains a lightweight span of the remaining regex string.
Also, the parser in the Automata.Core.Alang namespace only needs to maintain a single cursor instance through the entire parse process.
The contract of the AlangCursor is that it always points to a non-white-space character or EOI if the regex string is empty.
Consequently:
- All methods in AlangCursor that move the cursor must ensure on exit that leading
whitespace is skipped.
- All methods in AlangCursor can assume on entry that the cursor points to a non-whitespace character.
This struct is also the sole point for throwing all types of parsing exceptions. These are handled by the methods prefixed by Should
.
Constructors
| Edit this page View SourceAlangCursor(string)
Represents a cursor for parsing Alang regex strings.
Declaration
public AlangCursor(string regexString)
Parameters
Type | Name | Description |
---|---|---|
string | regexString | The regex string to parse. |
Remarks
A cursor is a lightweight struct that consumes characters from the left of the input regexString
as it is parsed.
For performance, regexString
is never split, copied or modified. Instead, the cursor maintains a lightweight span of the remaining regex string.
Also, the parser in the Automata.Core.Alang namespace only needs to maintain a single cursor instance through the entire parse process.
The contract of the AlangCursor is that it always points to a non-white-space character or EOI if the regex string is empty.
Consequently:
- All methods in AlangCursor that move the cursor must ensure on exit that leading
whitespace is skipped.
- All methods in AlangCursor can assume on entry that the cursor points to a non-whitespace character.
This struct is also the sole point for throwing all types of parsing exceptions. These are handled by the methods prefixed by Should
.
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 SourceConsumeSymbol()
Consumes an Symbol from the input.
Declaration
public Symbol ConsumeSymbol()
Returns
Type | Description |
---|---|
Symbol | The consumed Symbol. |
Remarks
This method will return an empty (invalid) Symbol if no characters could be consumed. It is up to the calling code to handle this.
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
|
IsNot(char)
Indicates whether the first character in the remaining input is not the specified character.
Declaration
public readonly bool IsNot(char c)
Parameters
Type | Name | Description |
---|---|---|
char | c | The character to check. |
Returns
Type | Description |
---|---|
bool | true
|
ShouldBeRightOperand(char)
Validates that there is right operand after a binary operator.
Declaration
public readonly void ShouldBeRightOperand(char binaryOperator)
Parameters
Type | Name | Description |
---|---|---|
char | binaryOperator | The preceding binary operator character. (Only used for better informed error messages if validation fails). |
Exceptions
Type | Condition |
---|---|
AlangFormatException | Thrown when the current character is not a right parenthesis ')'. |
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.
TryConsumeBinaryOperator(char)
Tries to consume a binary operator from the input and advances the cursor if successful.
Declaration
public bool TryConsumeBinaryOperator(char binaryOperator)
Parameters
Type | Name | Description |
---|---|---|
char | binaryOperator | The binary operator character to attempt to consume. |
Returns
Type | Description |
---|---|
bool | true
|
Exceptions
Type | Condition |
---|---|
AlangFormatException | Thrown when the binary operator was consumed but the next character was not IsExpressionStart . |