diff --git a/src/main/java/org/rumbledb/parser/imports/CommonParser.g4 b/src/main/java/org/rumbledb/parser/imports/CommonParser.g4 new file mode 100644 index 0000000000..87dbe77a0a --- /dev/null +++ b/src/main/java/org/rumbledb/parser/imports/CommonParser.g4 @@ -0,0 +1,1147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Authors: Matteo Agnoletto (EPMatt) and RumbleDB team. + * + * Shared parser rules for the JSONiq and XQuery grammars. + * This file is based on the XQuery parser grammar from the xqdoc project: + * https://github.com/xqdoc/xqdoc/blob/master/src/main/antlr4/org/xqdoc/XQueryParser.g4 + * + * See LICENSE-xqdoc.txt for the original license terms. + * + */ +parser grammar CommonParser; + +moduleAndThisIsIt + : module EOF + ; + +libraryModule + : KW_MODULE KW_NAMESPACE ncName EQUAL uri = uriLiteral SEMICOLON prolog + ; + +prolog + : (defaultNamespaceDecl | setter | namespaceDecl | schemaImport | moduleImport)* (annotatedDecl)* + ; + +defaultNamespaceDecl + : KW_DECLARE KW_DEFAULT type = (KW_ELEMENT | KW_FUNCTION) KW_NAMESPACE uri = stringLiteral SEMICOLON + ; + +setter + : boundarySpaceDecl + | defaultCollationDecl + | baseURIDecl + | constructionDecl + | orderingModeDecl + | emptyOrderDecl + | copyNamespacesDecl + | decimalFormatDecl + ; + +boundarySpaceDecl + : KW_DECLARE KW_BOUNDARY_SPACE type = (KW_PRESERVE | KW_STRIP) SEMICOLON + ; + +defaultCollationDecl + : KW_DECLARE KW_DEFAULT KW_COLLATION uriLiteral SEMICOLON + ; + +baseURIDecl + : KW_DECLARE KW_BASE_URI uriLiteral SEMICOLON + ; + +constructionDecl + : KW_DECLARE KW_CONSTRUCTION type = (KW_STRIP | KW_PRESERVE) SEMICOLON + ; + +orderingModeDecl + : KW_DECLARE KW_ORDERING type = (KW_ORDERED | KW_UNORDERED) SEMICOLON + ; + +emptyOrderDecl + : KW_DECLARE KW_DEFAULT KW_ORDER KW_EMPTY emptySequenceOrder = (KW_GREATEST | KW_LEAST) SEMICOLON + ; + +copyNamespacesDecl + : KW_DECLARE KW_COPY_NS preserveMode COMMA inheritMode SEMICOLON + ; + +preserveMode + : KW_PRESERVE + | KW_NO_PRESERVE + ; + +inheritMode + : KW_INHERIT + | KW_NO_INHERIT + ; + +decimalFormatDecl + : KW_DECLARE ((KW_DECIMAL_FORMAT eqName) | (KW_DEFAULT KW_DECIMAL_FORMAT)) (DFPropertyName EQUAL stringLiteral)* SEMICOLON + ; + +schemaImport + : KW_IMPORT KW_SCHEMA schemaPrefix? nsURI = uriLiteral (KW_AT locations += uriLiteral (COMMA locations += uriLiteral)*)? SEMICOLON + ; + +schemaPrefix + : (KW_NAMESPACE ncName EQUAL | KW_DEFAULT KW_ELEMENT KW_NAMESPACE) + ; + +moduleImport + : KW_IMPORT KW_MODULE (KW_NAMESPACE prefix = ncName EQUAL)? targetNamespace = uriLiteral (KW_AT locations += uriLiteral (COMMA locations += uriLiteral)*)? SEMICOLON + ; + +namespaceDecl + : KW_DECLARE KW_NAMESPACE ncName EQUAL uriLiteral SEMICOLON + ; + +varDecl + : KW_DECLARE (annotations | ncName) KW_VARIABLE varBinding + // replaced with the typeDeclaration production to match the JSONiq grammar + (KW_AS sequenceType)? ( + // replaced with the varValue production to match the JSONiq grammar + (COLON_EQ exprSingle) + // replaced with the varDefaultValue production to match the JSONiq grammar + | (external = KW_EXTERNAL (COLON_EQ exprSingle)?)) SEMICOLON + ; + +contextItemDecl + : KW_DECLARE KW_CONTEXT KW_ITEM +/* + * (KW_AS itemType)? + * TODO: this is out of spec. However, it is currently kept to match the JSONiq grammar + */ + + (KW_AS sequenceType)? // TODO: change to itemType, update expressions to use itemType, update back JSONiq grammar + ((COLON_EQ value = exprSingle) | (external = KW_EXTERNAL (COLON_EQ defaultValue = exprSingle)?)) SEMICOLON + ; + +functionDecl + : KW_DECLARE (annotations) KW_FUNCTION fn_name = functionName LPAREN paramList? RPAREN + // replaced with the functionReturn production to match the JSONiq grammar + (KW_AS return_type = sequenceType)? + // replaced functionBody to match the JSONiq grammar and the XQuery Scripting Extension spec + (LBRACE (fn_body = statementsAndOptionalExpr) RBRACE | is_external = KW_EXTERNAL) SEMICOLON + ; + +paramList + : param (COMMA param)* + ; + +param + : name = varBinding (KW_AS sequenceType)? + ; + +annotations + : annotation* + ; + +annotation + : MOD name = eqName (LPAREN literal (COMMA literal)* RPAREN)? + | updating = KW_UPDATING + ; + +optionDecl + : KW_DECLARE KW_OPTION name = eqName value = stringLiteral SEMICOLON + ; + +expr + : exprSingle (COMMA exprSingle)* + ; + +flworExpr + : // replaced with the initialClause production to match the JSONiq grammar + (start_for = forClause | start_let = letClause | start_window = windowClause) + // replaced with the intermediateClause production to match the JSONiq grammar + (forClause | letClause | windowClause | whereClause | groupByClause | orderByClause | countClause)* + // replaced with the returnClause production to match the JSONiq grammar + KW_RETURN return_expr = exprSingle + ; + +forClause + : KW_FOR vars += forVar (COMMA vars += forVar)* + ; + +forVar + : var_ref = varBinding + // replaced with the typeDeclaration production to match the JSONiq grammar + (KW_AS seq = sequenceType)? (flag = allowingEmpty)? + // replaced with the positionalVar production to match the JSONiq grammar + (KW_AT at = varBinding)? KW_IN ex = exprSingle + ; + +allowingEmpty + : KW_ALLOWING KW_EMPTY + ; + +positionalVar + : KW_AT pvar = varBinding + ; + +letClause + : KW_LET vars += letVar (COMMA vars += letVar)* + ; + +letVar + : var_ref = varBinding + // replaced with the typeDeclaration production to match the JSONiq grammar + (KW_AS seq = sequenceType)? COLON_EQ ex = exprSingle + ; + +windowClause + : KW_FOR (tumblingWindowClause | slidingWindowClause) + ; + +tumblingWindowClause + : KW_TUMBLING KW_WINDOW name = varBinding type = typeDeclaration? KW_IN exprSingle windowStartCondition windowEndCondition? + ; + +slidingWindowClause + : KW_SLIDING KW_WINDOW name = varBinding type = typeDeclaration? KW_IN exprSingle windowStartCondition windowEndCondition + ; + +windowStartCondition + : KW_START windowVars KW_WHEN exprSingle + ; + +windowEndCondition + : KW_ONLY? KW_END windowVars KW_WHEN exprSingle + ; + +windowVars + : (currentItem = varBinding)? positionalVar? (KW_PREVIOUS previousItem = varBinding)? (KW_NEXT nextItem = varBinding)? + ; + +countClause + : KW_COUNT varBinding + ; + +whereClause + : KW_WHERE exprSingle + ; + +groupByClause + : KW_GROUP KW_BY vars += groupByVar (COMMA vars += groupByVar)* + ; + +groupByVar + : var_ref = varBinding + // replaced with the typeDeclaration production to match the JSONiq grammar + ((KW_AS seq = sequenceType)? decl = COLON_EQ ex = exprSingle)? (KW_COLLATION uri = uriLiteral)? + ; + +orderByClause + : stb = KW_STABLE? KW_ORDER KW_BY specs += orderByExpr (COMMA specs += orderByExpr)* + ; + +orderByExpr + : ex = exprSingle (KW_ASCENDING | desc = KW_DESCENDING)? (KW_EMPTY (gr = KW_GREATEST | ls = KW_LEAST))? (KW_COLLATION uril = uriLiteral)? + ; + +quantifiedExpr + : (so = KW_SOME | ev = KW_EVERY) vars += quantifiedExprVar (COMMA vars += quantifiedExprVar)* KW_SATISFIES exprSingle + ; + +quantifiedExprVar + : var_ref = varBinding + // replaced with the typeDeclaration production to match the JSONiq grammar + (KW_AS seq = sequenceType)? KW_IN exprSingle + ; + +switchExpr + : KW_SWITCH LPAREN cond = expr RPAREN cases += switchCaseClause+ KW_DEFAULT KW_RETURN def = exprSingle + ; + +switchCaseClause + : (KW_CASE cond += exprSingle)+ KW_RETURN ret = exprSingle + ; + +typeswitchExpr + : KW_TYPESWITCH LPAREN cond = expr RPAREN cses += caseClause+ KW_DEFAULT (var_ref = varBinding)? KW_RETURN def = exprSingle + ; + +caseClause + : KW_CASE (var_ref = varBinding KW_AS)? union += sequenceType (VBAR union += sequenceType)* KW_RETURN ret = exprSingle + ; + +ifExpr + : KW_IF LPAREN test_condition = expr RPAREN KW_THEN branch = exprSingle KW_ELSE else_branch = exprSingle + ; + +tryCatchExpr + : KW_TRY LBRACE try_expression = expr? RBRACE catches += catchClause+ + ; + +catchClause + : KW_CATCH nameTest (VBAR nameTest)* + // replaced with the enclosedExpression production to match the JSONiq grammar + LBRACE catch_expression = expr? RBRACE + ; + +enclosedExpression + : LBRACE expr? RBRACE + ; + +orExpr + : main_expr = andExpr (KW_OR rhs += andExpr)* + ; + +comparisonExpr + : main_expr = stringConcatExpr (op += compOp rhs += stringConcatExpr)? + ; + +stringConcatExpr + : main_expr = rangeExpr (CONCATENATION rhs += rangeExpr)* + ; + +rangeExpr + : main_expr = additiveExpr (KW_TO rhs += additiveExpr)? + ; + +additiveExpr + : main_expr = multiplicativeExpr (op += (PLUS | MINUS) rhs += multiplicativeExpr)* + ; + +multiplicativeExpr + : main_expr = unionExpr (op += (STAR | KW_DIV | KW_IDIV | KW_MOD) rhs += unionExpr)* + ; + +unionExpr + : main_expr = intersectExceptExpr (op += (KW_UNION | VBAR) rhs += intersectExceptExpr)* + ; + +intersectExceptExpr + : main_expr = instanceOfExpr (op += (KW_INTERSECT | KW_EXCEPT) rhs += instanceOfExpr)* + ; + +instanceOfExpr + : main_expr = isStaticallyExpr (KW_INSTANCE KW_OF seq = sequenceType)? + ; + +isStaticallyExpr + : main_expr = treatExpr (KW_IS KW_STATICALLY seq = sequenceType)? + ; + +treatExpr + : main_expr = castableExpr (KW_TREAT KW_AS seq = sequenceType)? + ; + +castableExpr + : main_expr = castExpr (KW_CASTABLE KW_AS single = singleType)? + ; + +castExpr + : main_expr = arrowExpr (KW_CAST KW_AS single = singleType)? + ; + +arrowExpr + : main_expr = unaryExpr (ARROW function += arrowFunctionSpecifier arguments += argumentList)* + ; + +unaryExpr + : op += (MINUS | PLUS)* main_expr = valueExpr + ; + +valueExpr + : validate_expr = validateExpr + | extensionExpr + | simpleMap_expr = simpleMapExpr + ; + +compOp + : valueComp + | generalComp + | nodeComp + ; + +generalComp + : EQUAL + | NOT_EQUAL + | LANGLE + | (LANGLE EQUAL) + | RANGLE + | (RANGLE EQUAL) + ; + +valueComp + : KW_EQ + | KW_NE + | KW_LT + | KW_LE + | KW_GT + | KW_GE + ; + +nodeComp + : KW_IS + | (LANGLE LANGLE) + | (RANGLE RANGLE) + ; + +validateExpr + : KW_VALIDATE (validationMode | (KW_TYPE sequenceType))? LBRACE expr? RBRACE + ; + +validationMode + : KW_LAX + | KW_STRICT + ; + +extensionExpr + : PRAGMA+ LBRACE expr RBRACE + ; + +simpleMapExpr + : main_expr = pathExpr (BANG map_expr += pathExpr)* + ; + +pathExpr + : (SLASH singleslash = relativePathExpr?) + | (DSLASH doubleslash = relativePathExpr) + | relative = relativePathExpr + ; + +relativePathExpr + : stepExpr (sep += (SLASH | DSLASH) stepExpr)* + ; + +stepExpr + : postfixExpr + | axisStep + ; + +axisStep + : (reverseStep | forwardStep) predicateList + ; + +forwardStep + : (forwardAxis nodeTest) + | abbrevForwardStep + ; + +forwardAxis + : (KW_CHILD | KW_DESCENDANT | KW_ATTRIBUTE | KW_SELF | KW_DESCENDANT_OR_SELF | KW_FOLLOWING_SIBLING | KW_FOLLOWING) COLON COLON + ; + +abbrevForwardStep + : AT? nodeTest + ; + +reverseStep + : (reverseAxis nodeTest) + | abbrevReverseStep + ; + +reverseAxis + : (KW_PARENT | KW_ANCESTOR | KW_PRECEDING_SIBLING | KW_PRECEDING | KW_ANCESTOR_OR_SELF) COLON COLON + ; + +abbrevReverseStep + : DDOT + ; + +nodeTest + : nameTest + | kindTest + ; + +nameTest + : eqName + | wildcard + ; + +wildcard + : STAR # allNames + | NCNameWithLocalWildcard # allWithNS // walkers must strip out the trailing :* + | NCNameWithPrefixWildcard # allWithLocal // walkers must strip out the leading *: + | (BracedURILiteral STAR) # BracedURILiteral + ; + +argumentList + : LPAREN (args += argument (COMMA args += argument)*)? RPAREN + ; + +predicateList + : predicate* + ; + +predicate + : LBRACKET expr RBRACKET + ; + +lookup + : QUESTION keySpecifier + ; + +keySpecifier + : (nc = ncName | in = IntegerLiteral | pe = parenthesizedExpr | wc = STAR | lt = stringLiteral | vr = varRef) + ; + +arrowFunctionSpecifier + : eqName + | varRef + | parenthesizedExpr + ; + +literal + : numericLiteral + | stringLiteral + ; + +numericLiteral + : IntegerLiteral + | DecimalLiteral + | DoubleLiteral + ; + +varRef + : DOLLAR var_name = eqName + ; + +varBinding + : DOLLAR var_name = eqName + ; + +parenthesizedExpr + : LPAREN expr? RPAREN + ; + +orderedExpr + : KW_ORDERED enclosedExpression + ; + +unorderedExpr + : KW_UNORDERED enclosedExpression + ; + +functionCall + : fn_name = functionName argumentList + ; + +argument + : exprSingle + | QUESTION + ; + +nodeConstructor + : directConstructor + | computedConstructor + ; + +directConstructor + : LANGLE open_tag_name = qname attributes = dirAttributeList (open_close = dirElemConstructorOpenClose | single_tag = dirElemConstructorSingleTag) + | COMMENT + | PI + ; + +dirElemConstructorOpenClose + : endOpen = RANGLE dirElemContent* startClose = LANGLE slashClose = SLASH close_tag_name = qname RANGLE + ; + +dirElemConstructorSingleTag + : slashClose = SLASH RANGLE + ; + +dirAttributeList + : (attribute_qname += qname EQUAL attribute_value += dirAttributeValue)* + ; + +dirAttributeValueQuot + : Quot (PredefinedEntityRef | CharRef | escapedQuot | dirAttributeContentQuot)* Quot + ; + +dirAttributeValueApos + : Apos (PredefinedEntityRef | CharRef | escapedApos | dirAttributeContentApos)* Apos + ; + +dirAttributeValue + : dirAttributeValueQuot + | dirAttributeValueApos + ; + +dirAttributeContentQuot + : LBRACE LBRACE + | RBRACE RBRACE + | LBRACE expr? RBRACE + | ~ (Quot | LBRACE | RBRACE | Ampersand | PredefinedEntityRef | CharRef | LANGLE | COMMENT | XMLDECL | PI | CDATA) + ; + +dirAttributeContentApos + : LBRACE LBRACE + | RBRACE RBRACE + | LBRACE expr? RBRACE + | ~ (Apos | LBRACE | RBRACE | Ampersand | PredefinedEntityRef | CharRef | LANGLE | COMMENT | XMLDECL | PI | CDATA) + ; + +escapedQuot + : Quot Quot + ; + +escapedApos + : Apos Apos + ; + +dirElemContent + : directConstructor + | commonContent + | CDATA + // ~[{}<&] = '" + ~['"{}<&] + | Quot + | Apos + | noQuotesNoBracesNoAmpNoLAng + ; + +commonContent + : (PredefinedEntityRef | CharRef) + | LBRACE LBRACE + | RBRACE RBRACE + | (LBRACE expr? RBRACE) + ; + +computedConstructor + : compDocConstructor + | compElemConstructor + | compAttrConstructor + | compNamespaceConstructor + | compTextConstructor + | compCommentConstructor + | compPIConstructor + ; + +compDocConstructor + : KW_DOCUMENT enclosedExpression + ; + +compElemConstructor + : KW_ELEMENT (eqName | (LBRACE expr RBRACE)) enclosedContentExpr + ; + +enclosedContentExpr + : enclosedExpression + ; + +compAttrConstructor + : KW_ATTRIBUTE (name = eqName | (LBRACE name_expr = expr RBRACE)) enclosedExpression + ; + +compNamespaceConstructor + : KW_NAMESPACE (ncName | enclosedPrefixExpr) enclosedURIExpr + ; + +enclosedPrefixExpr + : enclosedExpression + ; + +enclosedURIExpr + : enclosedExpression + ; + +compTextConstructor + : KW_TEXT enclosedExpression + ; + +compCommentConstructor + : KW_COMMENT enclosedExpression + ; + +compPIConstructor + : KW_PI (ncName | (LBRACE expr RBRACE)) enclosedExpression + ; + +functionItemExpr + : namedFunctionRef + | inlineFunctionExpr + ; + +namedFunctionRef + : fn_name = functionName HASH arity = IntegerLiteral + ; + +inlineFunctionExpr + : annotations KW_FUNCTION LPAREN paramList? RPAREN (KW_AS return_type = sequenceType)? (LBRACE (fn_body = statementsAndOptionalExpr) RBRACE) + ; + +arrayConstructor + : squareArrayConstructor + | curlyArrayConstructor + ; + +squareArrayConstructor + : LBRACKET (exprSingle (COMMA exprSingle)*)? RBRACKET + ; + +curlyArrayConstructor + : KW_ARRAY enclosedExpression + ; + +stringConstructor + : ENTER_STRING stringConstructorContent EXIT_STRING + ; + +stringConstructorContent + : stringConstructorChars (stringConstructorInterpolation stringConstructorChars)* + ; + +charNoGrave + : BASIC_CHAR + | LBRACE + | RBRACKET + ; + +charNoLBrace + : BASIC_CHAR + | GRAVE + | RBRACKET + ; + +charNoRBrack + : BASIC_CHAR + | GRAVE + | LBRACE + ; + +stringConstructorChars + : (BASIC_CHAR | charNoGrave charNoLBrace | charNoRBrack charNoGrave charNoGrave | charNoGrave | LBRACE)* + ; + +stringConstructorInterpolation + : ENTER_INTERPOLATION expr EXIT_INTERPOLATION + ; + +unaryLookup + : QUESTION keySpecifier + ; + +singleType + : item = itemType (question += QUESTION)? + ; + +typeDeclaration + : KW_AS sequenceType + ; + +kindTest + : documentTest + | elementTest + | attributeTest + | schemaElementTest + | schemaAttributeTest + | piTest + | commentTest + | textTest + | namespaceNodeTest + | binaryNodeTest + | anyKindTest + ; + +anyKindTest + : KW_NODE LPAREN STAR? RPAREN + ; + +binaryNodeTest + : KW_BINARY LPAREN RPAREN + ; + +documentTest + : KW_DOCUMENT_NODE LPAREN (elementTest | schemaElementTest)? RPAREN + ; + +textTest + : KW_TEXT LPAREN RPAREN + ; + +commentTest + : KW_COMMENT LPAREN RPAREN + ; + +namespaceNodeTest + : KW_NAMESPACE_NODE LPAREN RPAREN + ; + +piTest + : KW_PI LPAREN (ncName | stringLiteral)? RPAREN + ; + +attributeTest + : KW_ATTRIBUTE LPAREN (attributeNameOrWildcard (COMMA type = typeName)?)? RPAREN + ; + +attributeNameOrWildcard + : attributeName + | STAR + ; + +schemaAttributeTest + : KW_SCHEMA_ATTR LPAREN attributeDeclaration RPAREN + ; + +elementTest + : KW_ELEMENT LPAREN (elementNameOrWildcard (COMMA type = typeName optional = QUESTION?)?)? RPAREN + ; + +elementNameOrWildcard + : elementName + | STAR + ; + +schemaElementTest + : KW_SCHEMA_ELEM LPAREN elementDeclaration RPAREN + ; + +elementDeclaration + : elementName + ; + +attributeName + : eqName + ; + +elementName + : eqName + ; + +simpleTypeName + : typeName + ; + +typeName + : eqName + ; + +functionTest + : annotation* (anyFunctionTest | typedFunctionTest) + ; + +anyFunctionTest + : KW_FUNCTION LPAREN STAR RPAREN + ; + +typedFunctionTest + : KW_FUNCTION LPAREN (st += sequenceType (COMMA st += sequenceType)*)? RPAREN KW_AS rt = sequenceType + ; + +mapTest + : anyMapTest + | typedMapTest + ; + +anyMapTest + : KW_MAP LPAREN STAR RPAREN + ; + +typedMapTest + : KW_MAP LPAREN eqName COMMA sequenceType RPAREN + ; + +arrayTest + : anyArrayTest + | typedArrayTest + ; + +anyArrayTest + : KW_ARRAY LPAREN STAR RPAREN + ; + +typedArrayTest + : KW_ARRAY LPAREN sequenceType RPAREN + ; + +parenthesizedItemTest + : LPAREN itemType RPAREN + ; + +attributeDeclaration + : attributeName + ; + +eqName + : qname + | URIQualifiedName + ; + +qname + : FullQName + | (ns = ncName COLON)? local_name = ncName + ; + +ncName + : NCName + | keyword + ; + +functionName + : FullQName + | NCName + | URIQualifiedName + | keywordOKForFunction + ; + +keyword + : keywordOKForFunction + | keywordNotOKForFunction + ; + +keywordNotOKForFunction + : KW_ATTRIBUTE + | KW_COMMENT + | KW_DOCUMENT_NODE + | KW_ELEMENT + | KW_EMPTY_SEQUENCE + | KW_IF + | KW_ITEM + | KW_CONTEXT + | KW_NODE + | KW_PI + | KW_SCHEMA_ATTR + | KW_SCHEMA_ELEM + | KW_BINARY + | KW_TEXT + | KW_TYPESWITCH + | KW_SWITCH + | KW_NAMESPACE_NODE + | KW_TYPE + | KW_TUMBLING + | KW_TRY + | KW_CATCH + | KW_ONLY + | KW_WHEN + | KW_SLIDING + | KW_DECIMAL_FORMAT + | KW_WINDOW + | KW_MAP + | KW_END + | KW_ALLOWING + | KW_ARRAY + | DFPropertyName + ; + +uriLiteral + : stringLiteral + ; + +stringLiteral + : stringLiteralQuot + | stringLiteralApos + ; + +noQuotesNoBracesNoAmpNoLAng + : (keyword | (IntegerLiteral | DecimalLiteral | DoubleLiteral + //| stringLiteral + | PRAGMA | EQUAL | HASH | NOT_EQUAL | LPAREN | RPAREN | LBRACKET | RBRACKET | STAR | PLUS | MINUS | TILDE | COMMA | ARROW | MOD | DOT | GRAVE | DDOT | COLON | CARAT | COLON_EQ | SEMICOLON | SLASH | DSLASH | BACKSLASH | VBAR | RANGLE | QUESTION | AT | DOLLAR | BANG | FullQName | URIQualifiedName | NCNameWithLocalWildcard | NCNameWithPrefixWildcard | NCName | ContentChar))+ + ; + +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 SEMICOLON + ; + +assignStatement + : var_ref = varRef COLON_EQ exprSingle SEMICOLON + ; + +blockStatement + : LBRACE statements RBRACE + ; + +breakStatement + : KW_BREAK KW_LOOP SEMICOLON + ; + +continueStatement + : KW_CONTINUE KW_LOOP SEMICOLON + ; + +exitStatement + : KW_EXIT KW_RETURNING exprSingle SEMICOLON + ; + +flworStatement + : (start_for = forClause | start_let = letClause) + // replaced with the intermediateClause production to match the JSONiq grammar + (forClause | letClause | whereClause | groupByClause | orderByClause | countClause)* + // replaced with the returnStatement production to match the JSONiq grammar + KW_RETURN returnStmt = statement + ; + +ifStatement + : KW_IF LPAREN test_expr = expr RPAREN KW_THEN branch = statement KW_ELSE else_branch = statement + ; + +switchStatement + : KW_SWITCH LPAREN condExpr = expr RPAREN cases += switchCaseStatement+ KW_DEFAULT KW_RETURN def = statement + ; + +switchCaseStatement + : (KW_CASE cond += exprSingle)+ KW_RETURN ret = statement + ; + +tryCatchStatement + : KW_TRY try_block = blockStatement catches += catchCaseStatement+ + ; + +catchCaseStatement + : KW_CATCH nameTest (VBAR nameTest)* catch_block = blockStatement + ; + +typeSwitchStatement + : KW_TYPESWITCH LPAREN cond = expr RPAREN cases += caseStatement+ KW_DEFAULT (var_ref = varBinding)? (KW_RETURN) def = statement + ; + +caseStatement + : KW_CASE (var_ref = varBinding KW_AS)? union += sequenceType (VBAR union += sequenceType)* (KW_RETURN) ret = statement + ; + +varDeclStatement + : annotations KW_VARIABLE varDeclForStatement (COMMA varDeclForStatement)* SEMICOLON + ; + +varDeclForStatement + : var_ref = varBinding (KW_AS sequenceType)? (COLON_EQ expr_vals += exprSingle)? + ; + +whileStatement + : KW_WHILE LPAREN test_expr = expr RPAREN stmt = statement + ; + +exprSingle + : exprSimple + | flworExpr + | ifExpr + | switchExpr + | tryCatchExpr + | typeswitchExpr + ; + +exprSimple + : quantifiedExpr + | orExpr + | insertExpr + | deleteExpr + | renameExpr + | replaceExpr + | transformExpr + | appendExpr + | createCollectionExpr + | truncateCollectionExpr + | deleteIndexExpr + | deleteSearchExpr + | editCollectionExpr + | insertIndexExpr + | insertSearchExpr + ; + +blockExpr + : LBRACE statementsAndExpr RBRACE + ; + +insertExpr + : KW_INSERT KW_JSON to_insert_expr = exprSingle KW_INTO main_expr = exprSingle (KW_AT KW_POSITION pos_expr = exprSingle)? + | KW_INSERT KW_JSON pairConstructor (COMMA pairConstructor)* KW_INTO main_expr = exprSingle + ; + +deleteExpr + : KW_DELETE KW_JSON updateLocator + ; + +renameExpr + : KW_RENAME KW_JSON updateLocator KW_AS name_expr = exprSingle + ; + +replaceExpr + : KW_REPLACE KW_VALUE KW_OF KW_JSON updateLocator KW_WITH replacer_expr = exprSingle + ; + +transformExpr + : KW_COPY copyDecl (COMMA copyDecl)* KW_MODIFY mod_expr = exprSingle KW_RETURN ret_expr = exprSingle + ; + +appendExpr + : KW_APPEND KW_JSON to_append_expr = exprSingle KW_INTO array_expr = exprSingle + ; + +updateLocator + : main_expr = postfixExpr + ; + +copyDecl + : var_ref = varBinding COLON_EQ src_expr = exprSingle + ; + +createCollectionExpr + : KW_CREATE KW_COLLECTION collectionMode = (KW_TABLE | KW_DELTA_FILE | KW_ICEBERG_TABLE) LPAREN collection_name = exprSimple RPAREN (KW_WITH content = exprSingle)? + ; + +deleteIndexExpr + : KW_DELETE ((first = KW_FIRST | last = KW_LAST) num = exprSingle?) KW_FROM KW_COLLECTION collectionMode = (KW_TABLE | KW_DELTA_FILE | KW_ICEBERG_TABLE) LPAREN collection_name = exprSimple RPAREN + ; + +deleteSearchExpr + : KW_DELETE content = exprSingle KW_FROM KW_COLLECTION + ; + +insertIndexExpr + : KW_INSERT content = exprSingle ((KW_AT pos = exprSingle) | first = KW_FIRST | last = KW_LAST) KW_INTO KW_COLLECTION collectionMode = (KW_TABLE | KW_DELTA_FILE | KW_ICEBERG_TABLE) LPAREN collection_name = exprSimple RPAREN + ; + +insertSearchExpr + : KW_INSERT content = exprSingle (before = KW_BEFORE | after = KW_AFTER) target = exprSingle KW_INTO KW_COLLECTION + ; + +truncateCollectionExpr + : (KW_DELETE | KW_TRUNCATE) KW_COLLECTION collectionMode = (KW_TABLE | KW_DELTA_FILE | KW_ICEBERG_TABLE) LPAREN collection_name = exprSimple RPAREN + ; + +editCollectionExpr + : KW_EDIT target = exprSingle KW_INTO content = exprSingle KW_IN KW_COLLECTION + ; + diff --git a/src/main/java/org/rumbledb/parser/jsoniq/JsoniqParser.g4 b/src/main/java/org/rumbledb/parser/jsoniq/JsoniqParser.g4 index 8c80e90c53..64b37db651 100644 --- a/src/main/java/org/rumbledb/parser/jsoniq/JsoniqParser.g4 +++ b/src/main/java/org/rumbledb/parser/jsoniq/JsoniqParser.g4 @@ -25,6 +25,7 @@ */ parser grammar JsoniqParser; +import CommonParser; @ header { // Java header @@ -32,885 +33,90 @@ package org.rumbledb.parser.jsoniq; } options { tokenVocab = JsoniqLexer; } -/* - Mostly taken from http://www.w3.org/TR/xquery/#id-grammar, with some simplications:CHANNELS - 1. The parser itself doesn't really enforce ws:explicit except for some easy - cases (QNames and wildcards). Walkers will need to do this (and also parse - wildcards a bit). - - 2. When collecting element content, we will need to check the HIDDEN - channel as well, for whitespace and XQuery comments (these should be - treated as regular text inside elements). -*/ - - -// MODULE HEADER /////////////////////////////////////////////////////////////// - -// this rule was added to match the JSONiq grammar - -moduleAndThisIsIt - : module EOF - ; - -module - // replaced with the versionDecl production to match the JSONiq grammar - : (KW_JSONIQ KW_VERSION vers = stringLiteral (KW_ENCODING encoding = stringLiteral)? SEMICOLON)? - // TODO: subsequent optional main modules are currently ignored - (libraryModule | main = mainModule) - ; - -versionDecl - : KW_JSONIQ KW_VERSION version = stringLiteral (KW_ENCODING encoding = stringLiteral)? SEMICOLON - ; - // mainModule and queryBody are replaced with the mainModule and program rules according to the XQuery Scripting Extension spec - -libraryModule - : KW_MODULE KW_NAMESPACE ncName EQUAL uri = uriLiteral SEMICOLON prolog - ; - // MODULE PROLOG /////////////////////////////////////////////////////////////// - -prolog - : (defaultNamespaceDecl | setter | namespaceDecl | schemaImport | moduleImport)* (annotatedDecl)* - ; - // added to match the JSONiq grammar - -annotatedDecl - : functionDecl - | varDecl - | typeDecl - | contextItemDecl - | optionDecl - ; - -defaultNamespaceDecl - : KW_DECLARE KW_DEFAULT type = (KW_ELEMENT | KW_FUNCTION) KW_NAMESPACE uri = stringLiteral SEMICOLON - ; - -setter - : boundarySpaceDecl - | defaultCollationDecl - | baseURIDecl - | constructionDecl - | orderingModeDecl - | emptyOrderDecl - | copyNamespacesDecl - | decimalFormatDecl - ; - -boundarySpaceDecl - : KW_DECLARE KW_BOUNDARY_SPACE type = (KW_PRESERVE | KW_STRIP) SEMICOLON - ; - -defaultCollationDecl - : KW_DECLARE KW_DEFAULT KW_COLLATION uriLiteral SEMICOLON - ; - -baseURIDecl - : KW_DECLARE KW_BASE_URI uriLiteral SEMICOLON - ; - -constructionDecl - : KW_DECLARE KW_CONSTRUCTION type = (KW_STRIP | KW_PRESERVE) SEMICOLON - ; - -orderingModeDecl - : KW_DECLARE KW_ORDERING type = (KW_ORDERED | KW_UNORDERED) SEMICOLON - ; - -emptyOrderDecl - : KW_DECLARE KW_DEFAULT KW_ORDER KW_EMPTY emptySequenceOrder = (KW_GREATEST | KW_LEAST) SEMICOLON - ; - -copyNamespacesDecl - : KW_DECLARE KW_COPY_NS preserveMode COMMA inheritMode SEMICOLON - ; - -preserveMode - : KW_PRESERVE - | KW_NO_PRESERVE - ; - -inheritMode - : KW_INHERIT - | KW_NO_INHERIT - ; - -decimalFormatDecl - : KW_DECLARE ((KW_DECIMAL_FORMAT eqName) | (KW_DEFAULT KW_DECIMAL_FORMAT)) (DFPropertyName EQUAL stringLiteral)* SEMICOLON - ; - -schemaImport - : KW_IMPORT KW_SCHEMA schemaPrefix? nsURI = uriLiteral (KW_AT locations += uriLiteral (COMMA locations += uriLiteral)*)? SEMICOLON - ; - -schemaPrefix - : (KW_NAMESPACE ncName EQUAL | KW_DEFAULT KW_ELEMENT KW_NAMESPACE) - ; - -moduleImport - : KW_IMPORT KW_MODULE (KW_NAMESPACE prefix = ncName EQUAL)? targetNamespace = uriLiteral (KW_AT locations += uriLiteral (COMMA locations += uriLiteral)*)? SEMICOLON - ; - -namespaceDecl - : KW_DECLARE KW_NAMESPACE ncName EQUAL uriLiteral SEMICOLON - ; - -varDecl - : KW_DECLARE (annotations | ncName) KW_VARIABLE varBinding - // replaced with the typeDeclaration production to match the JSONiq grammar - (KW_AS sequenceType)? ( - // replaced with the varValue production to match the JSONiq grammar - (COLON_EQ exprSingle) - // replaced with the varDefaultValue production to match the JSONiq grammar - | (external = KW_EXTERNAL (COLON_EQ exprSingle)?)) SEMICOLON - ; - -contextItemDecl - : KW_DECLARE KW_CONTEXT KW_ITEM -/* - * (KW_AS itemType)? - * TODO: this is out of spec. However, it is currently kept to match the JSONiq grammar - */ - - (KW_AS sequenceType)? // TODO: change to itemType, update expressions to use itemType, update back JSONiq grammar - ((COLON_EQ value = exprSingle) | (external = KW_EXTERNAL (COLON_EQ defaultValue = exprSingle)?)) SEMICOLON - ; - -/** - Constrains to valid function names only - See https://www.w3.org/TR/xquery-31/#parse-note-reserved-function-names -*/ functionDecl - : KW_DECLARE (annotations) KW_FUNCTION fn_name = functionName LPAREN paramList? RPAREN - // replaced with the functionReturn production to match the JSONiq grammar - (KW_AS return_type = sequenceType)? - // replaced functionBody to match the JSONiq grammar and the XQuery Scripting Extension spec - (LBRACE (fn_body = statementsAndOptionalExpr) RBRACE | is_external = KW_EXTERNAL) SEMICOLON - ; - // renamed from functionParams to paramList to match the JSONiq grammar - -paramList - : param (COMMA param)* - ; - -/** - Renamed from functionParam to param to match the JSONiq grammar - Replaced with the typeDeclaration production to match the JSONiq grammar -*/ param - : name = varBinding (KW_AS sequenceType)? - ; - -annotations - : annotation* - ; - -annotation - // added the updating keyword to support the out-of-spec updating expressions extension - : MOD name = eqName (LPAREN literal (COMMA literal)* RPAREN)? - | updating = KW_UPDATING - ; - -optionDecl - : KW_DECLARE KW_OPTION name = eqName value = stringLiteral SEMICOLON - ; - -typeDecl - : KW_DECLARE KW_TYPE type_name = qname KW_AS (schema = schemaLanguage)? type_definition = exprSingle SEMICOLON - ; - -schemaLanguage - : KW_JSOUND KW_COMPACT - | KW_JSOUND KW_VERBOSE - | KW_JSON KW_SCHEMA - ; - // EXPRESSIONS ///////////////////////////////////////////////////////////////// - -expr - : exprSingle (COMMA exprSingle)* - ; - -flworExpr - : // replaced with the initialClause production to match the JSONiq grammar - (start_for = forClause | start_let = letClause | start_window = windowClause) - // replaced with the intermediateClause production to match the JSONiq grammar - (forClause | letClause | windowClause | whereClause | groupByClause | orderByClause | countClause)* - // replaced with the returnClause production to match the JSONiq grammar - KW_RETURN return_expr = exprSingle - ; - -forClause - : KW_FOR vars += forVar (COMMA vars += forVar)* - ; - // renamed from forBinding to forVar to match the JSONiq grammar - -forVar - : var_ref = varBinding - // replaced with the typeDeclaration production to match the JSONiq grammar - (KW_AS seq = sequenceType)? (flag = allowingEmpty)? - // replaced with the positionalVar production to match the JSONiq grammar - (KW_AT at = varBinding)? KW_IN ex = exprSingle - ; - -allowingEmpty - : KW_ALLOWING KW_EMPTY - ; - -positionalVar - : KW_AT pvar = varBinding - ; - -letClause - : KW_LET vars += letVar (COMMA vars += letVar)* - ; - // renamed from letBinding to letVar to match the JSONiq grammar - -letVar - : var_ref = varBinding - // replaced with the typeDeclaration production to match the JSONiq grammar - (KW_AS seq = sequenceType)? COLON_EQ ex = exprSingle - ; - -windowClause - : KW_FOR (tumblingWindowClause | slidingWindowClause) - ; - -tumblingWindowClause - : KW_TUMBLING KW_WINDOW name = varBinding type = typeDeclaration? KW_IN exprSingle windowStartCondition windowEndCondition? - ; - -slidingWindowClause - : KW_SLIDING KW_WINDOW name = varBinding type = typeDeclaration? KW_IN exprSingle windowStartCondition windowEndCondition - ; - -windowStartCondition - : KW_START windowVars KW_WHEN exprSingle - ; - -windowEndCondition - : KW_ONLY? KW_END windowVars KW_WHEN exprSingle - ; - -windowVars - : (currentItem = varBinding)? positionalVar? (KW_PREVIOUS previousItem = varBinding)? (KW_NEXT nextItem = varBinding)? - ; - -countClause - : KW_COUNT varBinding - ; - -whereClause - : KW_WHERE exprSingle - ; - // replaced with the groupingSpecList production to match the JSONiq grammar - -groupByClause - : KW_GROUP KW_BY vars += groupByVar (COMMA vars += groupByVar)* - ; - -groupByVar - // renamed from groupingSpec to groupByVar to match the JSONiq grammar - : var_ref = varBinding - // replaced with the typeDeclaration production to match the JSONiq grammar - ((KW_AS seq = sequenceType)? decl = COLON_EQ ex = exprSingle)? (KW_COLLATION uri = uriLiteral)? - ; - -orderByClause - : stb = KW_STABLE? KW_ORDER KW_BY specs += orderByExpr (COMMA specs += orderByExpr)* - ; - -orderByExpr - // renamed from orderSpec to orderByExpr to match the JSONiq grammar - : ex = exprSingle (KW_ASCENDING | desc = KW_DESCENDING)? (KW_EMPTY (gr = KW_GREATEST | ls = KW_LEAST))? (KW_COLLATION uril = uriLiteral)? - ; - -quantifiedExpr - : (so = KW_SOME | ev = KW_EVERY) vars += quantifiedExprVar (COMMA vars += quantifiedExprVar)* KW_SATISFIES exprSingle - ; - -quantifiedExprVar - // renamed from quantifiedVar to quantifiedExprVar to match the JSONiq grammar - : var_ref = varBinding - // replaced with the typeDeclaration production to match the JSONiq grammar - (KW_AS seq = sequenceType)? KW_IN exprSingle - ; - -switchExpr - : KW_SWITCH LPAREN cond = expr RPAREN cases += switchCaseClause+ KW_DEFAULT KW_RETURN def = exprSingle - ; - -switchCaseClause - : (KW_CASE cond += exprSingle)+ KW_RETURN ret = exprSingle - ; - -typeswitchExpr - : KW_TYPESWITCH LPAREN cond = expr RPAREN cses += caseClause+ KW_DEFAULT (var_ref = varBinding)? KW_RETURN def = exprSingle - ; - -caseClause - : KW_CASE (var_ref = varBinding KW_AS)? union += sequenceType (VBAR union += sequenceType)* KW_RETURN ret = exprSingle - ; - -ifExpr - : KW_IF LPAREN test_condition = expr RPAREN KW_THEN branch = exprSingle KW_ELSE else_branch = exprSingle - ; - // replaced with the tryClause and the enclosedTryTargetExpression productions to match the JSONiq grammar - -tryCatchExpr - : KW_TRY LBRACE try_expression = expr? RBRACE catches += catchClause+ - ; - -catchClause - : KW_CATCH nameTest (VBAR nameTest)* - // replaced with the enclosedExpression production to match the JSONiq grammar - LBRACE catch_expression = expr? RBRACE - ; - -enclosedExpression - : LBRACE expr? RBRACE - ; - -orExpr - : main_expr = andExpr (KW_OR rhs += andExpr)* - ; - -andExpr - : main_expr = notExpr (KW_AND rhs += notExpr)* - ; - -notExpr - : op += KW_NOT? main_expr = comparisonExpr - ; - -comparisonExpr - : main_expr = stringConcatExpr (op += compOp rhs += stringConcatExpr)? - ; - -stringConcatExpr - : main_expr = rangeExpr (CONCATENATION rhs += rangeExpr)* - ; - -rangeExpr - : main_expr = additiveExpr (KW_TO rhs += additiveExpr)? - ; - -additiveExpr - : main_expr = multiplicativeExpr (op += (PLUS | MINUS) rhs += multiplicativeExpr)* - ; - -multiplicativeExpr - : main_expr = unionExpr (op += (STAR | KW_DIV | KW_IDIV | KW_MOD) rhs += unionExpr)* - ; - -unionExpr - : main_expr = intersectExceptExpr (op += (KW_UNION | VBAR) rhs += intersectExceptExpr)* - ; - -intersectExceptExpr - : main_expr = instanceOfExpr (op += (KW_INTERSECT | KW_EXCEPT) rhs += instanceOfExpr)* - ; - -instanceOfExpr - : main_expr = isStaticallyExpr (KW_INSTANCE KW_OF seq = sequenceType)? - ; - -isStaticallyExpr - : main_expr = treatExpr (KW_IS KW_STATICALLY seq = sequenceType)? - ; - -treatExpr - : main_expr = castableExpr (KW_TREAT KW_AS seq = sequenceType)? - ; - -castableExpr - : main_expr = castExpr (KW_CASTABLE KW_AS single = singleType)? - ; - -castExpr - : main_expr = arrowExpr (KW_CAST KW_AS single = singleType)? - ; - -arrowExpr - : main_expr = unaryExpr (ARROW function += arrowFunctionSpecifier arguments += argumentList)* - ; - -unaryExpr - : op += (MINUS | PLUS)* main_expr = valueExpr - ; - -valueExpr - : validate_expr = validateExpr - | extensionExpr - | simpleMap_expr = simpleMapExpr - ; -/* - * this token was added to prevent the antlr error - * "label assigned to a block which is not a set" in the comparisonExpr token definition - */ - - -compOp - : valueComp - | generalComp - | nodeComp - ; - -generalComp - : EQUAL - | NOT_EQUAL - | LANGLE - | (LANGLE EQUAL) - | RANGLE - | (RANGLE EQUAL) - ; - -valueComp - : KW_EQ - | KW_NE - | KW_LT - | KW_LE - | KW_GT - | KW_GE - ; - -nodeComp - : KW_IS - | (LANGLE LANGLE) - | (RANGLE RANGLE) - ; -/* - * replaced with the enclosedExpression production to match the JSONiq grammar - * TODO: this is out of spec. However, it is currently kept to match the JSONiq grammar - * TODO: replace with the proper rule, throw excep. - * validateExpr: KW_VALIDATE (validationMode | (KW_TYPE typeName))? LBRACE expr? RBRACE ; - */ - - -validateExpr - : KW_VALIDATE (validationMode | (KW_TYPE sequenceType))? LBRACE expr? RBRACE - ; - -validationMode - : KW_LAX - | KW_STRICT - ; - -extensionExpr - : PRAGMA+ LBRACE expr RBRACE - ; - -simpleMapExpr - : main_expr = pathExpr (BANG map_expr += pathExpr)* - ; - -arrayLookup - : LBRACKET LBRACKET expr RBRACKET RBRACKET - ; - -arrayUnboxing - : LBRACKET RBRACKET - ; - -objectLookup - : DOT (kw = keyword | lt = stringLiteral | nc = NCName | pe = parenthesizedExpr | vr = varRef | ci = contextItemExpr) - ; - // /////////////////////// XPath - - // PATHS /////////////////////////////////////////////////////////////////////// - -pathExpr - : (SLASH singleslash = relativePathExpr?) - | (DSLASH doubleslash = relativePathExpr) - | relative = relativePathExpr - ; - -relativePathExpr - : stepExpr (sep += (SLASH | DSLASH) stepExpr)* - ; - -stepExpr - : postfixExpr - | axisStep - ; - -axisStep - : (reverseStep | forwardStep) predicateList - ; - -forwardStep - : (forwardAxis nodeTest) - | abbrevForwardStep - ; - -forwardAxis - : (KW_CHILD | KW_DESCENDANT | KW_ATTRIBUTE | KW_SELF | KW_DESCENDANT_OR_SELF | KW_FOLLOWING_SIBLING | KW_FOLLOWING) COLON COLON - ; - -abbrevForwardStep - : AT? nodeTest - ; - -reverseStep - : (reverseAxis nodeTest) - | abbrevReverseStep - ; - -reverseAxis - : (KW_PARENT | KW_ANCESTOR | KW_PRECEDING_SIBLING | KW_PRECEDING | KW_ANCESTOR_OR_SELF) COLON COLON - ; - -abbrevReverseStep - : DDOT - ; - -nodeTest - : nameTest - | kindTest - ; - -nameTest - : eqName - | wildcard - ; - -wildcard - : STAR # allNames - | NCNameWithLocalWildcard # allWithNS // walkers must strip out the trailing :* - | NCNameWithPrefixWildcard # allWithLocal // walkers must strip out the leading *: - | (BracedURILiteral STAR) # BracedURILiteral - ; - -postfixExpr - : main_expr = primaryExpr (arrayLookup | predicate | objectLookup | arrayUnboxing | argumentList | lookup)* - ; - -argumentList - : LPAREN (args += argument (COMMA args += argument)*)? RPAREN - ; - -predicateList - : predicate* - ; - -predicate - : LBRACKET expr RBRACKET - ; - -lookup - : QUESTION keySpecifier - ; - // stringLiteral and varRef will be in XQuery 4.0 - -keySpecifier - : (nc = ncName | in = IntegerLiteral | pe = parenthesizedExpr | wc = STAR | lt = stringLiteral | vr = varRef) - ; - -arrowFunctionSpecifier - : eqName - | varRef - | parenthesizedExpr - ; - -primaryExpr - : literal - | KW_NULL - | KW_TRUE - | KW_FALSE - | varRef - | parenthesizedExpr - | contextItemExpr - | functionCall - | orderedExpr - | unorderedExpr - | nodeConstructor - | functionItemExpr - | objectConstructor - | arrayConstructor - | stringConstructor - | unaryLookup - | blockExpr - ; - -literal - : numericLiteral - | stringLiteral - ; - -numericLiteral - : IntegerLiteral - | DecimalLiteral - | DoubleLiteral - ; - -varRef - : DOLLAR var_name = eqName - ; - -/** - * Variable bindings and references have the same lexical form, but distinct parser contexts make - * their semantic roles explicit to parse-tree consumers. - */ varBinding - : DOLLAR var_name = eqName - ; - -parenthesizedExpr - : LPAREN expr? RPAREN - ; - -contextItemExpr - : DOUBLE_DOLLAR - ; - -orderedExpr - : KW_ORDERED enclosedExpression - ; - -unorderedExpr - : KW_UNORDERED enclosedExpression - ; - -functionCall - : fn_name = functionName argumentList - ; - -argument - : exprSingle - | QUESTION - ; - // CONSTRUCTORS //////////////////////////////////////////////////////////////// - -nodeConstructor - : directConstructor - | computedConstructor - ; - // Keep the shared start-tag prefix outside the open/close and self-closing alternatives. - -directConstructor - : LANGLE open_tag_name = qname attributes = dirAttributeList (open_close = dirElemConstructorOpenClose | single_tag = dirElemConstructorSingleTag) - | COMMENT - | PI - ; -/* - * [96]: we don't check that the closing tag is the same here. It should be - * done elsewhere, if we really want to know. We've also simplified the rule - * by removing the S? bits from ws:explicit. Tree walkers could handle this. - */ - - -dirElemConstructorOpenClose - : endOpen = RANGLE dirElemContent* startClose = LANGLE slashClose = SLASH close_tag_name = qname RANGLE - ; - -dirElemConstructorSingleTag - : slashClose = SLASH RANGLE - ; - // [97]: again, ws:explicit is better handled through the walker. - -dirAttributeList - : (attribute_qname += qname EQUAL attribute_value += dirAttributeValue)* - ; - -dirAttributeValueQuot - : Quot (PredefinedEntityRef | CharRef | escapedQuot | dirAttributeContentQuot)* Quot - ; - -dirAttributeValueApos - : Apos (PredefinedEntityRef | CharRef | escapedApos | dirAttributeContentApos)* Apos - ; - -dirAttributeValue - : dirAttributeValueQuot - | dirAttributeValueApos - ; - -dirAttributeContentQuot - : LBRACE LBRACE - | RBRACE RBRACE - | LBRACE expr? RBRACE - | ~ (Quot | LBRACE | RBRACE | Ampersand | PredefinedEntityRef | CharRef | LANGLE | COMMENT | XMLDECL | PI | CDATA) - ; - -dirAttributeContentApos - : LBRACE LBRACE - | RBRACE RBRACE - | LBRACE expr? RBRACE - | ~ (Apos | LBRACE | RBRACE | Ampersand | PredefinedEntityRef | CharRef | LANGLE | COMMENT | XMLDECL | PI | CDATA) - ; - -escapedQuot - : Quot Quot - ; - -escapedApos - : Apos Apos - ; - -dirElemContent - : directConstructor - | commonContent - | CDATA - // ~[{}<&] = '" + ~['"{}<&] - | Quot - | Apos - | noQuotesNoBracesNoAmpNoLAng - ; - -commonContent - : (PredefinedEntityRef | CharRef) - | LBRACE LBRACE - | RBRACE RBRACE - | (LBRACE expr? RBRACE) - ; - -computedConstructor - : compDocConstructor - | compElemConstructor - | compAttrConstructor - | compNamespaceConstructor - | compTextConstructor - | compCommentConstructor - | compPIConstructor - ; - -compDocConstructor - : KW_DOCUMENT enclosedExpression - ; - -compElemConstructor - : KW_ELEMENT (eqName | (LBRACE expr RBRACE)) enclosedContentExpr - ; - -enclosedContentExpr - : enclosedExpression - ; - -compAttrConstructor - : KW_ATTRIBUTE (name = eqName | (LBRACE name_expr = expr RBRACE)) enclosedExpression - ; - // replaced with the prefix production to allow the usage of the prefix label in the moduleImport rule - -compNamespaceConstructor - : KW_NAMESPACE (ncName | enclosedPrefixExpr) enclosedURIExpr - ; - -enclosedPrefixExpr - : enclosedExpression - ; - -enclosedURIExpr - : enclosedExpression - ; - -compTextConstructor - : KW_TEXT enclosedExpression - ; - -compCommentConstructor - : KW_COMMENT enclosedExpression - ; - -compPIConstructor - : KW_PI (ncName | (LBRACE expr RBRACE)) enclosedExpression - ; - -functionItemExpr - : namedFunctionRef - | inlineFunctionExpr - ; -/* - * constrains to valid function names only - * see https://www.w3.org/TR/xquery-31/#parse-note-reserved-function-names - */ - - -namedFunctionRef - : fn_name = functionName HASH arity = IntegerLiteral - ; -/* - * renamed from inlineFunctionRef to inlineFunctionExpr to match the JSONiq grammar - * replaced with the functionBody production to match the JSONiq grammar - */ - - -inlineFunctionExpr - : annotations KW_FUNCTION LPAREN paramList? RPAREN (KW_AS return_type = sequenceType)? (LBRACE (fn_body = statementsAndOptionalExpr) RBRACE) - ; - // renamed from mapConstructor to objectConstructor to match the JSONiq grammar - -objectConstructor - : KW_MAP? LBRACE (pairConstructor (COMMA pairConstructor)*)? RBRACE - | merge_operator += LBRACE_VBAR expr RBRACE_VBAR +module + // replaced with the versionDecl production to match the JSONiq grammar + : (KW_JSONIQ KW_VERSION vers = stringLiteral (KW_ENCODING encoding = stringLiteral)? SEMICOLON)? + // TODO: subsequent optional main modules are currently ignored + (libraryModule | main = mainModule) ; - // renamed from mapConstructorEntry to pairConstructor to match the JSONiq grammar - -pairConstructor - : lhs = exprSingle (COLON | COLON_EQ | QUESTION) rhs = exprSingle + +versionDecl + : KW_JSONIQ KW_VERSION version = stringLiteral (KW_ENCODING encoding = stringLiteral)? SEMICOLON ; -arrayConstructor - : squareArrayConstructor - | curlyArrayConstructor +annotatedDecl + : functionDecl + | varDecl + | typeDecl + | contextItemDecl + | optionDecl ; -squareArrayConstructor - : LBRACKET (exprSingle (COMMA exprSingle)*)? RBRACKET +typeDecl + : KW_DECLARE KW_TYPE type_name = qname KW_AS (schema = schemaLanguage)? type_definition = exprSingle SEMICOLON ; -curlyArrayConstructor - : KW_ARRAY enclosedExpression +schemaLanguage + : KW_JSOUND KW_COMPACT + | KW_JSOUND KW_VERBOSE + | KW_JSON KW_SCHEMA ; -stringConstructor - : ENTER_STRING stringConstructorContent EXIT_STRING +andExpr + : main_expr = notExpr (KW_AND rhs += notExpr)* ; -stringConstructorContent - : stringConstructorChars (stringConstructorInterpolation stringConstructorChars)* +notExpr + : op += KW_NOT? main_expr = comparisonExpr ; -charNoGrave - : BASIC_CHAR - | LBRACE - | RBRACKET +arrayLookup + : LBRACKET LBRACKET expr RBRACKET RBRACKET ; -charNoLBrace - : BASIC_CHAR - | GRAVE - | RBRACKET +arrayUnboxing + : LBRACKET RBRACKET ; -charNoRBrack - : BASIC_CHAR - | GRAVE - | LBRACE +objectLookup + : DOT (kw = keyword | lt = stringLiteral | nc = NCName | pe = parenthesizedExpr | vr = varRef | ci = contextItemExpr) ; -stringConstructorChars - : (BASIC_CHAR | charNoGrave charNoLBrace | charNoRBrack charNoGrave charNoGrave | charNoGrave | LBRACE)* +postfixExpr + : main_expr = primaryExpr (arrayLookup | predicate | objectLookup | arrayUnboxing | argumentList | lookup)* ; -stringConstructorInterpolation - : ENTER_INTERPOLATION expr EXIT_INTERPOLATION +primaryExpr + : literal + | KW_NULL + | KW_TRUE + | KW_FALSE + | varRef + | parenthesizedExpr + | contextItemExpr + | functionCall + | orderedExpr + | unorderedExpr + | nodeConstructor + | functionItemExpr + | objectConstructor + | arrayConstructor + | stringConstructor + | unaryLookup + | blockExpr ; -unaryLookup - : QUESTION keySpecifier +contextItemExpr + : DOUBLE_DOLLAR ; - // TYPES AND TYPE TESTS //////////////////////////////////////////////////////// - -/* - * TODO: this is out of spec. However, it is currently kept to match the JSONiq grammar - * singleType: item=simpleTypeName (question+=QUESTION)? ; - * TODO: change to simpletypeName, update expressions. - * but this is not required to pass all the xquery qt3-tests. - */ - - -singleType - : item = itemType (question += QUESTION)? + +objectConstructor + : KW_MAP? LBRACE (pairConstructor (COMMA pairConstructor)*)? RBRACE + | merge_operator += LBRACE_VBAR expr RBRACE_VBAR ; -typeDeclaration - : KW_AS sequenceType +pairConstructor + : lhs = exprSingle (COLON | COLON_EQ | QUESTION) rhs = exprSingle ; sequenceType @@ -934,223 +140,6 @@ itemType | parenthesizedItemTest ; -kindTest - : documentTest - | elementTest - | attributeTest - | schemaElementTest - | schemaAttributeTest - | piTest - | commentTest - | textTest - | namespaceNodeTest - | binaryNodeTest - | anyKindTest - ; - -anyKindTest - : KW_NODE LPAREN STAR? RPAREN - ; - -binaryNodeTest - : KW_BINARY LPAREN RPAREN - ; - -documentTest - : KW_DOCUMENT_NODE LPAREN (elementTest | schemaElementTest)? RPAREN - ; - -textTest - : KW_TEXT LPAREN RPAREN - ; - -commentTest - : KW_COMMENT LPAREN RPAREN - ; - -namespaceNodeTest - : KW_NAMESPACE_NODE LPAREN RPAREN - ; - -piTest - : KW_PI LPAREN (ncName | stringLiteral)? RPAREN - ; - -attributeTest - : KW_ATTRIBUTE LPAREN (attributeNameOrWildcard (COMMA type = typeName)?)? RPAREN - ; - -attributeNameOrWildcard - : attributeName - | STAR - ; - -schemaAttributeTest - : KW_SCHEMA_ATTR LPAREN attributeDeclaration RPAREN - ; - -elementTest - : KW_ELEMENT LPAREN (elementNameOrWildcard (COMMA type = typeName optional = QUESTION?)?)? RPAREN - ; - -elementNameOrWildcard - : elementName - | STAR - ; - -schemaElementTest - : KW_SCHEMA_ELEM LPAREN elementDeclaration RPAREN - ; - -elementDeclaration - : elementName - ; - -attributeName - : eqName - ; - -elementName - : eqName - ; - -simpleTypeName - : typeName - ; - -typeName - : eqName - ; - -functionTest - : annotation* (anyFunctionTest | typedFunctionTest) - ; - -anyFunctionTest - : KW_FUNCTION LPAREN STAR RPAREN - ; - -typedFunctionTest - : KW_FUNCTION LPAREN (st += sequenceType (COMMA st += sequenceType)*)? RPAREN KW_AS rt = sequenceType - ; - -mapTest - : anyMapTest - | typedMapTest - ; - -anyMapTest - : KW_MAP LPAREN STAR RPAREN - ; - -typedMapTest - : KW_MAP LPAREN eqName COMMA sequenceType RPAREN - ; - -arrayTest - : anyArrayTest - | typedArrayTest - ; - -anyArrayTest - : KW_ARRAY LPAREN STAR RPAREN - ; - -typedArrayTest - : KW_ARRAY LPAREN sequenceType RPAREN - ; - -parenthesizedItemTest - : LPAREN itemType RPAREN - ; - -attributeDeclaration - : attributeName - ; - // NAMES /////////////////////////////////////////////////////////////////////// - - // walkers need to split into prefix+localpart by the ':' - -eqName - : qname - | URIQualifiedName - ; -/* - * renamed from qName to qname to match the JSONiq grammar - * added support for keywords as namespace names - * the FullQName production catches the case where the namespace name is NOT a keyword - * whereas the (ns=ncName COLON)? local_name=ncName production catches the case where the (optional) namespace name is a keyword - */ - - -qname - : FullQName - | (ns = ncName COLON)? local_name = ncName - ; -/* - * matches the definition of NCName in the XQuery 3.1 spec - * this includes all the valid characters, including all the keywords - */ - - -ncName - : NCName - | keyword - ; -/* - * function names should be valid NCNames, but limited by the constraint of reserved-function-names - * as defined in the XQuery 3.1 spec - * see https://www.w3.org/TR/xquery-31/#parse-note-reserved-function-names - * replaced with the FullQName production. the FullQName production was removed to prevent ambiguities - */ - - -functionName - : FullQName - | NCName - | URIQualifiedName - | keywordOKForFunction - ; - -keyword - : keywordOKForFunction - | keywordNotOKForFunction - ; - -keywordNotOKForFunction - : KW_ATTRIBUTE - | KW_COMMENT - | KW_DOCUMENT_NODE - | KW_ELEMENT - | KW_EMPTY_SEQUENCE - | KW_IF - | KW_ITEM - | KW_CONTEXT - | KW_NODE - | KW_PI - | KW_SCHEMA_ATTR - | KW_SCHEMA_ELEM - | KW_BINARY - | KW_TEXT - | KW_TYPESWITCH - | KW_SWITCH - | KW_NAMESPACE_NODE - | KW_TYPE - | KW_TUMBLING - | KW_TRY - | KW_CATCH - | KW_ONLY - | KW_WHEN - | KW_SLIDING - | KW_DECIMAL_FORMAT - | KW_WINDOW - | KW_MAP - | KW_END - | KW_ALLOWING - | KW_ARRAY - | DFPropertyName - ; - keywordOKForFunction : KW_ANCESTOR | KW_ANCESTOR_OR_SELF @@ -1280,11 +269,6 @@ keywordOKForFunction | KW_NEXT | KW_PREVIOUS ; - // STRING LITERALS ///////////////////////////////////////////////////////////// - -uriLiteral - : stringLiteral - ; escapedJsoniqStringCharacter : BACKSLASH . @@ -1298,244 +282,3 @@ stringLiteralApos : Apos (escapedJsoniqStringCharacter | ~ (Apos | BACKSLASH))* Apos ; -stringLiteral - : stringLiteralQuot - | stringLiteralApos - ; - // ~['"{}<&]: a very common (and long!) subexpression in the W3C EBNF grammar // - -noQuotesNoBracesNoAmpNoLAng - : (keyword | (IntegerLiteral | DecimalLiteral | DoubleLiteral - //| stringLiteral - | PRAGMA | EQUAL | HASH | NOT_EQUAL | LPAREN | RPAREN | LBRACKET | RBRACKET | STAR | PLUS | MINUS | TILDE | COMMA | ARROW | MOD | DOT | GRAVE | DDOT | COLON | CARAT | COLON_EQ | SEMICOLON | SLASH | DSLASH | BACKSLASH | VBAR | RANGLE | QUESTION | AT | DOLLAR | BANG | FullQName | URIQualifiedName | NCNameWithLocalWildcard | NCNameWithPrefixWildcard | NCName | ContentChar))+ - ; -/* - * XQuery Scripting Extension ///////////////////////////////////////////////////////////// - * the following section contains rules for the XQuery Scripting Extension Proposal - */ - - - // New query body for main modules - -mainModule - : prolog program - ; - -program - : statementsAndOptionalExpr - ; - // Mixing Expressions and Statements - -statements - : statement* - ; - -statementsAndExpr - : statements expr - ; - -statementsAndOptionalExpr - : statements expr? - ; - // Statements - -statement - : applyStatement - | assignStatement - | blockStatement - | breakStatement - | continueStatement - | exitStatement - | flworStatement - | ifStatement - | switchStatement - | tryCatchStatement - | typeSwitchStatement - | varDeclStatement - | whileStatement - ; - -applyStatement - : exprSimple SEMICOLON - ; - -assignStatement - : var_ref = varRef COLON_EQ exprSingle SEMICOLON - ; - -blockStatement - : LBRACE statements RBRACE - ; - -breakStatement - : KW_BREAK KW_LOOP SEMICOLON - ; - -continueStatement - : KW_CONTINUE KW_LOOP SEMICOLON - ; - -exitStatement - : KW_EXIT KW_RETURNING exprSingle SEMICOLON - ; - // replaced with the initialClause production to match the JSONiq grammar - -flworStatement - : (start_for = forClause | start_let = letClause) - // replaced with the intermediateClause production to match the JSONiq grammar - (forClause | letClause | whereClause | groupByClause | orderByClause | countClause)* - // replaced with the returnStatement production to match the JSONiq grammar - KW_RETURN returnStmt = statement - ; - -ifStatement - : KW_IF LPAREN test_expr = expr RPAREN KW_THEN branch = statement KW_ELSE else_branch = statement - ; - -switchStatement - : KW_SWITCH LPAREN condExpr = expr RPAREN cases += switchCaseStatement+ KW_DEFAULT KW_RETURN def = statement - ; - // replaced with the switchCaseOperand production to match the JSONiq grammar - -switchCaseStatement - : (KW_CASE cond += exprSingle)+ KW_RETURN ret = statement - ; - -tryCatchStatement - : KW_TRY try_block = blockStatement catches += catchCaseStatement+ - ; -/* - * added to match the JSONiq grammar - * replaced the CatchErrorList production rule to match the JSONiq grammar - */ - - -catchCaseStatement - : KW_CATCH nameTest (VBAR nameTest)* catch_block = blockStatement - ; - // The optional variable is local to the default branch. - -typeSwitchStatement - : KW_TYPESWITCH LPAREN cond = expr RPAREN cases += caseStatement+ KW_DEFAULT (var_ref = varBinding)? (KW_RETURN) def = statement - ; - // The optional variable is local to this case branch. - -caseStatement - : KW_CASE (var_ref = varBinding KW_AS)? union += sequenceType (VBAR union += sequenceType)* (KW_RETURN) ret = statement - ; - -varDeclStatement - : annotations KW_VARIABLE varDeclForStatement (COMMA varDeclForStatement)* SEMICOLON - ; - // added to match the JSONiq grammar - -varDeclForStatement - : var_ref = varBinding (KW_AS sequenceType)? (COLON_EQ expr_vals += exprSingle)? - ; - -whileStatement - : KW_WHILE LPAREN test_expr = expr RPAREN stmt = statement - ; - // Expressions - - // redefined according to the XQuery Scripting Extension spec - -exprSingle - : exprSimple - | flworExpr - | ifExpr - | switchExpr - | tryCatchExpr - | typeswitchExpr - ; - -exprSimple - : quantifiedExpr - | orExpr - | insertExpr - | deleteExpr - | renameExpr - | replaceExpr - | transformExpr - | appendExpr - | createCollectionExpr - | truncateCollectionExpr - | deleteIndexExpr - | deleteSearchExpr - | editCollectionExpr - | insertIndexExpr - | insertSearchExpr - ; - -blockExpr - : LBRACE statementsAndExpr RBRACE - ; -/* - * Updating expressions (out-of-spec) - * these are not referenced anywhere in the XQuery spec or in the XQuery Scripting Extension spec - * they are ported from the original grammar, and likely to be derived from JSONiq - */ - - -insertExpr - : KW_INSERT KW_JSON to_insert_expr = exprSingle KW_INTO main_expr = exprSingle (KW_AT KW_POSITION pos_expr = exprSingle)? - | KW_INSERT KW_JSON pairConstructor (COMMA pairConstructor)* KW_INTO main_expr = exprSingle - ; - -deleteExpr - : KW_DELETE KW_JSON updateLocator - ; - -renameExpr - : KW_RENAME KW_JSON updateLocator KW_AS name_expr = exprSingle - ; - -replaceExpr - : KW_REPLACE KW_VALUE KW_OF KW_JSON updateLocator KW_WITH replacer_expr = exprSingle - ; - -transformExpr - : KW_COPY copyDecl (COMMA copyDecl)* KW_MODIFY mod_expr = exprSingle KW_RETURN ret_expr = exprSingle - ; - -appendExpr - : KW_APPEND KW_JSON to_append_expr = exprSingle KW_INTO array_expr = exprSingle - ; - -updateLocator - : main_expr = postfixExpr - ; - -copyDecl - : var_ref = varBinding COLON_EQ src_expr = exprSingle - ; - ///////////////////////// Top Level Updating Expressions - -createCollectionExpr - : KW_CREATE KW_COLLECTION collectionMode = (KW_TABLE | KW_DELTA_FILE | KW_ICEBERG_TABLE) LPAREN collection_name = exprSimple RPAREN (KW_WITH content = exprSingle)? - ; - -deleteIndexExpr - : KW_DELETE ((first = KW_FIRST | last = KW_LAST) num = exprSingle?) KW_FROM KW_COLLECTION collectionMode = (KW_TABLE | KW_DELTA_FILE | KW_ICEBERG_TABLE) LPAREN collection_name = exprSimple RPAREN - ; - -deleteSearchExpr - : KW_DELETE content = exprSingle KW_FROM KW_COLLECTION - ; - -insertIndexExpr - : KW_INSERT content = exprSingle ((KW_AT pos = exprSingle) | first = KW_FIRST | last = KW_LAST) KW_INTO KW_COLLECTION collectionMode = (KW_TABLE | KW_DELTA_FILE | KW_ICEBERG_TABLE) LPAREN collection_name = exprSimple RPAREN - ; - -insertSearchExpr - : KW_INSERT content = exprSingle (before = KW_BEFORE | after = KW_AFTER) target = exprSingle KW_INTO KW_COLLECTION - ; - -truncateCollectionExpr - : (KW_DELETE | KW_TRUNCATE) KW_COLLECTION collectionMode = (KW_TABLE | KW_DELTA_FILE | KW_ICEBERG_TABLE) LPAREN collection_name = exprSimple RPAREN - ; - -editCollectionExpr - : KW_EDIT target = exprSingle KW_INTO content = exprSingle KW_IN KW_COLLECTION - ; - diff --git a/src/main/java/org/rumbledb/parser/xquery/XQueryParser.g4 b/src/main/java/org/rumbledb/parser/xquery/XQueryParser.g4 index 00f2561795..7ab7ffe4a2 100644 --- a/src/main/java/org/rumbledb/parser/xquery/XQueryParser.g4 +++ b/src/main/java/org/rumbledb/parser/xquery/XQueryParser.g4 @@ -25,6 +25,7 @@ */ parser grammar XQueryParser; +import CommonParser; @ header { // Java header @@ -32,28 +33,6 @@ package org.rumbledb.parser.xquery; } options { tokenVocab = XQueryLexer; } -/* - * Mostly taken from http://www.w3.org/TR/xquery/#id-grammar, with some - * simplifications: - * - * 1. The parser itself doesn't really enforce ws:explicit except for some easy - * cases (QNames and wildcards). Walkers will need to do this (and also parse - * wildcards a bit). - * - * 2. When collecting element content, we will need to check the HIDDEN - * channel as well, for whitespace and XQuery comments (these should be - * treated as regular text inside elements). - */ - - -// MODULE HEADER /////////////////////////////////////////////////////////////// - -// this rule was added to match the JSONiq grammar - -moduleAndThisIsIt - : module EOF - ; - module : // replaced with the versionDecl production to match the JSONiq grammar (KW_XQUERY KW_VERSION vers = stringLiteral (KW_ENCODING encoding = stringLiteral)? SEMICOLON)? @@ -64,828 +43,51 @@ module versionDecl : KW_XQUERY KW_VERSION version = stringLiteral (KW_ENCODING encoding = stringLiteral)? SEMICOLON ; - // mainModule and queryBody are replaced with the mainModule and program rules according to the XQuery Scripting Extension spec - -libraryModule - : KW_MODULE KW_NAMESPACE ncName EQUAL uri = uriLiteral SEMICOLON prolog - ; - // MODULE PROLOG /////////////////////////////////////////////////////////////// - -prolog - : (defaultNamespaceDecl | setter | namespaceDecl | schemaImport | moduleImport)* (annotatedDecl)* - ; - // added to match the JSONiq grammar - -annotatedDecl - : functionDecl - | varDecl - | contextItemDecl - | optionDecl - ; - -defaultNamespaceDecl - : KW_DECLARE KW_DEFAULT type = (KW_ELEMENT | KW_FUNCTION) KW_NAMESPACE uri = stringLiteral SEMICOLON - ; - -setter - : boundarySpaceDecl - | defaultCollationDecl - | baseURIDecl - | constructionDecl - | orderingModeDecl - | emptyOrderDecl - | copyNamespacesDecl - | decimalFormatDecl - ; - -boundarySpaceDecl - : KW_DECLARE KW_BOUNDARY_SPACE type = (KW_PRESERVE | KW_STRIP) SEMICOLON - ; - -defaultCollationDecl - : KW_DECLARE KW_DEFAULT KW_COLLATION uriLiteral SEMICOLON - ; - -baseURIDecl - : KW_DECLARE KW_BASE_URI uriLiteral SEMICOLON - ; - -constructionDecl - : KW_DECLARE KW_CONSTRUCTION type = (KW_STRIP | KW_PRESERVE) SEMICOLON - ; - -orderingModeDecl - : KW_DECLARE KW_ORDERING type = (KW_ORDERED | KW_UNORDERED) SEMICOLON - ; - -emptyOrderDecl - : KW_DECLARE KW_DEFAULT KW_ORDER KW_EMPTY emptySequenceOrder = (KW_GREATEST | KW_LEAST) SEMICOLON - ; - -copyNamespacesDecl - : KW_DECLARE KW_COPY_NS preserveMode COMMA inheritMode SEMICOLON - ; - -preserveMode - : KW_PRESERVE - | KW_NO_PRESERVE - ; - -inheritMode - : KW_INHERIT - | KW_NO_INHERIT - ; - -decimalFormatDecl - : KW_DECLARE ((KW_DECIMAL_FORMAT eqName) | (KW_DEFAULT KW_DECIMAL_FORMAT)) (DFPropertyName EQUAL stringLiteral)* SEMICOLON - ; - -schemaImport - : KW_IMPORT KW_SCHEMA schemaPrefix? nsURI = uriLiteral (KW_AT locations += uriLiteral (COMMA locations += uriLiteral)*)? SEMICOLON - ; - -schemaPrefix - : (KW_NAMESPACE ncName EQUAL | KW_DEFAULT KW_ELEMENT KW_NAMESPACE) - ; - -moduleImport - : KW_IMPORT KW_MODULE (KW_NAMESPACE prefix = ncName EQUAL)? targetNamespace = uriLiteral (KW_AT locations += uriLiteral (COMMA locations += uriLiteral)*)? SEMICOLON - ; - -namespaceDecl - : KW_DECLARE KW_NAMESPACE ncName EQUAL uriLiteral SEMICOLON - ; - -varDecl - : KW_DECLARE (annotations | ncName) KW_VARIABLE varBinding - // replaced with the typeDeclaration production to match the JSONiq grammar - (KW_AS sequenceType)? ( - // replaced with the varValue production to match the JSONiq grammar - (COLON_EQ exprSingle) - // replaced with the varDefaultValue production to match the JSONiq grammar - | (external = KW_EXTERNAL (COLON_EQ exprSingle)?)) SEMICOLON - ; - -contextItemDecl - : KW_DECLARE KW_CONTEXT KW_ITEM -/* - * (KW_AS itemType)? - * TODO: this is out of spec. However, it is currently kept to match the JSONiq grammar - */ - - (KW_AS sequenceType)? // TODO: change to itemType, update expressions to use itemType, update back JSONiq grammar - ((COLON_EQ value = exprSingle) | (external = KW_EXTERNAL (COLON_EQ defaultValue = exprSingle)?)) SEMICOLON - ; -/* - * constrains to valid function names only - * see https://www.w3.org/TR/xquery-31/#parse-note-reserved-function-names - */ - - -functionDecl - : KW_DECLARE (annotations) KW_FUNCTION fn_name = functionName LPAREN paramList? RPAREN - // replaced with the functionReturn production to match the JSONiq grammar - (KW_AS return_type = sequenceType)? - // replaced functionBody to match the JSONiq grammar and the XQuery Scripting Extension spec - (LBRACE (fn_body = statementsAndOptionalExpr) RBRACE | is_external = KW_EXTERNAL) SEMICOLON - ; - // renamed from functionParams to paramList to match the JSONiq grammar - -paramList - : param (COMMA param)* - ; -/* - * renamed from functionParam to param to match the JSONiq grammar - * replaced with the typeDeclaration production to match the JSONiq grammar - */ - - -param - : name = varBinding (KW_AS sequenceType)? - ; - -annotations - : annotation* - ; - // added the updating keyword to support the out-of-spec updating expressions extension - -annotation - : MOD name = eqName (LPAREN literal (COMMA literal)* RPAREN)? - | updating = KW_UPDATING - ; - -optionDecl - : KW_DECLARE KW_OPTION name = eqName value = stringLiteral SEMICOLON - ; - // EXPRESSIONS ///////////////////////////////////////////////////////////////// - -expr - : exprSingle (COMMA exprSingle)* - ; - -flworExpr - : // replaced with the initialClause production to match the JSONiq grammar - (start_for = forClause | start_let = letClause | start_window = windowClause) - // replaced with the intermediateClause production to match the JSONiq grammar - (forClause | letClause | windowClause | whereClause | groupByClause | orderByClause | countClause)* - // replaced with the returnClause production to match the JSONiq grammar - KW_RETURN return_expr = exprSingle - ; - -forClause - : KW_FOR vars += forVar (COMMA vars += forVar)* - ; - // renamed from forBinding to forVar to match the JSONiq grammar - -forVar - : var_ref = varBinding - // replaced with the typeDeclaration production to match the JSONiq grammar - (KW_AS seq = sequenceType)? (flag = allowingEmpty)? - // replaced with the positionalVar production to match the JSONiq grammar - (KW_AT at = varBinding)? KW_IN ex = exprSingle - ; - -allowingEmpty - : KW_ALLOWING KW_EMPTY - ; - -positionalVar - : KW_AT pvar = varBinding - ; - -letClause - : KW_LET vars += letVar (COMMA vars += letVar)* - ; - // renamed from letBinding to letVar to match the JSONiq grammar - -letVar - : var_ref = varBinding - // replaced with the typeDeclaration production to match the JSONiq grammar - (KW_AS seq = sequenceType)? COLON_EQ ex = exprSingle - ; - -windowClause - : KW_FOR (tumblingWindowClause | slidingWindowClause) - ; - -tumblingWindowClause - : KW_TUMBLING KW_WINDOW name = varBinding type = typeDeclaration? KW_IN exprSingle windowStartCondition windowEndCondition? - ; - -slidingWindowClause - : KW_SLIDING KW_WINDOW name = varBinding type = typeDeclaration? KW_IN exprSingle windowStartCondition windowEndCondition - ; - -windowStartCondition - : KW_START windowVars KW_WHEN exprSingle - ; - -windowEndCondition - : KW_ONLY? KW_END windowVars KW_WHEN exprSingle - ; - -windowVars - : (currentItem = varBinding)? positionalVar? (KW_PREVIOUS previousItem = varBinding)? (KW_NEXT nextItem = varBinding)? - ; - -countClause - : KW_COUNT varBinding - ; - -whereClause - : KW_WHERE exprSingle - ; - // replaced with the groupingSpecList production to match the JSONiq grammar - -groupByClause - : KW_GROUP KW_BY vars += groupByVar (COMMA vars += groupByVar)* - ; - // renamed from groupingSpec to groupByVar to match the JSONiq grammar - -groupByVar - : var_ref = varBinding - // replaced with the typeDeclaration production to match the JSONiq grammar - ((KW_AS seq = sequenceType)? decl = COLON_EQ ex = exprSingle)? (KW_COLLATION uri = uriLiteral)? - ; - -orderByClause - : stb = KW_STABLE? KW_ORDER KW_BY specs += orderByExpr (COMMA specs += orderByExpr)* - ; - // renamed from orderSpec to orderByExpr to match the JSONiq grammar - -orderByExpr - : ex = exprSingle (KW_ASCENDING | desc = KW_DESCENDING)? (KW_EMPTY (gr = KW_GREATEST | ls = KW_LEAST))? (KW_COLLATION uril = uriLiteral)? - ; - -quantifiedExpr - : (so = KW_SOME | ev = KW_EVERY) vars += quantifiedExprVar (COMMA vars += quantifiedExprVar)* KW_SATISFIES exprSingle - ; - // renamed from quantifiedVar to quantifiedExprVar to match the JSONiq grammar - -quantifiedExprVar - : var_ref = varBinding - // replaced with the typeDeclaration production to match the JSONiq grammar - (KW_AS seq = sequenceType)? KW_IN exprSingle - ; - -switchExpr - : KW_SWITCH LPAREN cond = expr RPAREN cases += switchCaseClause+ KW_DEFAULT KW_RETURN def = exprSingle - ; - -switchCaseClause - : (KW_CASE cond += exprSingle)+ KW_RETURN ret = exprSingle - ; - -typeswitchExpr - : KW_TYPESWITCH LPAREN cond = expr RPAREN cses += caseClause+ KW_DEFAULT (var_ref = varBinding)? KW_RETURN def = exprSingle - ; - -caseClause - : KW_CASE (var_ref = varBinding KW_AS)? union += sequenceType (VBAR union += sequenceType)* KW_RETURN ret = exprSingle - ; - -ifExpr - : KW_IF LPAREN test_condition = expr RPAREN KW_THEN branch = exprSingle KW_ELSE else_branch = exprSingle - ; - // replaced with the tryClause and the enclosedTryTargetExpression productions to match the JSONiq grammar - -tryCatchExpr - : KW_TRY LBRACE try_expression = expr? RBRACE catches += catchClause+ - ; - -catchClause - : KW_CATCH nameTest (VBAR nameTest)* - // replaced with the enclosedExpression production to match the JSONiq grammar - LBRACE catch_expression = expr? RBRACE - ; - -enclosedExpression - : LBRACE expr? RBRACE - ; - -orExpr - : main_expr = andExpr (KW_OR rhs += andExpr)* - ; - -andExpr - : main_expr = comparisonExpr (KW_AND rhs += comparisonExpr)* - ; - -comparisonExpr - : main_expr = stringConcatExpr (op += compOp rhs += stringConcatExpr)? - ; - -stringConcatExpr - : main_expr = rangeExpr (CONCATENATION rhs += rangeExpr)* - ; - -rangeExpr - : main_expr = additiveExpr (KW_TO rhs += additiveExpr)? - ; - -additiveExpr - : main_expr = multiplicativeExpr (op += (PLUS | MINUS) rhs += multiplicativeExpr)* - ; - -multiplicativeExpr - : main_expr = unionExpr (op += (STAR | KW_DIV | KW_IDIV | KW_MOD) rhs += unionExpr)* - ; - -unionExpr - : main_expr = intersectExceptExpr (op += (KW_UNION | VBAR) rhs += intersectExceptExpr)* - ; - -intersectExceptExpr - : main_expr = instanceOfExpr (op += (KW_INTERSECT | KW_EXCEPT) rhs += instanceOfExpr)* - ; - -instanceOfExpr - : main_expr = isStaticallyExpr (KW_INSTANCE KW_OF seq = sequenceType)? - ; - -isStaticallyExpr - : main_expr = treatExpr (KW_IS KW_STATICALLY seq = sequenceType)? - ; - -treatExpr - : main_expr = castableExpr (KW_TREAT KW_AS seq = sequenceType)? - ; - -castableExpr - : main_expr = castExpr (KW_CASTABLE KW_AS single = singleType)? - ; - -castExpr - : main_expr = arrowExpr (KW_CAST KW_AS single = singleType)? - ; - -arrowExpr - : main_expr = unaryExpr (ARROW function += arrowFunctionSpecifier arguments += argumentList)* - ; - -unaryExpr - : op += (MINUS | PLUS)* main_expr = valueExpr - ; - -valueExpr - : validate_expr = validateExpr - | extensionExpr - | simpleMap_expr = simpleMapExpr - ; -/* - * this token was added to prevent the antlr error - * "label assigned to a block which is not a set" in the comparisonExpr token definition - */ - - -compOp - : valueComp - | generalComp - | nodeComp - ; - -generalComp - : EQUAL - | NOT_EQUAL - | LANGLE - | (LANGLE EQUAL) - | RANGLE - | (RANGLE EQUAL) - ; - -valueComp - : KW_EQ - | KW_NE - | KW_LT - | KW_LE - | KW_GT - | KW_GE - ; - -nodeComp - : KW_IS - | (LANGLE LANGLE) - | (RANGLE RANGLE) - ; -/* - * replaced with the enclosedExpression production to match the JSONiq grammar - * TODO: this is out of spec. However, it is currently kept to match the JSONiq grammar - * TODO: replace with the proper rule, throw excep. - * validateExpr: KW_VALIDATE (validationMode | (KW_TYPE typeName))? LBRACE expr? RBRACE ; - */ - - -validateExpr - : KW_VALIDATE (validationMode | (KW_TYPE sequenceType))? LBRACE expr? RBRACE - ; - -validationMode - : KW_LAX - | KW_STRICT - ; - -extensionExpr - : PRAGMA+ LBRACE expr RBRACE - ; - -simpleMapExpr - : main_expr = pathExpr (BANG map_expr += pathExpr)* - ; - // PATHS /////////////////////////////////////////////////////////////////////// - -pathExpr - : (SLASH singleslash = relativePathExpr?) - | (DSLASH doubleslash = relativePathExpr) - | relative = relativePathExpr - ; - -relativePathExpr - : stepExpr (sep += (SLASH | DSLASH) stepExpr)* - ; - -stepExpr - : postfixExpr - | axisStep - ; - -axisStep - : (reverseStep | forwardStep) predicateList - ; - -forwardStep - : (forwardAxis nodeTest) - | abbrevForwardStep - ; - -forwardAxis - : (KW_CHILD | KW_DESCENDANT | KW_ATTRIBUTE | KW_SELF | KW_DESCENDANT_OR_SELF | KW_FOLLOWING_SIBLING | KW_FOLLOWING) COLON COLON - ; - -abbrevForwardStep - : AT? nodeTest - ; - -reverseStep - : (reverseAxis nodeTest) - | abbrevReverseStep - ; - -reverseAxis - : (KW_PARENT | KW_ANCESTOR | KW_PRECEDING_SIBLING | KW_PRECEDING | KW_ANCESTOR_OR_SELF) COLON COLON - ; - -abbrevReverseStep - : DDOT - ; - -nodeTest - : nameTest - | kindTest - ; - -nameTest - : eqName - | wildcard - ; - -wildcard - : STAR # allNames - | NCNameWithLocalWildcard # allWithNS // walkers must strip out the trailing :* - | NCNameWithPrefixWildcard # allWithLocal // walkers must strip out the leading *: - | (BracedURILiteral STAR) # BracedURILiteral - ; - -postfixExpr - : main_expr = primaryExpr (predicate | argumentList | lookup)* - ; - -argumentList - : LPAREN (args += argument (COMMA args += argument)*)? RPAREN - ; - -predicateList - : predicate* - ; - -predicate - : LBRACKET expr RBRACKET - ; - -lookup - : QUESTION keySpecifier - ; - // stringLiteral and varRef will be in XQuery 4.0 - -keySpecifier - : (nc = ncName | in = IntegerLiteral | pe = parenthesizedExpr | wc = STAR | lt = stringLiteral | vr = varRef) - ; - -arrowFunctionSpecifier - : eqName - | varRef - | parenthesizedExpr - ; - -primaryExpr - : literal - | varRef - | parenthesizedExpr - | contextItemExpr - | functionCall - | orderedExpr - | unorderedExpr - | nodeConstructor - | functionItemExpr - | objectConstructor - | arrayConstructor - | stringConstructor - | unaryLookup - | blockExpr - ; - -literal - : numericLiteral - | stringLiteral - ; - -numericLiteral - : IntegerLiteral - | DecimalLiteral - | DoubleLiteral - ; - -varRef - : DOLLAR var_name = eqName - ; - -/** - * Variable bindings and references have the same lexical form, but distinct parser contexts make - * their semantic roles explicit to parse-tree consumers. - */ varBinding - : DOLLAR var_name = eqName - ; - -parenthesizedExpr - : LPAREN expr? RPAREN - ; - -contextItemExpr - : DOT - ; - -orderedExpr - : KW_ORDERED enclosedExpression - ; - -unorderedExpr - : KW_UNORDERED enclosedExpression - ; - -functionCall - : fn_name = functionName argumentList - ; - -argument - : exprSingle - | QUESTION - ; - // CONSTRUCTORS //////////////////////////////////////////////////////////////// - -nodeConstructor - : directConstructor - | computedConstructor - ; - // Keep the shared start-tag prefix outside the open/close and self-closing alternatives. - -directConstructor - : LANGLE open_tag_name = qname attributes = dirAttributeList (open_close = dirElemConstructorOpenClose | single_tag = dirElemConstructorSingleTag) - | COMMENT - | PI - ; -/* - * [96]: we don't check that the closing tag is the same here. It should be - * done elsewhere, if we really want to know. We've also simplified the rule - * by removing the S? bits from ws:explicit. Tree walkers could handle this. - */ - - -dirElemConstructorOpenClose - : endOpen = RANGLE dirElemContent* startClose = LANGLE slashClose = SLASH close_tag_name = qname RANGLE - ; - -dirElemConstructorSingleTag - : slashClose = SLASH RANGLE - ; - // [97]: again, ws:explicit is better handled through the walker. - -dirAttributeList - : (attribute_qname += qname EQUAL attribute_value += dirAttributeValue)* - ; - -dirAttributeValueQuot - : Quot (PredefinedEntityRef | CharRef | escapedQuot | dirAttributeContentQuot)* Quot - ; - -dirAttributeValueApos - : Apos (PredefinedEntityRef | CharRef | escapedApos | dirAttributeContentApos)* Apos - ; - -dirAttributeValue - : dirAttributeValueQuot - | dirAttributeValueApos - ; - -dirAttributeContentQuot - : LBRACE LBRACE - | RBRACE RBRACE - | LBRACE expr? RBRACE - | ~ (Quot | LBRACE | RBRACE | Ampersand | PredefinedEntityRef | CharRef | LANGLE | COMMENT | XMLDECL | PI | CDATA) - ; - -dirAttributeContentApos - : LBRACE LBRACE - | RBRACE RBRACE - | LBRACE expr? RBRACE - | ~ (Apos | LBRACE | RBRACE | Ampersand | PredefinedEntityRef | CharRef | LANGLE | COMMENT | XMLDECL | PI | CDATA) - ; - -escapedQuot - : Quot Quot - ; - -escapedApos - : Apos Apos - ; - -dirElemContent - : directConstructor - | commonContent - | CDATA - // ~[{}<&] = '" + ~['"{}<&] - | Quot - | Apos - | noQuotesNoBracesNoAmpNoLAng - ; - -commonContent - : (PredefinedEntityRef | CharRef) - | LBRACE LBRACE - | RBRACE RBRACE - | (LBRACE expr? RBRACE) - ; - -computedConstructor - : compDocConstructor - | compElemConstructor - | compAttrConstructor - | compNamespaceConstructor - | compTextConstructor - | compCommentConstructor - | compPIConstructor - ; - -compDocConstructor - : KW_DOCUMENT enclosedExpression - ; - -compElemConstructor - : KW_ELEMENT (eqName | (LBRACE expr RBRACE)) enclosedContentExpr - ; - -enclosedContentExpr - : enclosedExpression - ; - -compAttrConstructor - : KW_ATTRIBUTE (name = eqName | (LBRACE name_expr = expr RBRACE)) enclosedExpression - ; - // replaced with the prefix production to allow the usage of the prefix label in the moduleImport rule - -compNamespaceConstructor - : KW_NAMESPACE (ncName | enclosedPrefixExpr) enclosedURIExpr - ; -enclosedPrefixExpr - : enclosedExpression +annotatedDecl + : functionDecl + | varDecl + | contextItemDecl + | optionDecl ; -enclosedURIExpr - : enclosedExpression +andExpr + : main_expr = comparisonExpr (KW_AND rhs += comparisonExpr)* ; -compTextConstructor - : KW_TEXT enclosedExpression +postfixExpr + : main_expr = primaryExpr (predicate | argumentList | lookup)* ; -compCommentConstructor - : KW_COMMENT enclosedExpression +primaryExpr + : literal + | varRef + | parenthesizedExpr + | contextItemExpr + | functionCall + | orderedExpr + | unorderedExpr + | nodeConstructor + | functionItemExpr + | objectConstructor + | arrayConstructor + | stringConstructor + | unaryLookup + | blockExpr ; -compPIConstructor - : KW_PI (ncName | (LBRACE expr RBRACE)) enclosedExpression +contextItemExpr + : DOT ; -functionItemExpr - : namedFunctionRef - | inlineFunctionExpr - ; -/* - * constrains to valid function names only - * see https://www.w3.org/TR/xquery-31/#parse-note-reserved-function-names - */ - - -namedFunctionRef - : fn_name = functionName HASH arity = IntegerLiteral - ; -/* - * renamed from inlineFunctionRef to inlineFunctionExpr to match the JSONiq grammar - * replaced with the functionBody production to match the JSONiq grammar - */ - - -inlineFunctionExpr - : annotations KW_FUNCTION LPAREN paramList? RPAREN (KW_AS return_type = sequenceType)? (LBRACE (fn_body = statementsAndOptionalExpr) RBRACE) - ; - // renamed from mapConstructor to objectConstructor to match the JSONiq grammar - objectConstructor : KW_MAP LBRACE (pairConstructor (COMMA pairConstructor)*)? RBRACE ; - // renamed from mapConstructorEntry to pairConstructor to match the JSONiq grammar - + pairConstructor : lhs = exprSingle (COLON | COLON_EQ) rhs = exprSingle ; -arrayConstructor - : squareArrayConstructor - | curlyArrayConstructor - ; - -squareArrayConstructor - : LBRACKET (exprSingle (COMMA exprSingle)*)? RBRACKET - ; - -curlyArrayConstructor - : KW_ARRAY enclosedExpression - ; - -stringConstructor - : ENTER_STRING stringConstructorContent EXIT_STRING - ; - -stringConstructorContent - : stringConstructorChars (stringConstructorInterpolation stringConstructorChars)* - ; - -charNoGrave - : BASIC_CHAR - | LBRACE - | RBRACKET - ; - -charNoLBrace - : BASIC_CHAR - | GRAVE - | RBRACKET - ; - -charNoRBrack - : BASIC_CHAR - | GRAVE - | LBRACE - ; - -stringConstructorChars - : (BASIC_CHAR | charNoGrave charNoLBrace | charNoRBrack charNoGrave charNoGrave | charNoGrave | LBRACE)* - ; - -stringConstructorInterpolation - : ENTER_INTERPOLATION expr EXIT_INTERPOLATION - ; - -unaryLookup - : QUESTION keySpecifier - ; - // TYPES AND TYPE TESTS //////////////////////////////////////////////////////// - -/* - * TODO: this is out of spec. However, it is currently kept to match the JSONiq grammar - * singleType: item=simpleTypeName (question+=QUESTION)? ; - * TODO: change to simpletypeName, update expressions. - * but this is not required to pass all the xquery qt3-tests. - */ - - -singleType - : item = itemType (question += QUESTION)? - ; - -typeDeclaration - : KW_AS sequenceType - ; - sequenceType : (KW_EMPTY_SEQUENCE LPAREN RPAREN) | (item = itemType (question += QUESTION | star += STAR | plus += PLUS)?) @@ -906,223 +108,6 @@ itemType | parenthesizedItemTest ; -kindTest - : documentTest - | elementTest - | attributeTest - | schemaElementTest - | schemaAttributeTest - | piTest - | commentTest - | textTest - | namespaceNodeTest - | binaryNodeTest - | anyKindTest - ; - -anyKindTest - : KW_NODE LPAREN STAR? RPAREN - ; - -binaryNodeTest - : KW_BINARY LPAREN RPAREN - ; - -documentTest - : KW_DOCUMENT_NODE LPAREN (elementTest | schemaElementTest)? RPAREN - ; - -textTest - : KW_TEXT LPAREN RPAREN - ; - -commentTest - : KW_COMMENT LPAREN RPAREN - ; - -namespaceNodeTest - : KW_NAMESPACE_NODE LPAREN RPAREN - ; - -piTest - : KW_PI LPAREN (ncName | stringLiteral)? RPAREN - ; - -attributeTest - : KW_ATTRIBUTE LPAREN (attributeNameOrWildcard (COMMA type = typeName)?)? RPAREN - ; - -attributeNameOrWildcard - : attributeName - | STAR - ; - -schemaAttributeTest - : KW_SCHEMA_ATTR LPAREN attributeDeclaration RPAREN - ; - -elementTest - : KW_ELEMENT LPAREN (elementNameOrWildcard (COMMA type = typeName optional = QUESTION?)?)? RPAREN - ; - -elementNameOrWildcard - : elementName - | STAR - ; - -schemaElementTest - : KW_SCHEMA_ELEM LPAREN elementDeclaration RPAREN - ; - -elementDeclaration - : elementName - ; - -attributeName - : eqName - ; - -elementName - : eqName - ; - -simpleTypeName - : typeName - ; - -typeName - : eqName - ; - -functionTest - : annotation* (anyFunctionTest | typedFunctionTest) - ; - -anyFunctionTest - : KW_FUNCTION LPAREN STAR RPAREN - ; - -typedFunctionTest - : KW_FUNCTION LPAREN (st += sequenceType (COMMA st += sequenceType)*)? RPAREN KW_AS rt = sequenceType - ; - -mapTest - : anyMapTest - | typedMapTest - ; - -anyMapTest - : KW_MAP LPAREN STAR RPAREN - ; - -typedMapTest - : KW_MAP LPAREN eqName COMMA sequenceType RPAREN - ; - -arrayTest - : anyArrayTest - | typedArrayTest - ; - -anyArrayTest - : KW_ARRAY LPAREN STAR RPAREN - ; - -typedArrayTest - : KW_ARRAY LPAREN sequenceType RPAREN - ; - -parenthesizedItemTest - : LPAREN itemType RPAREN - ; - -attributeDeclaration - : attributeName - ; - // NAMES /////////////////////////////////////////////////////////////////////// - - // walkers need to split into prefix+localpart by the ':' - -eqName - : qname - | URIQualifiedName - ; -/* - * renamed from qName to qname to match the JSONiq grammar - * added support for keywords as namespace names - * the FullQName production catches the case where the namespace name is NOT a keyword - * whereas the (ns=ncName COLON)? local_name=ncName production catches the case where the (optional) namespace name is a keyword - */ - - -qname - : FullQName - | (ns = ncName COLON)? local_name = ncName - ; -/* - * matches the definition of NCName in the XQuery 3.1 spec - * this includes all the valid characters, including all the keywords - */ - - -ncName - : NCName - | keyword - ; -/* - * function names should be valid NCNames, but limited by the constraint of reserved-function-names - * as defined in the XQuery 3.1 spec - * see https://www.w3.org/TR/xquery-31/#parse-note-reserved-function-names - * replaced with the FullQName production. the FullQName production was removed to prevent ambiguities - */ - - -functionName - : FullQName - | NCName - | URIQualifiedName - | keywordOKForFunction - ; - -keyword - : keywordOKForFunction - | keywordNotOKForFunction - ; - -keywordNotOKForFunction - : KW_ATTRIBUTE - | KW_COMMENT - | KW_DOCUMENT_NODE - | KW_ELEMENT - | KW_EMPTY_SEQUENCE - | KW_IF - | KW_ITEM - | KW_CONTEXT - | KW_NODE - | KW_PI - | KW_SCHEMA_ATTR - | KW_SCHEMA_ELEM - | KW_BINARY - | KW_TEXT - | KW_TYPESWITCH - | KW_SWITCH - | KW_NAMESPACE_NODE - | KW_TYPE - | KW_TUMBLING - | KW_TRY - | KW_CATCH - | KW_ONLY - | KW_WHEN - | KW_SLIDING - | KW_DECIMAL_FORMAT - | KW_WINDOW - | KW_MAP - | KW_END - | KW_ALLOWING - | KW_ARRAY - | DFPropertyName - ; - keywordOKForFunction : KW_ANCESTOR | KW_ANCESTOR_OR_SELF @@ -1248,18 +233,7 @@ keywordOKForFunction | KW_NEXT | KW_PREVIOUS ; - // STRING LITERALS ///////////////////////////////////////////////////////////// - -uriLiteral - : stringLiteral - ; -/* - * The matching delimiter terminates the string, a doubled delimiter represents - * one literal delimiter, and a bare ampersand is not allowed. The negated sets - * below operate on lexer token types, not individual characters. - */ - - + stringLiteralQuot : Quot (escapedQuot | ~ (Quot | Ampersand))* Quot ; @@ -1268,244 +242,3 @@ stringLiteralApos : Apos (escapedApos | ~ (Apos | Ampersand))* Apos ; -stringLiteral - : stringLiteralQuot - | stringLiteralApos - ; - // ~['"{}<&]: a very common (and long!) subexpression in the W3C EBNF grammar // - -noQuotesNoBracesNoAmpNoLAng - : (keyword | (IntegerLiteral | DecimalLiteral | DoubleLiteral - //| stringLiteral - | PRAGMA | EQUAL | HASH | NOT_EQUAL | LPAREN | RPAREN | LBRACKET | RBRACKET | STAR | PLUS | MINUS | TILDE | COMMA | ARROW | MOD | DOT | GRAVE | DDOT | COLON | CARAT | COLON_EQ | SEMICOLON | SLASH | DSLASH | BACKSLASH | VBAR | RANGLE | QUESTION | AT | DOLLAR | BANG | FullQName | URIQualifiedName | NCNameWithLocalWildcard | NCNameWithPrefixWildcard | NCName | ContentChar))+ - ; -/* - * XQuery Scripting Extension ///////////////////////////////////////////////////////////// - * the following section contains rules for the XQuery Scripting Extension Proposal - */ - - - // New query body for main modules - -mainModule - : prolog program - ; - -program - : statementsAndOptionalExpr - ; - // Mixing Expressions and Statements - -statements - : statement* - ; - -statementsAndExpr - : statements expr - ; - -statementsAndOptionalExpr - : statements expr? - ; - // Statements - -statement - : applyStatement - | assignStatement - | blockStatement - | breakStatement - | continueStatement - | exitStatement - | flworStatement - | ifStatement - | switchStatement - | tryCatchStatement - | typeSwitchStatement - | varDeclStatement - | whileStatement - ; - -applyStatement - : exprSimple SEMICOLON - ; - -assignStatement - : var_ref = varRef COLON_EQ exprSingle SEMICOLON - ; - -blockStatement - : LBRACE statements RBRACE - ; - -breakStatement - : KW_BREAK KW_LOOP SEMICOLON - ; - -continueStatement - : KW_CONTINUE KW_LOOP SEMICOLON - ; - -exitStatement - : KW_EXIT KW_RETURNING exprSingle SEMICOLON - ; - // replaced with the initialClause production to match the JSONiq grammar - -flworStatement - : (start_for = forClause | start_let = letClause) - // replaced with the intermediateClause production to match the JSONiq grammar - (forClause | letClause | whereClause | groupByClause | orderByClause | countClause)* - // replaced with the returnStatement production to match the JSONiq grammar - KW_RETURN returnStmt = statement - ; - -ifStatement - : KW_IF LPAREN test_expr = expr RPAREN KW_THEN branch = statement KW_ELSE else_branch = statement - ; - -switchStatement - : KW_SWITCH LPAREN condExpr = expr RPAREN cases += switchCaseStatement+ KW_DEFAULT KW_RETURN def = statement - ; - // replaced with the switchCaseOperand production to match the JSONiq grammar - -switchCaseStatement - : (KW_CASE cond += exprSingle)+ KW_RETURN ret = statement - ; - -tryCatchStatement - : KW_TRY try_block = blockStatement catches += catchCaseStatement+ - ; -/* - * added to match the JSONiq grammar - * replaced the CatchErrorList production rule to match the JSONiq grammar - */ - - -catchCaseStatement - : KW_CATCH nameTest (VBAR nameTest)* catch_block = blockStatement - ; - // The optional variable is local to the default branch. - -typeSwitchStatement - : KW_TYPESWITCH LPAREN cond = expr RPAREN cases += caseStatement+ KW_DEFAULT (var_ref = varBinding)? (KW_RETURN) def = statement - ; - // The optional variable is local to this case branch. - -caseStatement - : KW_CASE (var_ref = varBinding KW_AS)? union += sequenceType (VBAR union += sequenceType)* (KW_RETURN) ret = statement - ; - -varDeclStatement - : annotations KW_VARIABLE varDeclForStatement (COMMA varDeclForStatement)* SEMICOLON - ; - // added to match the JSONiq grammar - -varDeclForStatement - : var_ref = varBinding (KW_AS sequenceType)? (COLON_EQ expr_vals += exprSingle)? - ; - -whileStatement - : KW_WHILE LPAREN test_expr = expr RPAREN stmt = statement - ; - // Expressions - - // redefined according to the XQuery Scripting Extension spec - -exprSingle - : exprSimple - | flworExpr - | ifExpr - | switchExpr - | tryCatchExpr - | typeswitchExpr - ; - -exprSimple - : quantifiedExpr - | orExpr - | insertExpr - | deleteExpr - | renameExpr - | replaceExpr - | transformExpr - | appendExpr - | createCollectionExpr - | truncateCollectionExpr - | deleteIndexExpr - | deleteSearchExpr - | editCollectionExpr - | insertIndexExpr - | insertSearchExpr - ; - -blockExpr - : LBRACE statementsAndExpr RBRACE - ; -/* - * Updating expressions (out-of-spec) - * these are not referenced anywhere in the XQuery spec or in the XQuery Scripting Extension spec - * they are ported from the original grammar, and likely to be derived from JSONiq - */ - - -insertExpr - : KW_INSERT KW_JSON to_insert_expr = exprSingle KW_INTO main_expr = exprSingle (KW_AT KW_POSITION pos_expr = exprSingle)? - | KW_INSERT KW_JSON pairConstructor (COMMA pairConstructor)* KW_INTO main_expr = exprSingle - ; - -deleteExpr - : KW_DELETE KW_JSON updateLocator - ; - -renameExpr - : KW_RENAME KW_JSON updateLocator KW_AS name_expr = exprSingle - ; - -replaceExpr - : KW_REPLACE KW_VALUE KW_OF KW_JSON updateLocator KW_WITH replacer_expr = exprSingle - ; - -transformExpr - : KW_COPY copyDecl (COMMA copyDecl)* KW_MODIFY mod_expr = exprSingle KW_RETURN ret_expr = exprSingle - ; - -appendExpr - : KW_APPEND KW_JSON to_append_expr = exprSingle KW_INTO array_expr = exprSingle - ; - -updateLocator - : main_expr = postfixExpr - ; - -copyDecl - : var_ref = varBinding COLON_EQ src_expr = exprSingle - ; - ///////////////////////// Top Level Updating Expressions - -createCollectionExpr - : KW_CREATE KW_COLLECTION collectionMode = (KW_TABLE | KW_DELTA_FILE | KW_ICEBERG_TABLE) LPAREN collection_name = exprSimple RPAREN (KW_WITH content = exprSingle)? - ; - -deleteIndexExpr - : KW_DELETE ((first = KW_FIRST | last = KW_LAST) num = exprSingle?) KW_FROM KW_COLLECTION collectionMode = (KW_TABLE | KW_DELTA_FILE | KW_ICEBERG_TABLE) LPAREN collection_name = exprSimple RPAREN - ; - -deleteSearchExpr - : KW_DELETE content = exprSingle KW_FROM KW_COLLECTION - ; - -insertIndexExpr - : KW_INSERT content = exprSingle ((KW_AT pos = exprSingle) | first = KW_FIRST | last = KW_LAST) KW_INTO KW_COLLECTION collectionMode = (KW_TABLE | KW_DELTA_FILE | KW_ICEBERG_TABLE) LPAREN collection_name = exprSimple RPAREN - ; - -insertSearchExpr - : KW_INSERT content = exprSingle (before = KW_BEFORE | after = KW_AFTER) target = exprSingle KW_INTO KW_COLLECTION - ; - -truncateCollectionExpr - : (KW_DELETE | KW_TRUNCATE) KW_COLLECTION collectionMode = (KW_TABLE | KW_DELTA_FILE | KW_ICEBERG_TABLE) LPAREN collection_name = exprSimple RPAREN - ; - -editCollectionExpr - : KW_EDIT target = exprSingle KW_INTO content = exprSingle KW_IN KW_COLLECTION - ; -