-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start very basic work on actually parsing
- Loading branch information
1 parent
4315430
commit 4e84e82
Showing
2 changed files
with
117 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
namespace ConsoleBuffer | ||
{ | ||
enum ParserAppendResult | ||
{ | ||
/// <summary> | ||
/// Sequence incomplete. | ||
/// </summary> | ||
Pending = 0, | ||
/// <summary> | ||
/// Sequence complete, parser is ready to query for command. | ||
/// </summary> | ||
Complete, | ||
/// <summary> | ||
/// Sequence terminated due to invalid/unexpected terminator. | ||
/// </summary> | ||
Invalid, | ||
/// <summary> | ||
/// Character is not part of a parsing sequence and may be rendered. | ||
/// </summary> | ||
Render, | ||
/// <summary> | ||
/// Invalid parser state, should not occur. | ||
/// </summary> | ||
None, | ||
} | ||
|
||
enum ParserCommand | ||
{ | ||
/// <summary> | ||
/// Line-feed (\n) | ||
/// </summary> | ||
LF = 0, | ||
/// <summary> | ||
/// Carriage return (\r) | ||
/// </summary> | ||
CR, | ||
} | ||
|
||
sealed class AnsiParser | ||
{ | ||
enum SequenceType | ||
{ | ||
/// <summary> | ||
/// Indicates we are in a standard (\033) escape sequence. | ||
/// </summary> | ||
Standard = 0, | ||
/// <summary> | ||
/// Indicates we are in an operating system (\033 ]) escape sequence. | ||
/// </summary> | ||
OSCommand, | ||
/// <summary> | ||
/// We are not in a sequence. | ||
/// </summary> | ||
None, | ||
} | ||
private SequenceType sequenceType = SequenceType.None; | ||
private bool inSequence = false; | ||
|
||
public ParserCommand CurrentCommand { get; private set; } | ||
|
||
public AnsiParser() | ||
{ | ||
} | ||
|
||
public ParserAppendResult Append(int character) | ||
{ | ||
switch (this.sequenceType) | ||
{ | ||
case SequenceType.None: | ||
switch (character) | ||
{ | ||
case '\n': | ||
this.CurrentCommand = ParserCommand.LF; | ||
return ParserAppendResult.Complete; | ||
case '\r': | ||
this.CurrentCommand = ParserCommand.CR; | ||
return ParserAppendResult.Complete; | ||
default: | ||
return ParserAppendResult.Render; | ||
} | ||
} | ||
|
||
return ParserAppendResult.None; // should be unreachable. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters