Package net.sf.saxon.expr
Class ExpressionParser
- java.lang.Object
-
- net.sf.saxon.expr.ExpressionParser
-
- Direct Known Subclasses:
QueryParser
public class ExpressionParser extends java.lang.Object
Parser for XPath expressions and XSLT patterns. This code was originally inspired by James Clark's xt but has been totally rewritten (several times)- Author:
- Michael Kay
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ExpressionParser.ForClause
protected static class
ExpressionParser.TemporaryContainer
-
Field Summary
Fields Modifier and Type Field Description protected StaticContext
env
protected int
language
protected NameChecker
nameChecker
protected java.util.Stack
rangeVariables
protected boolean
scanOnly
protected static int
SEQUENCE_TYPE
protected Tokenizer
t
protected static int
XPATH
protected static int
XQUERY
protected static int
XSLT_PATTERN
-
Constructor Summary
Constructors Constructor Description ExpressionParser()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.String
currentTokenDisplay()
Display the current token in an error messageprotected void
declareRangeVariable(VariableDeclaration declaration)
Declare a range variable (record its existence within the parser).protected void
expect(int token)
Expect a given token; fail if the current token is different.protected java.lang.String
getLanguage()
Get the current language (XPath or XQuery)java.util.Stack
getRangeVariableStack()
Get the range variable stack.Tokenizer
getTokenizer()
protected void
grumble(java.lang.String message)
Report a syntax error (a static error with error code XP0003)protected void
grumble(java.lang.String message, java.lang.String errorCode)
Report a static errorprotected boolean
isKeyword(java.lang.String s)
Test whether the current token is a given keyword.LocalNameTest
makeLocalNameTest(short nodeType, java.lang.String localName)
Make a LocalNameTest (*:name)int
makeNameCode(java.lang.String qname, boolean useDefault)
Make a NameCode, using this Element as the context for namespace resolutionNamespaceTest
makeNamespaceTest(short nodeType, java.lang.String prefix)
Make a NamespaceTest (name:*)NameTest
makeNameTest(short nodeType, java.lang.String qname, boolean useDefault)
Make a NameTest, using the static context for namespace resolutionprotected StringValue
makeStringLiteral(java.lang.String currentTokenValue)
Method to make a string literal from a token identified as a string literal.protected Expression
makeTracer(int startOffset, Expression exp, int construct, int objectNameCode)
If tracing, wrap an instruction in a trace instructionprotected void
nextToken()
Read the next token, catching any exception thrown by the tokenizerExpression
parse(java.lang.String expression, int start, int terminator, int lineNumber, StaticContext env)
Parse a string representing an expressionprotected Expression
parseConstructor()
Parse a node constructor.protected Expression
parseExpression()
Parse a top-level Expression: ExprSingle ( ',' ExprSingle )*protected Expression
parseExprSingle()
Parse an ExprSingleprotected Expression
parseExtensionExpression()
Parse an Extension Expression This construct is XQuery-only, so the XPath version of this method throws an error unconditionallyprotected Expression
parseForExpression()
Parse a FOR expression: for $x in expr (',' $y in expr)* 'return' exprprotected Expression
parseFunctionCall()
Parse a function call.protected Expression
parseMappingExpression()
Parse a mapping expression.protected NodeTest
parseNodeTest(short nodeType)
Parse a NodeTest.Pattern
parsePattern(java.lang.String pattern, StaticContext env)
Parse a string representing an XSLT patternprotected Expression
parseRelativePath()
Parse a relative path (a sequence of steps).protected Expression
parseRemainingPath(Expression start)
Parse the remaining steps of an absolute path expression (one starting in "/" or "//").protected SequenceType
parseSequenceType()
Parse the sequence type production.SequenceType
parseSequenceType(java.lang.String input, StaticContext env)
Parse a string representing a sequence typeprotected Expression
parseStepExpression()
Parse a step (including an optional sequence of predicates)protected Expression
parseTypeswitchExpression()
Parse a Typeswitch Expression.protected Expression
parseValidateExpression()
Parse a Validate Expression.protected void
setLocation(Expression exp)
Set location information on an expression.protected void
setLocation(Expression exp, int offset)
Set location information on an expression.void
setRangeVariableStack(java.util.Stack stack)
Set the range variable stack.void
setScanOnly(boolean scanOnly)
protected void
undeclareRangeVariable()
Note when the most recently declared range variable has gone out of scopeprotected void
warning(java.lang.String message)
Output a warning message
-
-
-
Field Detail
-
t
protected Tokenizer t
-
env
protected StaticContext env
-
rangeVariables
protected java.util.Stack rangeVariables
-
nameChecker
protected NameChecker nameChecker
-
scanOnly
protected boolean scanOnly
-
language
protected int language
-
XPATH
protected static final int XPATH
- See Also:
- Constant Field Values
-
XSLT_PATTERN
protected static final int XSLT_PATTERN
- See Also:
- Constant Field Values
-
SEQUENCE_TYPE
protected static final int SEQUENCE_TYPE
- See Also:
- Constant Field Values
-
XQUERY
protected static final int XQUERY
- See Also:
- Constant Field Values
-
-
Method Detail
-
getTokenizer
public Tokenizer getTokenizer()
-
nextToken
protected void nextToken() throws StaticError
Read the next token, catching any exception thrown by the tokenizer- Throws:
StaticError
-
expect
protected void expect(int token) throws StaticError
Expect a given token; fail if the current token is different. Note that this method does not read any tokens.- Parameters:
token
- the expected token- Throws:
StaticError
- if the current token is not the expected token
-
grumble
protected void grumble(java.lang.String message) throws StaticError
Report a syntax error (a static error with error code XP0003)- Parameters:
message
- the error message- Throws:
StaticError
- always thrown: an exception containing the supplied message
-
grumble
protected void grumble(java.lang.String message, java.lang.String errorCode) throws StaticError
Report a static error- Parameters:
message
- the error messageerrorCode
- the error code- Throws:
StaticError
- always thrown: an exception containing the supplied message
-
warning
protected void warning(java.lang.String message) throws StaticError
Output a warning message- Throws:
StaticError
-
getLanguage
protected java.lang.String getLanguage()
Get the current language (XPath or XQuery)
-
currentTokenDisplay
protected java.lang.String currentTokenDisplay()
Display the current token in an error message- Returns:
- the display representation of the token
-
parse
public Expression parse(java.lang.String expression, int start, int terminator, int lineNumber, StaticContext env) throws StaticError
Parse a string representing an expression- Parameters:
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- Returns:
- an Expression object representing the result of parsing
- Throws:
StaticError
- if the expression contains a syntax error
-
parsePattern
public Pattern parsePattern(java.lang.String pattern, StaticContext env) throws StaticError
Parse a string representing an XSLT pattern- Parameters:
pattern
- the pattern expressed as a Stringenv
- the static context for the pattern- Returns:
- a Pattern object representing the result of parsing
- Throws:
StaticError
- if the pattern contains a syntax error
-
parseSequenceType
public SequenceType parseSequenceType(java.lang.String input, StaticContext env) throws StaticError
Parse a string representing a sequence type- Parameters:
input
- the string, which should conform to the XPath SequenceType productionenv
- the static context- Returns:
- a SequenceType object representing the type
- Throws:
StaticError
- if any error is encountered
-
parseExpression
protected Expression parseExpression() throws StaticError
Parse a top-level Expression: ExprSingle ( ',' ExprSingle )*- Returns:
- the Expression object that results from parsing
- Throws:
StaticError
- if the expression contains a syntax error
-
parseExprSingle
protected Expression parseExprSingle() throws StaticError
Parse an ExprSingle- Returns:
- the resulting subexpression
- Throws:
StaticError
- if any error is encountered
-
parseTypeswitchExpression
protected Expression parseTypeswitchExpression() throws StaticError
Parse a Typeswitch Expression. This construct is XQuery-only, so the XPath version of this method throws an error unconditionally- Throws:
StaticError
-
parseValidateExpression
protected Expression parseValidateExpression() throws StaticError
Parse a Validate Expression. This construct is XQuery-only, so the XPath version of this method throws an error unconditionally- Throws:
StaticError
-
parseExtensionExpression
protected Expression parseExtensionExpression() throws StaticError
Parse an Extension Expression This construct is XQuery-only, so the XPath version of this method throws an error unconditionally- Throws:
StaticError
-
parseForExpression
protected Expression parseForExpression() throws StaticError
Parse a FOR expression: for $x in expr (',' $y in expr)* 'return' expr- Returns:
- the resulting subexpression
- Throws:
StaticError
- if any error is encountered
-
parseMappingExpression
protected Expression parseMappingExpression() throws StaticError
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) exprOn entry, the current token indicates whether a for, some, or every expression is expected.
- Returns:
- the resulting subexpression
- Throws:
StaticError
- if any error is encountered
-
parseSequenceType
protected SequenceType parseSequenceType() throws StaticError
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.- Returns:
- the resulting subexpression
- Throws:
StaticError
- if any error is encountered
-
parseRelativePath
protected Expression parseRelativePath() throws StaticError
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)- Returns:
- the resulting subexpression
- Throws:
StaticError
- if any error is encountered
-
parseRemainingPath
protected Expression parseRemainingPath(Expression start) throws StaticError
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.- Parameters:
start
- the initial implicit expression: root() in the case of "/", root()/descendant-or-self::node in the case of "//"- Returns:
- the completed path expression
- Throws:
StaticError
-
parseStepExpression
protected Expression parseStepExpression() throws StaticError
Parse a step (including an optional sequence of predicates)- Returns:
- the resulting subexpression
- Throws:
StaticError
- if any error is encountered
-
makeStringLiteral
protected StringValue makeStringLiteral(java.lang.String currentTokenValue) throws StaticError
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.- Parameters:
currentTokenValue
-- Returns:
- The string value of the string literal
- Throws:
StaticError
-
parseConstructor
protected Expression parseConstructor() throws StaticError
Parse a node constructor. This is allowed only in XQuery, so the method throws an error for XPath.- Throws:
StaticError
-
parseNodeTest
protected NodeTest parseNodeTest(short nodeType) throws StaticError
Parse a NodeTest. One of QName, prefix:*, *:suffix, *, text(), node(), comment(), or processing-instruction(literal?), or element(~,~), attribute(~,~), etc.- Parameters:
nodeType
- the node type being sought if one is specified- Returns:
- the resulting NodeTest object
- Throws:
StaticError
- if any error is encountered
-
parseFunctionCall
protected Expression parseFunctionCall() throws StaticError
Parse a function call. function-name '(' ( Expression (',' Expression )* )? ')'- Returns:
- the resulting subexpression
- Throws:
StaticError
- if any error is encountered
-
declareRangeVariable
protected void declareRangeVariable(VariableDeclaration declaration) throws StaticError
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.- Parameters:
declaration
- the VariableDeclaration to be added to the stack- Throws:
StaticError
- if any error is encountered
-
undeclareRangeVariable
protected void undeclareRangeVariable()
Note when the most recently declared range variable has gone out of scope
-
getRangeVariableStack
public java.util.Stack getRangeVariableStack()
Get the range variable stack. Used when parsing a nested subexpression inside an attribute constructor
-
setRangeVariableStack
public void setRangeVariableStack(java.util.Stack stack)
Set the range variable stack. Used when parsing a nested subexpression inside an attribute constructor.
-
makeNameCode
public final int makeNameCode(java.lang.String qname, boolean useDefault) throws StaticError
Make a NameCode, using this Element as the context for namespace resolution- Parameters:
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).- Returns:
- the namecode, which can be used to identify this name in the name pool
- Throws:
StaticError
- if the name is invalid, or the prefix undeclared
-
makeNameTest
public NameTest makeNameTest(short nodeType, java.lang.String qname, boolean useDefault) throws StaticError
Make a NameTest, using the static context for namespace resolution- Parameters:
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- Returns:
- a NameTest, representing a pattern that tests for a node of a given node kind and a given name
- Throws:
StaticError
- if the QName is invalid
-
makeNamespaceTest
public NamespaceTest makeNamespaceTest(short nodeType, java.lang.String prefix) throws StaticError
Make a NamespaceTest (name:*)- Parameters:
nodeType
- integer code identifying the type of node requiredprefix
- the namespace prefix- Returns:
- the NamespaceTest, a pattern that matches all nodes in this namespace
- Throws:
StaticError
- if the namespace prefix is not declared
-
makeLocalNameTest
public LocalNameTest makeLocalNameTest(short nodeType, java.lang.String localName) throws StaticError
Make a LocalNameTest (*:name)- Parameters:
nodeType
- the kind of node to be matchedlocalName
- the requred local name- Returns:
- a LocalNameTest, a pattern which matches all nodes of a given local name, regardless of namespace
- Throws:
StaticError
- if the local name is invalid
-
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.
-
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.
-
makeTracer
protected Expression makeTracer(int startOffset, Expression exp, int construct, int objectNameCode)
If tracing, wrap an instruction in a trace instruction
-
isKeyword
protected boolean isKeyword(java.lang.String s)
Test whether the current token is a given keyword.- Parameters:
s
- The string to be compared with the current token- Returns:
- true if they are the same
-
setScanOnly
public void setScanOnly(boolean scanOnly)
-
-