8using System.Collections.Generic;
51 public const char EOF = (char)0xff;
669 throw new ApplicationException(
string.Format(
"Unknown state: {0}. Cannot create a state class.", state));
672 return stateBehavior;
716 private Dictionary<CurrentState, IStateBehavior>
_stateBehaviors =
new Dictionary<CurrentState, IStateBehavior>();
751 Console.WriteLine(
" --> State Transition: {0} -> {1}",
_currentState, newState);
Represents constants used in the State machine. This is needed in a separate class because 1) C# requ...
const char EOF
Indicates End-of-file (no more data available).
Represents being done with input.
Represents being inside a double-quote string where filtering is essentially turned off until the end...
Represents being in an escaped character sequence inside a double- quoted string. We don't do anythin...
Represents being in an escaped character sequence inside a single- quoted string. We don't do anythin...
Class factory for generating the state class instances.
static IStateBehavior CreateState(CurrentState state)
Create an instance of the specified state class.
Represents normal text behavior.
Represents being inside a single-quoted string where filtering is effectively turned off until the en...
Represents the state machine. This maintains the context in which the state machine runs.
Dictionary< CurrentState, IStateBehavior > _stateBehaviors
Maps values from the CurrentState enumeration to instances of the IStateBehavior representing the beh...
int _textIndex
Index into the text to be filtered.
string _inputText
The text to be filtered.
CurrentState _currentState
The current state of the machine.
string RemoveComments(string text)
Entry point for callers to filter text. Removes C++-style line and block comments from the text.
IStateBehavior _currentStateBehavior
The current behavior (that is, a reference to the state behavior class) for the current state.
void _SetNextState(CurrentState newState)
Helper method to transition the state machine to the specified state. Does nothing if the new state i...
StringBuilder _outputText
The results of the filtering.
Represents a class that implements one state of the state machine.
CurrentState GoNext(IStateContext context)
Represents the context as passed to each state class.
char GetNextCharacter()
Get the next character from the input.
void OutputCharacter(char character)
Write the character to the context. This is how the parser accumulates the filtered text.
The namespace containing all Design Pattern Examples implemented in C#.
CurrentState
Represents the current state of the state machine.
@ StartComment
/ transitions to LineComment, * transitions to BlockComment, EOF transitions to Done,...
@ BlockComment
* transitions to EndBlockComment, EOF transitions to Done
@ Initial
State before the state machine actually starts. transitions to NormalText.
@ DoubleQuotedText
\ transitions to EscapedDoubleQuoteText, " transitions to NormalText, EOF transitions to Done
@ EscapedDoubleQuoteText
\ transitions to QuotedText, EOF transitions to Done
@ EscapedSingleQuoteText
\ transitions to SingleQuotedText, EOF transitions to Done
@ SingleQuotedText
‘’` transitions to EscapedSingleQuoteText, \ transitions to NormalText, EOF transitions to Done
@ EndBlockComment
/ transitions to NormalText, EOF transitions to Done, all else transitions to BlockComment
@ LineComment
\\n transitions to NormalText, EOF transitions to Done
@ NormalText
" transitions to QuotedText, / transitions to StartComment, EOF transitions to Done
@ Done
Indicates processing is done.