net.sourceforge.webcompmath.data
Class ParserContext

java.lang.Object
  extended by net.sourceforge.webcompmath.data.ParserContext
All Implemented Interfaces:
java.io.Serializable

public class ParserContext
extends java.lang.Object
implements java.io.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:
Serialized Form

Field Summary
 java.lang.String data
          The string that is being parsed.
static int END_OF_STRING
          One of the possible token types returned by look() and next().
static int IDENTIFIER
          One of the possible token types returned by look() and next().
static int NUMBER
          One of the possible token types returned by look() and next().
static int OPCHARS
          One of the possible token types returned by look() and next().
 int options
          The options from the Parser.
 int pos
          Current position in that string, indicating how many characters have been consumed.
 ExpressionProgram prog
          The ExpressionProgram that is being generated as the string is parsed.
protected  SymbolTable symbols
          The Parser's symbol table, which is used for looking up tokens of type IDENTIFIER.
 int token
          The most recently read token type, or NONE if that token has been consumed by a call to next().
 MathObject 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.
 java.lang.String tokenString
          The substring of the parse string that corresponds to the most recently read token.
 double tokenValue
          If the most recently read token was of type NUMBER, then this is its numerical value.
 
Constructor Summary
ParserContext(java.lang.String data, int options, SymbolTable symbols)
          Create a ParserContext for parsing the data String, using the specified options and symbol table.
 
Method Summary
 void add(MathObject sym)
          Add a new MathObject to the symbol table.
 MathObject get(java.lang.String name)
          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()
          After a call to mark(), revert() must be called to restore the state of the symbol table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

END_OF_STRING

public static final int END_OF_STRING
One of the possible token types returned by look() and next(). Represents the end of the string that is being parsed.

See Also:
Constant Field Values

NUMBER

public static final int NUMBER
One 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:
Constant Field Values

IDENTIFIER

public static final int IDENTIFIER
One 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:
Constant Field Values

OPCHARS

public static final int OPCHARS
One 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:
Constant Field Values

data

public java.lang.String data
The string that is being parsed.


pos

public int pos
Current position in that string, indicating how many characters have been consumed.


prog

public ExpressionProgram 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 token
The 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

public java.lang.String 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

public MathObject 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 tokenValue
If the most recently read token was of type NUMBER, then this is its numerical value.


options

public int options
The options from the Parser. Some of these options affect tokenization, such as whether BOOLEANS is enabled.


symbols

protected SymbolTable symbols
The Parser's symbol table, which is used for looking up tokens of type IDENTIFIER.

Constructor Detail

ParserContext

public ParserContext(java.lang.String data,
                     int options,
                     SymbolTable symbols)
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.

Parameters:
data - the string to parse
options - parsing options
symbols - symbol table
Method Detail

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()
After a call to mark(), revert() must be called to restore the state of the symbol table.


get

public MathObject get(java.lang.String name)
Get the MathObject associated with name in the symbol table.

Parameters:
name - name of mathobject to find
Returns:
corresponding mathobject

add

public void add(MathObject sym)
Add a new MathObject to the symbol table.

Parameters:
sym - mathobject to add

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.

Returns:
token type

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.

Returns:
token type of next token