Package edu.hws.jcm.data
Class ParserContext
java.lang.Object
edu.hws.jcm.data.ParserContext
- All Implemented Interfaces:
Serializable
A ParserContext holds all the state data for a parsing operation, including the
string that is being parsed, a pointer to the current position in that string,
and the most recently parsed token from the string. The ParserContext object
does the tokenization. Token types are retrieved by calling look() and
next(). Attributes of the token are then available in the member variables
tokenString, tokenObject, and tokenValue. You will probably only use this
if you write a ParserExtension.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe string that is being parsed.static final int
One of the possible token types returned by look() and next().static final int
One of the possible token types returned by look() and next().static final int
One of the possible token types returned by look() and next().static final int
One of the possible token types returned by look() and next().int
The options from the Parser.int
Current position in that string, indicating how many characters have been consumed.The ExpressionProgram that is being generated as the string is parsed.protected SymbolTable
The Parser's symbol table, which is used for looking up tokens of type IDENTIFIER.int
The most recently read token type, or NONE if that token has been consumed by a call to next().If the most recently read token was of type IDENTIFIER, then this is the corresponding MathObject from the symbol table, or null if the identifier is not in the symbol table.The substring of the parse string that corresponds to the most recently read token.double
If the most recently read token was of type NUMBER, then this is its numerical value. -
Constructor Summary
ConstructorsConstructorDescriptionParserContext
(String data, int options, SymbolTable symbols) Create a ParserContext for parsing the data String, using the specified options and symbol table. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(MathObject sym) Add a new MathObject to the symbol table.Get the MathObject associated with name in the symbol table.int
look()
Look ahead at the next token in the data string, without consuming it.void
mark()
MathObjects added to the symbol table after a call to mark() will be removed by a later, matching call to revert().int
next()
Consume one token from the string.void
revert()
-
Field Details
-
END_OF_STRING
public static final int END_OF_STRINGOne of the possible token types returned by look() and next(). Represents the end of the string that is being parsed.- See Also:
-
NUMBER
public static final int NUMBEROne of the possible token types returned by look() and next(). Indicates aht the token is a number. The numerical value of the token is in the tokenValue member variable.- See Also:
-
IDENTIFIER
public static final int IDENTIFIEROne of the possible token types returned by look() and next(). The token is a word. If there is a MathObject in the symbol table associated with this word, then that object is in the tokenObject member variable. If not, tokenObject is null.- See Also:
-
OPCHARS
public static final int OPCHARSOne of the possible token types returned by look() and next(). Any other token besides end-of-string, number, or word. The only information about the token is the tokenString member variable. For some special operators (<> <= <=), the tokenString has two characters, but generally it has only one. Note that ** is translated to ^. Also, the special tokens "and", "or", and "not" are translated to type OPCHARS with tokenString equal to "&", "|", or "~" (but only if options & BOOLEANS is != 0).- See Also:
-
data
The string that is being parsed. -
pos
public int posCurrent position in that string, indicating how many characters have been consumed. -
prog
The ExpressionProgram that is being generated as the string is parsed. Note that while parsing a ConditionalExpression, the value of prog is temporarily changed. ParserExtensions might want to do something similar. -
token
public int tokenThe most recently read token type, or NONE if that token has been consumed by a call to next(). The value NONE is never returned by look() or next(). -
tokenString
The substring of the parse string that corresponds to the most recently read token. This can change when look() or next() is called. -
tokenObject
If the most recently read token was of type IDENTIFIER, then this is the corresponding MathObject from the symbol table, or null if the identifier is not in the symbol table. -
tokenValue
public double tokenValueIf the most recently read token was of type NUMBER, then this is its numerical value. -
options
public int optionsThe options from the Parser. Some of these options affect tokenization, such as whether BOOLEANS is enabled. -
symbols
The Parser's symbol table, which is used for looking up tokens of type IDENTIFIER.
-
-
Constructor Details
-
ParserContext
Create a ParserContext for parsing the data String, using the specified options and symbol table. A new ExpressionProgram is created to hold the program that will be generated from the string.
-
-
Method Details
-
mark
public void mark()MathObjects added to the symbol table after a call to mark() will be removed by a later, matching call to revert(). In the meantime, older symbols of the same name will only be hidden, not replaced, so they will still be there after the revert. It is important that a call to this routine is followed by a later call to revert! No error checking is done to make sure that this is true. -
revert
public void revert() -
get
Get the MathObject associated with name in the symbol table. -
add
Add a new MathObject to the symbol table. -
next
public int next()Consume one token from the string. The token type is returned. After this is called, attributes of the token can be obtained from the public member variables tokenString, tokenObject, and tokenValue. Note that the END_OF_STRING token is never really consumed and can be returned multiple times. Can throw a ParseError in the case of an illegal numeric token. -
look
public int look()Look ahead at the next token in the data string, without consuming it. Successive calls to look() will return the same token. (The token must be consumed by a call to next().) The token type is returned. After a call to look(), attributes of the token can be obtained from the public member variables tokenString, tokenObject, and tokenValue. Can throw a ParseError in the case of an illegal numeric token.
-