Skip to content

Commit

Permalink
added rules for hinting the variable ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaralis committed Dec 11, 2023
1 parent 279294d commit 4a5d560
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
15 changes: 3 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions SparqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
20 changes: 17 additions & 3 deletions SparqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ subSelect
;

selectClause
: SELECT selectModifier? (selectVariables+ | '*')
: hintsClause SELECT selectModifier? (selectVariables+ | '*')
;

selectModifier
Expand All @@ -68,15 +68,29 @@ selectVariables
;

constructQuery
: CONSTRUCT (constructTemplate datasetClause* whereClause solutionModifier | datasetClause* WHERE '{' triplesTemplate? '}' solutionModifier)
: hintsClause CONSTRUCT (constructTemplate datasetClause* whereClause solutionModifier | datasetClause* WHERE '{' triplesTemplate? '}' solutionModifier)
;

describeQuery
: DESCRIBE (varOrIRI+ | '*') datasetClause* whereClause? solutionModifier
;

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
Expand Down

0 comments on commit 4a5d560

Please sign in to comment.