net.sf.saxon.query
Class QueryParser

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

public class QueryParser
extends ExpressionParser

This class defines extensions to the XPath parser to handle the additional syntax supported in XQuery


Nested Class Summary
protected static class QueryParser.LetClause
           
 
Nested classes/interfaces inherited from class net.sf.saxon.expr.ExpressionParser
ExpressionParser.FLWORClause, ExpressionParser.ForClause, ExpressionParser.TemporaryContainer
 
Field Summary
protected  Executable executable
           
protected static int FUNCTION_IS_NONDETERMINISTIC
           
protected static int FUNCTION_IS_PRIVATE
           
protected static int FUNCTION_IS_UPDATING
           
 Set importedModules
           
protected  String queryVersion
           
static String XQUERY10
           
static String XQUERY30
           
 
Fields inherited from class net.sf.saxon.expr.ExpressionParser
allowXPath30Syntax, compileWithTracing, defaultContainer, env, language, languageVersion, nameChecker, rangeVariables, scanOnly, SEQUENCE_TYPE, t, XPATH, XQUERY, XSLT_PATTERN
 
Constructor Summary
QueryParser()
          Constructor for internal use: this class should be instantiated via the QueryModule
 
Method Summary
 void applyModuleImport(net.sf.saxon.query.QueryParser.Import mImport)
           
protected  boolean atStartOfRelativePath()
          Test whether the current token is one that can start a RelativePathExpression
protected  void checkForClauseAllowingEmpty(ExpressionParser.ForClause clause)
          Check a ForClause for an "outer for"
 Executable getExecutable()
          Get the executable containing this expression.
protected  String getLanguage()
          Get the current language (XPath or XQuery)
 int getPermittedFunctions()
          Get the permitted set of standard functions in this environment
protected  void grumble(String message, StructuredQName errorCode, int offset)
          Report a static error
protected  void lookAhead()
          Lookahead one token, catching any exception thrown by the tokenizer.
protected  LetExpression makeLetExpression()
          Make a LetExpression.
protected  void makeSimpleContent(Expression content, SimpleNodeConstructor inst, int offset)
          Make the instructions for the children of a node with simple content (attribute, text, PI, etc)
static Expression makeStringJoin(Expression exp, StaticContext env)
          Make a string-join expression that concatenates the string-values of items in a sequence with intervening spaces.
protected  Literal makeStringLiteral(String token)
          Method to make a string literal from a token identified as a string literal.
 XQueryExpression makeXQueryExpression(String query, QueryModule mainModule, Configuration config)
          Create an XQueryExpression
 QueryParser newParser()
          Create a new parser of the same kind
protected  String normalizeEQName(String s)
          Convert a QName in expanded-name format "uri":local into Clark format
protected  Expression parseConstructor()
          Parse a node constructor.
protected  void parseContextItemDeclaration()
          Parse a context item declaration.
protected  void parseDecimalFormatDeclaration()
          Parse the "declare decimal-format" declaration.
protected  void parseDefaultDecimalFormat()
           
protected  Expression parseExtensionExpression()
          Parse an Extension Expression.
protected  ForExpression parseForClauseAllowingVersion(ExpressionParser.ForClause clause, StructuredQName varQName)
          Parse an "outer for" clause - XQuery 1.1 only
protected  Expression parseForExpression()
          Parse a FLWOR expression.
protected  void parseFunctionDeclaration(int functionOptions)
          Parse a function declaration.
protected  void parseGroupByClause(List clauseList)
          Parse a Group By clause.
 void parseLibraryModule(String queryString, QueryModule env)
          Parse a library module.
protected  Expression parseNamedNamespaceConstructor(int offset)
           
protected  Expression parseNamespaceConstructor(int offset)
           
protected  void parseRevalidationDeclaration()
          Parse the "declare revalidation" declaration.
protected  Expression parseTypeswitchExpression()
          Parse a Typeswitch Expression.
protected  void parseUpdatingFunctionDeclaration()
          Parse an updating function declaration (allowed in XQuery Update only)
protected  Expression parseValidateExpression()
          Parse a Validate Expression.
protected  Expression processGroupingExpression(List<ExpressionParser.FLWORClause> clauseList, Expression action)
          Process a grouping expression Not supported in 1.0; subclassed in the XQuery 1.1 parser
protected  String readCollationName()
           
 Expression setDefaultValue(String exp)
          Parse the expression (inside a string literal) used to define default values for external variables.
 void setDisableCycleChecks(boolean disable)
          Disable checks for certain kinds of cycle.
 void setExecutable(Executable exec)
          Set the executable used for this query expression
