Class OPToken
java.lang.Object
com.tangosol.coherence.dsltools.precedence.OPToken
- Direct Known Subclasses:
BetweenOPToken
,ColonToken
,ContainsOPToken
,IdentifierOPToken
,InfixOPToken
,InfixRightOPToken
,KeywordOPToken
,LiteralOPToken
,NestingOPToken
,NotOPToken
,PathOPToken
,PeekOPToken
,PrefixOPToken
,PunctuationOPToken
OPToken is the root class for the Top Down Operator Precedence Parsing
framework's tokens. This framework was first done by Vaughan Pratt in 1973
an is undergoing a bit of a renaissance with people that find the typical
formal grammer tool a bit to heavyweight.
The fundamental idea behind "Pratt Parsers" is that Tokens are objects that
posses methods that allow them to make precedence decisions, match other
tokens, and build abstract syntax trees (AST). The central issue of the
precedence problem is that given an operand object between two operators,
should the operand be bound to the left operator or the right operator?
obj1 OP1 obj2 OP2 obj3 like: (1 + 2 * 3)
Does obj2 bind to OP1 or to OP2? The technique we will use has Token
objects "know" their precedence levels, and implement methods called "nud"
(the null denotation in Pratt speak) and "led" (the left denotation).
A nud method "should" have no interest in the tokens to the left while
a "led" method does. A nud method is typically used on values such as
variables and literals and by prefix operators like '-', or 'not'. A led
method is used by infix operators and suffix operators. A token will often
have both a nud method and a led method with the canonical example being '-'
which is both a prefix operator and an infix operator.
The heart of Pratt's technique is the "expression" function. It takes a
right binding power that controls how aggressively that it should bind to
tokens on its right. We also pass the parser to the token objects so that
their functions may look at context and have access to the tokenizer.
- Author:
- djl 2009.03.14
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic String
The AST node name for a Binary Operator node.static String
The AST node name for a Binding Node.static String
The AST node name for a Method Call Node.static String
The AST node name for a De-referencing Node.static String
The AST node name for a Field List Node.static String
The AST node name for an Identifier Node.static String
The AST node name for a List of Values Node.static String
The AST node name for a Literal Node.protected int
The power that this token binds, typically to the left.protected String
A functor for building ast for led method.protected String
A functor for building ast for nud method.protected String
The string value of the literal.static final int
The binding precedence for assignment operators assignment such as = += -= *= /= %= &= ^= |= <<= >>= >>>=static final int
The binding precedence for bitwise operators such as >>> >> and <<static final int
The binding precedence for exponent arithmetic, i.e. raising by a powerstatic final int
The binding precedence for identifier tokensstatic final int
The binding precedence for keyword tokensstatic final int
The binding precedence for logical tokens such as &&, ||, etcstatic final int
The binding precedence for bitwise logical tokens such as &, |, ^ etcstatic final int
The binding precedence for parentheses ( ) [ ]static final int
The binding precedence for product arithmetic, multiplication, division, mod * / %static final int
The binding precedence for relational operators such as ==, <=, like, contains etcstatic final int
The binding precedence for sum arithmetic, i.e. + and -static final int
The binding precedence for other unary operators: pre-increment, pre-decrement, plus, minus, logical negation, bitwise complement, type cast ++expr --expr +expr -expr !static final int
The binding precedence for unary post operators such as post-increment and post-decrement of the form expr++ or expr--static String
The AST node name for a Unary Operator Node. -
Constructor Summary
ConstructorDescriptionOPToken()
Construct a new OPToken.Construct a new OPToken with the given parameters.Construct a new OPToken with the given parameters.Construct a new OPToken with the given parameters.Construct a new OPToken with the given parameters.Construct a new OPToken with the given parameters. -
Method Summary
Modifier and TypeMethodDescriptionboolean
int
Get The binding precedence of this token.getId()
Obtain the string representation of this token.Get led AST Name for this tokenGet nud AST Name for this tokengetValue()
Get a string value that identifies this tokenint
hashCode()
Process this token and possibly the given leftNodein the context of a parser with the left denotation.int
Obtain the power that this token will bind to the left.protected Term
Create an Abstract Syntax Node for the given arguments.protected Term
Create an Abstract Syntax Node for the given arguments.protected Term
Create an Abstract Syntax Node for the given arguments.protected Term
Create an Abstract Syntax Node for the given arguments.protected Term
Create an Abstract Syntax Node for the given arguments.Process this token in the context of parser p with the null denotation.void
setBindingPower
(int nBp) Set The binding precedence that this token will use for binding to the right.void
Set the string representation of this token to the given id.void
setLedASTName
(String sAstName) Set the led AST Name for this token to be the given stringvoid
setNudASTName
(String sAstName) Set the nud AST Name for this token to be the given stringvoid
Set the AST Name for this token to be the given stringtoString()
Return a human-readable description for this token.
-
Field Details
-
BINARY_OPERATOR_NODE
The AST node name for a Binary Operator node. -
BINDING_NODE
The AST node name for a Binding Node. -
CALL_NODE
The AST node name for a Method Call Node. -
DEREF_NODE
The AST node name for a De-referencing Node. -
FIELD_LIST
The AST node name for a Field List Node. -
IDENTIFIER_NODE
The AST node name for an Identifier Node. -
LIST_NODE
The AST node name for a List of Values Node. -
LITERAL_NODE
The AST node name for a Literal Node. -
UNARY_OPERATOR_NODE
The AST node name for a Unary Operator Node. -
PRECEDENCE_KEYWORD
public static final int PRECEDENCE_KEYWORDThe binding precedence for keyword tokens- See Also:
-
PRECEDENCE_IDENTIFIER
public static final int PRECEDENCE_IDENTIFIERThe binding precedence for identifier tokens- See Also:
-
PRECEDENCE_ASSIGNMENT
public static final int PRECEDENCE_ASSIGNMENTThe binding precedence for assignment operators assignment such as = += -= *= /= %= &= ^= |= <<= >>= >>>=- See Also:
-
PRECEDENCE_LOGICAL
public static final int PRECEDENCE_LOGICALThe binding precedence for logical tokens such as &&, ||, etc- See Also:
-
PRECEDENCE_LOGICAL_BITWISE
public static final int PRECEDENCE_LOGICAL_BITWISEThe binding precedence for bitwise logical tokens such as &, |, ^ etc- See Also:
-
PRECEDENCE_RELATIONAL
public static final int PRECEDENCE_RELATIONALThe binding precedence for relational operators such as ==, <=, like, contains etc- See Also:
-
PRECEDENCE_BITWISE
public static final int PRECEDENCE_BITWISEThe binding precedence for bitwise operators such as >>> >> and <<- See Also:
-
PRECEDENCE_SUM
public static final int PRECEDENCE_SUMThe binding precedence for sum arithmetic, i.e. + and -- See Also:
-
PRECEDENCE_PRODUCT
public static final int PRECEDENCE_PRODUCTThe binding precedence for product arithmetic, multiplication, division, mod * / %- See Also:
-
PRECEDENCE_EXPONENT
public static final int PRECEDENCE_EXPONENTThe binding precedence for exponent arithmetic, i.e. raising by a power- See Also:
-
PRECEDENCE_UNARY
public static final int PRECEDENCE_UNARYThe binding precedence for other unary operators: pre-increment, pre-decrement, plus, minus, logical negation, bitwise complement, type cast ++expr --expr +expr -expr ! ~ (type)- See Also:
-
PRECEDENCE_UNARY_POST
public static final int PRECEDENCE_UNARY_POSTThe binding precedence for unary post operators such as post-increment and post-decrement of the form expr++ or expr--- See Also:
-
PRECEDENCE_PARENTHESES
public static final int PRECEDENCE_PARENTHESESThe binding precedence for parentheses ( ) [ ]- See Also:
-
m_sValue
The string value of the literal. -
m_sLedASTName
A functor for building ast for led method. -
m_sNudASTName
A functor for building ast for nud method. -
m_nBindingPower
protected int m_nBindingPowerThe power that this token binds, typically to the left.
-
-
Constructor Details
-
OPToken
public OPToken()Construct a new OPToken. -
OPToken
Construct a new OPToken with the given parameters.- Parameters:
sId
- string representation of the literal
-
OPToken
Construct a new OPToken with the given parameters.- Parameters:
sId
- string representation of the tokennBp
- The binding precedence for this token
-
OPToken
Construct a new OPToken with the given parameters.- Parameters:
sId
- string representation of the tokensAstName
- the type code for this literal token
-
OPToken
Construct a new OPToken with the given parameters.- Parameters:
sId
- string representation of the tokennBp
- The binding precedence for this tokensAstName
- the name for this tokens AST
-
OPToken
Construct a new OPToken with the given parameters.- Parameters:
sId
- string representation of the tokennBp
- The binding precedence for this tokensLedASTName
- the name for this tokens ASTsNudASTName
- the name for this tokens AST
-
-
Method Details
-
leftBindingPower
public int leftBindingPower()Obtain the power that this token will bind to the left.- Returns:
- the left binding power
-
nud
Process this token in the context of parser p with the null denotation. A nud method typically will have no interest in the token to the left. The processing results in an Abstract Syntax Tree Node that captures the meaning- Parameters:
parser
- the parser that is the context for parsing- Returns:
- an AstNode
-
led
Process this token and possibly the given leftNodein the context of a parser with the left denotation. A led method typically will be interested t in the token to the left. The processing results in an Abstract Syntax Tree Node that captures the meaning- Parameters:
parser
- the parser that is the context for parsingleftNode
- an ast Term that the token is possibly interested in- Returns:
- an AstNode
-
newAST
Create an Abstract Syntax Node for the given arguments. If the astName argument is not null then use it for the functor and the given functor argument become the first child Term.- Parameters:
sAstName
- classification functor or nullsFunctor
- functor for ast node to be constructed- Returns:
- a Term representing the AST
-
newAST
Create an Abstract Syntax Node for the given arguments. If the astName argument is not null then use it for the functor otherwise just assume the Term t is good.- Parameters:
sAstName
- classification functor or nullterm
- an Term that is part of the ast- Returns:
- a Term representing the AST
-
newAST
Create an Abstract Syntax Node for the given arguments. If the astName argument is not null then use it for the functor and the given functor argument become the first child Term.- Parameters:
sAstName
- classification functor or nullsFunctor
- functor for ast node to be constructedterm
- an Term that is part of the ast- Returns:
- a Term representing the AST
-
newAST
Create an Abstract Syntax Node for the given arguments. If the astName argument is not null then use it for the functor and the given functor argument become the first child Term.- Parameters:
sAstName
- classification functor or nullsFunctor
- functor for ast node to be constructedt1
- an Term that is part of the astt2
- an Term that is part of the ast- Returns:
- a Term representing the AST
-
newAST
Create an Abstract Syntax Node for the given arguments. If the astName argument is not null then use it for the functor and the given functor argument become the first child Term.- Parameters:
sAstName
- classification functor or nullsFunctor
- functor for ast node to be constructedt1
- an Term that is part of the astt2
- an Term that is part of the astt3
- an Term that is part of the ast- Returns:
- a Term representing the AST
-
getId
Obtain the string representation of this token.- Returns:
- the string representation
-
setId
Set the string representation of this token to the given id.- Parameters:
sId
- the string representation for the token
-
getBindingPower
public int getBindingPower()Get The binding precedence of this token.- Returns:
- The binding precedence
-
setBindingPower
public void setBindingPower(int nBp) Set The binding precedence that this token will use for binding to the right.- Parameters:
nBp
- the power power for this token
-
getNudASTName
Get nud AST Name for this token- Returns:
- the nud ast name for this token
-
setNudASTName
Set the nud AST Name for this token to be the given string- Parameters:
sAstName
- the nud ast name for this token
-
getLedASTName
Get led AST Name for this token- Returns:
- the led ast name for this token
-
setLedASTName
Set the led AST Name for this token to be the given string- Parameters:
sAstName
- the led ast name for this token
-
getValue
Get a string value that identifies this token- Returns:
- the a string that identifies this token
-
setValue
Set the AST Name for this token to be the given string- Parameters:
s
- the ast name for this token
-
toString
Return a human-readable description for this token. -
equals
-
hashCode
public int hashCode()
-