Implementation of the State_RemoveComments() function, along with the state machine used to filter out comments out of a piece of source code, as used in the State Pattern. More...
Go to the source code of this file.
Classes | |
struct | StateContext |
Represents the context in which the state machine runs. More... | |
struct | StateHandler |
Maps a value from the CurrentState enumeration to a function that handles the transition from that state to the next state based on the context. More... | |
Typedefs | |
typedef CurrentState(* | StateFunctionPtr) (struct StateContext *) |
Alias for a function pointer that takes a StateContext object and returns a value from the CurrentState enumeration. | |
typedef struct StateContext | StateContext |
Represents the context in which the state machine runs. | |
Enumerations | |
enum | CurrentState { State_Initial , State_NormalText , State_DoubleQuotedText , State_SingleQuotedText , State_EscapedDoubleQuoteText , State_EscapedSingleQuoteText , State_StartComment , State_LineComment , State_BlockComment , State_EndBlockComment , State_Done , State_Error } |
Represents the current state of the state machine. More... | |
enum | _Constants { EOF_CHAR = (char)-1 } |
Contains constants used in the state machine. More... | |
Functions | |
static const char * | _CurrentStateToString (CurrentState state) |
Convert the CurrentState enumeration to a string for output purposes. | |
static char | _GetNextCharacter (StateContext *context) |
Retrieve the next character from the input. | |
static void | _OutputCharacter (StateContext *context, char character) |
Save the character to the accumulation of the filtered text. | |
static CurrentState | _State_NormalText_GoNextState (StateContext *context) |
Handles the state of normal text behavior. | |
static CurrentState | _State_DoubleQuotedText_GoNextState (StateContext *context) |
Handles the state of being inside a double-quote string where filtering is essentially turned off until the end of the string is reached. | |
static CurrentState | _State_SingleQuotedText_GoNextState (StateContext *context) |
Handles the state of being inside a single-quoted string where filtering is effectively turned off until the end of the string is reached. | |
static CurrentState | _State_EscapedDoubleQuoteText_GoNextState (StateContext *context) |
Handles the state of being in an escaped character sequence inside a double-quoted string. We don't do anything with the escaped character other than output it. Handling escaped characters allows us to more accurately detect the end of the string. | |
static CurrentState | _State_EscapedSingleQuoteText_GoNextState (StateContext *context) |
Handles the state of being in an escaped character sequence inside a single-quoted string. We don't do anything with the escaped character other than output it. Handling escaped characters allows us to more accurately detect the end of the string. | |
static CurrentState | _State_StartComment_GoNextState (StateContext *context) |
Handles the state of being at the possible start of a line or block comment. | |
static CurrentState | _State_LineComment_GoNextState (StateContext *context) |
Handles the state of being in a line comment. | |
static CurrentState | _State_BlockComment_GoNextState (StateContext *context) |
Handles the state of being in a block comment. | |
static CurrentState | _State_EndBlockComment_GoNextState (StateContext *context) |
Handles the state of possibly being at the end of a block comment. | |
static CurrentState | _State_Done_GoNextState (StateContext *context) |
Handles the state of being done with input. | |
static StateFunctionPtr | _GetStateFunction (CurrentState state) |
Retrieve the function that is used to transition from the given state to another state. | |
static bool | _SetNextState (StateContext *context, CurrentState newState) |
Helper method to transition the state machine to the specified state. Does nothing if the new state is the same as the old state. Instantiates the state class the first time the state class is needed. | |
bool | State_RemoveComments (const char *text, DynamicString *filteredText) |
Entry point for callers to filter text. Removes C++-style line and block comments from the text. | |
Variables | |
static StateHandler | _stateHandlers [] |
Array of StateHandler objects that map CurrentState value to functions that transition from that state to the next based on the context. | |
Implementation of the State_RemoveComments() function, along with the state machine used to filter out comments out of a piece of source code, as used in the State Pattern.
Definition in file State_RemoveComments.c.
typedef struct StateContext StateContext |
Represents the context in which the state machine runs.
typedef CurrentState(* StateFunctionPtr) (struct StateContext *) |
Alias for a function pointer that takes a StateContext object and returns a value from the CurrentState enumeration.
Definition at line 36 of file State_RemoveComments.c.
enum _Constants |
Contains constants used in the state machine.
Enumerator | |
---|---|
EOF_CHAR | Indicates end of string. |
Definition at line 41 of file State_RemoveComments.c.
enum CurrentState |
Represents the current state of the state machine.
Definition at line 15 of file State_RemoveComments.c.
|
static |
Convert the CurrentState enumeration to a string for output purposes.
state | A value from the CurrentState enumeration. |
Definition at line 69 of file State_RemoveComments.c.
References State_BlockComment, State_Done, State_DoubleQuotedText, State_EndBlockComment, State_Error, State_EscapedDoubleQuoteText, State_EscapedSingleQuoteText, State_Initial, State_LineComment, State_NormalText, State_SingleQuotedText, and State_StartComment.
Referenced by _SetNextState().
|
static |
Retrieve the next character from the input.
Definition at line 151 of file State_RemoveComments.c.
References EOF_CHAR, StateContext::inputText, and StateContext::textIndex.
Referenced by _State_BlockComment_GoNextState(), _State_DoubleQuotedText_GoNextState(), _State_EndBlockComment_GoNextState(), _State_EscapedDoubleQuoteText_GoNextState(), _State_EscapedSingleQuoteText_GoNextState(), _State_LineComment_GoNextState(), _State_NormalText_GoNextState(), _State_SingleQuotedText_GoNextState(), and _State_StartComment_GoNextState().
|
static |
Retrieve the function that is used to transition from the given state to another state.
state | A value from the CurrentState enumeration indicating state for which to get a transition function. |
Definition at line 679 of file State_RemoveComments.c.
References _stateHandlers, and StateHandler::stateHandler.
Referenced by _SetNextState().
|
static |
Save the character to the accumulation of the filtered text.
context | The StateContext object controlling the state machine and which contains the DynamicString object being used to accumulate the filtered text. |
character | The character to save. |
Definition at line 173 of file State_RemoveComments.c.
References EOF_CHAR, StateContext::outputText, and StateContext::outputTextIndex.
Referenced by _State_DoubleQuotedText_GoNextState(), _State_EscapedDoubleQuoteText_GoNextState(), _State_EscapedSingleQuoteText_GoNextState(), _State_LineComment_GoNextState(), _State_NormalText_GoNextState(), _State_SingleQuotedText_GoNextState(), and _State_StartComment_GoNextState().
|
static |
Helper method to transition the state machine to the specified state. Does nothing if the new state is the same as the old state. Instantiates the state class the first time the state class is needed.
context | The StateContext object that controls the state machine. |
newState | A value from the CurrentState enumeration indicating the state to which to transition. |
Definition at line 713 of file State_RemoveComments.c.
References _CurrentStateToString(), _GetStateFunction(), StateContext::currentState, and StateContext::currentStateBehavior.
Referenced by State_RemoveComments().
|
static |
Handles the state of being in a block comment.
Process the next character from the context, returning the next state the context should move to.
Transitions to the following states for the seen input:
context | A StateContext object representing the context to use for getting the next character of input and to which to output characters. |
Definition at line 541 of file State_RemoveComments.c.
References _GetNextCharacter(), EOF_CHAR, State_BlockComment, State_Done, State_EndBlockComment, and State_Error.
|
static |
Handles the state of being done with input.
Process the next character from the context, returning the next state the context should move to.
Transitions to the following states for the seen input:
context | A StateContext object representing the context to use for getting the next character of input and to which to output characters. |
Definition at line 626 of file State_RemoveComments.c.
References State_Done, and State_Error.
|
static |
Handles the state of being inside a double-quote string where filtering is essentially turned off until the end of the string is reached.
Process the next character from the context, returning the next state the context should move to.
Transitions to the following states for the seen input:
context | A StateContext object representing the context to use for getting the next character of input and to which to output characters. |
Definition at line 259 of file State_RemoveComments.c.
References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_Done, State_DoubleQuotedText, State_Error, State_EscapedDoubleQuoteText, and State_NormalText.
|
static |
Handles the state of possibly being at the end of a block comment.
Process the next character from the context, returning the next state the context should move to.
Transitions to the following states for the seen input:
context | A StateContext object representing the context to use for getting the next character of input and to which to output characters. |
Definition at line 584 of file State_RemoveComments.c.
References _GetNextCharacter(), EOF_CHAR, State_BlockComment, State_Done, State_Error, and State_NormalText.
|
static |
Handles the state of being in an escaped character sequence inside a double-quoted string. We don't do anything with the escaped character other than output it. Handling escaped characters allows us to more accurately detect the end of the string.
Process the next character from the context, returning the next state the context should move to.
Transitions to the following states for the seen input:
context | A StateContext object representing the context to use for getting the next character of input and to which to output characters. |
Definition at line 361 of file State_RemoveComments.c.
References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_Done, State_DoubleQuotedText, and State_Error.
|
static |
Handles the state of being in an escaped character sequence inside a single-quoted string. We don't do anything with the escaped character other than output it. Handling escaped characters allows us to more accurately detect the end of the string.
Process the next character from the context, returning the next state the context should move to.
Transitions to the following states for the seen input:
context | A StateContext object representing the context to use for getting the next character of input and to which to output characters. |
Definition at line 403 of file State_RemoveComments.c.
References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_Done, State_Error, and State_SingleQuotedText.
|
static |
Handles the state of being in a line comment.
Process the next character from the context, returning the next state the context should move to.
Transitions to the following states for the seen input:
context | A StateContext object representing the context to use for getting the next character of input and to which to output characters. |
Definition at line 497 of file State_RemoveComments.c.
References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_Done, State_Error, State_LineComment, and State_NormalText.
|
static |
Handles the state of normal text behavior.
Process the next character from the context, returning the next state the context should move to.
Transitions to the following states for the seen input:
context | A StateContext object representing the context to use for getting the next character of input and to which to output characters. |
Definition at line 204 of file State_RemoveComments.c.
References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_Done, State_DoubleQuotedText, State_Error, State_NormalText, State_SingleQuotedText, and State_StartComment.
|
static |
Handles the state of being inside a single-quoted string where filtering is effectively turned off until the end of the string is reached.
Process the next character from the context, returning the next state the context should move to.
Transitions to the following states for the seen input:
context | A StateContext object representing the context to use for getting the next character of input and to which to output characters. |
Definition at line 310 of file State_RemoveComments.c.
References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_Done, State_Error, State_EscapedSingleQuoteText, State_NormalText, and State_SingleQuotedText.
|
static |
Handles the state of being at the possible start of a line or block comment.
Process the next character from the context, returning the next state the context should move to.
Transitions to the following states for the seen input:
context | A StateContext object representing the context to use for getting the next character of input and to which to output characters. |
Definition at line 445 of file State_RemoveComments.c.
References _GetNextCharacter(), _OutputCharacter(), EOF_CHAR, State_BlockComment, State_Done, State_Error, State_LineComment, State_NormalText, and State_StartComment.
bool State_RemoveComments | ( | const char * | text, |
DynamicString * | filteredText | ||
) |
Entry point for callers to filter text. Removes C++-style line and block comments from the text.
text | The text to filter. |
filteredText | A DynamicString that returns the filtered text. |
Definition at line 745 of file State_RemoveComments.c.
References _SetNextState(), StateContext::currentState, StateContext::currentStateBehavior, DynamicString_Set(), StateContext::inputText, StateContext::outputText, StateContext::outputTextIndex, State_Done, State_Error, State_Initial, State_NormalText, and StateContext::textIndex.
Referenced by State_Exercise().
|
static |
Array of StateHandler objects that map CurrentState value to functions that transition from that state to the next based on the context.
Definition at line 658 of file State_RemoveComments.c.
Referenced by _GetStateFunction().