static Expression stringify(Expression exp, boolean noNodeIfEmpty)
          Convert an expression so it generates a space-separated sequence of strings
 String URILiteral(String in)
          Handle a URI literal.
 
Methods inherited from class net.sf.saxon.expr.ExpressionParser
currentTokenDisplay, declareRangeVariable, disallowedAtStartOfRelativePath, expect, findRangeVariable, getDefaultContainer, getNameChecker, getRangeVariables, getStaticContext, getTokenizer, grumble, grumble, grumble, isCompileWithTracing, isKeyword, isNamespaceTestAllowed, makeCurriedFunction, makeLocalNameTest, makeNameCode, makeNameCodeSilently, makeNamespaceTest, makeNameTest, makeStructuredQName, makeTracer, nextToken, parse, parseBasicStep, parseDynamicFunctionCall, parseExpression, parseExprSingle, parseFunctionArgument, parseFunctionCall, parseFunctionItemType, parseInlineFunction, parseItemType, parseLiteralFunctionItem, parseNodeTest, parseNumericLiteral, parseParenthesizedItemType, parsePathExpression, parsePredicate, parseRelativePath, parseRemainingPath, parseSequenceType, parseSequenceType, parseStepExpression, parseStringLiteral, parseSwitchExpression, parseTryCatchExpression, parseUnionExpression, parseVariableReference, resolveFunctionName, setCompileWithTracing, setDefaultContainer, setLanguage, setLocation, setLocation, setRangeVariables, setRangeVariableStack, setScanOnly, undeclareRangeVariable, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XQUERY10

public static String XQUERY10

XQUERY30

public static String XQUERY30

queryVersion

protected String queryVersion

executable

protected Executable executable

importedModules

public Set importedModules

FUNCTION_IS_UPDATING

protected static final int FUNCTION_IS_UPDATING
See Also:
Constant Field Values

FUNCTION_IS_PRIVATE

protected static final int FUNCTION_IS_PRIVATE
See Also:
Constant Field Values

FUNCTION_IS_NONDETERMINISTIC

protected static final int FUNCTION_IS_NONDETERMINISTIC
See Also:
Constant Field Values
Constructor Detail

QueryParser

public QueryParser()
Constructor for internal use: this class should be instantiated via the QueryModule

Method Detail

newParser

public QueryParser newParser()
Create a new parser of the same kind


makeXQueryExpression

public XQueryExpression makeXQueryExpression(String query,
                                             QueryModule mainModule,
                                             Configuration config)
                                      throws XPathException
Create an XQueryExpression

Parameters:
query - the source text of the query
mainModule - the static context of the query
config - the Saxon configuration
Returns:
the compiled XQuery expression
Throws:
XPathException

getPermittedFunctions

public int getPermittedFunctions()
Get the permitted set of standard functions in this environment


getExecutable

public Executable getExecutable()
Get the executable containing this expression.

Returns:
the executable

setExecutable

public void setExecutable(Executable exec)
Set the executable used for this query expression

Parameters:
exec - the executable

setDisableCycleChecks

public void setDisableCycleChecks(boolean disable)
Disable checks for certain kinds of cycle. This is equivalent to

declare option saxon:allow-cycles "true"

Parameters:
disable - true if checks for import cycles are to be suppressed, that is, if cycles should be allowed

parseLibraryModule

public final void parseLibraryModule(String queryString,
                                     QueryModule env)
                              throws XPathException
Parse a library module. Prolog? Expression

Parameters:
queryString - The text of the library module.
env - The static context. The result of parsing a library module is that the static context is populated with a set of function declarations and variable declarations. Each library module must have its own static context objext.
Throws:
XPathException - if the expression contains a syntax error

grumble

protected void grumble(String message,
                       StructuredQName errorCode,
                       int offset)
                throws XPathException
Report a static error

Overrides:
grumble in class ExpressionParser
Parameters:
message - the error message
offset -
errorCode - the error code
Throws:
XPathException - always thrown: an exception containing the supplied message

parseRevalidationDeclaration

protected void parseRevalidationDeclaration()
                                     throws XPathException
Parse the "declare revalidation" declaration. Syntax: not allowed unless XQuery update is in use

Throws:
XPathException

applyModuleImport

public void applyModuleImport(net.sf.saxon.query.QueryParser.Import mImport)
                       throws XPathException
Throws:
XPathException

parseDecimalFormatDeclaration

protected void parseDecimalFormatDeclaration()
                                      throws XPathException
