Parser for XPath expressions and XSLT patterns.
This code was originally inspired by James Clark's xt but has been totally rewritten (several times)
SEQUENCE_TYPE
protected static final int SEQUENCE_TYPE
XPATH
protected static final int XPATH
XQUERY
protected static final int XQUERY
XSLT_PATTERN
protected static final int XSLT_PATTERN
compileWithTracing
protected boolean compileWithTracing
language
protected int language
rangeVariables
protected Stack rangeVariables
scanOnly
protected boolean scanOnly
currentTokenDisplay
protected String currentTokenDisplay()
Display the current token in an error message
- the display representation of the token
declareRangeVariable
protected void declareRangeVariable(Binding declaration)
throws XPathException
Declare a range variable (record its existence within the parser).
A range variable is a variable declared within an expression, as distinct
from a variable declared in the context.
declaration
- the variable declaration to be added to the stack
expect
protected void expect(int token)
throws XPathException
Expect a given token; fail if the current token is different. Note that this method
does not read any tokens.
token
- the expected token
getLanguage
protected String getLanguage()
Get the current language (XPath or XQuery)
- a string representation of the language being parsed, for use in error messages
getRangeVariableStack
public Stack getRangeVariableStack()
Get the range variable stack. Used when parsing a nested subexpression
inside an attribute constructor
- the stack used for locally-declared variables
getTokenizer
public Tokenizer getTokenizer()
Get the tokenizer (the lexical analyzer)
- the tokenizer (the lexical analyzer)
grumble
protected void grumble(String message)
throws XPathException
Report a syntax error (a static error with error code XP0003)
message
- the error message
XPathException
- always thrown: an exception containing the
supplied message
grumble
protected void grumble(String message,
String errorCode)
throws XPathException
Report a static error
message
- the error messageerrorCode
- the error code
XPathException
- always thrown: an exception containing the
supplied message
isCompileWithTracing
public boolean isCompileWithTracing()
Determine whether trace hooks are included in the compiled code.
- true if trace hooks are included, false if not.
isKeyword
protected boolean isKeyword(String s)
Test whether the current token is a given keyword.
s
- The string to be compared with the current token
- true if they are the same
makeLocalNameTest
public LocalNameTest makeLocalNameTest(short nodeType,
String localName)
throws XPathException
Make a LocalNameTest (*:name)
nodeType
- the kind of node to be matchedlocalName
- the requred local name
- a LocalNameTest, a pattern which matches all nodes of a given
local name, regardless of namespace
makeNameCode
public final int makeNameCode(String qname,
boolean useDefault)
throws XPathException
Make a NameCode, using the static context for namespace resolution
qname
- The name as written, in the form "[prefix:]localname"useDefault
- Defines the action when there is no prefix. If
true, use the default namespace URI for element names. If false,
use no namespace URI (as for attribute names).
- the namecode, which can be used to identify this name in the
name pool
makeNameCodeSilently
public final int makeNameCodeSilently(String qname,
boolean useDefault)
throws XPathException,
QNameException
Make a NameCode, using the static context for namespace resolution.
This variant of the method does not call "grumble" to report any errors
to the ErrorListener, it only reports errors by throwing exceptions. This
allows the caller to control the message output.
qname
- The name as written, in the form "[prefix:]localname"useDefault
- Defines the action when there is no prefix. If
true, use the default namespace URI for element names. If false,
use no namespace URI (as for attribute names).
- the namecode, which can be used to identify this name in the
name pool
makeNameTest
public NameTest makeNameTest(short nodeType,
String qname,
boolean useDefault)
throws XPathException
Make a NameTest, using the static context for namespace resolution
nodeType
- the type of node required (identified by a constant in
class Type)qname
- the lexical QName of the required nodeuseDefault
- true if the default namespace should be used when
the QName is unprefixed
- a NameTest, representing a pattern that tests for a node of a
given node kind and a given name
makeNamespaceTest
public NamespaceTest makeNamespaceTest(short nodeType,
String prefix)
throws XPathException
Make a NamespaceTest (name:*)
nodeType
- integer code identifying the type of node requiredprefix
- the namespace prefix
- the NamespaceTest, a pattern that matches all nodes in this
namespace
makeStringLiteral
protected Literal makeStringLiteral(String currentTokenValue)
throws XPathException
Method to make a string literal from a token identified as a string
literal. This is trivial in XPath, but in XQuery the method is overridden
to identify pseudo-XML character and entity references. Note that the job of handling
doubled string delimiters is done by the tokenizer.
currentTokenValue
- the token as read (excluding quotation marks)
- The string value of the string literal
makeStructuredQName
public final StructuredQName makeStructuredQName(String qname,
boolean useDefault)
throws XPathException
Make a Structured QName, using the static context for namespace resolution
qname
- The name as written, in the form "[prefix:]localname"useDefault
- Defines the action when there is no prefix. If
true, use the default namespace URI for element names. If false,
use no namespace URI (as for attribute names).
- the namecode, which can be used to identify this name in the
name pool
makeTracer
protected Expression makeTracer(int startOffset,
Expression exp,
int construct,
StructuredQName qName)
If tracing, wrap an expression in a trace instruction
startOffset
- the position of the expression in the soruceexp
- the expression to be wrappedconstruct
- integer constant identifying the kind of constructqName
- the name of the construct (if applicable)
- the expression that does the tracing
nextToken
protected void nextToken()
throws XPathException
Read the next token, catching any exception thrown by the tokenizer
parse
public Expression parse(String expression,
int start,
int terminator,
int lineNumber,
StaticContext env)
throws XPathException
Parse a string representing an expression
expression
- the expression expressed as a Stringstart
- offset within the string where parsing is to startterminator
- character to treat as terminating the expressionlineNumber
- location of the start of the expression, for diagnosticsenv
- the static context for the expression
- an Expression object representing the result of parsing
parseConstructor
protected Expression parseConstructor()
throws XPathException
Parse a node constructor. This is allowed only in XQuery, so the method throws
an error for XPath.
- the expression that results from the parsing
parseExprSingle
protected Expression parseExprSingle()
throws XPathException
Parse an ExprSingle
- the resulting subexpression
parseExpression
protected Expression parseExpression()
throws XPathException
Parse a top-level Expression:
ExprSingle ( ',' ExprSingle )*
- the Expression object that results from parsing
parseExtensionExpression
protected Expression parseExtensionExpression()
throws XPathException
Parse an Extension Expression
This construct is XQuery-only, so the XPath version of this
method throws an error unconditionally
- the parsed expression; except that this version of the method
always throws an exception
parseForExpression
protected Expression parseForExpression()
throws XPathException
Parse a FOR expression:
for $x in expr (',' $y in expr)* 'return' expr
- the resulting subexpression
parseFunctionCall
protected Expression parseFunctionCall()
throws XPathException
Parse a function call.
function-name '(' ( Expression (',' Expression )* )? ')'
- the resulting subexpression
parseMappingExpression
protected Expression parseMappingExpression()
throws XPathException
Parse a mapping expression. This is a common routine that handles
XPath 'for' expressions and quantified expressions.
Syntax:
(for|some|every) $x in expr (',' $y in expr)* (return|satisfies) expr
On entry, the current token indicates whether a for, some, or every
expression is expected.
- the resulting subexpression
parseNodeTest
protected NodeTest parseNodeTest(short nodeType)
throws XPathException
Parse a NodeTest.
One of QName, prefix:*, *:suffix, *, text(), node(), comment(), or
processing-instruction(literal?), or element(~,~), attribute(~,~), etc.
nodeType
- the node type being sought if one is specified
- the resulting NodeTest object
parsePattern
public Pattern parsePattern(String pattern,
StaticContext env)
throws XPathException
Parse a string representing an XSLT pattern
pattern
- the pattern expressed as a Stringenv
- the static context for the pattern
- a Pattern object representing the result of parsing
parseRelativePath
protected Expression parseRelativePath()
throws XPathException
Parse a relative path (a sequence of steps). Called when the current token immediately
follows a separator (/ or //), or an implicit separator (XYZ is equivalent to ./XYZ)
- the resulting subexpression
parseRemainingPath
protected Expression parseRemainingPath(Expression start)
throws XPathException
Parse the remaining steps of an absolute path expression (one starting in "/" or "//"). Note that the
token immediately after the "/" or "//" has already been read, and in the case of "/", it has been confirmed
that we have a path expression starting with "/" rather than a standalone "/" expression.
start
- the initial implicit expression: root() in the case of "/", root()/descendant-or-self::node in
the case of "//"
- the completed path expression
parseSequenceType
protected SequenceType parseSequenceType()
throws XPathException
Parse the sequence type production.
Provisionally, we use the syntax (QName | node-kind "()") ( "*" | "+" | "?" )?
We also allow "element of type QName" and "attribute of type QName"
The QName must be the name of a built-in schema-defined data type.
- the resulting subexpression
parseSequenceType
public SequenceType parseSequenceType(String input,
StaticContext env)
throws XPathException
Parse a string representing a sequence type
input
- the string, which should conform to the XPath SequenceType
productionenv
- the static context
- a SequenceType object representing the type
parseStepExpression
protected Expression parseStepExpression()
throws XPathException
Parse a step (including an optional sequence of predicates)
- the resulting subexpression
parseTypeswitchExpression
protected Expression parseTypeswitchExpression()
throws XPathException
Parse a Typeswitch Expression.
This construct is XQuery-only, so the XPath version of this
method throws an error unconditionally
- the expression that results from the parsing
parseValidateExpression
protected Expression parseValidateExpression()
throws XPathException
Parse a Validate Expression.
This construct is XQuery-only, so the XPath version of this
method throws an error unconditionally
- the parsed expression; except that this version of the method always
throws an exception
setCompileWithTracing
public void setCompileWithTracing(boolean trueOrFalse)
Set whether trace hooks are to be included in the compiled code. To use tracing, it is necessary
both to compile the code with trace hooks included, and to supply a TraceListener at run-time
trueOrFalse
- true if trace code is to be compiled in, false otherwise
setLocation
protected void setLocation(Expression exp)
Set location information on an expression. At present this consists of a simple
line number. Needed mainly for XQuery.
exp
- the expression whose location information is to be set
setLocation
protected void setLocation(Expression exp,
int offset)
Set location information on an expression. At present only the line number
is retained. Needed mainly for XQuery. This version of the method supplies an
explicit offset (character position within the expression or query), which the tokenizer
can convert to a line number and column number.
exp
- the expression whose location information is to be setoffset
- the character position within the expression (ignoring newlines)
setRangeVariableStack
public void setRangeVariableStack(Stack stack)
Set the range variable stack. Used when parsing a nested subexpression
inside an attribute constructor.
stack
- the stack to be used for local variables declared within the expression
setScanOnly
public void setScanOnly(boolean scanOnly)
Set that we are parsing in "scan only"
scanOnly
- true if parsing is to proceed in scan-only mode. In this mode
namespace bindings are not yet known, so no attempt is made to look up namespace
prefixes.
undeclareRangeVariable
protected void undeclareRangeVariable()
Note when the most recently declared range variable has gone out of scope
warning
protected void warning(String message)
throws XPathException
Output a warning message
message
- the text of the message