Skip to content

Commit

Permalink
Add support for prefixes in CONSTRUCT queries (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTF committed Jan 3, 2022
1 parent 1b5aff9 commit 4c16ab5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
7 changes: 6 additions & 1 deletion src/parser/SparqlParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ ParsedQuery SparqlParser::parse() {
void SparqlParser::parseQuery(ParsedQuery* query, QueryType queryType) {
if (queryType == CONSTRUCT_QUERY) {
auto str = _lexer.getUnconsumedInput();
auto parseResult = sparqlParserHelpers::parseConstructTemplate(str);
SparqlQleverVisitor::PrefixMap prefixes;
for (const auto& prefix : query->_prefixes) {
prefixes[prefix._prefix] = prefix._uri;
}
auto parseResult =
sparqlParserHelpers::parseConstructTemplate(str, std::move(prefixes));
query->_clause = std::move(parseResult._resultOfParse);
_lexer.reset(std::move(parseResult._remainingText));
_lexer.expect("where");
Expand Down
14 changes: 8 additions & 6 deletions src/parser/SparqlParserHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "SparqlParserHelpers.h"

#include "../util/antlr/ThrowingErrorStrategy.h"
#include "sparqlParser/SparqlQleverVisitor.h"
#include "sparqlParser/generated/SparqlAutomaticLexer.h"

namespace sparqlParserHelpers {
Expand All @@ -21,7 +20,9 @@ struct ParserAndVisitor {
public:
SparqlAutomaticParser _parser{&_tokens};
SparqlQleverVisitor _visitor;
explicit ParserAndVisitor(string input) : _input{std::move(input)} {
explicit ParserAndVisitor(string input,
SparqlQleverVisitor::PrefixMap prefixes)
: _input{std::move(input)}, _visitor{std::move(prefixes)} {
_parser.setErrorHandler(std::make_shared<ThrowingErrorStrategy>());
}

Expand All @@ -46,7 +47,7 @@ struct ParserAndVisitor {
// ____________________________________________________________________________
ResultOfParseAndRemainingText<sparqlExpression::SparqlExpressionPimpl>
parseExpression(const std::string& input) {
ParserAndVisitor p{input};
ParserAndVisitor p{input, {}};
auto resultOfParseAndRemainingText =
p.parse<sparqlExpression::SparqlExpression::Ptr>(
input, "expression", &SparqlAutomaticParser::expression);
Expand All @@ -60,15 +61,16 @@ parseExpression(const std::string& input) {
// ____________________________________________________________________________
ResultOfParseAndRemainingText<ParsedQuery::Alias> parseAlias(
const std::string& input) {
ParserAndVisitor p{input};
ParserAndVisitor p{input, {}};
return p.parse<ParsedQuery::Alias>(
input, "alias", &SparqlAutomaticParser::aliasWithouBrackes);
}
// _____________________________________________________________________________

ResultOfParseAndRemainingText<std::vector<std::array<VarOrTerm, 3>>>
parseConstructTemplate(const std::string& input) {
ParserAndVisitor p{input};
parseConstructTemplate(const std::string& input,
SparqlQleverVisitor::PrefixMap prefixes) {
ParserAndVisitor p{input, std::move(prefixes)};
return p.parse<std::vector<std::array<VarOrTerm, 3>>>(
input, "construct template", &SparqlAutomaticParser::constructTemplate);
}
Expand Down
4 changes: 3 additions & 1 deletion src/parser/SparqlParserHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "../engine/sparqlExpressions/SparqlExpressionPimpl.h"
#include "./ParsedQuery.h"
#include "sparqlParser/SparqlQleverVisitor.h"

namespace sparqlParserHelpers {

Expand All @@ -32,7 +33,8 @@ ResultOfParseAndRemainingText<ParsedQuery::Alias> parseAlias(
const std::string& input);

ResultOfParseAndRemainingText<std::vector<std::array<VarOrTerm, 3>>>
parseConstructTemplate(const std::string& input);
parseConstructTemplate(const std::string& input,
SparqlQleverVisitor::PrefixMap prefixes);
} // namespace sparqlParserHelpers

#endif // QLEVER_SPARQLPARSERHELPERS_H
13 changes: 8 additions & 5 deletions src/parser/sparqlParser/SparqlQleverVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class SparqlQleverVisitor : public SparqlAutomaticVisitor {
PrefixMap& prefixMap() { return _prefixMap; }
FRIEND_TEST(SparqlParser, Prefix);

PrefixMap _prefixMap{{":", "<>"}};
PrefixMap _prefixMap{{"", "<>"}};

template <typename T>
void appendVector(std::vector<T>& destination, std::vector<T>&& source) {
Expand Down Expand Up @@ -125,14 +125,16 @@ class SparqlQleverVisitor : public SparqlAutomaticVisitor {
// ___________________________________________________________________________
antlrcpp::Any visitBaseDecl(
SparqlAutomaticParser::BaseDeclContext* ctx) override {
_prefixMap[":"] = visitIriref(ctx->iriref()).as<string>();
_prefixMap[""] = visitIriref(ctx->iriref()).as<string>();
return nullptr;
}

// ___________________________________________________________________________
antlrcpp::Any visitPrefixDecl(
SparqlAutomaticParser::PrefixDeclContext* ctx) override {
_prefixMap[ctx->PNAME_NS()->getText()] =
auto text = ctx->PNAME_NS()->getText();
// Strip trailing ':'.
_prefixMap[text.substr(0, text.length() - 1)] =
visitIriref(ctx->iriref()).as<string>();
return nullptr;
}
Expand Down Expand Up @@ -1172,7 +1174,7 @@ class SparqlQleverVisitor : public SparqlAutomaticVisitor {
SparqlAutomaticParser::PnameLnContext* ctx) override {
string text = ctx->getText();
auto pos = text.find(':');
auto pnameNS = text.substr(0, pos + 1);
auto pnameNS = text.substr(0, pos);
auto pnLocal = text.substr(pos + 1);
if (!_prefixMap.contains(pnameNS)) {
// TODO<joka921> : proper name
Expand All @@ -1189,7 +1191,8 @@ class SparqlQleverVisitor : public SparqlAutomaticVisitor {

antlrcpp::Any visitPnameNs(
SparqlAutomaticParser::PnameNsContext* ctx) override {
auto prefix = ctx->getText();
auto text = ctx->getText();
auto prefix = text.substr(0, text.length() - 1);
if (!_prefixMap.contains(prefix)) {
// TODO<joka921> : proper name
throw SparqlParseException{
Expand Down
12 changes: 6 additions & 6 deletions test/SparqlAntlrParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ TEST(SparqlParser, Prefix) {
p.visitor.visitPrefixDecl(context);
const auto& m = p.visitor.prefixMap();
ASSERT_EQ(2ul, m.size());
ASSERT_TRUE(m.at("wd:") == "<www.wikidata.org/>");
ASSERT_EQ(m.at(":"), "<>");
ASSERT_TRUE(m.at("wd") == "<www.wikidata.org/>");
ASSERT_EQ(m.at(""), "<>");
}
{
string s = "wd:bimbam";
ParserAndVisitor p{s};
auto& m = p.visitor.prefixMap();
m["wd:"] = "<www.wikidata.org/>";
m["wd"] = "<www.wikidata.org/>";

auto context = p.parser.pnameLn();
auto result = p.visitor.visitPnameLn(context).as<string>();
Expand All @@ -75,7 +75,7 @@ TEST(SparqlParser, Prefix) {
string s = "wd:";
ParserAndVisitor p{s};
auto& m = p.visitor.prefixMap();
m["wd:"] = "<www.wikidata.org/>";
m["wd"] = "<www.wikidata.org/>";

auto context = p.parser.pnameNs();
auto result = p.visitor.visitPnameNs(context).as<string>();
Expand All @@ -85,7 +85,7 @@ TEST(SparqlParser, Prefix) {
string s = "wd:bimbam";
ParserAndVisitor p{s};
auto& m = p.visitor.prefixMap();
m["wd:"] = "<www.wikidata.org/>";
m["wd"] = "<www.wikidata.org/>";

auto context = p.parser.prefixedName();
auto result = p.visitor.visitPrefixedName(context).as<string>();
Expand All @@ -95,7 +95,7 @@ TEST(SparqlParser, Prefix) {
string s = "<somethingsomething> <rest>";
ParserAndVisitor p{s};
auto& m = p.visitor.prefixMap();
m["wd:"] = "<www.wikidata.org/>";
m["wd"] = "<www.wikidata.org/>";

auto context = p.parser.iriref();
auto result = p.visitor.visitIriref(context).as<string>();
Expand Down

0 comments on commit 4c16ab5

Please sign in to comment.