Parse the "declare decimal-format" declaration. Allowed in XQuery 1.1 only

Throws:
XPathException

parseDefaultDecimalFormat

protected void parseDefaultDecimalFormat()
                                  throws XPathException
Throws:
XPathException

parseContextItemDeclaration

protected void parseContextItemDeclaration()
                                    throws XPathException
Parse a context item declaration. Allowed only in XQuery 1.1

Throws:
XPathException

parseFunctionDeclaration

protected void parseFunctionDeclaration(int functionOptions)
                                 throws XPathException
Parse a function declaration.

Syntax:
<"declare" "function"> QName "(" ParamList? ")" ("as" SequenceType)? (EnclosedExpr | "external")

On entry, the "define function" has already been recognized

Throws:
XPathException - if a syntax error is found

parseUpdatingFunctionDeclaration

protected void parseUpdatingFunctionDeclaration()
                                         throws XPathException
Parse an updating function declaration (allowed in XQuery Update only)

Throws:
XPathException

setDefaultValue

public Expression setDefaultValue(String exp)
Parse the expression (inside a string literal) used to define default values for external variables. This requires instantiating a nested XPath parser. (This is a Saxon extension for XQuery 1.0 which becomes obsolete with XQuery 1.1)

Parameters:
exp - holds the expression used to define a default value
Returns:
the compiled expression that computes the default value

parseForExpression

protected Expression parseForExpression()
                                 throws XPathException
Parse a FLWOR expression. This replaces the XPath "for" expression. Full syntax:

[41] FLWORExpr ::= (ForClause | LetClause)+ WhereClause? OrderByClause? "return" ExprSingle [42] ForClause ::= <"for" "$"> VarName TypeDeclaration? PositionalVar? "in" ExprSingle ("," "$" VarName TypeDeclaration? PositionalVar? "in" ExprSingle)* [43] PositionalVar ::= "at" "$" VarName [44] LetClause ::= <"let" "$"> VarName TypeDeclaration? ":=" ExprSingle ("," "$" VarName TypeDeclaration? ":=" ExprSingle)* [45] WhereClause ::= "where" Expr [46] OrderByClause ::= (<"order" "by"> | <"stable" "order" "by">) OrderSpecList [47] OrderSpecList ::= OrderSpec ("," OrderSpec)* [48] OrderSpec ::= ExprSingle OrderModifier [49] OrderModifier ::= ("ascending" | "descending")? (<"empty" "greatest"> | <"empty" "least">)? ("collation" StringLiteral)?

Overrides:
parseForExpression in class ExpressionParser
Returns:
the resulting subexpression
Throws:
XPathException - if any error is encountered

makeLetExpression

protected LetExpression makeLetExpression()
Make a LetExpression. This returns an ordinary LetExpression if tracing is off, and an EagerLetExpression if tracing is on. This is so that trace events occur in an order that the user can follow.

Returns:
the constructed "let" expression

parseForClauseAllowingVersion

protected ForExpression parseForClauseAllowingVersion(ExpressionParser.ForClause clause,
                                                      StructuredQName varQName)
                                               throws XPathException
Parse an "outer for" clause - XQuery 1.1 only

Parameters:
clause - the "for clause"
varQName -
Throws:
XPathException

checkForClauseAllowingEmpty

protected void checkForClauseAllowingEmpty(ExpressionParser.ForClause clause)
                                    throws XPathException
Check a ForClause for an "outer for"

Throws:
XPathException - if invalid

parseGroupByClause

protected void parseGroupByClause(List clauseList)
                           throws XPathException
Parse a Group By clause. Not supported in 1.0; subclassed in the XQuery 1.1 parser

Throws:
XPathException

processGroupingExpression

protected Expression processGroupingExpression(List<ExpressionParser.FLWORClause> clauseList,
                                               Expression action)
                                        throws XPathException
Process a grouping expression Not supported in 1.0; subclassed in the XQuery 1.1 parser

Parameters:
clauseList - the list of clauses (for, let, group by)
action - the return clause, optionally wrapped with if-then-else to reflect the where clause
Throws:
XPathException

makeStringJoin

public static Expression makeStringJoin(Expression exp,
                                        StaticContext env)
Make a string-join expression that concatenates the string-values of items in a sequence with intervening spaces. This may be simplified later as a result of type-checking.

Parameters:
exp - the base expression, evaluating to a sequence
env - the static context
Returns:
a call on string-join to create a string containing the representations of the items in the sequence separated by spaces.

readCollationName

