10from .state_interface
import CurrentState, IStateBehavior, IStateContext, SpecialCase
28 case CurrentState.Initial:
29 stateAsString =
"Initial"
31 case CurrentState.NormalText:
32 stateAsString =
"NormalText"
34 case CurrentState.DoubleQuotedText:
35 stateAsString =
"DoubleQuotedText"
37 case CurrentState.SingleQuotedText:
38 stateAsString =
"SingleQuotedText"
40 case CurrentState.EscapedDoubleQuoteText:
41 stateAsString =
"EscapedDoubleQuoteText"
43 case CurrentState.EscapedSingleQuoteText:
44 stateAsString =
"EscapedSingleQuoteText"
46 case CurrentState.StartComment:
47 stateAsString =
"StartComment"
49 case CurrentState.LineComment:
50 stateAsString =
"LineComment"
52 case CurrentState.BlockComment:
53 stateAsString =
"BlockComment"
55 case CurrentState.EndBlockComment:
56 stateAsString =
"EndBlockComment"
58 case CurrentState.Done:
59 stateAsString =
"Done"
62 stateAsString =
"Unknown ({0})".format(state)
96 def GoNext(self, context : IStateContext) -> CurrentState:
97 nextState = CurrentState.NormalText
98 character = context.GetNextCharacter()
101 case SpecialCase.EOF_CHAR:
102 nextState = CurrentState.Done
105 context.OutputCharacter(character)
106 nextState = CurrentState.DoubleQuotedText
109 context.OutputCharacter(character)
110 nextState = CurrentState.SingleQuotedText
113 nextState = CurrentState.StartComment
116 context.OutputCharacter(character)
148 def GoNext(self, context : IStateContext) -> CurrentState:
149 nextState = CurrentState.DoubleQuotedText
151 character = context.GetNextCharacter()
154 case SpecialCase.EOF_CHAR:
155 nextState = CurrentState.Done
158 context.OutputCharacter(character)
159 nextState = CurrentState.NormalText
162 context.OutputCharacter(character)
163 nextState = CurrentState.EscapedDoubleQuoteText
166 context.OutputCharacter(character)
198 def GoNext(self, context : IStateContext) -> CurrentState:
199 nextState = CurrentState.SingleQuotedText
201 character = context.GetNextCharacter()
204 case SpecialCase.EOF_CHAR:
205 nextState = CurrentState.Done
208 context.OutputCharacter(character)
209 nextState = CurrentState.NormalText
212 context.OutputCharacter(character)
213 nextState = CurrentState.EscapedSingleQuoteText
216 context.OutputCharacter(character)
249 def GoNext(self, context : IStateContext) -> CurrentState:
250 nextState = CurrentState.DoubleQuotedText
252 character = context.GetNextCharacter()
255 case SpecialCase.EOF_CHAR:
256 nextState = CurrentState.Done
259 context.OutputCharacter(character)
292 def GoNext(self, context : IStateContext) -> CurrentState:
293 nextState = CurrentState.SingleQuotedText
295 character = context.GetNextCharacter()
298 case SpecialCase.EOF_CHAR:
299 nextState = CurrentState.Done
302 context.OutputCharacter(character)
334 def GoNext(self, context : IStateContext) -> CurrentState:
335 nextState = CurrentState.StartComment
337 character = context.GetNextCharacter()
340 case SpecialCase.EOF_CHAR:
341 nextState = CurrentState.Done
344 nextState = CurrentState.LineComment
347 nextState = CurrentState.BlockComment
353 context.OutputCharacter(
'/')
354 context.OutputCharacter(character)
355 nextState = CurrentState.NormalText
385 def GoNext(self, context : IStateContext) -> CurrentState:
386 nextState = CurrentState.LineComment
388 character = context.GetNextCharacter()
391 case SpecialCase.EOF_CHAR:
392 nextState = CurrentState.Done
395 context.OutputCharacter(character)
396 nextState = CurrentState.NormalText
430 def GoNext(self, context : IStateContext) -> CurrentState:
431 nextState = CurrentState.BlockComment
433 character = context.GetNextCharacter()
436 case SpecialCase.EOF_CHAR:
437 nextState = CurrentState.Done
440 nextState = CurrentState.EndBlockComment
475 def GoNext(self, context : IStateContext) -> CurrentState:
476 nextState = CurrentState.BlockComment
478 character = context.GetNextCharacter()
481 case SpecialCase.EOF_CHAR:
482 nextState = CurrentState.Done
485 nextState = CurrentState.NormalText
512 def GoNext(self, context : IStateContext) -> CurrentState:
514 return CurrentState.Done
541 case CurrentState.NormalText:
544 case CurrentState.DoubleQuotedText:
547 case CurrentState.SingleQuotedText:
550 case CurrentState.EscapedDoubleQuoteText:
553 case CurrentState.EscapedSingleQuoteText:
556 case CurrentState.StartComment:
559 case CurrentState.LineComment:
562 case CurrentState.BlockComment:
565 case CurrentState.EndBlockComment:
568 case CurrentState.Done:
572 msg =
"Unknown state: {0}. Cannot create a state class.".format(
574 raise ValueError(msg)
612 print(
" --> State Transition: {0} -> {1}".format(
687 character = SpecialCase.EOF_CHAR
699 if character != SpecialCase.EOF_CHAR:
Represents being done with input.
CurrentState GoNext(self, IStateContext context)
Process the next character from the context, returning the next state the context should move to.
Represents being inside a double-quote string where filtering is essentially turned off until the end...
CurrentState GoNext(self, IStateContext context)
Process the next character from the context, returning the next state the context should move to.
Represents being in an escaped character sequence inside a double- quoted string.
CurrentState GoNext(self, IStateContext context)
Process the next character from the context, returning the next state the context should move to.
Represents being in an escaped character sequence inside a single- quoted string.
CurrentState GoNext(self, IStateContext context)
Process the next character from the context, returning the next state the context should move to.
Class factory for generating the state class instances.
IStateBehavior CreateState(CurrentState state)
Create an instance of the specified state class.
Represents normal text behavior.
CurrentState GoNext(self, IStateContext context)
Process the next character from the context, returning the next state the context should move to.
Represents being inside a single-quoted string where filtering is effectively turned off until the en...
CurrentState GoNext(self, IStateContext context)
Process the next character from the context, returning the next state the context should move to.
Implementation of the state machine.
None _SetNextState(self, CurrentState newState)
Helper method to transition the state machine to the specified state.
_currentState
A value from the CurrentState enumeration indicating the current state of the machine.
_textIndex
Index into the text to be filtered.
_stateBehaviors
Maps values from the CurrentState enumeration to instances of the IStateBehavior representing the beh...
_currentStateBehavior
The current behavior (that is, a reference to the IStateBehavior instance) for the current state.
str RemoveComments(self, str text)
Entry point for callers to filter text.
None OutputCharacter(self, int character)
Save the character to the accumulation of the filtered text.
_inputText
The text to be filtered.
_outputText
The results of the filtering.
None __init__(self)
Constructor.
int GetNextCharacter(self)
Retrieve the next character from the input.
Represents a class that implements one state of the state machine.
Represents the context as passed to each state class.
str _CurrentStateToString(CurrentState state)
Convert the CurrentState enumeration to a string for output purposes.