net.sf.saxon.expr
Class ExpressionParser

java.lang.Object
  extended by net.sf.saxon.expr.ExpressionParser
Direct Known Subclasses:
QueryParser

public class ExpressionParser
extends 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
static class ExpressionParser.ForClause
           
 
Field Summary
protected  StaticContext env
           
protected  int numberOfRangeVariables
           
protected  Stack rangeVariables
           
protected  boolean scanOnly
           
protected  Tokenizer t
           
 
Constructor Summary
ExpressionParser()
           
 
Method Summary
protected  String currentTokenDisplay()
          Display the current token in an error message
protected  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  String getLanguage()
          Get the current language (XPath or XQuery)
 Stack getRangeVariableStack()
          Get the range variable stack.
 Tokenizer getTokenizer()
           
protected  void grumble(String message)
          Report a parsing error
protected  boolean isKeyword(String s)
          Test whether the current token is a given keyword.
 LocalNameTest makeLocalNameTest(short nodeType, String localName)
          Make a LocalNameTest (*:name)
protected  SourceLocator makeLocator()
          Create a SourceLocator for use in an exception
 int makeNameCode(String qname, boolean useDefault)
          Make a NameCode, using this Element as the context for namespace resolution
 NamespaceTest makeNamespaceTest(short nodeType, String prefix)
          Make a NamespaceTest (name:*)
 NameTest makeNameTest(short nodeType, String qname, boolean useDefault)
          Make a NameTest, using the static context for namespace resolution
protected  StringValue makeStringLiteral(String currentTokenValue)
          Method to make a string literal from a token identified as a string literal.
protected  void nextToken()
          Read the next token, catching any exception thrown by the tokenizer
 Expression parse(String expression, int start, int terminator, StaticContext env)
          Parse a string representing an expression
protected  Expression parseConstructor()
          Parse a node constructor.
protected  Expression parseExpression()
          Parse a top-level Expression: ExprSingle ( ',' ExprSingle )*
protected  Expression parseExprSingle()
          Parse an ExprSingle
protected  Expression parseForExpression()
          Parse a FOR expression: for $x in expr (',' $y in expr)* 'return' expr
protected  Expression parseMappingExpression()
          Parse a mapping expression.
protected  NodeTest parseNodeTest(short nodeType)
          Parse a NodeTest.
 Pattern parsePattern(String pattern, StaticContext env)
          Parse a string representing an XSLT pattern
protected  Expression parseRelativePath()
          Parse a relative path (a sequence of steps).
protected  SequenceType parseSequenceType()
          Parse the sequence type production.
 SequenceType parseSequenceType(String input, StaticContext env)
          Parse a string representing a sequence type
protected  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 line)
          Set location information on an expression.
 void setRangeVariableStack(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 scope
protected  void warning(String message)
          Output a warning message
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

t

protected Tokenizer t

env

protected StaticContext env

numberOfRangeVariables

protected int numberOfRangeVariables

rangeVariables

protected Stack rangeVariables

scanOnly

protected boolean scanOnly
Constructor Detail

ExpressionParser

public ExpressionParser()
Method Detail

getTokenizer

public Tokenizer getTokenizer()

nextToken

protected void nextToken()
                  throws XPathException
Read the next token, catching any exception thrown by the tokenizer

Throws:
XPathException

expect

protected void expect(int token)
               throws XPathException
Expect a given token, fail if the current token is different

Parameters:
token - the expected token
Throws:
XPathException - if the current token is not the expected token

grumble

protected void grumble(String message)
                throws XPathException
Report a parsing error

Parameters:
message - the error message
Throws:
XPathException - always thrown: an exception containing the supplied message

warning

protected void warning(String message)
                throws XPathException
Output a warning message

Throws:
XPathException

getLanguage

protected String getLanguage()
Get the current language (XPath or XQuery)


currentTokenDisplay

protected String currentTokenDisplay()
Display the current token in an error message

Returns:
the display representation of the token

parse

public Expression parse(String expression,
                        int start,
                        int terminator,
                        StaticContext env)
                 throws XPathException
Parse a string representing an expression

Parameters:
expression - the expression expressed as a String
env - the static context for the expression
Returns:
an Expression object representing the result of parsing
Throws:
XPathException - if the expression contains a syntax error

parsePattern

public Pattern parsePattern(String pattern,
                            StaticContext env)
                     throws XPathException
Parse a string representing an XSLT pattern

Parameters:
pattern - the pattern expressed as a String
env - the static context for the pattern
Returns:
a Pattern object representing the result of parsing
Throws:
XPathException - if the pattern contains a syntax error

parseSequenceType

public SequenceType parseSequenceType(String input,
                                      StaticContext env)
                               throws XPathException
Parse a string representing a sequence type

Parameters:
input - the string, which should conform to the XPath SequenceType production
env - the static context
Returns:
a SequenceType object representing the type
Throws:
XPathException - if any error is encountered

parseExpression

protected Expression parseExpression()
                              throws XPathException
Parse a top-level Expression: ExprSingle ( ',' ExprSingle )*

Returns:
the Expression object that results from parsing
Throws:
XPathException - if the expression contains a syntax error

parseExprSingle

protected Expression parseExprSingle()
                              throws XPathException
Parse an ExprSingle

Returns:
the resulting subexpression
Throws:
XPathException - if any error is encountered

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

Throws:
XPathException

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

Throws:
XPathException

parseForExpression

protected Expression parseForExpression()
                                 throws XPathException
Parse a FOR expression: for $x in expr (',' $y in expr)* 'return' expr

Returns:
the resulting subexpression
Throws:
XPathException - if any error is encountered

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.

Returns:
the resulting subexpression
Throws:
XPathException - if any error is encountered

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.

Returns:
the resulting subexpression
Throws:
XPathException - if any error is encountered

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)