protected String readCollationName()
                            throws XPathException
Throws:
XPathException

parseTypeswitchExpression

protected Expression parseTypeswitchExpression()
                                        throws XPathException
Parse a Typeswitch Expression. This construct is XQuery-only. TypeswitchExpr ::= "typeswitch" "(" Expr ")" CaseClause+ "default" ("$" VarName)? "return" ExprSingle CaseClause ::= "case" ("$" VarName "as")? SequenceType "return" ExprSingle

Overrides:
parseTypeswitchExpression in class ExpressionParser
Returns:
the expression that results from the parsing
Throws:
XPathException

parseValidateExpression

protected Expression parseValidateExpression()
                                      throws XPathException
Parse a Validate Expression. This construct is XQuery-only. The syntax allows: validate mode? { Expr } mode ::= "strict" | "lax"

Overrides:
parseValidateExpression in class ExpressionParser
Returns:
the parsed expression; except that this version of the method always throws an exception
Throws:
XPathException

parseExtensionExpression

protected Expression parseExtensionExpression()
                                       throws XPathException
Parse an Extension Expression. Syntax: "(#" QName arbitrary-text "#)")+ "{" expr? "}"

Overrides:
parseExtensionExpression in class ExpressionParser
Returns:
the parsed expression; except that this version of the method always throws an exception
Throws:
XPathException

parseConstructor

protected Expression parseConstructor()
                               throws XPathException
Parse a node constructor. This is allowed only in XQuery. This method handles both the XML-like "direct" constructors, and the XQuery-based "computed" constructors.

Overrides:
parseConstructor in class ExpressionParser
Returns:
an Expression for evaluating the parsed constructor
Throws:
XPathException - in the event of a syntax error.

parseNamespaceConstructor

protected Expression parseNamespaceConstructor(int offset)
                                        throws XPathException
Throws:
XPathException

parseNamedNamespaceConstructor

protected Expression parseNamedNamespaceConstructor(int offset)
                                             throws XPathException
Throws:
XPathException

makeSimpleContent

protected void makeSimpleContent(Expression content,
                                 SimpleNodeConstructor inst,
                                 int offset)
                          throws XPathException
Make the instructions for the children of a node with simple content (attribute, text, PI, etc)

Parameters:
content - the expression making up the simple content
inst - the skeletal instruction for creating the node
offset - the character position of this construct within the source query
Throws:
XPathException

stringify

public static Expression stringify(Expression exp,
                                   boolean noNodeIfEmpty)
                            throws XPathException
Convert an expression so it generates a space-separated sequence of strings

Parameters:
exp - the expression that calculates the content
noNodeIfEmpty - if true, no node is produced when the value of the content expression is an empty sequence. If false, the effect of supplying an empty sequence is that a node is created whose string-value is a zero-length string. Set to true for text node constructors, false for other kinds of node.
Returns:
an expression that computes the content and converts the result to a character string
Throws:
XPathException

makeStringLiteral

protected Literal makeStringLiteral(String token)
                             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

Overrides:
makeStringLiteral in class ExpressionParser
Parameters:
token - the string as written (or as returned by the tokenizer)
Returns:
The string value of the string literal, after dereferencing entity and character references
Throws:
XPathException

URILiteral

public String URILiteral(String in)
                  throws XPathException
Handle a URI literal. This is whitespace-normalized as well as being unescaped

Parameters:
in - the string as written
Returns:
the URI after unescaping of entity and character references followed by whitespace normalization
Throws:
XPathException

normalizeEQName

protected String normalizeEQName(String s)
                          throws XPathException
Convert a QName in expanded-name format "uri":local into Clark format

Overrides:
normalizeEQName in class ExpressionParser
Parameters:
s - the QName in expanded-name format
Returns:
the corresponding expanded name in Clark format
Throws:
XPathException

lookAhead

protected void lookAhead()
                  throws XPathException
Lookahead one token, catching any exception thrown by the tokenizer. This method is only called from the query parser when switching from character-at-a-time mode to tokenizing mode

Throws:
XPathException

atStartOfRelativePath

protected boolean atStartOfRelativePath()
Description copied from class: ExpressionParser
Test whether the current token is one that can start a RelativePathExpression

Overrides:
atStartOfRelativePath in class ExpressionParser
Returns:
the resulting subexpression

getLanguage

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

Overrides:
getLanguage in class ExpressionParser
Returns:
a string representation of the language being parsed, for use in error messages


Copyright (c) 2004-2010 Saxonica Limited. All rights reserved.