|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectptolemy.data.expr.PtParser
This file implements an expression parser for Ptolemy II using the JavaCC parser generator. It can handle all the basic arithmetic operators (*, /, +, -,, %), relational operators (<, <=, >, >=, == !=), logical operators(&&, ||, !), bitwise operators (&, |, ^, ~) and, using reflection, all of the functionality available in the java.lang.Math package.
By editing the ASTFunctionNode file it is also relatively easy to allow references to other functions. This provides an easy mechanism to extend the range to the parser e.g. have a tcl(...) function that passes the string to a Tcl interpreter and retuns the result.
Functional if is supported via the following syntax: (boolean) ? (value1) : (value2)
Extensibility is also supported by allowing method calls on the Tokens, the syntax is (value1).method(comma separated arguments)
JavaCC by itself simply generates a file (and support files) that allow an input to be parsed, it does not return a parse tree. For the purposes of type checking we require a parse tree, and this is obtained using JJTree, a preprocessor for JavaCC.
JJtree operates by annotating the grammar file to support the generation of the parse tree. Thus the process is
The parser can also be passed a symbol table of ptolemy.data.expr.Variables which the expression to be parsed can reference.
Anything between quotes(") or apostrophes(') is taken to be one string. Strings are allowed to contain newlines or carriage returns. In addition, these characters, as well as other special characters, can be escaped using the standard Java syntax (\n, \t, \077, etc.).
The expressions recognized follow as close as possible the syntax of Java. In particular the operator precedences implemented here follow exactly those in Java. Any type conversions that are performed are lossless. If the user wants lossy conversions, explicit casts will be necessary.
Complex number are specified by an i or j after the imaginary part of the number. Long numbers are specified by an l or L after an integer number.
Users can register constants with the parser and also register classes where functions that may be called are defined. For a more thorough description of what the Parser is designed to do, please consult the Ptolemy II design document (or contact nsmyth@eecs)
Note that the parsers created by JavaCC generally have quite a bit of internal state. On the other hand, the parse trees generated by this parser are much smaller. It is also fairly cheap to traverse a parse tree in order to evaluate it. Thus it is usually preferable to cache parse trees and create a new parser when the cached parse tree becomes invalid.
ASTPtBitwiseNode,
ASTPtFunctionApplicationNode,
ASTPtFunctionDefinitionNode,
ASTPtFunctionalIfNode,
ASTPtLeafNode,
ASTPtLogicalNode,
ASTPtMethodCallNode,
ASTPtProductNode,
ASTPtRelationalNode,
ASTPtRootNode,
ASTPtSumNode,
ASTPtUnaryNode,
Token
| Yellow (yuhong) |
| Yellow (nsmyth) |
| Field Summary | |
Token |
jj_nt
|
protected ptolemy.data.expr.JJTPtParserState |
jjtree
|
boolean |
lookingAhead
|
Token |
token
|
PtParserTokenManager |
token_source
|
| Fields inherited from interface ptolemy.data.expr.PtParserConstants |
AND, BITWISE_NOT, BOOL_NOT, BOOLEAN, CLOSEBRACE, CLOSEBRACKET, CLOSEPAREN, COLON, COMMA, COMPLEX, COND_AND, COND_OR, DECIMAL_LITERAL, DEFAULT, DIVIDE, DOUBLE, EOF, EQUALS, ERROR, EXPONENT, FUNCTION, GT, GTE, HEX_LITERAL, ID, INTEGER, INTEGER_FORMAT_SPEC, LETTER, LSHR, LT, LTE, MINUS, MODULO, MULTI_LINE_COMMENT, MultiLineCommentMode, MULTIPLY, NOTEQUALS, OCTAL_LITERAL, OPENBRACE, OPENBRACKET, OPENPAREN, OR, PERIOD, PLUS, POWER, QUESTION, SEPARATOR, SETEQUALS, SHL, SHR, SINGLE_LINE_COMMENT, SingleLineCommentMode, SMBRACE, SMDOLLAR, SMDOLLARBRACE, SMDOLLARPAREN, SMID, SMIDBRACE, SMIDPAREN, SMLETTER, SMPAREN, SMSTRING, STRING, StringMode, StringModeIDBrace, StringModeIDNone, StringModeIDParen, tokenImage, XOR |
| Constructor Summary | |
PtParser()
|
|
PtParser(java.io.InputStream stream)
|
|
PtParser(PtParserTokenManager tm)
|
|
PtParser(java.io.Reader stream)
|
|
| Method Summary | |
void |
arrayConstruct()
|
void |
assignment()
|
void |
assignmentIdentifier()
|
void |
bitwiseAnd()
|
void |
bitwiseOr()
|
void |
bitwiseXor()
|
void |
disable_tracing()
|
void |
element()
|
void |
enable_tracing()
|
void |
expression()
|
void |
funcIf()
|
void |
function()
|
void |
functionDefinition()
|
java.util.Map |
generateAssignmentMap(java.lang.String stringIn)
Generates a parse tree from the given String. |
ParseException |
generateParseException()
|
ASTPtRootNode |
generateParseTree(java.lang.String stringIn)
Generates a parse tree from the given String. |
ASTPtRootNode |
generateSimpleAssignmentParseTree(java.lang.String stringIn)
Generates a parse tree from the given String, which may optionally contain an assignment. |
ASTPtRootNode |
generateStringParseTree(java.lang.String stringIn)
Generates a parse tree from the given String, which is interpreted in "String Mode" instead of as an operator expression. |
Token |
getNextToken()
|
static java.util.List |
getRegisteredClasses()
Return the list of classes the parser searches when a function call is encountered. |
Token |
getToken(int index)
|
java.util.LinkedList |
getUndefinedList(java.lang.String stringIn)
Deprecated. Use a visitor with a ParseTreeFreeVariableCollector instead. |
void |
identifier()
|
void |
logicalAnd()
|
void |
logicalEquals()
|
void |
logicalOr()
|
void |
matrixConstruct()
|
void |
optTypeSpecifier()
|
void |
power()
|
void |
primaryElement()
|
void |
recordConstruct()
|
static void |
registerConstant(java.lang.String name,
java.lang.Object value)
Add a constant to the list of constants that the parser recognizes. |
static void |
registerFunctionClass(java.lang.String newClassName)
Add a class to the list of classes that the parser searches when a function call is encountered. |
void |
ReInit(java.io.InputStream stream)
|
void |
ReInit(PtParserTokenManager tm)
|
void |
ReInit(java.io.Reader stream)
|
void |
relational()
|
void |
shift()
|
ASTPtRootNode |
start()
|
java.util.Map |
startAssignmentList()
|
ASTPtRootNode |
startSimpleAssignment()
|
ASTPtRootNode |
startString()
|
void |
stringModeElement()
|
void |
stringModeString()
|
void |
sum()
|
void |
term()
|
void |
unary()
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
protected ptolemy.data.expr.JJTPtParserState jjtree
public PtParserTokenManager token_source
public Token token
public Token jj_nt
public boolean lookingAhead
| Constructor Detail |
public PtParser()
public PtParser(java.io.InputStream stream)
public PtParser(java.io.Reader stream)
public PtParser(PtParserTokenManager tm)
| Method Detail |
public java.util.LinkedList getUndefinedList(java.lang.String stringIn)
throws IllegalActionException
stringIn - The expression to be parsed
IllegalActionException - If the parse fails.
public ASTPtRootNode generateParseTree(java.lang.String stringIn)
throws IllegalActionException
stringIn - The expression to be parsed.
IllegalActionException - If the parse fails.
public ASTPtRootNode generateSimpleAssignmentParseTree(java.lang.String stringIn)
throws IllegalActionException
stringIn - The expression to be parsed.
IllegalActionException - If the parse fails.
public ASTPtRootNode generateStringParseTree(java.lang.String stringIn)
throws IllegalActionException
stringIn - The expression to be parsed.
IllegalActionException - If the parse fails.
public java.util.Map generateAssignmentMap(java.lang.String stringIn)
throws IllegalActionException
stringIn - The expression to be parsed.
IllegalActionException - If the parse fails.public static java.util.List getRegisteredClasses()
public static void registerConstant(java.lang.String name,
java.lang.Object value)
throws java.lang.IllegalArgumentException
name - The string name that the parser will recognize.value - An Object constraining the value associated with
the constant.
java.lang.IllegalArgumentException - If the constant cannot
be registered with the parser.
public static void registerFunctionClass(java.lang.String newClassName)
throws java.lang.IllegalArgumentException
newClassName - The fully qualified name of the Class to
be added to the search path for functions.
java.lang.IllegalArgumentException - If the Class named by the
argument cannot not be found.
public final ASTPtRootNode startSimpleAssignment()
throws ParseException
ParseException
public final java.util.Map startAssignmentList()
throws ParseException
ParseException
public final void assignment()
throws ParseException
ParseException
public final ASTPtRootNode startString()
throws ParseException
ParseException
public final void stringModeElement()
throws ParseException
ParseException
public final void stringModeString()
throws ParseException
ParseException
public final ASTPtRootNode start()
throws ParseException
ParseException
public final void expression()
throws ParseException
ParseException
public final void funcIf()
throws ParseException
ParseException
public final void logicalOr()
throws ParseException
ParseException
public final void logicalAnd()
throws ParseException
ParseException
public final void bitwiseOr()
throws ParseException
ParseException
public final void bitwiseXor()
throws ParseException
ParseException
public final void bitwiseAnd()
throws ParseException
ParseException
public final void logicalEquals()
throws ParseException
ParseException
public final void power()
throws ParseException
ParseException
public final void relational()
throws ParseException
ParseException
public final void shift()
throws ParseException
ParseException
public final void sum()
throws ParseException
ParseException
public final void term()
throws ParseException
ParseException
public final void unary()
throws ParseException
ParseException
public final void element()
throws ParseException
ParseException
public final void primaryElement()
throws ParseException
ParseException
public final void functionDefinition()
throws ParseException
ParseException
public final void optTypeSpecifier()
throws ParseException
ParseException
public final void function()
throws ParseException
ParseException
public final void assignmentIdentifier()
throws ParseException
ParseException
public final void identifier()
throws ParseException
ParseException
public final void matrixConstruct()
throws ParseException
ParseException
public final void recordConstruct()
throws ParseException
ParseException
public final void arrayConstruct()
throws ParseException
ParseExceptionpublic void ReInit(java.io.InputStream stream)
public void ReInit(java.io.Reader stream)
public void ReInit(PtParserTokenManager tm)
public final Token getNextToken()
public final Token getToken(int index)
public ParseException generateParseException()
public final void enable_tracing()
public final void disable_tracing()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||