From 65249ac4ba0b03992ccbe89a1b8807e65fe291b0 Mon Sep 17 00:00:00 2001 From: joshsh Date: Mon, 16 Sep 2024 13:00:57 -0700 Subject: [PATCH] Add OpenGQL ANTLR grammar --- .../src/main/antlr/com/github/opengql/GQL.g4 | 3781 +++++++++++++++++ 1 file changed, 3781 insertions(+) create mode 100644 hydra-java/src/main/antlr/com/github/opengql/GQL.g4 diff --git a/hydra-java/src/main/antlr/com/github/opengql/GQL.g4 b/hydra-java/src/main/antlr/com/github/opengql/GQL.g4 new file mode 100644 index 000000000..a9f65e353 --- /dev/null +++ b/hydra-java/src/main/antlr/com/github/opengql/GQL.g4 @@ -0,0 +1,3781 @@ +// Source: https://github.com/opengql/grammar/blob/main/GQL.g4 +// Revision: 15b256b (2024-09-05) +// Apache License 2.0 (https://github.com/opengql/grammar/blob/main/LICENSE) + +grammar GQL; + +options { caseInsensitive = true; } + +// 6 + +gqlProgram + : programActivity sessionCloseCommand? EOF + | sessionCloseCommand EOF + ; + +programActivity + : sessionActivity + | transactionActivity + ; + +sessionActivity + : sessionResetCommand+ + | sessionSetCommand+ sessionResetCommand* + ; + +transactionActivity + : startTransactionCommand (procedureSpecification endTransactionCommand?)? + | procedureSpecification endTransactionCommand? + | endTransactionCommand + ; + +endTransactionCommand + : rollbackCommand + | commitCommand + ; + +// 7.1 + +sessionSetCommand + : SESSION SET (sessionSetSchemaClause | sessionSetGraphClause | sessionSetTimeZoneClause | sessionSetParameterClause) + ; + +sessionSetSchemaClause + : SCHEMA schemaReference + ; + +sessionSetGraphClause + : PROPERTY? GRAPH graphExpression + ; + +sessionSetTimeZoneClause + : TIME ZONE setTimeZoneValue + ; + +setTimeZoneValue + : timeZoneString + ; + +sessionSetParameterClause + : sessionSetGraphParameterClause + | sessionSetBindingTableParameterClause + | sessionSetValueParameterClause + ; + +sessionSetGraphParameterClause + : PROPERTY? GRAPH sessionSetParameterName optTypedGraphInitializer + ; + +sessionSetBindingTableParameterClause + : BINDING? TABLE sessionSetParameterName optTypedBindingTableInitializer + ; + +sessionSetValueParameterClause + : VALUE sessionSetParameterName optTypedValueInitializer + ; + +sessionSetParameterName + : (IF NOT EXISTS)? sessionParameterSpecification + ; + +// 7.2 + +sessionResetCommand + : SESSION RESET sessionResetArguments? + ; + +sessionResetArguments + : ALL? (PARAMETERS | CHARACTERISTICS) + | SCHEMA + | PROPERTY? GRAPH + | TIME ZONE + | PARAMETER? sessionParameterSpecification + ; + +// 7.3 + +sessionCloseCommand + : SESSION CLOSE + ; + +// 7.4 + +sessionParameterSpecification + : GENERAL_PARAMETER_REFERENCE + ; + +// 8.1 + +startTransactionCommand + : START TRANSACTION transactionCharacteristics? + ; + +// 8.2 + +transactionCharacteristics + : transactionMode (COMMA transactionMode)* + ; + +transactionMode + : transactionAccessMode + ; + +transactionAccessMode + : READ ONLY + | READ WRITE + ; + +// 8.3 + +rollbackCommand + : ROLLBACK + ; + +// 8.4 + +commitCommand + : COMMIT + ; + +// 9.1 + +nestedProcedureSpecification + : LEFT_BRACE procedureSpecification RIGHT_BRACE + ; + +// , and are +// identical productions. The specification distinguishes them in the BNF, but in the implementation, the distinction +// has to be made sematically, in code, based on the kind of statements contained in the . +procedureSpecification + : procedureBody +// : catalogModifyingProcedureSpecification +// | dataModifyingProcedureSpecification +// | querySpecification + ; + +//catalogModifyingProcedureSpecification +// : procedureBody +// ; + +nestedDataModifyingProcedureSpecification + : LEFT_BRACE procedureBody RIGHT_BRACE + ; + +//dataModifyingProcedureSpecification +// : procedureBody +// ; + +nestedQuerySpecification + : LEFT_BRACE procedureBody RIGHT_BRACE + ; + +//querySpecification +// : procedureBody +// ; + +// 9.2 + +procedureBody + : atSchemaClause? bindingVariableDefinitionBlock? statementBlock + ; + +bindingVariableDefinitionBlock + : bindingVariableDefinition+ + ; + +bindingVariableDefinition + : graphVariableDefinition + | bindingTableVariableDefinition + | valueVariableDefinition + ; + +statementBlock + : statement nextStatement* + ; + +statement + : linearCatalogModifyingStatement + | linearDataModifyingStatement + | compositeQueryStatement + ; + +nextStatement + : NEXT yieldClause? statement + ; + +// 10.1 + +graphVariableDefinition + : PROPERTY? GRAPH bindingVariable optTypedGraphInitializer + ; + +optTypedGraphInitializer + : (typed? graphReferenceValueType)? graphInitializer + ; + +graphInitializer + : EQUALS_OPERATOR graphExpression + ; + +// 10.2 + +bindingTableVariableDefinition + : BINDING? TABLE bindingVariable optTypedBindingTableInitializer + ; + +optTypedBindingTableInitializer + : (typed? bindingTableReferenceValueType)? bindingTableInitializer + ; + +bindingTableInitializer + : EQUALS_OPERATOR bindingTableExpression + ; + +// 10.3 + +valueVariableDefinition + : VALUE bindingVariable optTypedValueInitializer + ; + +optTypedValueInitializer + : (typed? valueType)? valueInitializer + ; + +valueInitializer + : EQUALS_OPERATOR valueExpression + ; + +// 11.1 + +graphExpression + : objectExpressionPrimary + | graphReference + | objectNameOrBindingVariable + | currentGraph + ; + +currentGraph + : CURRENT_PROPERTY_GRAPH + | CURRENT_GRAPH + ; + +// 11.2 + +bindingTableExpression + : nestedBindingTableQuerySpecification + | objectExpressionPrimary + | bindingTableReference + | objectNameOrBindingVariable + ; + +nestedBindingTableQuerySpecification + : nestedQuerySpecification + ; + +// 11.3 + +objectExpressionPrimary + : VARIABLE valueExpressionPrimary + | parenthesizedValueExpression + | nonParenthesizedValueExpressionPrimarySpecialCase + ; + +// 12.1 + +linearCatalogModifyingStatement + : simpleCatalogModifyingStatement+ + ; + +simpleCatalogModifyingStatement + : primitiveCatalogModifyingStatement + | callCatalogModifyingProcedureStatement + ; + +primitiveCatalogModifyingStatement + : createSchemaStatement + | dropSchemaStatement + | createGraphStatement + | dropGraphStatement + | createGraphTypeStatement + | dropGraphTypeStatement + ; + +// 12.2 + +createSchemaStatement + : CREATE SCHEMA (IF NOT EXISTS)? catalogSchemaParentAndName + ; + +// 12.3 + +dropSchemaStatement + : DROP SCHEMA (IF EXISTS)? catalogSchemaParentAndName + ; + +// 12.4 + +createGraphStatement + : CREATE (PROPERTY? GRAPH (IF NOT EXISTS)? | OR REPLACE PROPERTY? GRAPH) catalogGraphParentAndName (openGraphType | ofGraphType) graphSource? + ; + +openGraphType + : typed? ANY (PROPERTY? GRAPH)? + ; + +ofGraphType + : graphTypeLikeGraph + | typed? graphTypeReference + | typed? (PROPERTY? GRAPH)? nestedGraphTypeSpecification + ; + +graphTypeLikeGraph + : LIKE graphExpression + ; + +graphSource + : AS COPY OF graphExpression + ; + +// 12.5 + +dropGraphStatement + : DROP PROPERTY? GRAPH (IF EXISTS)? catalogGraphParentAndName + ; + +// 12.6 + +createGraphTypeStatement + : CREATE (PROPERTY? GRAPH TYPE (IF NOT EXISTS)? | OR REPLACE PROPERTY? GRAPH TYPE) catalogGraphTypeParentAndName graphTypeSource + ; + +graphTypeSource + : AS? copyOfGraphType + | graphTypeLikeGraph + | AS? nestedGraphTypeSpecification + ; + +copyOfGraphType + : COPY OF graphTypeReference + ; + +// 12.7 + +dropGraphTypeStatement + : DROP PROPERTY? GRAPH TYPE (IF EXISTS)? catalogGraphTypeParentAndName + ; + +// 12.8 + +callCatalogModifyingProcedureStatement + : callProcedureStatement + ; + +// 13.1 + +linearDataModifyingStatement + : focusedLinearDataModifyingStatement + | ambientLinearDataModifyingStatement + ; + +focusedLinearDataModifyingStatement + : focusedLinearDataModifyingStatementBody + | focusedNestedDataModifyingProcedureSpecification + ; + +focusedLinearDataModifyingStatementBody + : useGraphClause simpleLinearDataAccessingStatement primitiveResultStatement? + ; + +focusedNestedDataModifyingProcedureSpecification + : useGraphClause nestedDataModifyingProcedureSpecification + ; + +ambientLinearDataModifyingStatement + : ambientLinearDataModifyingStatementBody + | nestedDataModifyingProcedureSpecification + ; + +ambientLinearDataModifyingStatementBody + : simpleLinearDataAccessingStatement primitiveResultStatement? + ; + +simpleLinearDataAccessingStatement + : simpleDataAccessingStatement+ + ; + +simpleDataAccessingStatement + : simpleQueryStatement + | simpleDataModifyingStatement + ; + +simpleDataModifyingStatement + : primitiveDataModifyingStatement + | callDataModifyingProcedureStatement + ; + +primitiveDataModifyingStatement + : insertStatement + | setStatement + | removeStatement + | deleteStatement + ; + +// 13.2 + +insertStatement + : INSERT insertGraphPattern + ; + +// 13.3 + +setStatement + : SET setItemList + ; + +setItemList + : setItem (COMMA setItem)* + ; + +setItem + : setPropertyItem + | setAllPropertiesItem + | setLabelItem + ; + +setPropertyItem + : bindingVariableReference PERIOD propertyName EQUALS_OPERATOR valueExpression + ; + +setAllPropertiesItem + : bindingVariableReference EQUALS_OPERATOR LEFT_BRACE propertyKeyValuePairList? RIGHT_BRACE + ; + +setLabelItem + : bindingVariableReference isOrColon labelName + ; + +// 13.4 + +removeStatement + : REMOVE removeItemList + ; + +removeItemList + : removeItem (COMMA removeItem)* + ; + +removeItem + : removePropertyItem + | removeLabelItem + ; + +removePropertyItem + : bindingVariableReference PERIOD propertyName + ; + +removeLabelItem + : bindingVariableReference isOrColon labelName + ; + +// 13.5 + +deleteStatement + : (DETACH | NODETACH)? DELETE deleteItemList + ; + +deleteItemList + : deleteItem (COMMA deleteItem)* + ; + +deleteItem + : valueExpression + ; + +// 13.6 + +callDataModifyingProcedureStatement + : callProcedureStatement + ; + +// 14.1 + +compositeQueryStatement + : compositeQueryExpression + ; + +// 14.2 + +compositeQueryExpression + : compositeQueryExpression queryConjunction compositeQueryPrimary + | compositeQueryPrimary + ; + +queryConjunction + : setOperator + | OTHERWISE + ; + +setOperator + : UNION setQuantifier? + | EXCEPT setQuantifier? + | INTERSECT setQuantifier? + ; + +compositeQueryPrimary + : linearQueryStatement + ; + +// 14.3 and + +linearQueryStatement + : focusedLinearQueryStatement + | ambientLinearQueryStatement + ; + +focusedLinearQueryStatement + : focusedLinearQueryStatementPart* focusedLinearQueryAndPrimitiveResultStatementPart + | focusedPrimitiveResultStatement + | focusedNestedQuerySpecification + | selectStatement + ; + +focusedLinearQueryStatementPart + : useGraphClause simpleLinearQueryStatement + ; + +focusedLinearQueryAndPrimitiveResultStatementPart + : useGraphClause simpleLinearQueryStatement primitiveResultStatement + ; + +focusedPrimitiveResultStatement + : useGraphClause primitiveResultStatement + ; + +focusedNestedQuerySpecification + : useGraphClause nestedQuerySpecification + ; + +ambientLinearQueryStatement + : simpleLinearQueryStatement? primitiveResultStatement + | nestedQuerySpecification + ; + +simpleLinearQueryStatement + : simpleQueryStatement+ + ; + +simpleQueryStatement + : primitiveQueryStatement + | callQueryStatement + ; + +primitiveQueryStatement + : matchStatement + | letStatement + | forStatement + | filterStatement + | orderByAndPageStatement + ; + +// 14.4 + +matchStatement + : simpleMatchStatement + | optionalMatchStatement + ; + +simpleMatchStatement + : MATCH graphPatternBindingTable + ; + +optionalMatchStatement + : OPTIONAL optionalOperand + ; + +optionalOperand + : simpleMatchStatement + | LEFT_BRACE matchStatementBlock RIGHT_BRACE + | LEFT_PAREN matchStatementBlock RIGHT_PAREN + ; + +matchStatementBlock + : matchStatement+ + ; + +// 14.5 + +callQueryStatement + : callProcedureStatement + ; + +// 14.6 + +filterStatement + : FILTER (whereClause | searchCondition) + ; + +// 14.7 + +letStatement + : LET letVariableDefinitionList + ; + +letVariableDefinitionList + : letVariableDefinition (COMMA letVariableDefinition)* + ; + +letVariableDefinition + : valueVariableDefinition + | bindingVariable EQUALS_OPERATOR valueExpression + ; + +// 14.8 + +forStatement + : FOR forItem forOrdinalityOrOffset? + ; + +forItem + : forItemAlias forItemSource + ; + +forItemAlias + : bindingVariable IN + ; + +forItemSource + : valueExpression + ; + +forOrdinalityOrOffset + : WITH (ORDINALITY | OFFSET) bindingVariable + ; + +// 14.9 + +orderByAndPageStatement + : orderByClause offsetClause? limitClause? + | offsetClause limitClause? + | limitClause + ; + +// 14.10 + +primitiveResultStatement + : returnStatement orderByAndPageStatement? + | FINISH + ; + +// 14.11 + +returnStatement + : RETURN returnStatementBody + ; + +returnStatementBody + : setQuantifier? (ASTERISK | returnItemList) groupByClause? + | NO BINDINGS + ; + +returnItemList + : returnItem (COMMA returnItem)* + ; + +returnItem + : aggregatingValueExpression returnItemAlias? + ; + +returnItemAlias + : AS identifier + ; + +// 14.12