13#include "absl/status/status.h"
14#include "absl/status/statusor.h"
15#include "absl/strings/str_cat.h"
22constexpr int kMaxHops = 10;
24constexpr std::string_view kOperators[] = {
"=",
"<>",
"<",
"<=",
">",
">="};
26bool EqualsIgnoringCase(std::string_view a, std::string_view b) {
27 if (a.size() != b.size())
return false;
28 for (
size_t i = 0; i < a.size(); ++i) {
29 if (std::toupper(
static_cast<unsigned char>(a[i])) !=
30 std::toupper(
static_cast<unsigned char>(b[i]))) {
43 explicit Parser(std::vector<Token> tokens) : tokens_(std::move(tokens)) {}
46 const absl::Status& status()
const {
return status_; }
52 bool ParseQuery(Query& query) {
53 query.explain = Keyword(
"EXPLAIN");
54 if (!ExpectKeyword(
"MATCH"))
return false;
57 query.path = Next().text;
59 query.shortest = Keyword(
"shortestPath");
60 if (query.shortest && !ExpectSymbol(
"("))
return false;
63 if (!ParseNode(node))
return false;
64 query.nodes.push_back(node);
65 while (IsSymbol(
"-") || IsSymbol(
"<")) {
66 RelationshipPattern relationship;
67 if (!ParseRelationship(relationship) || !ParseNode(node))
return false;
68 query.relationships.push_back(relationship);
69 query.nodes.push_back(node);
71 if (query.shortest && !ExpectSymbol(
")"))
return false;
72 if (Keyword(
"WHERE")) {
74 Comparison comparison;
75 if (!ParseComparison(comparison))
return false;
76 query.where.push_back(std::move(comparison));
77 }
while (Keyword(
"AND"));
79 if (!ExpectKeyword(
"RETURN"))
return false;
80 query.distinct = Keyword(
"DISTINCT");
83 if (!ParseReturnItem(item))
return false;
84 query.returns.push_back(std::move(item));
85 }
while (Symbol(
","));
86 if (Peek().kind !=
TokenKind::kEnd)
return Fail(
"the end of the query");
91 bool ParsePropertiesAlone(std::vector<Property>&
properties) {
94 return Fail(
"the end of the properties");
101 bool ParseNode(NodePattern& node) {
103 if (!ExpectSymbol(
"("))
return false;
107 node.label = Next().text;
110 return ExpectSymbol(
")");
116 bool ParseRelationship(RelationshipPattern& relationship) {
118 if (!ExpectSymbol(
"-") || !ExpectSymbol(
"[") || !ExpectSymbol(
":")) {
125 relationship.types.push_back(Next().
text);
126 }
while (Symbol(
"|"));
128 const std::string&
text = Peek().text;
129 auto [end, failed] = std::from_chars(
130 text.data(),
text.data() +
text.size(), relationship.hops);
135 if (Keyword(
"WSHORTEST")) {
136 if (!ExpectSymbol(
"("))
return false;
138 relationship.weight = Next().text;
139 if (!ExpectSymbol(
")"))
return false;
141 }
else if (failed != std::errc() || relationship.hops < 1 ||
142 relationship.hops > kMaxHops) {
143 return Fail(
"a number of hops from 1 to " + std::to_string(kMaxHops));
148 if (!ExpectSymbol(
"]") || !ExpectSymbol(
"-"))
return false;
159 if (!ExpectSymbol(
"{"))
return false;
163 property.key = Next().text;
164 if (!ExpectSymbol(
":") || !ParseValue(property.value))
return false;
166 }
while (Symbol(
","));
167 return ExpectSymbol(
"}");
171 bool ParseValue(
Value& value) {
176 const std::string&
text = Peek().text;
179 std::from_chars(
text.data(),
text.data() +
text.size(), number);
181 return Fail(
"a string or a whole number");
189 bool ParseComparison(Comparison& comparison) {
190 if (!ParseLookup(comparison.property))
return false;
192 std::find(std::begin(kOperators), std::end(kOperators), Peek().
text) ==
193 std::end(kOperators)) {
194 return Fail(
"a comparison");
196 comparison.op = Next().text;
197 return ParseValue(comparison.value);
201 bool ParseLookup(PropertyLookup& lookup) {
203 lookup.variable = Next().text;
204 if (!ExpectSymbol(
"."))
return false;
206 lookup.key = Next().text;
211 bool ParseReturnItem(ReturnItem& item) {
213 if (Keyword(
"length")) {
214 item.function =
"length";
215 }
else if (Keyword(
"cost")) {
216 item.function =
"cost";
218 return Fail(
"length or cost");
220 if (!ExpectSymbol(
"("))
return false;
222 item.variable = Next().text;
223 return ExpectSymbol(
")");
226 item.variable = Next().text;
229 if (!Symbol(
"."))
return true;
231 item.key = Next().text;
235 const Token& Peek()
const {
return tokens_[next_]; }
236 const Token& Next() {
return tokens_[next_++]; }
238 bool IsSymbol(std::string_view symbol)
const {
return IsSymbolAt(0, symbol); }
241 bool IsSymbolAt(
size_t ahead, std::string_view symbol)
const {
242 const Token& token = tokens_[std::min(next_ + ahead, tokens_.size() - 1)];
246 bool Symbol(std::string_view symbol) {
247 if (!IsSymbol(symbol))
return false;
253 bool Keyword(std::string_view keyword) {
255 !EqualsIgnoringCase(Peek().
text, keyword)) {
261 bool ExpectSymbol(std::string_view symbol) {
262 return Symbol(symbol) || Fail(std::string(symbol));
264 bool ExpectKeyword(std::string_view keyword) {
265 return Keyword(keyword) || Fail(std::string(keyword));
269 bool Fail(
const std::string& expected) {
270 const Token& token = Peek();
271 status_ = absl::InvalidArgumentError(
272 absl::StrCat(
"expected ", expected,
" at ", token.at,
", not ",
277 std::vector<Token> tokens_;
279 absl::Status status_;
285 absl::StatusOr<std::vector<Token>> tokens =
Lex(
text);
286 if (!tokens.ok())
return tokens.status();
287 Parser parser(*std::move(tokens));
289 if (!parser.ParseQuery(query))
return parser.status();
294 absl::StatusOr<std::vector<Token>> tokens =
Lex(
text);
295 if (!tokens.ok())
return tokens.status();
296 Parser parser(*std::move(tokens));
298 if (!parser.ParsePropertiesAlone(
properties))
return parser.status();
std::vector< Property > properties
std::variant< int64_t, std::string > Value
A property's value, as the graph hands it out: a whole number or a string.
absl::StatusOr< Query > Parse(std::string_view text)
Reads a query.
absl::StatusOr< std::vector< Property > > ParseProperties(std::string_view text)
Reads properties as Cypher writes them, in braces, such as {legs: 4, diet: 'omnivore'},...
absl::StatusOr< std::vector< Token > > Lex(std::string_view query)
Splits a query into tokens, ending with a kEnd token.
constexpr int kAnyHops
The hops of a relationship written with a star and no number, such as -[:KNOWS*]-,...
A query in our subset of Cypher, such as MATCH (a {name: 'Red fox'})-[:EATS]->(b) WHERE b....