From 279294d948428be689ce18214db727bd0a794c7a Mon Sep 17 00:00:00 2001 From: Nikolaos Karalis Date: Mon, 11 Dec 2023 11:15:12 +0100 Subject: [PATCH 1/5] removed version 1.0 --- .gitignore | 2 - CMakeLists.txt | 105 ++--- LICENSE-GRAMMAR-1.1 => LICENSE-GRAMMAR | 0 LICENSE-GRAMMAR-1.0 | 34 -- README.md | 8 +- SparqlLexer_1.1.g4 => SparqlLexer.g4 | 0 SparqlParser_1.1.g4 => SparqlParser.g4 | 0 SparqlParser_1.0.g4 | 521 ------------------------- conanfile.py | 4 +- 9 files changed, 42 insertions(+), 632 deletions(-) rename LICENSE-GRAMMAR-1.1 => LICENSE-GRAMMAR (100%) delete mode 100644 LICENSE-GRAMMAR-1.0 rename SparqlLexer_1.1.g4 => SparqlLexer.g4 (100%) rename SparqlParser_1.1.g4 => SparqlParser.g4 (100%) delete mode 100644 SparqlParser_1.0.g4 diff --git a/.gitignore b/.gitignore index c772e07..8f434b4 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,4 @@ venv/ # project specific /sparql-grammer/generated/ -SparqlParser.g4 -SparqlLexer.g4 Sparql.g4 diff --git a/CMakeLists.txt b/CMakeLists.txt index e684cb0..9e541be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,10 +19,6 @@ endif () find_package(antlr4-runtime REQUIRED) -set(SPARQL_VERSION "1.1" CACHE STRING "SPARQL version" FORCE) -mark_as_advanced(SPARQL_VERSION) - - set(ANTLR4_TAG "4.13.1" CACHE STRING "Antlr4 version" FORCE) mark_as_advanced(ANTLR4_TAG) set(ANTLR4_JAR_LOCATION "${PROJECT_BINARY_DIR}/antlr-${ANTLR4_TAG}-complete.jar") @@ -37,72 +33,48 @@ include(${CMAKE_CURRENT_BINARY_DIR}/antlr4cmake/antlr4-generator.cmake) message("antlr4-runtime_DIR ${antlr4-runtime_DIR}") -if (${SPARQL_VERSION} STREQUAL "1.1") - file(CREATE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/SparqlLexer_${SPARQL_VERSION}.g4 - ${CMAKE_CURRENT_BINARY_DIR}/SparqlLexer.g4) - file(CREATE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/SparqlParser_${SPARQL_VERSION}.g4 - ${CMAKE_CURRENT_BINARY_DIR}/SparqlParser.g4) -else () - file(CREATE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/SparqlParser_${SPARQL_VERSION}.g4 - ${CMAKE_CURRENT_BINARY_DIR}/Sparql.g4 - ) -endif () - - -if (${SPARQL_VERSION} STREQUAL "1.1") - message("Generating SPARQL 1.1 Parser. If you want to generate the legacy SPARQL 1.0 parser, use -SPARQL_VERSION=1.0 or the conan package option sparql-parser-base:sparql_version=1.0 .") - antlr4_generate( - SparqlLexer - "${CMAKE_CURRENT_BINARY_DIR}/SparqlLexer.g4" - LEXER - FALSE # don't generate listener for lexer (not possible) - FALSE # don't generate visitor for lexer (not possible) - "dice::sparql_parser::base" - ) - file(CREATE_LINK ${ANTLR4_GENERATED_SRC_DIR}/SparqlLexer/SparqlLexer.tokens - ${CMAKE_CURRENT_BINARY_DIR}/SparqlLexer.tokens) - file(CREATE_LINK ${ANTLR4_GENERATED_SRC_DIR}/SparqlLexer/SparqlLexer.interp - ${CMAKE_CURRENT_BINARY_DIR}/SparqlLexer.interp) - antlr4_generate( - SparqlParser - "${CMAKE_CURRENT_BINARY_DIR}/SparqlParser.g4" - PARSER - TRUE # generate listener - TRUE # generate visitor - "dice::sparql_parser::base" - ) -else () # SPARQL 1.1 - message("Generating SPARQL 1.0 Parser. This is a legacy version and will not receive any more updates.") - antlr4_generate( - Sparql - "${CMAKE_CURRENT_BINARY_DIR}/Sparql.g4" - BOTH - TRUE # generate listener - TRUE # generate visitor - "dice::sparql_parser::base" - ) -endif () - -if (${SPARQL_VERSION} STREQUAL "1.1") - add_library("${PROJECT_NAME}" - ${ANTLR4_SRC_FILES_SparqlLexer} - ${ANTLR4_SRC_FILES_SparqlParser}) - target_include_directories(sparql-parser-base PUBLIC - "$" - "$") - -else () - add_library("${PROJECT_NAME}" - ${ANTLR4_SRC_FILES_Sparql}) - target_include_directories(sparql-parser-base PUBLIC - "$") -endif () +file(CREATE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/SparqlLexer.g4 + ${CMAKE_CURRENT_BINARY_DIR}/SparqlLexer.g4 +) +file(CREATE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/SparqlParser.g4 + ${CMAKE_CURRENT_BINARY_DIR}/SparqlParser.g4 +) + + +message("Generating SPARQL 1.1 Parser") +antlr4_generate( + SparqlLexer + "${CMAKE_CURRENT_BINARY_DIR}/SparqlLexer.g4" + LEXER + FALSE # don't generate listener for lexer (not possible) + FALSE # don't generate visitor for lexer (not possible) + "dice::sparql_parser::base" +) +file(CREATE_LINK ${ANTLR4_GENERATED_SRC_DIR}/SparqlLexer/SparqlLexer.tokens + ${CMAKE_CURRENT_BINARY_DIR}/SparqlLexer.tokens) +file(CREATE_LINK ${ANTLR4_GENERATED_SRC_DIR}/SparqlLexer/SparqlLexer.interp + ${CMAKE_CURRENT_BINARY_DIR}/SparqlLexer.interp) +antlr4_generate( + SparqlParser + "${CMAKE_CURRENT_BINARY_DIR}/SparqlParser.g4" + PARSER + TRUE # generate listener + TRUE # generate visitor + "dice::sparql_parser::base" +) + +add_library("${PROJECT_NAME}" + ${ANTLR4_SRC_FILES_SparqlLexer} + ${ANTLR4_SRC_FILES_SparqlParser}) +target_include_directories(sparql-parser-base PUBLIC + "$" + "$") add_library("${PROJECT_NAME}::${PROJECT_NAME}" ALIAS "${PROJECT_NAME}") target_link_libraries(sparql-parser-base PUBLIC $,antlr4_shared,antlr4_static> - ) +) set_target_properties(${PROJECT_NAME} PROPERTIES @@ -110,7 +82,8 @@ set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR} CXX_STANDARD 17 CXX_EXTENSIONS OFF - CXX_STANDARD_REQUIRED ON) + CXX_STANDARD_REQUIRED ON +) if (PROJECT_IS_TOP_LEVEL) include(cmake/install_library.cmake) diff --git a/LICENSE-GRAMMAR-1.1 b/LICENSE-GRAMMAR similarity index 100% rename from LICENSE-GRAMMAR-1.1 rename to LICENSE-GRAMMAR diff --git a/LICENSE-GRAMMAR-1.0 b/LICENSE-GRAMMAR-1.0 deleted file mode 100644 index 50ec26e..0000000 --- a/LICENSE-GRAMMAR-1.0 +++ /dev/null @@ -1,34 +0,0 @@ -Copyright 2007 the original author or authors. - -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following -conditions are met: - -Redistributions of source code must retain the above copyright notice, this list of conditions and the following -disclaimer. - -Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following -disclaimer in the documentation andor other materials provided with the distribution. - -Neither the name of the author or authors nor the names of its contributors may be used to endorse or promote -products derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -@author Simone Tripodi (simone) -@author Michele Mostarda (michele) -@version $Id: Sparql.g 5 2007-10-30 17:20:36Z simone $ - - -Ported to Antlr4 by Tom Everett \ No newline at end of file diff --git a/README.md b/README.md index cada8f2..76dad94 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # SPARQL-parser-base -[ANTLR-v4-based](https://github.com/antlr/antlr4)-based C++17 parser -for [SPARQL 1.0](https://www.w3.org/TR/rdf-sparql-query/) and [SPARQL 1.1](https://www.w3.org/TR/sparql11-overview/) +[ANTLR-v4-based](https://github.com/antlr/antlr4)-based C++17 parser for[SPARQL 1.1](https://www.w3.org/TR/sparql11-overview/) with visitors and listeners. During CMake configuration, the ANTLR v4 code generator is called. ## Requirements @@ -30,9 +29,6 @@ To use sparql-parser-base, add it to your `conanfile.txt`: sparql-parser-base/0.3.4 ``` -If you want to use SPARQL 1.0 instead, add `sparql-parser-base:sparql_version=1.0` to the `[options]` section of your -conan file. - ### With FetchContent Use @@ -70,7 +66,7 @@ make -j sparql-parser-base `-DBUILD_EXAMPLES=ON/OFF [default: OFF]`: Build the examples. -`-DSPARQL_VERSION="1.0"/"1.1" [default: "1.1"]`: SPARQL version of the generated parser. + `-DANTLR4_TAG=... [default: "4.13.1"]`: ANTLR4 version to be used. diff --git a/SparqlLexer_1.1.g4 b/SparqlLexer.g4 similarity index 100% rename from SparqlLexer_1.1.g4 rename to SparqlLexer.g4 diff --git a/SparqlParser_1.1.g4 b/SparqlParser.g4 similarity index 100% rename from SparqlParser_1.1.g4 rename to SparqlParser.g4 diff --git a/SparqlParser_1.0.g4 b/SparqlParser_1.0.g4 deleted file mode 100644 index a09fb4e..0000000 --- a/SparqlParser_1.0.g4 +++ /dev/null @@ -1,521 +0,0 @@ -/* - * Copyright 2007 the original author or authors. - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following - * conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided with the distribution. - * - * Neither the name of the author or authors nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * @author Simone Tripodi (simone) - * @author Michele Mostarda (michele) - * @version $Id: Sparql.g 5 2007-10-30 17:20:36Z simone $ - */ - /* - * Ported to Antlr4 by Tom Everett - */ - -grammar Sparql; - -query - : prologue ( selectQuery | constructQuery | describeQuery | askQuery ) EOF - ; - -prologue - : baseDecl? prefixDecl* - ; - -baseDecl - : 'BASE' IRI_REF - ; - -prefixDecl - : 'PREFIX' PNAME_NS IRI_REF - ; - -selectQuery - : 'SELECT' selectModifier ( var+ | '*' ) datasetClause* whereClause solutionModifier - ; - -selectModifier - : ('DISTINCT'|'REDUCE')? - ; - - -constructQuery - : 'CONSTRUCT' constructTemplate datasetClause* whereClause solutionModifier - ; - -describeQuery - : 'DESCRIBE' ( varOrIRIref+ | '*' ) datasetClause* whereClause? solutionModifier - ; - -askQuery - : 'ASK' datasetClause* whereClause - ; - -datasetClause - : 'FROM' ( defaultGraphClause | namedGraphClause ) - ; - -defaultGraphClause - : sourceSelector - ; - -namedGraphClause - : 'NAMED' sourceSelector - ; - -sourceSelector - : iriRef - ; - -whereClause - : 'WHERE'? groupGraphPattern - ; - -solutionModifier - : orderClause? limitOffsetClauses? - ; - -limitOffsetClauses - : ( limitClause offsetClause? | offsetClause limitClause? ) - ; - -orderClause - : 'ORDER' 'BY' orderCondition+ - ; - -orderCondition - : ( ( 'ASC' | 'DESC' ) brackettedExpression ) - | ( constraint | var ) - ; - -limitClause - : 'LIMIT' INTEGER - ; - -offsetClause - : 'OFFSET' INTEGER - ; - -groupGraphPattern - : '{' triplesBlock? ( ( graphPatternNotTriples | filter ) '.'? triplesBlock? )* '}' - ; - -triplesBlock - : triplesSameSubject ( '.' triplesBlock? )? - ; - -graphPatternNotTriples - : optionalGraphPattern | groupOrUnionGraphPattern | graphGraphPattern - ; - -optionalGraphPattern - : 'OPTIONAL' groupGraphPattern - ; - -graphGraphPattern - : 'GRAPH' varOrIRIref groupGraphPattern - ; - -groupOrUnionGraphPattern - : groupGraphPattern ( 'UNION' groupGraphPattern )* - ; - -filter - : 'FILTER' constraint - ; - -constraint - : brackettedExpression | builtInCall | functionCall - ; - -functionCall - : iriRef argList - ; - -argList - : ( NIL | '(' expression ( ',' expression )* ')' ) - ; - -constructTemplate - : '{' constructTriples? '}' - ; - -constructTriples - : triplesSameSubject ( '.' constructTriples? )? - ; - -triplesSameSubject - : varOrTerm propertyListNotEmpty | triplesNode propertyList - ; - -propertyListNotEmpty - : verb objectList ( ';' ( verb objectList )? )* - ; - -propertyList - : propertyListNotEmpty? - ; - -objectList - : object ( ',' object )* - ; - -object - : graphNode - ; - -verb - : varOrIRIref - | 'a' - ; - -triplesNode - : collection - | blankNodePropertyList - ; - -blankNodePropertyList - : '[' propertyListNotEmpty ']' - ; - -collection - : '(' graphNode+ ')' - ; - -graphNode - : varOrTerm | triplesNode - ; - -varOrTerm - : var - | graphTerm - ; - -varOrIRIref - : var | iriRef - ; - -var - : VAR1 - | VAR2 - ; - -graphTerm - : iriRef - | rdfLiteral - | numericLiteral - | booleanLiteral - | blankNode - | NIL - ; - -expression - : conditionalOrExpression - ; - -conditionalOrExpression - : conditionalAndExpression ( '||' conditionalAndExpression )* - ; - -conditionalAndExpression - : valueLogical ( '&&' valueLogical )* - ; - -valueLogical - : relationalExpression - ; - -relationalExpression - : numericExpression ( '=' numericExpression | '!=' numericExpression | '<' numericExpression | '>' numericExpression | '<=' numericExpression | '>=' numericExpression )? - ; - -numericExpression - : additiveExpression - ; - -additiveExpression - : multiplicativeExpression ( '+' multiplicativeExpression | '-' multiplicativeExpression | numericLiteralPositive | numericLiteralNegative )* - ; - -multiplicativeExpression - : unaryExpression ( '*' unaryExpression | '/' unaryExpression )* - ; - -unaryExpression - : '!' primaryExpression - | '+' primaryExpression - | '-' primaryExpression - | primaryExpression - ; - -primaryExpression - : brackettedExpression | builtInCall | iriRefOrFunction | rdfLiteral | numericLiteral | booleanLiteral | var - ; - -brackettedExpression - : '(' expression ')' - ; - -builtInCall - : 'STR' '(' expression ')' - | 'LANG' '(' expression ')' - | 'LANGMATCHES' '(' expression ',' expression ')' - | 'DATATYPE' '(' expression ')' - | 'BOUND' '(' var ')' - | 'sameTerm' '(' expression ',' expression ')' - | 'isIRI' '(' expression ')' - | 'isURI' '(' expression ')' - | 'isBLANK' '(' expression ')' - | 'isLITERAL' '(' expression ')' - | regexExpression - ; - -regexExpression - : 'REGEX' '(' expression ',' expression ( ',' expression )? ')' - ; - -iriRefOrFunction - : iriRef argList? - ; - -rdfLiteral - : string ( LANGTAG | ( '^^' iriRef ) )? - ; - -numericLiteral - : integerNumeric | decimalNumeric | doubleNumberic; - -integerNumeric - : (('+')? INTEGER) | INTEGER_NEGATIVE - ; - -decimalNumeric - : (('+')? DECIMAL) | DECIMAL_NEGATIVE - ; - -doubleNumberic - : ('+'? DOUBLE) |DOUBLE_NEGATIVE - ; - -numericLiteralUnsigned - : INTEGER - | DECIMAL - | DOUBLE - ; - -numericLiteralPositive - : INTEGER_POSITIVE - | DECIMAL_POSITIVE - | DOUBLE_POSITIVE - ; - -numericLiteralNegative - : INTEGER_NEGATIVE - | DECIMAL_NEGATIVE - | DOUBLE_NEGATIVE - ; - -booleanLiteral - : 'true' - | 'false' - ; - -string - : STRING_LITERAL1 - | STRING_LITERAL2 - /* | STRING_LITERAL_LONG('0'..'9') | STRING_LITERAL_LONG('0'..'9')*/ - ; - -iriRef - : IRI_REF - | prefixedName - ; - -prefixedName - : PNAME_LN - | PNAME_NS - ; - -blankNode - : BLANK_NODE_LABEL - | ANON - ; - -// LEXER RULES - -IRI_REF - : '<' ( ~('<' | '>' | '"' | '{' | '}' | '|' | '^' | '\\' | '`') | (PN_CHARS))* '>' - ; - -PNAME_NS - : PN_PREFIX? ':' - ; - -PNAME_LN - : PNAME_NS PN_LOCAL - ; - -BLANK_NODE_LABEL - : '_:' PN_LOCAL - ; - -VAR1 - : '?' VARNAME - ; - -VAR2 - : '$' VARNAME - ; - -LANGTAG - : '@' PN_CHARS_BASE+ ('-' (PN_CHARS_BASE DIGIT)+)* - ; - -INTEGER - : DIGIT+ - ; - -DECIMAL - : DIGIT+ '.' DIGIT* - | '.' DIGIT+ - ; - -DOUBLE - : DIGIT+ '.' DIGIT* EXPONENT - | '.' DIGIT+ EXPONENT - | DIGIT+ EXPONENT - ; - -INTEGER_POSITIVE - : '+' INTEGER - ; - -DECIMAL_POSITIVE - : '+' DECIMAL - ; - -DOUBLE_POSITIVE - : '+' DOUBLE - ; - -INTEGER_NEGATIVE - : '-' INTEGER - ; - -DECIMAL_NEGATIVE - : '-' DECIMAL - ; - -DOUBLE_NEGATIVE - : '-' DOUBLE - ; - -EXPONENT - : ('e'|'E') ('+'|'-')? DIGIT+ - ; - -STRING_LITERAL1 - : '\'' ( ~('\u0027' | '\u005C' | '\u000A' | '\u000D') | ECHAR )* '\'' - ; - -STRING_LITERAL2 - : '"' ( ~('\u0022' | '\u005C' | '\u000A' | '\u000D') | ECHAR )* '"' - ; - -STRING_LITERAL_LONG1 - : '\'\'\'' ( ( '\'' | '\'\'' )? (~('\'' | '\\') | ECHAR ) )* '\'\'\'' - ; - -STRING_LITERAL_LONG2 - : '"""' ( ( '"' | '""' )? ( ~('\'' | '\\') | ECHAR ) )* '"""' - ; - -ECHAR - : '\\' ('t' | 'b' | 'n' | 'r' | 'f' | '"' | '\'') - ; - -NIL - : '(' WS* ')' - ; - -ANON - : '[' WS* ']' - ; - -PN_CHARS_U - : PN_CHARS_BASE | '_' - ; - -VARNAME - : ( PN_CHARS_U | DIGIT ) ( PN_CHARS_U | DIGIT | '\u00B7' | ('\u0300'..'\u036F') | ('\u203F'..'\u2040') )* - ; - -fragment -PN_CHARS - : PN_CHARS_U - | '-' - | DIGIT - /*| '\u00B7' - | '\u0300'..'\u036F' - | '\u203F'..'\u2040'*/ - ; - -PN_PREFIX - : PN_CHARS_BASE ((PN_CHARS|'.')* PN_CHARS)? - ; - -PN_LOCAL - : ( PN_CHARS_U | DIGIT ) ((PN_CHARS|'.')* PN_CHARS)? - ; - -fragment -PN_CHARS_BASE - : 'A'..'Z' - | 'a'..'z' - | '\u00C0'..'\u00D6' - | '\u00D8'..'\u00F6' - | '\u00F8'..'\u02FF' - | '\u0370'..'\u037D' - | '\u037F'..'\u1FFF' - | '\u200C'..'\u200D' - | '\u2070'..'\u218F' - | '\u2C00'..'\u2FEF' - | '\u3001'..'\uD7FF' - | '\uF900'..'\uFDCF' - | '\uFDF0'..'\uFFFD' - ; - -fragment -DIGIT - : '0'..'9' - ; - -WS - : (' ' - | '\t' - | '\n' - | '\r')+ ->skip - ; diff --git a/conanfile.py b/conanfile.py index 49a853b..3b0f7d1 100644 --- a/conanfile.py +++ b/conanfile.py @@ -21,16 +21,14 @@ class Recipe(ConanFile): options = { "shared": [True, False], "fPIC": [True, False], - 'sparql_version': ['1.0', '1.1'], } default_options = { "shared": False, "fPIC": True, - 'sparql_version': '1.1', } settings = "os", "compiler", "build_type", "arch" generators = ("CMakeDeps", "CMakeToolchain") - exports_sources = "CMakeLists.txt", "antlr4cmake/antlr4-generator.cmake.in", "cmake/*", "SparqlLexer_1.1.g4", "SparqlParser_1.0.g4", "SparqlParser_1.1.g4" + exports_sources = "CMakeLists.txt", "antlr4cmake/antlr4-generator.cmake.in", "cmake/*", "SparqlLexer.g4", "SparqlParser.g4" def requirements(self): self.requires("antlr4-cppruntime/4.13.1", transitive_headers=True) From 4a5d560d7fa4ec80fbf3abbc7dc70b81b775f151 Mon Sep 17 00:00:00 2001 From: Nikolaos Karalis Date: Mon, 11 Dec 2023 16:58:16 +0100 Subject: [PATCH 2/5] added rules for hinting the variable ordering --- CMakeLists.txt | 15 +++------------ README.md | 4 ++-- SparqlLexer.g4 | 2 ++ SparqlParser.g4 | 20 +++++++++++++++++--- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e541be..428dccd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.21) -project(sparql-parser-base VERSION 0.3.4) +project(sparql-parser-base VERSION 0.3.5) include(cmake/boilerplate_init.cmake) boilerplate_init() @@ -32,19 +32,10 @@ include(${CMAKE_CURRENT_BINARY_DIR}/antlr4cmake/antlr4-generator.cmake) message("antlr4-runtime_DIR ${antlr4-runtime_DIR}") - -file(CREATE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/SparqlLexer.g4 - ${CMAKE_CURRENT_BINARY_DIR}/SparqlLexer.g4 -) -file(CREATE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/SparqlParser.g4 - ${CMAKE_CURRENT_BINARY_DIR}/SparqlParser.g4 -) - - message("Generating SPARQL 1.1 Parser") antlr4_generate( SparqlLexer - "${CMAKE_CURRENT_BINARY_DIR}/SparqlLexer.g4" + "${CMAKE_CURRENT_SOURCE_DIR}/SparqlLexer.g4" LEXER FALSE # don't generate listener for lexer (not possible) FALSE # don't generate visitor for lexer (not possible) @@ -56,7 +47,7 @@ file(CREATE_LINK ${ANTLR4_GENERATED_SRC_DIR}/SparqlLexer/SparqlLexer.interp ${CMAKE_CURRENT_BINARY_DIR}/SparqlLexer.interp) antlr4_generate( SparqlParser - "${CMAKE_CURRENT_BINARY_DIR}/SparqlParser.g4" + "${CMAKE_CURRENT_SOURCE_DIR}/SparqlParser.g4" PARSER TRUE # generate listener TRUE # generate visitor diff --git a/README.md b/README.md index 76dad94..35ed34a 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ To use sparql-parser-base, add it to your `conanfile.txt`: ``` [requires] -sparql-parser-base/0.3.4 +sparql-parser-base/0.3.5 ``` ### With FetchContent @@ -38,7 +38,7 @@ include(FetchContent) FetchContent_Declare( sparql-parser-base GIT_REPOSITORY "${CMAKE_CURRENT_SOURCE_DIR}/../" - GIT_TAG 0.3.4 + GIT_TAG 0.3.5 GIT_SHALLOW TRUE ) FetchContent_MakeAvailable(sparql-parser-base) diff --git a/SparqlLexer.g4 b/SparqlLexer.g4 index c93acf8..3c70962 100644 --- a/SparqlLexer.g4 +++ b/SparqlLexer.g4 @@ -26,6 +26,8 @@ lexer grammar SparqlLexer; WS : (' '| '\t'| EOL)+ -> channel(99); +Q_HINT_VAR_ORD: ('Q'|'q')'_'('H'|'h')('I'|'i')('N'|'n')('T'|'t')'_'('V'|'v')('A'|'a')('R'|'r')'_'('O'|'o')('R'|'r')('D'|'d'); + BASE : ('B'|'b')('A'|'a')('S'|'s')('E'|'e'); PREFIX : ('P'|'p')('R'|'r')('E'|'e')('F'|'f')('I'|'i')('X'|'x'); diff --git a/SparqlParser.g4 b/SparqlParser.g4 index dbc9537..639b28b 100644 --- a/SparqlParser.g4 +++ b/SparqlParser.g4 @@ -55,7 +55,7 @@ subSelect ; selectClause - : SELECT selectModifier? (selectVariables+ | '*') + : hintsClause SELECT selectModifier? (selectVariables+ | '*') ; selectModifier @@ -68,7 +68,7 @@ selectVariables ; constructQuery - : CONSTRUCT (constructTemplate datasetClause* whereClause solutionModifier | datasetClause* WHERE '{' triplesTemplate? '}' solutionModifier) + : hintsClause CONSTRUCT (constructTemplate datasetClause* whereClause solutionModifier | datasetClause* WHERE '{' triplesTemplate? '}' solutionModifier) ; describeQuery @@ -76,7 +76,21 @@ describeQuery ; askQuery - : ASK datasetClause* whereClause solutionModifier + : hintsClause ASK datasetClause* whereClause solutionModifier + ; + +// NOT PART OF THE OFFICIAL GRAMMAR (tentris specific) +hintsClause + : variableOrdering? + ; + +// NOT PART OF THE OFFICIAL GRAMMAR (tentris specific) +variableOrdering + : Q_HINT_VAR_ORD varOrBlankNode+ + ; + +varOrBlankNode + : var | blankNode ; datasetClause From 3871ec35193dd48ff8576178fa55bc447b83acbf Mon Sep 17 00:00:00 2001 From: Nikolaos Karalis Date: Wed, 13 Dec 2023 10:51:23 +0100 Subject: [PATCH 3/5] query hints for cardinality estimation --- .gitignore | 4 ++++ SparqlLexer.g4 | 9 +++++++++ SparqlParser.g4 | 8 +++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8f434b4..6ddf7d4 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,7 @@ venv/ # project specific /sparql-grammer/generated/ Sparql.g4 + +# ANTLR tool +gen/ +SparqlLexer.tokens \ No newline at end of file diff --git a/SparqlLexer.g4 b/SparqlLexer.g4 index 3c70962..f03ee30 100644 --- a/SparqlLexer.g4 +++ b/SparqlLexer.g4 @@ -28,6 +28,15 @@ WS : (' '| '\t'| EOL)+ -> channel(99); Q_HINT_VAR_ORD: ('Q'|'q')'_'('H'|'h')('I'|'i')('N'|'n')('T'|'t')'_'('V'|'v')('A'|'a')('R'|'r')'_'('O'|'o')('R'|'r')('D'|'d'); +Q_HINT_CARD_EST: ('Q'|'q')'_'('H'|'h')('I'|'i')('N'|'n')('T'|'t')'_'('C'|'c')('A'|'a')('R'|'r')('D'|'d')'_'('E'|'e')('S'|'s')('T'|'t'); + +CARD_EST_REDUCTION_FACTOR: ('C'|'c')('A'|'a')('R'|'r')('D'|'d')'_'('E'|'e')('S'|'s')('T'|'t')'_' + ('R'|'r')('E'|'e')('D'|'d')('U'|'u')('C'|'c')('T'|'t')('I'|'i')('O'|'o')('N'|'n')'_' + ('F'|'f')('A'|'a')('C'|'c')('T'|'t')('O'|'o')('R'|'r'); + +CARD_EST_MIN_CARDINALITY: ('C'|'c')('A'|'a')('R'|'r')('D'|'d')'_'('E'|'e')('S'|'s')('T'|'t')'_'('M'|'m')('I'|'i')('N'|'n')'_' + ('C'|'c')('A'|'a')('R'|'r')('D'|'d')('I'|'i')('N'|'n')('A'|'a')('L'|'l')('I'|'i')('T'|'t')('Y'|'y'); + BASE : ('B'|'b')('A'|'a')('S'|'s')('E'|'e'); PREFIX : ('P'|'p')('R'|'r')('E'|'e')('F'|'f')('I'|'i')('X'|'x'); diff --git a/SparqlParser.g4 b/SparqlParser.g4 index 639b28b..a8f3069 100644 --- a/SparqlParser.g4 +++ b/SparqlParser.g4 @@ -81,7 +81,7 @@ askQuery // NOT PART OF THE OFFICIAL GRAMMAR (tentris specific) hintsClause - : variableOrdering? + : variableOrdering? cardinalityEstimation? ; // NOT PART OF THE OFFICIAL GRAMMAR (tentris specific) @@ -89,6 +89,12 @@ variableOrdering : Q_HINT_VAR_ORD varOrBlankNode+ ; +// NOT PART OF THE OFFICIAL GRAMMAR (tentris specific) +cardinalityEstimation + : Q_HINT_CARD_EST (CARD_EST_REDUCTION_FACTOR|CARD_EST_MIN_CARDINALITY) + ; + + varOrBlankNode : var | blankNode ; From 95773a1172532f63b0e8a015d04e29d893bec7d2 Mon Sep 17 00:00:00 2001 From: Nikolaos Karalis Date: Wed, 13 Dec 2023 10:55:17 +0100 Subject: [PATCH 4/5] comment --- SparqlParser.g4 | 1 + 1 file changed, 1 insertion(+) diff --git a/SparqlParser.g4 b/SparqlParser.g4 index a8f3069..d9cf1db 100644 --- a/SparqlParser.g4 +++ b/SparqlParser.g4 @@ -80,6 +80,7 @@ askQuery ; // NOT PART OF THE OFFICIAL GRAMMAR (tentris specific) +// NOTE: The order matters (if there is a variableOrdering hint, it must be provided before the cardinalityEstimation hint hintsClause : variableOrdering? cardinalityEstimation? ; From ab0e3206ec13bd02997b9ec31c1ee1da663964d9 Mon Sep 17 00:00:00 2001 From: Nikolaos Karalis Date: Tue, 19 Dec 2023 10:43:03 +0100 Subject: [PATCH 5/5] fixed typos --- README.md | 2 +- SparqlParser.g4 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 35ed34a..41cc39f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # SPARQL-parser-base -[ANTLR-v4-based](https://github.com/antlr/antlr4)-based C++17 parser for[SPARQL 1.1](https://www.w3.org/TR/sparql11-overview/) +[ANTLR-v4-based](https://github.com/antlr/antlr4)-based C++17 parser for [SPARQL 1.1](https://www.w3.org/TR/sparql11-overview/) with visitors and listeners. During CMake configuration, the ANTLR v4 code generator is called. ## Requirements diff --git a/SparqlParser.g4 b/SparqlParser.g4 index d9cf1db..25c0984 100644 --- a/SparqlParser.g4 +++ b/SparqlParser.g4 @@ -80,7 +80,7 @@ askQuery ; // NOT PART OF THE OFFICIAL GRAMMAR (tentris specific) -// NOTE: The order matters (if there is a variableOrdering hint, it must be provided before the cardinalityEstimation hint +// NOTE: The order matters (if there is a variableOrdering hint, it must be provided before the cardinalityEstimation hint) hintsClause : variableOrdering? cardinalityEstimation? ;