Returns:
the resulting subexpression
Throws:
XPathException - if any error is encountered

parseStepExpression

protected Expression parseStepExpression()
                                  throws XPathException
Parse a step (including an optional sequence of predicates)

Returns:
the resulting subexpression
Throws:
XPathException - if any error is encountered

makeStringLiteral

protected StringValue 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

Parameters:
currentTokenValue -
Returns:
The string value of the string literal
Throws:
XPathException

parseConstructor

protected Expression parseConstructor()
                               throws XPathException
Parse a node constructor. This is allowed only in XQuery, so the method throws an error for XPath.

Throws:
XPathException

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.

Parameters:
nodeType - the node type being sought if one is specified
Returns:
the resulting NodeTest object
Throws:
XPathException - if any error is encountered

declareRangeVariable

protected void declareRangeVariable(VariableDeclaration 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.

Parameters:
declaration - the VariableDeclaration to be added to the stack
Throws:
XPathException - if any error is encountered

undeclareRangeVariable

protected void undeclareRangeVariable()
Note when the most recently declared range variable has gone out of scope


getRangeVariableStack

public Stack getRangeVariableStack()
Get the range variable stack. Used when parsing a nested subexpression inside an attribute constructor


setRangeVariableStack

public void setRangeVariableStack(Stack stack)
Set the range variable stack. Used when parsing a nested subexpression inside an attribute constructor.


makeNameCode

public final int makeNameCode(String qname,
                              boolean useDefault)
                       throws XPathException
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:
XPathException - if the name is invalid, or the prefix undeclared

makeNameTest

public NameTest makeNameTest(short nodeType,
                             String qname,
                             boolean useDefault)
                      throws XPathException
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 node
useDefault - 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:
XPathException - if the QName is invalid

makeNamespaceTest

public NamespaceTest makeNamespaceTest(short nodeType,
                                       String prefix)
                                throws XPathException
Make a NamespaceTest (name:*)

Parameters:
nodeType - integer code identifying the type of node required
prefix - the namespace prefix
Returns:
the NamespaceTest, a pattern that matches all nodes in this namespace
Throws:
XPathException - if the namespace prefix is not declared

makeLocalNameTest

public LocalNameTest makeLocalNameTest(short nodeType,
                                       String localName)
                                throws XPathException
Make a LocalNameTest (*:name)

Parameters:
nodeType - the kind of node to be matched
localName - the requred local name
Returns:
a LocalNameTest, a pattern which matches all nodes of a given local name, regardless of namespace
Throws:
XPathException - 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 line)
Set location information on an expression. At present this consists of a simple line number. Needed mainly for XQuery. This version of the method supplies an explicit line number.


makeLocator

protected SourceLocator makeLocator()
Create a SourceLocator for use in an exception


isKeyword

protected boolean isKeyword(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)