<?xqlint
/* ***** BEGIN LICENSE BLOCK *****
 * Distributed under the BSD license:
 *
 * Copyright (c) 2010, Ajax.org B.V.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Ajax.org B.V. nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ***** END LICENSE BLOCK ***** */

define(function(require, exports, module){
var XQueryParser = exports.XQueryParser = function XQueryParser(string, parsingEventHandler)
{
  init(string, parsingEventHandler);
?>

XQuery   ::= Module EOF
Module   ::= VersionDecl? ( LibraryModule | MainModule )
VersionDecl
         ::= 'xquery' ( 'encoding' StringLiteral | 'version' StringLiteral ( 'encoding' StringLiteral )? ) Separator
LibraryModule
         ::= ModuleDecl Prolog
ModuleDecl
         ::= 'module' 'namespace' NCName '=' URILiteral Separator
Prolog   ::= ( ( DefaultNamespaceDecl | Setter | NamespaceDecl | Import | FTOptionDecl ) Separator )* ( ( ContextItemDecl | AnnotatedDecl | OptionDecl ) Separator )*
Separator
         ::= ';'
Setter   ::= BoundarySpaceDecl
           | DefaultCollationDecl
           | BaseURIDecl
           | ConstructionDecl
           | OrderingModeDecl
           | EmptyOrderDecl
           | RevalidationDecl
           | CopyNamespacesDecl
           | DecimalFormatDecl
BoundarySpaceDecl
         ::= 'declare' 'boundary-space' ( 'preserve' | 'strip' )
DefaultCollationDecl
         ::= 'declare' 'default' 'collation' URILiteral
BaseURIDecl
         ::= 'declare' 'base-uri' URILiteral
ConstructionDecl
         ::= 'declare' 'construction' ( 'strip' | 'preserve' )
OrderingModeDecl
         ::= 'declare' 'ordering' ( 'ordered' | 'unordered' )
EmptyOrderDecl
         ::= 'declare' 'default' 'order' 'empty' ( 'greatest' | 'least' )
CopyNamespacesDecl
         ::= 'declare' 'copy-namespaces' PreserveMode ',' InheritMode
PreserveMode
         ::= 'preserve'
           | 'no-preserve'
InheritMode
         ::= 'inherit'
           | 'no-inherit'
DecimalFormatDecl
         ::= 'declare' ( 'decimal-format' EQName | 'default' 'decimal-format' ) ( DFPropertyName '=' StringLiteral )*
DFPropertyName
         ::= 'decimal-separator'
           | 'grouping-separator'
           | 'infinity'
           | 'minus-sign'
           | 'NaN'
           | 'percent'
           | 'per-mille'
           | 'zero-digit'
           | 'digit'
           | 'pattern-separator'
Import   ::= SchemaImport
           | ModuleImport
SchemaImport
         ::= 'import' 'schema' SchemaPrefix? URILiteral ( 'at' URILiteral ( ',' URILiteral )* )?
SchemaPrefix
         ::= 'namespace' NCName '='
           | 'default' 'element' 'namespace'
ModuleImport
         ::= 'import' 'module' ( 'namespace' NCName '=' )? URILiteral ( 'at' URILiteral ( ',' URILiteral )* )?
NamespaceDecl
         ::= 'declare' 'namespace' NCName '=' URILiteral
DefaultNamespaceDecl
         ::= 'declare' 'default' ( 'element' | 'function' ) 'namespace' URILiteral
FTOptionDecl
         ::= 'declare' 'ft-option' FTMatchOptions
AnnotatedDecl
         ::= 'declare' ( CompatibilityAnnotation | Annotation )* ( VarDecl | FunctionDecl | CollectionDecl | IndexDecl | ICDecl )
CompatibilityAnnotation
         ::= 'updating'
Annotation
         ::= '%' EQName ( '(' Literal ( ',' Literal )* ')' )?
VarDecl  ::= 'variable' '$' VarName TypeDeclaration? ( ':=' VarValue | 'external' ( ':=' VarDefaultValue )? )
VarValue ::= ExprSingle
VarDefaultValue
         ::= ExprSingle
ContextItemDecl
         ::= 'declare' 'context' 'item' ( 'as' ItemType )? ( ':=' VarValue | 'external' ( ':=' VarDefaultValue )? )
ParamList
         ::= Param ( ',' Param )*
Param    ::= '$' EQName TypeDeclaration?
FunctionBody
         ::= EnclosedExpr
EnclosedExpr
         ::= '{' Expr '}'
OptionDecl
         ::= 'declare' 'option' EQName StringLiteral
Expr     ::= ExprSingle ( ',' ExprSingle )*
FLWORExpr
         ::= InitialClause IntermediateClause* ReturnClause
InitialClause
         ::= ForClause
           | LetClause
           | WindowClause
IntermediateClause
         ::= InitialClause
           | WhereClause
           | GroupByClause
           | OrderByClause
           | CountClause
ForClause
         ::= 'for' ForBinding ( ',' ForBinding )*
ForBinding
         ::= '$' VarName TypeDeclaration? AllowingEmpty? PositionalVar? FTScoreVar? 'in' ExprSingle
AllowingEmpty
         ::= 'allowing' 'empty'
PositionalVar
         ::= 'at' '$' VarName
FTScoreVar
         ::= 'score' '$' VarName
LetClause
         ::= 'let' LetBinding ( ',' LetBinding )*
LetBinding
         ::= ( '$' VarName TypeDeclaration? | FTScoreVar ) ':=' ExprSingle
WindowClause
         ::= 'for' ( TumblingWindowClause | SlidingWindowClause )
TumblingWindowClause
         ::= 'tumbling' 'window' '$' VarName TypeDeclaration? 'in' ExprSingle WindowStartCondition WindowEndCondition?
SlidingWindowClause
         ::= 'sliding' 'window' '$' VarName TypeDeclaration? 'in' ExprSingle WindowStartCondition WindowEndCondition
WindowStartCondition
         ::= 'start' WindowVars 'when' ExprSingle
WindowEndCondition
         ::= 'only'? 'end' WindowVars 'when' ExprSingle
WindowVars
         ::= ( '$' CurrentItem )? PositionalVar? ( 'previous' '$' PreviousItem )? ( 'next' '$' NextItem )?
CurrentItem
         ::= EQName
PreviousItem
         ::= EQName
NextItem ::= EQName
CountClause
         ::= 'count' '$' VarName
WhereClause
         ::= 'where' ExprSingle
GroupByClause
         ::= 'group' 'by' GroupingSpecList
GroupingSpecList
         ::= GroupingSpec ( ',' GroupingSpec )*
GroupingSpec
         ::= GroupingVariable ( TypeDeclaration? ':=' ExprSingle )? ( 'collation' URILiteral )?
GroupingVariable
         ::= '$' VarName
OrderByClause
         ::= ( 'order' 'by' | 'stable' 'order' 'by' ) OrderSpecList
OrderSpecList
         ::= OrderSpec ( ',' OrderSpec )*
OrderSpec
         ::= ExprSingle OrderModifier
OrderModifier
         ::= ( 'ascending' | 'descending' )? ( 'empty' ( 'greatest' | 'least' ) )? ( 'collation' URILiteral )?
ReturnClause
         ::= 'return' ExprSingle
QuantifiedExpr
         ::= ( 'some' | 'every' ) '$' VarName TypeDeclaration? 'in' ExprSingle ( ',' '$' VarName TypeDeclaration? 'in' ExprSingle )* 'satisfies' ExprSingle
SwitchExpr
         ::= 'switch' '(' Expr ')' SwitchCaseClause+ 'default' 'return' ExprSingle
SwitchCaseClause
         ::= ( 'case' SwitchCaseOperand )+ 'return' ExprSingle
SwitchCaseOperand
         ::= ExprSingle
TypeswitchExpr
         ::= 'typeswitch' '(' Expr ')' CaseClause+ 'default' ( '$' VarName )? 'return' ExprSingle
CaseClause
         ::= 'case' ( '$' VarName 'as' )? SequenceTypeUnion 'return' ExprSingle
SequenceTypeUnion
         ::= SequenceType ( '|' SequenceType )*
IfExpr   ::= 'if' '(' Expr ')' 'then' ExprSingle 'else' ExprSingle
TryCatchExpr
         ::= TryClause CatchClause+
TryClause
         ::= 'try' '{' TryTargetExpr '}'
TryTargetExpr
         ::= Expr
CatchClause
         ::= 'catch' CatchErrorList '{' Expr '}'
CatchErrorList
         ::= NameTest ( '|' NameTest )*
OrExpr   ::= AndExpr ( 'or' AndExpr )*
AndExpr  ::= ComparisonExpr ( 'and' ComparisonExpr )*
ComparisonExpr
         ::= FTContainsExpr ( ( ValueComp | GeneralComp | NodeComp ) FTContainsExpr )?
FTContainsExpr
         ::= StringConcatExpr ( 'contains' 'text' FTSelection FTIgnoreOption? )?
StringConcatExpr
         ::= RangeExpr ( '||' RangeExpr )*
RangeExpr
         ::= AdditiveExpr ( 'to' AdditiveExpr )?
AdditiveExpr
         ::= MultiplicativeExpr ( ( '+' | '-' ) MultiplicativeExpr )*
MultiplicativeExpr
         ::= UnionExpr ( ( '*' | 'div' | 'idiv' | 'mod' ) UnionExpr )*
UnionExpr
         ::= IntersectExceptExpr ( ( 'union' | '|' ) IntersectExceptExpr )*
IntersectExceptExpr
         ::= InstanceofExpr ( ( 'intersect' | 'except' ) InstanceofExpr )*
InstanceofExpr
         ::= TreatExpr ( 'instance' 'of' SequenceType )?
TreatExpr
         ::= CastableExpr ( 'treat' 'as' SequenceType )?
CastableExpr
         ::= CastExpr ( 'castable' 'as' SingleType )?
CastExpr ::= UnaryExpr ( 'cast' 'as' SingleType )?
UnaryExpr
         ::= ( '-' | '+' )* ValueExpr
ValueExpr
         ::= ValidateExpr
           | SimpleMapExpr
           | ExtensionExpr
SimpleMapExpr
         ::= PathExpr ('!' PathExpr)*
GeneralComp
         ::= '='
           | '!='
           | '<'
           | '<='
           | '>'
           | '>='
ValueComp
         ::= 'eq'
           | 'ne'
           | 'lt'
           | 'le'
           | 'gt'
           | 'ge'
NodeComp ::= 'is'
           | '<<'
           | '>>'
ValidateExpr
         ::= 'validate' ( ValidationMode | 'type' TypeName )? '{' Expr '}'
ValidationMode
         ::= 'lax'
           | 'strict'
ExtensionExpr
         ::= Pragma+ '{' Expr? '}'
Pragma   ::= '(#' S? EQName ( S PragmaContents )? '#)'
          /* ws: explicit */
PathExpr ::= '/' ( RelativePathExpr / )
           | '//' RelativePathExpr
           | RelativePathExpr
RelativePathExpr
         ::= StepExpr ( ( '/' | '//' | '!' ) StepExpr )*
StepExpr ::= PostfixExpr
           | AxisStep
AxisStep ::= ( ReverseStep | ForwardStep ) PredicateList
ForwardStep
         ::= ForwardAxis NodeTest
           | AbbrevForwardStep
ForwardAxis
         ::= 'child' '::'
           | 'descendant' '::'
           | 'attribute' '::'
           | 'self' '::'
           | 'descendant-or-self' '::'
           | 'following-sibling' '::'
           | 'following' '::'
AbbrevForwardStep
         ::= '@'? NodeTest
ReverseStep
         ::= ReverseAxis NodeTest
           | AbbrevReverseStep
ReverseAxis
         ::= 'parent' '::'
           | 'ancestor' '::'
           | 'preceding-sibling' '::'
           | 'preceding' '::'
           | 'ancestor-or-self' '::'
AbbrevReverseStep
         ::= '..'
NodeTest ::= KindTest
           | NameTest
NameTest ::= EQName
           | Wildcard
PostfixExpr
         ::= PrimaryExpr ( Predicate | ArgumentList )*
ArgumentList
         ::= '(' ( Argument ( ',' Argument )* )? ')'
PredicateList
         ::= Predicate*
Predicate
         ::= '[' Expr ']'
Literal  ::= NumericLiteral
           | StringLiteral
NumericLiteral
         ::= IntegerLiteral
           | DecimalLiteral
           | DoubleLiteral
VarRef   ::= '$' VarName
VarName  ::= EQName
ParenthesizedExpr
         ::= '(' Expr? ')'
ContextItemExpr
         ::= '.'
OrderedExpr
         ::= 'ordered' '{' Expr '}'
UnorderedExpr
         ::= 'unordered' '{' Expr '}'
FunctionCall
         ::= FunctionName ArgumentList
Argument ::= ExprSingle
           | ArgumentPlaceholder
ArgumentPlaceholder
         ::= '?'
Constructor
         ::= DirectConstructor
           | ComputedConstructor
DirectConstructor
         ::= DirElemConstructor
           | DirCommentConstructor
           | DirPIConstructor
DirElemConstructor
         ::= '<' QName DirAttributeList ( '/>' | '>' DirElemContent* '</' QName S? '>' )
          /* ws: explicit */
DirAttributeList
         ::= ( S ( QName S? '=' S? DirAttributeValue )? )*
          /* ws: explicit */
DirAttributeValue
         ::= '"' ( EscapeQuot | QuotAttrValueContent )* '"'
           | "'" ( EscapeApos | AposAttrValueContent )* "'"
          /* ws: explicit */
QuotAttrValueContent
         ::= QuotAttrContentChar
           | CommonContent
AposAttrValueContent
         ::= AposAttrContentChar
           | CommonContent
DirElemContent
         ::= DirectConstructor
           | CDataSection
           | CommonContent
           | ElementContentChar
DirCommentConstructor
         ::= '<!--' DirCommentContents '-->'
          /* ws: explicit */
DirPIConstructor
         ::= '<?' PITarget ( S DirPIContents )? '?>'
          /* ws: explicit */
ComputedConstructor
         ::= CompDocConstructor
           | CompElemConstructor
           | CompAttrConstructor
           | CompNamespaceConstructor
           | CompTextConstructor
           | CompCommentConstructor
           | CompPIConstructor
CompElemConstructor
         ::= 'element' ( EQName | '{' Expr '}' ) '{' ContentExpr? '}'
CompNamespaceConstructor
         ::= 'namespace' ( Prefix | '{' PrefixExpr '}' ) '{' URIExpr '}'
Prefix   ::= NCName
PrefixExpr
         ::= Expr
URIExpr  ::= Expr
FunctionItemExpr
         ::= NamedFunctionRef
           | InlineFunctionExpr
NamedFunctionRef
         ::= EQName '#' IntegerLiteral
InlineFunctionExpr
         ::= Annotation* 'function' '(' ParamList? ')' ( 'as' SequenceType )? FunctionBody
SingleType
         ::= SimpleTypeName '?'?
TypeDeclaration
         ::= 'as' SequenceType
SequenceType
         ::= 'empty-sequence' '(' ')'
           | ItemType ( OccurrenceIndicator / )
OccurrenceIndicator
         ::= '?'
           | '*'^OccurrenceIndicator
           | '+'
ItemType ::= KindTest
           | 'item' '(' ')'
           | FunctionTest
           | AtomicOrUnionType
           | ParenthesizedItemType
           | JSONTest
           | StructuredItemTest
JSONTest ::= 
          JSONItemTest
        | JSONObjectTest
        | JSONArrayTest

StructuredItemTest ::= "structured-item" "(" ")"

JSONItemTest ::= "json-item" "(" ")"

JSONObjectTest ::= "object" "(" ")"

JSONArrayTest ::= "array" "(" ")"

AtomicOrUnionType
         ::= EQName
KindTest ::= DocumentTest
           | ElementTest
           | AttributeTest
           | SchemaElementTest
           | SchemaAttributeTest
           | PITest
           | CommentTest
           | TextTest
           | NamespaceNodeTest
           | JSONTest 
           | AnyKindTest
AnyKindTest
         ::= 'node' '(' ')'
DocumentTest
         ::= 'document-node' '(' ( ElementTest | SchemaElementTest )? ')'
TextTest ::= 'text' '(' ')'
CommentTest
         ::= 'comment' '(' ')'
NamespaceNodeTest
         ::= 'namespace-node' '(' ')'
PITest   ::= 'processing-instruction' '(' ( NCName | StringLiteral )? ')'
AttributeTest
         ::= 'attribute' '(' ( AttribNameOrWildcard ( ',' TypeName )? )? ')'
AttribNameOrWildcard
         ::= AttributeName
           | '*'
SchemaAttributeTest
         ::= 'schema-attribute' '(' AttributeDeclaration ')'
AttributeDeclaration
         ::= AttributeName
ElementTest
         ::= 'element' '(' ( ElementNameOrWildcard ( ',' TypeName '?'? )? )? ')'
ElementNameOrWildcard
         ::= ElementName
           | '*'
SchemaElementTest
         ::= 'schema-element' '(' ElementDeclaration ')'
ElementDeclaration
         ::= ElementName
AttributeName
         ::= EQName
ElementName
         ::= EQName
SimpleTypeName
         ::= TypeName
TypeName ::= EQName
FunctionTest
         ::= Annotation* ( AnyFunctionTest | TypedFunctionTest )
AnyFunctionTest
         ::= 'function' '(' '*' ')'
TypedFunctionTest
         ::= 'function' '(' ( SequenceType ( ',' SequenceType )* )? ')' 'as' SequenceType
ParenthesizedItemType
         ::= '(' ItemType ')'
RevalidationDecl
         ::= 'declare' 'revalidation' ( 'strict' | 'lax' | 'skip' )
InsertExprTargetChoice
         ::= ( 'as' ( 'first' | 'last' ) )? 'into'
           | 'after'
           | 'before'
InsertExpr
         ::= 'insert' ( 'node' | 'nodes' ) SourceExpr InsertExprTargetChoice TargetExpr
DeleteExpr
         ::= 'delete' ( 'node' | 'nodes' ) TargetExpr
ReplaceExpr
         ::= 'replace' ( 'value' 'of' )? 'node' TargetExpr 'with' ExprSingle
RenameExpr
         ::= 'rename' 'node' TargetExpr 'as' NewNameExpr
SourceExpr
         ::= ExprSingle
TargetExpr
         ::= ExprSingle
NewNameExpr
         ::= ExprSingle
TransformExpr
         ::= 'copy' '$' VarName ':=' ExprSingle ( ',' '$' VarName ':=' ExprSingle )* 'modify' ExprSingle 'return' ExprSingle
FTSelection
         ::= FTOr FTPosFilter*
FTWeight ::= 'weight' '{' Expr '}'
FTOr     ::= FTAnd ( 'ftor' FTAnd )*
FTAnd    ::= FTMildNot ( 'ftand' FTMildNot )*
FTMildNot
         ::= FTUnaryNot ( 'not' 'in' FTUnaryNot )*
FTUnaryNot
         ::= 'ftnot'? FTPrimaryWithOptions
FTPrimaryWithOptions
         ::= FTPrimary FTMatchOptions? FTWeight?
FTPrimary
         ::= FTWords FTTimes?
           | '(' FTSelection ')'
           | FTExtensionSelection
FTWords  ::= FTWordsValue FTAnyallOption?
FTWordsValue
         ::= StringLiteral
           | '{' Expr '}'
FTExtensionSelection
         ::= Pragma+ '{' FTSelection? '}'
FTAnyallOption
         ::= 'any' 'word'?
           | 'all' 'words'?
           | 'phrase'
FTTimes  ::= 'occurs' FTRange 'times'
FTRange  ::= 'exactly' AdditiveExpr
           | 'at' ( 'least' AdditiveExpr | 'most' AdditiveExpr )
           | 'from' AdditiveExpr 'to' AdditiveExpr
FTPosFilter
         ::= FTOrder
           | FTWindow
           | FTDistance
           | FTScope
           | FTContent
FTOrder  ::= 'ordered'
FTWindow ::= 'window' AdditiveExpr FTUnit
FTDistance
         ::= 'distance' FTRange FTUnit
FTUnit   ::= 'words'
           | 'sentences'
           | 'paragraphs'
FTScope  ::= ( 'same' | 'different' ) FTBigUnit
FTBigUnit
         ::= 'sentence'
           | 'paragraph'
FTContent
         ::= 'at' ( 'start' | 'end' )
           | 'entire' 'content'
FTMatchOptions
         ::= ( 'using' FTMatchOption )+
FTMatchOption
         ::= FTLanguageOption
           | FTWildCardOption
           | FTThesaurusOption
           | FTStemOption
           | FTCaseOption
           | FTDiacriticsOption
           | FTStopWordOption
           | FTExtensionOption
FTCaseOption
         ::= 'case' ( 'insensitive' | 'sensitive' )
           | 'lowercase'
           | 'uppercase'
FTDiacriticsOption
         ::= 'diacritics' ( 'insensitive' | 'sensitive' )
FTStemOption
         ::= 'stemming'
           | 'no' 'stemming'
FTThesaurusOption
         ::= 'thesaurus' ( FTThesaurusID | 'default' | '(' ( FTThesaurusID | 'default' ) ( ',' FTThesaurusID )* ')' )
           | 'no' 'thesaurus'
FTThesaurusID
         ::= 'at' URILiteral ( 'relationship' StringLiteral )? ( FTLiteralRange 'levels' )?
FTLiteralRange
         ::= 'exactly' IntegerLiteral
           | 'at' ( 'least' IntegerLiteral | 'most' IntegerLiteral )
           | 'from' IntegerLiteral 'to' IntegerLiteral
FTStopWordOption
         ::= 'stop' 'words' ( FTStopWords FTStopWordsInclExcl* | 'default' FTStopWordsInclExcl* )
           | 'no' 'stop' 'words'
FTStopWords
         ::= 'at' URILiteral
           | '(' StringLiteral ( ',' StringLiteral )* ')'
FTStopWordsInclExcl
         ::= ( 'union' | 'except' ) FTStopWords
FTLanguageOption
         ::= 'language' StringLiteral
FTWildCardOption
         ::= 'wildcards'
           | 'no' 'wildcards'
FTExtensionOption
         ::= 'option' EQName StringLiteral
FTIgnoreOption
         ::= 'without' 'content' UnionExpr
CollectionDecl
         ::= 'collection' EQName CollectionTypeDecl?
CollectionTypeDecl
         ::= 'as' KindTest OccurrenceIndicator?
IndexName
         ::= EQName
IndexDomainExpr
         ::= PathExpr
IndexKeySpec
         ::= IndexKeyExpr IndexKeyTypeDecl? IndexKeyCollation?
IndexKeyExpr
         ::= PathExpr
IndexKeyTypeDecl
         ::= 'as' AtomicType OccurrenceIndicator?
AtomicType
         ::= EQName
IndexKeyCollation
         ::= 'collation' URILiteral
IndexDecl
         ::= 'index' IndexName 'on' 'nodes' IndexDomainExpr 'by' IndexKeySpec ( ',' IndexKeySpec )*
ICDecl   ::= 'integrity' 'constraint' EQName ( ICCollection | ICForeignKey )
ICCollection
         ::= 'on' 'collection' EQName ( ICCollSequence | ICCollSequenceUnique | ICCollNode )
ICCollSequence
         ::= VarRef 'check' ExprSingle
ICCollSequenceUnique
         ::= 'node' VarRef 'check' 'unique' 'key' PathExpr
ICCollNode
         ::= 'foreach' 'node' VarRef 'check' ExprSingle
ICForeignKey
         ::= 'foreign' 'key' ICForeignKeySource ICForeignKeyTarget
ICForeignKeySource
         ::= 'from' ICForeignKeyValues
ICForeignKeyTarget
         ::= 'to' ICForeignKeyValues
ICForeignKeyValues
         ::= 'collection' EQName 'node' VarRef 'key' PathExpr
Comment  ::= '(:' ( CommentContents | Comment )* ':)'
          /* ws: explicit */
Whitespace
         ::= S^WS
           | Comment
          /* ws: definition */
EQName   ::= FunctionName
           | 'attribute'
           | 'comment'
           | 'document-node'
           | 'element'
           | 'empty-sequence'
           | 'function'
           | 'if'
           | 'item'
           | 'namespace-node'
           | 'node'
           | 'processing-instruction'
           | 'schema-attribute'
           | 'schema-element'
           | 'switch'
           | 'text'
           | 'typeswitch'
FunctionName
         ::= EQName^Token
           | 'after'
           | 'ancestor'
           | 'ancestor-or-self'
           | 'and'
           | 'as'
           | 'ascending'
           | 'before'
           | 'case'
           | 'cast'
           | 'castable'
           | 'child'
           | 'collation'
           | 'copy'
           | 'count'
           | 'declare'
           | 'default'
           | 'delete'
           | 'descendant'
           | 'descendant-or-self'
           | 'descending'
           | 'div'
           | 'document'
           | 'else'
           | 'empty'
           | 'end'
           | 'eq'
           | 'every'
           | 'except'
           | 'first'
           | 'following'
           | 'following-sibling'
           | 'for'
           | 'ge'
           | 'group'
           | 'gt'
           | 'idiv'
           | 'import'
           | 'insert'
           | 'instance'
           | 'intersect'
           | 'into'
           | 'is'
           | 'last'
           | 'le'
           | 'let'
           | 'lt'
           | 'mod'
           | 'modify'
           | 'module'
           | 'namespace'
           | 'ne'
           | 'only'
           | 'or'
           | 'order'
           | 'ordered'
           | 'parent'
           | 'preceding'
           | 'preceding-sibling'
           | 'rename'
           | 'replace'
           | 'return'
           | 'satisfies'
           | 'self'
           | 'some'
           | 'stable'
           | 'start'
           | 'to'
           | 'treat'
           | 'try'
           | 'union'
           | 'unordered'
           | 'validate'
           | 'where'
           | 'with'
           | 'xquery'
           | 'allowing'
           | 'at'
           | 'base-uri'
           | 'boundary-space'
           | 'break'
           | 'catch'
           | 'construction'
           | 'context'
           | 'continue'
           | 'copy-namespaces'
           | 'decimal-format'
           | 'encoding'
           | 'exit'
           | 'external'
           | 'ft-option'
           | 'in'
           | 'index'
           | 'integrity'
           | 'lax'
           | 'nodes'
           | 'option'
           | 'ordering'
           | 'revalidation'
           | 'schema'
           | 'score'
           | 'sliding'
           | 'strict'
           | 'tumbling'
           | 'type'
           | 'updating'
           | 'value'
           | 'variable'
           | 'version'
           | 'while'
           | 'constraint'
           | 'loop'
           | 'returning'
           | 'object'
           | 'json-item'
           | 'array'
NCName   ::= NCName^Token
           | 'after'
           | 'and'
           | 'as'
           | 'ascending'
           | 'before'
           | 'case'
           | 'cast'
           | 'castable'
           | 'collation'
           | 'count'
           | 'default'
           | 'descending'
           | 'div'
           | 'else'
           | 'empty'
           | 'end'
           | 'eq'
           | 'except'
           | 'for'
           | 'ge'
           | 'group'
           | 'gt'
           | 'idiv'
           | 'instance'
           | 'intersect'
           | 'into'
           | 'is'
           | 'le'
           | 'let'
           | 'lt'
           | 'mod'
           | 'modify'
           | 'ne'
           | 'only'
           | 'or'
           | 'order'
           | 'return'
           | 'satisfies'
           | 'stable'
           | 'start'
           | 'to'
           | 'treat'
           | 'union'
           | 'where'
           | 'with'
           | 'ancestor'
           | 'ancestor-or-self'
           | 'attribute'
           | 'child'
           | 'comment'
           | 'copy'
           | 'declare'
           | 'delete'
           | 'descendant'
           | 'descendant-or-self'
           | 'document'
           | 'document-node'
           | 'element'
           | 'empty-sequence'
           | 'every'
           | 'first'
           | 'following'
           | 'following-sibling'
           | 'function'
           | 'if'
           | 'import'
           | 'insert'
           | 'item'
           | 'last'
           | 'module'
           | 'namespace'
           | 'namespace-node'
           | 'node'
           | 'ordered'
           | 'parent'
           | 'preceding'
           | 'preceding-sibling'
           | 'processing-instruction'
           | 'rename'
           | 'replace'
           | 'schema-attribute'
           | 'schema-element'
           | 'self'
           | 'some'
           | 'switch'
           | 'text'
           | 'try'
           | 'typeswitch'
           | 'unordered'
           | 'validate'
           | 'variable'
           | 'xquery'
           | 'allowing'
           | 'at'
           | 'base-uri'
           | 'boundary-space'
           | 'break'
           | 'catch'
           | 'construction'
           | 'context'
           | 'continue'
           | 'copy-namespaces'
           | 'decimal-format'
           | 'encoding'
           | 'exit'
           | 'external'
           | 'ft-option'
           | 'in'
           | 'index'
           | 'integrity'
           | 'lax'
           | 'nodes'
           | 'option'
           | 'ordering'
           | 'revalidation'
           | 'schema'
           | 'score'
           | 'sliding'
           | 'strict'
           | 'tumbling'
           | 'type'
           | 'updating'
           | 'value'
           | 'version'
           | 'while'
           | 'constraint'
           | 'loop'
           | 'returning'
MainModule
         ::= Prolog Program
Program  ::= StatementsAndOptionalExpr
Statements
         ::= Statement*
StatementsAndExpr
         ::= Statements Expr
StatementsAndOptionalExpr
         ::= Statements Expr?
Statement
         ::= ApplyStatement
           | AssignStatement
           | BlockStatement
           | BreakStatement
           | ContinueStatement
           | ExitStatement
           | FLWORStatement
           | IfStatement
           | SwitchStatement
           | TryCatchStatement
           | TypeswitchStatement
           | VarDeclStatement
           | WhileStatement
ApplyStatement
         ::= ExprSimple ';'
AssignStatement
         ::= '$' VarName ':=' ExprSingle ';'
BlockStatement
         ::= '{' Statements '}'
BreakStatement
         ::= 'break' 'loop' ';'
ContinueStatement
         ::= 'continue' 'loop' ';'
ExitStatement
         ::= 'exit' 'returning' ExprSingle ';'
FLWORStatement
         ::= InitialClause IntermediateClause* ReturnStatement
ReturnStatement
         ::= 'return' Statement
IfStatement
         ::= 'if' '(' Expr ')' 'then' Statement 'else' Statement
SwitchStatement
         ::= 'switch' '(' Expr ')' SwitchCaseStatement+ 'default' 'return' Statement
SwitchCaseStatement
         ::= ( 'case' SwitchCaseOperand )+ 'return' Statement
TryCatchStatement
         ::= 'try' BlockStatement ( 'catch' CatchErrorList BlockStatement )+
TypeswitchStatement
         ::= 'typeswitch' '(' Expr ')' CaseStatement+ 'default' ( '$' VarName )? 'return' Statement
CaseStatement
         ::= 'case' ( '$' VarName 'as' )? SequenceType 'return' Statement
VarDeclStatement
         ::= Annotation* 'variable' '$' VarName TypeDeclaration? ( ':=' ExprSingle )? ( ',' '$' VarName TypeDeclaration? ( ':=' ExprSingle )? )* ';'
WhileStatement
         ::= 'while' '(' Expr ')' Statement
ExprSingle
         ::= ExprSimple
           | FLWORExpr
           | IfExpr
           | SwitchExpr
           | TryCatchExpr
           | TypeswitchExpr
ExprSimple
         ::= QuantifiedExpr
           | OrExpr
           | InsertExpr
           | DeleteExpr
           | RenameExpr
           | ReplaceExpr
           | TransformExpr
           | JSONDeleteExpr
           | JSONInsertExpr
           | JSONRenameExpr
           | JSONReplaceExpr
           | JSONAppendExpr
           
JSONDeleteExpr ::= "delete" "json" PostfixExpr
      
JSONInsertExpr ::= "insert" "json" ExprSingle "into" ExprSingle ("at" "position" ExprSingle)?
      
JSONRenameExpr ::= "rename" "json" PostfixExpr "as" ExprSingle
      
JSONReplaceExpr ::= "replace" "json" "value" "of" PostfixExpr  "with" ExprSingle
      
JSONAppendExpr ::= "append" "json" ExprSingle "into" ExprSingle

CommonContent
         ::= PredefinedEntityRef
           | CharRef
           | '{{'
           | '}}'
           | BlockExpr
ContentExpr
         ::= StatementsAndExpr
CompDocConstructor
         ::= 'document' BlockExpr
CompAttrConstructor
         ::= 'attribute' ( EQName | '{' Expr '}' ) ( '{' '}' | BlockExpr )
CompPIConstructor
         ::= 'processing-instruction' ( NCName | '{' Expr '}' ) ( '{' '}' | BlockExpr )
CompCommentConstructor
         ::= 'comment' BlockExpr
CompTextConstructor
         ::= 'text' BlockExpr
PrimaryExpr
         ::= Literal
           | VarRef
           | ParenthesizedExpr
           | ContextItemExpr
           | FunctionCall
           | OrderedExpr
           | UnorderedExpr
           | Constructor
           | FunctionItemExpr
           | BlockExpr
           | ObjectConstructor
           | ArrayConstructor
           | JSONSimpleObjectUnion

JSONSimpleObjectUnion
         ::= '{|' Expr? '|}'

 
ObjectConstructor ::=  "{" ( PairConstructor ("," PairConstructor)* )? "}"
      
PairConstructor ::=  ExprSingle ":" ExprSingle
      
ArrayConstructor ::=  "[" Expr? "]"
      
BlockExpr
         ::= '{' StatementsAndOptionalExpr '}'
FunctionDecl
         ::= 'function' EQName '(' ParamList? ')' ( 'as' SequenceType )? ( '{' StatementsAndOptionalExpr '}' | 'external' )

<?TOKENS?>

PragmaContents
         ::= ( Char* - ( Char* '#)' Char* ) ) &'#'
DirCommentContents
         ::= ( ( Char - '-' ) | '-' ( Char - '-' ) )*
DirPIContents
         ::= ( Char* - ( Char* '?>' Char* ) ) &'?'
CDataSection
         ::= '<![CDATA[' CDataSectionContents ']]>'
CDataSectionContents
         ::= Char* - ( Char* ']]>' Char* )
Wildcard ::= "*"
         | (NCName ":" "*")
         | ("*" ":" NCName)
         | (BracedURILiteral "*")
EQName   ::= QName
           | URIQualifiedName
URIQualifiedName
         ::= BracedURILiteral NCName
BracedURILiteral
         ::= 'Q' '{' (PredefinedEntityRef | CharRef  | [^&{}] )* '}'
URILiteral
         ::= StringLiteral
IntegerLiteral
         ::= Digits
DecimalLiteral
         ::= '.' Digits
           | Digits '.' [0-9]*
          /* ws: explicit */
DoubleLiteral
         ::= ( '.' Digits | Digits ( '.' [0-9]* )? ) [Ee] [+#x002D]? Digits
          /* ws: explicit */
StringLiteral
         ::= '"' ( PredefinedEntityRef | CharRef | EscapeQuot | [^"&] )* '"'
           | "'" ( PredefinedEntityRef | CharRef | EscapeApos | [^&'] )* "'"
          /* ws: explicit */
PredefinedEntityRef
         ::= '&' ( 'lt' | 'gt' | 'amp' | 'quot' | 'apos' ) ';'
          /* ws: explicit */
EscapeQuot
         ::= '""'
EscapeApos
         ::= "''"
ElementContentChar
         ::= Char - [&<{}]
QuotAttrContentChar
         ::= Char - ["&<{}]
AposAttrContentChar
         ::= Char - [&'<{}]
PITarget ::= NCName - ( ( 'X' | 'x' ) ( 'M' | 'm' ) ( 'L' | 'l' ) )
Name     ::= NameStartChar NameChar*
NameStartChar
         ::= [:A-Z_a-z#x00C0-#x00D6#x00D8-#x00F6#x00F8-#x02FF#x0370-#x037D#x037F-#x1FFF#x200C-#x200D#x2070-#x218F#x2C00-#x2FEF#x3001-#xD7FF#xF900-#xFDCF#xFDF0-#xFFFD#x10000-#xEFFFF]
NameChar ::= NameStartChar
           | [-.0-9#x00B7#x0300-#x036F#x203F-#x2040]
NCName   ::= Name - ( Char* ':' Char* )
Char     ::= [#x0009#x000A#x000D#x0020-#xD7FF#xE000-#xFFFD#x10000-#x10FFFF]
QName    ::= PrefixedName
           | UnprefixedName
PrefixedName
         ::= Prefix ':' LocalPart
UnprefixedName
         ::= LocalPart
Prefix   ::= NCName
LocalPart
         ::= NCName
S        ::= [#x0009#x000A#x000D#x0020]+
CharRef  ::= '&#' [0-9]+ ';'
           | '&#x' [0-9A-Fa-f]+ ';'
Digits   ::= [0-9]+
CommentContents
         ::= ( ( Char+ - ( Char* ( '(:' | ':)' ) Char* ) ) - ( Char* '(' ) ) &':'
           |   ( Char+ - ( Char* ( '(:' | ':)' ) Char* ) )                   &'('
EOF      ::= $
NonNCNameChar
          ::= $
            | ':'
            | ( Char - NameChar )
DelimitingChar
          ::= NonNCNameChar
            | '-'
            | '.'
DelimitingChar
           \\ IntegerLiteral DecimalLiteral DoubleLiteral 
NonNCNameChar
           \\ EQName^Token QName NCName^Token 'NaN' 'after' 'all'
              'allowing' 'ancestor' 'ancestor-or-self' 'and' 'any' 
              'append' 'array' 'as' 'ascending' 'at' 'attribute' 
              'base-uri' 'before' 'boundary-space' 'break' 'by' 'case'
              'cast' 'castable' 'catch' 'check' 'child' 'collation' 
              'collection' 'comment' 'constraint' 'construction'
              'contains' 'content' 'context' 'continue' 'copy' 
              'copy-namespaces' 'count' 'decimal-format' 
              'decimal-separator' 'declare' 'default' 'delete' 
              'descendant' 'descendant-or-self' 'descending' 
              'diacritics' 'different' 'digit' 'distance' 'div' 
              'document' 'document-node' 'element' 'else' 'empty'
              'empty-sequence' 'encoding' 'end' 'entire' 'eq' 'every'
              'exactly' 'except' 'exit' 'external' 'first' 'following'
              'following-sibling' 'for' 'foreach' 'foreign' 'from'
              'ft-option' 'ftand' 'ftnot' 'ftor' 'function' 'ge'
              'greatest' 'group' 'grouping-separator' 'gt' 'idiv' 'if'
              'import' 'in' 'index' 'infinity' 'inherit' 'insensitive'
              'insert' 'instance' 'integrity' 'intersect' 'into' 'is'
              'item' 'json' 'json-item' 'key' 'language' 'last' 'lax'
              'le' 'least' 'let' 'levels' 'loop' 'lowercase' 'lt'
              'minus-sign' 'mod' 'modify' 'module' 'most' 'namespace'
              'namespace-node' 'ne' 'next' 'no' 'no-inherit'
              'no-preserve' 'node' 'nodes' 'not' 'object' 'occurs'
              'of' 'on' 'only' 'option' 'or' 'order' 'ordered'
              'ordering' 'paragraph' 'paragraphs' 'parent'
              'pattern-separator' 'per-mille' 'percent' 'phrase'
              'position' 'preceding' 'preceding-sibling' 'preserve'
              'previous' 'processing-instruction' 'relationship'
              'rename' 'replace' 'return' 'returning' 'revalidation'
              'same' 'satisfies' 'schema' 'schema-attribute'
              'schema-element' 'score' 'self' 'sensitive' 'sentence'
              'sentences' 'skip' 'sliding' 'some' 'stable' 'start'
              'stemming' 'stop' 'strict' 'strip' 'structured-item'
              'switch' 'text' 'then' 'thesaurus' 'times' 'to'
              'treat' 'try' 'tumbling' 'type' 'typeswitch' 'union'
              'unique' 'unordered' 'updating' 'uppercase' 'using'
              'validate' 'value' 'variable' 'version' 'weight'
              'when' 'where' 'while' 'wildcards' 'window' 'with'
              'without' 'word' 'words' 'xquery' 'zero-digit'
'*'       << Wildcard '*'^OccurrenceIndicator
EQName^Token
          << 'after' 'ancestor' 'ancestor-or-self' 'and' 'as' 'ascending' 'attribute' 'before' 'case' 'cast' 'castable' 'child' 'collation' 'comment' 'copy' 'count' 'declare' 'default' 'delete' 'descendant' 'descendant-or-self' 'descending' 'div' 'document' 'document-node' 'element' 'else' 'empty' 'empty-sequence' 'end' 'eq' 'every' 'except' 'first' 'following' 'following-sibling' 'for' 'function' 'ge' 'group' 'gt' 'idiv' 'if' 'import' 'insert' 'instance' 'intersect' 'into' 'is' 'item' 'last' 'le' 'let' 'lt' 'mod' 'modify' 'module' 'namespace' 'namespace-node' 'ne' 'node' 'only' 'or' 'order' 'ordered' 'parent' 'preceding' 'preceding-sibling' 'processing-instruction' 'rename' 'replace' 'return' 'satisfies' 'schema-attribute' 'schema-element' 'self' 'some' 'stable' 'start' 'switch' 'text' 'to' 'treat' 'try' 'typeswitch' 'union' 'unordered' 'validate' 'where' 'with' 'xquery' 'contains' 'paragraphs' 'sentences' 'times' 'words' 'by' 'collection' 'allowing' 'at' 'base-uri' 'boundary-space' 'break' 'catch' 'construction' 'context' 'continue' 'copy-namespaces' 'decimal-format' 'encoding' 'exit' 'external' 'ft-option' 'in' 'index' 'integrity' 'lax' 'nodes' 'option' 'ordering' 'revalidation' 'schema' 'score' 'sliding' 'strict' 'tumbling' 'type' 'updating' 'value' 'variable' 'version' 'while' 'constraint' 'loop' 'returning' 'append' 'array' 'json-item' 'object' 'structured-item'
NCName^Token
          << 'after' 'and' 'as' 'ascending' 'before' 'case' 'cast' 'castable' 'collation' 'count' 'default' 'descending' 'div' 'else' 'empty' 'end' 'eq' 'except' 'for' 'ge' 'group' 'gt' 'idiv' 'instance' 'intersect' 'into' 'is' 'le' 'let' 'lt' 'mod' 'modify' 'ne' 'only' 'or' 'order' 'return' 'satisfies' 'stable' 'start' 'to' 'treat' 'union' 'where' 'with' 'contains' 'paragraphs' 'sentences' 'times' 'words' 'by' 'ancestor' 'ancestor-or-self' 'attribute' 'child' 'comment' 'copy' 'declare' 'delete' 'descendant' 'descendant-or-self' 'document' 'document-node' 'element' 'empty-sequence' 'every' 'first' 'following' 'following-sibling' 'function' 'if' 'import' 'insert' 'item' 'last' 'module' 'namespace' 'namespace-node' 'node' 'ordered' 'parent' 'preceding' 'preceding-sibling' 'processing-instruction' 'rename' 'replace' 'schema-attribute' 'schema-element' 'self' 'some' 'switch' 'text' 'try' 'typeswitch' 'unordered' 'validate' 'variable' 'xquery' 'allowing' 'at' 'base-uri' 'boundary-space' 'break' 'catch' 'construction' 'context' 'continue' 'copy-namespaces' 'decimal-format' 'encoding' 'exit' 'external' 'ft-option' 'in' 'index' 'integrity' 'lax' 'nodes' 'option' 'ordering' 'revalidation' 'schema' 'score' 'sliding' 'strict' 'tumbling' 'type' 'updating' 'value' 'version' 'while' 'constraint' 'loop' 'returning'

<?ENCORE?>

<?xqlint
});
?>
