public class GenericParser extends Object implements Parser
Parser interface that is common to both
HtmlParser and JavascriptParser.
Provides methods for parsing input and ensuring that all in-state, entering-a-state and exiting-a-state callbacks are invoked as appropriate.
This class started as abstract but it was found better for testing to make it instantiatable so that the parsing logic can be tested with dummy state transitions.
| Modifier and Type | Field and Description |
|---|---|
protected int |
columnNumber |
protected com.google.streamhtmlparser.impl.InternalState |
currentState |
protected com.google.streamhtmlparser.impl.InternalState |
initialState |
protected Map<com.google.streamhtmlparser.impl.InternalState,ExternalState> |
intToExtStateTable |
protected int |
lineNumber |
protected com.google.streamhtmlparser.impl.ParserStateTable |
parserStateTable |
STATE_ERROR| Modifier | Constructor and Description |
|---|---|
protected |
GenericParser(GenericParser aGenericParser)
Constructs a generic parser that is an exact copy of the
one given.
|
protected |
GenericParser(com.google.streamhtmlparser.impl.ParserStateTable parserStateTable,
Map<com.google.streamhtmlparser.impl.InternalState,ExternalState> intToExtStateTable,
com.google.streamhtmlparser.impl.InternalState initialState) |
| Modifier and Type | Method and Description |
|---|---|
int |
getColumnNumber()
Returns the current column number.
|
int |
getLineNumber()
Returns the current line number.
|
ExternalState |
getState()
Return the current state of the parser.
|
protected com.google.streamhtmlparser.impl.InternalState |
handleEnterState(com.google.streamhtmlparser.impl.InternalState currentState,
com.google.streamhtmlparser.impl.InternalState expectedNextState,
char input)
Invoked when the parser enters a new state.
|
protected com.google.streamhtmlparser.impl.InternalState |
handleExitState(com.google.streamhtmlparser.impl.InternalState currentState,
com.google.streamhtmlparser.impl.InternalState expectedNextState,
char input)
Invoked when the parser exits a state.
|
protected com.google.streamhtmlparser.impl.InternalState |
handleInState(com.google.streamhtmlparser.impl.InternalState currentState,
char input)
Invoked for each character read when no state change occured.
|
void |
parse(char input)
Main loop for parsing of input.
|
void |
parse(String input)
Tell the parser to process the provided
String. |
protected void |
record(char input)
Perform some processing on the given character.
|
void |
reset()
Reset the parser back to its initial default state.
|
void |
setColumnNumber(int columnNumber)
Sets the current column number which is returned during error messages.
|
void |
setLineNumber(int lineNumber)
Sets the current line number which is returned during error messages.
|
protected void |
setNextState(com.google.streamhtmlparser.impl.InternalState nextState) |
protected final com.google.streamhtmlparser.impl.ParserStateTable parserStateTable
protected final Map<com.google.streamhtmlparser.impl.InternalState,ExternalState> intToExtStateTable
protected final com.google.streamhtmlparser.impl.InternalState initialState
protected com.google.streamhtmlparser.impl.InternalState currentState
protected int lineNumber
protected int columnNumber
protected GenericParser(com.google.streamhtmlparser.impl.ParserStateTable parserStateTable,
Map<com.google.streamhtmlparser.impl.InternalState,ExternalState> intToExtStateTable,
com.google.streamhtmlparser.impl.InternalState initialState)
protected GenericParser(GenericParser aGenericParser)
aGenericParser - the GenericParser to copypublic void parse(String input) throws ParseException
String. This is just a
convenience method that wraps over Parser.parse(char).parse in interface Parserinput - the String to parseParseException - if an unrecoverable error occurred during parsingpublic void parse(char input)
throws ParseException
Absent any callbacks defined, this function simply determines the
next state to switch to based on the ParserStateTable which is
derived from a state-machine configuration file in the original C++ parser.
However some states have specific callbacks defined which when
receiving specific characters may decide to overwrite the next state to
go to. Hence the next state is a function both of the main state table
in ParserStateTable as well as specific run-time information
from the callback functions.
Also note that the callbacks are called in a proper sequence, first the exit-state one then the enter-state one and finally the in-state one. Changing the order may result in a functional change.
parse in interface Parserinput - the input character to parse (process)ParseException - if an unrecoverable error occurred during parsingpublic ExternalState getState()
public void reset()
public void setLineNumber(int lineNumber)
setLineNumber in interface ParserlineNumber - the line number to set in the parserpublic int getLineNumber()
getLineNumber in interface Parserpublic void setColumnNumber(int columnNumber)
setColumnNumber in interface ParsercolumnNumber - the column number to set in the parserpublic int getColumnNumber()
getColumnNumber in interface Parserprotected void setNextState(com.google.streamhtmlparser.impl.InternalState nextState)
throws ParseException
ParseExceptionprotected com.google.streamhtmlparser.impl.InternalState handleEnterState(com.google.streamhtmlparser.impl.InternalState currentState,
com.google.streamhtmlparser.impl.InternalState expectedNextState,
char input)
throws ParseException
currentState - the current state of the parserexpectedNextState - the next state according to the
state table definitioninput - the last character parsedexpectedNextState providedParseException - if an unrecoverable error occurred during parsingprotected com.google.streamhtmlparser.impl.InternalState handleExitState(com.google.streamhtmlparser.impl.InternalState currentState,
com.google.streamhtmlparser.impl.InternalState expectedNextState,
char input)
throws ParseException
currentState - the current state of the parserexpectedNextState - the next state according to the
state table definitioninput - the last character parsedexpectedNextState providedParseException - if an unrecoverable error occurred during parsingprotected com.google.streamhtmlparser.impl.InternalState handleInState(com.google.streamhtmlparser.impl.InternalState currentState,
char input)
throws ParseException
currentState - the current state of the parserinput - the last character parsedexpectedNextState providedParseException - if an unrecoverable error occurred during parsingprotected void record(char input)
input - the input character to operate onCopyright © 2010-2012 Google. All Rights Reserved.