gqlparser 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gqlparser/__init__.py +23 -0
- gqlparser/__version__.py +3 -0
- gqlparser/ast.py +297 -0
- gqlparser/errors.py +24 -0
- gqlparser/lexer.py +271 -0
- gqlparser/parser.py +581 -0
- gqlparser/py.typed +0 -0
- gqlparser-0.0.1.dist-info/METADATA +192 -0
- gqlparser-0.0.1.dist-info/RECORD +12 -0
- gqlparser-0.0.1.dist-info/WHEEL +5 -0
- gqlparser-0.0.1.dist-info/licenses/LICENSE +201 -0
- gqlparser-0.0.1.dist-info/top_level.txt +1 -0
gqlparser/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""gqlparser: a pure-Python parser for GQL (ISO/IEC 39075)."""
|
|
2
|
+
|
|
3
|
+
from gqlparser.__version__ import __version__
|
|
4
|
+
from gqlparser.ast import Span
|
|
5
|
+
from gqlparser.errors import GqlError
|
|
6
|
+
from gqlparser.errors import GqlNotSupportedError
|
|
7
|
+
from gqlparser.errors import GqlSyntaxError
|
|
8
|
+
from gqlparser.lexer import Token
|
|
9
|
+
from gqlparser.lexer import TokenKind
|
|
10
|
+
from gqlparser.lexer import tokenize
|
|
11
|
+
from gqlparser.parser import parse
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"GqlError",
|
|
15
|
+
"GqlNotSupportedError",
|
|
16
|
+
"GqlSyntaxError",
|
|
17
|
+
"Span",
|
|
18
|
+
"Token",
|
|
19
|
+
"TokenKind",
|
|
20
|
+
"__version__",
|
|
21
|
+
"parse",
|
|
22
|
+
"tokenize",
|
|
23
|
+
]
|
gqlparser/__version__.py
ADDED
gqlparser/ast.py
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"""Syntax tree nodes. Public API: every change is listed in CHANGELOG.md.
|
|
2
|
+
|
|
3
|
+
Every node is a frozen dataclass whose last field is its `span`. Numbers keep
|
|
4
|
+
their source text (`IntegerLiteral.text`), so the tree never rounds or
|
|
5
|
+
converts a value: that is the consumer's decision.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from enum import Enum
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True, slots=True)
|
|
15
|
+
class Span:
|
|
16
|
+
"""Where a node sits in the source text.
|
|
17
|
+
|
|
18
|
+
`start` and `end` are character offsets (end exclusive); `line` and
|
|
19
|
+
`column` are 1-based and describe `start`.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
start: int
|
|
23
|
+
end: int
|
|
24
|
+
line: int
|
|
25
|
+
column: int
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# --- names --------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True, slots=True)
|
|
32
|
+
class Identifier:
|
|
33
|
+
"""A name as written: a graph, label, property, variable or alias."""
|
|
34
|
+
|
|
35
|
+
name: str
|
|
36
|
+
span: Span
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# --- expressions ---------------------------------------------------------------
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True, slots=True)
|
|
43
|
+
class IntegerLiteral:
|
|
44
|
+
text: str
|
|
45
|
+
span: Span
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass(frozen=True, slots=True)
|
|
49
|
+
class ExactNumberLiteral:
|
|
50
|
+
"""`1.5`, `.5`, `1.`"""
|
|
51
|
+
|
|
52
|
+
text: str
|
|
53
|
+
span: Span
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True, slots=True)
|
|
57
|
+
class ApproximateNumberLiteral:
|
|
58
|
+
"""`1e3`, `1.5E-2`"""
|
|
59
|
+
|
|
60
|
+
text: str
|
|
61
|
+
span: Span
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(frozen=True, slots=True)
|
|
65
|
+
class StringLiteral:
|
|
66
|
+
value: str
|
|
67
|
+
span: Span
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(frozen=True, slots=True)
|
|
71
|
+
class BooleanLiteral:
|
|
72
|
+
value: bool
|
|
73
|
+
span: Span
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass(frozen=True, slots=True)
|
|
77
|
+
class NullLiteral:
|
|
78
|
+
span: Span
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@dataclass(frozen=True, slots=True)
|
|
82
|
+
class TemporalLiteral:
|
|
83
|
+
"""`DATE '2026-01-01'`, `DURATION 'P30D'`: `type_name` is upper case."""
|
|
84
|
+
|
|
85
|
+
type_name: str
|
|
86
|
+
value: str
|
|
87
|
+
span: Span
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@dataclass(frozen=True, slots=True)
|
|
91
|
+
class CurrentTemporal:
|
|
92
|
+
"""`CURRENT_DATE`, `CURRENT_TIME`, `CURRENT_TIMESTAMP`: `name` is upper case."""
|
|
93
|
+
|
|
94
|
+
name: str
|
|
95
|
+
span: Span
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@dataclass(frozen=True, slots=True)
|
|
99
|
+
class Parameter:
|
|
100
|
+
"""`$name`; `name` excludes the `$`."""
|
|
101
|
+
|
|
102
|
+
name: str
|
|
103
|
+
span: Span
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@dataclass(frozen=True, slots=True)
|
|
107
|
+
class Variable:
|
|
108
|
+
"""A reference to a variable bound by a pattern, or to a `RETURN` alias."""
|
|
109
|
+
|
|
110
|
+
name: str
|
|
111
|
+
span: Span
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@dataclass(frozen=True, slots=True)
|
|
115
|
+
class PropertyReference:
|
|
116
|
+
"""`subject.property`"""
|
|
117
|
+
|
|
118
|
+
subject: Expression
|
|
119
|
+
property: Identifier
|
|
120
|
+
span: Span
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@dataclass(frozen=True, slots=True)
|
|
124
|
+
class UnaryOperation:
|
|
125
|
+
"""`operator` is `NOT`, `-` or `+`."""
|
|
126
|
+
|
|
127
|
+
operator: str
|
|
128
|
+
operand: Expression
|
|
129
|
+
span: Span
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
@dataclass(frozen=True, slots=True)
|
|
133
|
+
class BinaryOperation:
|
|
134
|
+
"""`operator` is one of `OR XOR AND = <> < <= > >= || + - * / %` (words upper case)."""
|
|
135
|
+
|
|
136
|
+
operator: str
|
|
137
|
+
left: Expression
|
|
138
|
+
right: Expression
|
|
139
|
+
span: Span
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@dataclass(frozen=True, slots=True)
|
|
143
|
+
class IsNull:
|
|
144
|
+
"""`operand IS NULL`, or `operand IS NOT NULL` when `negated`."""
|
|
145
|
+
|
|
146
|
+
operand: Expression
|
|
147
|
+
negated: bool
|
|
148
|
+
span: Span
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
@dataclass(frozen=True, slots=True)
|
|
152
|
+
class FunctionCall:
|
|
153
|
+
"""`name(arguments)`. `name` is as written; the parser does not check it
|
|
154
|
+
against GQL's built-in functions, and allows `DISTINCT` on any call."""
|
|
155
|
+
|
|
156
|
+
name: str
|
|
157
|
+
arguments: tuple[Expression, ...]
|
|
158
|
+
distinct: bool
|
|
159
|
+
span: Span
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
@dataclass(frozen=True, slots=True)
|
|
163
|
+
class CountStar:
|
|
164
|
+
"""`COUNT(*)`"""
|
|
165
|
+
|
|
166
|
+
span: Span
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@dataclass(frozen=True, slots=True)
|
|
170
|
+
class WhenClause:
|
|
171
|
+
"""In a simple `CASE`, `condition` is the value compared with the operand."""
|
|
172
|
+
|
|
173
|
+
condition: Expression
|
|
174
|
+
result: Expression
|
|
175
|
+
span: Span
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
@dataclass(frozen=True, slots=True)
|
|
179
|
+
class Case:
|
|
180
|
+
"""Simple `CASE x WHEN ...` when `operand` is set, searched `CASE WHEN ...` otherwise."""
|
|
181
|
+
|
|
182
|
+
operand: Expression | None
|
|
183
|
+
when_clauses: tuple[WhenClause, ...]
|
|
184
|
+
else_result: Expression | None
|
|
185
|
+
span: Span
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
Expression = (
|
|
189
|
+
IntegerLiteral
|
|
190
|
+
| ExactNumberLiteral
|
|
191
|
+
| ApproximateNumberLiteral
|
|
192
|
+
| StringLiteral
|
|
193
|
+
| BooleanLiteral
|
|
194
|
+
| NullLiteral
|
|
195
|
+
| TemporalLiteral
|
|
196
|
+
| CurrentTemporal
|
|
197
|
+
| Parameter
|
|
198
|
+
| Variable
|
|
199
|
+
| PropertyReference
|
|
200
|
+
| UnaryOperation
|
|
201
|
+
| BinaryOperation
|
|
202
|
+
| IsNull
|
|
203
|
+
| FunctionCall
|
|
204
|
+
| CountStar
|
|
205
|
+
| Case
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
# --- patterns -----------------------------------------------------------------
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
class EdgeDirection(Enum):
|
|
213
|
+
POINTING_RIGHT = "pointing_right" # (a)-[e]->(b)
|
|
214
|
+
POINTING_LEFT = "pointing_left" # (a)<-[e]-(b)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
@dataclass(frozen=True, slots=True)
|
|
218
|
+
class NodePattern:
|
|
219
|
+
"""`(variable:label WHERE where)`; each part optional."""
|
|
220
|
+
|
|
221
|
+
variable: Identifier | None
|
|
222
|
+
label: Identifier | None
|
|
223
|
+
where: Expression | None
|
|
224
|
+
span: Span
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
@dataclass(frozen=True, slots=True)
|
|
228
|
+
class EdgePattern:
|
|
229
|
+
"""`-[variable:label WHERE where]->` or `<-[...]-`; each part optional."""
|
|
230
|
+
|
|
231
|
+
variable: Identifier | None
|
|
232
|
+
label: Identifier | None
|
|
233
|
+
where: Expression | None
|
|
234
|
+
direction: EdgeDirection
|
|
235
|
+
span: Span
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
@dataclass(frozen=True, slots=True)
|
|
239
|
+
class PathPattern:
|
|
240
|
+
"""Alternating node and edge patterns, starting and ending with a node."""
|
|
241
|
+
|
|
242
|
+
elements: tuple[NodePattern | EdgePattern, ...]
|
|
243
|
+
span: Span
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
# --- statements ----------------------------------------------------------------
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
@dataclass(frozen=True, slots=True)
|
|
250
|
+
class MatchStatement:
|
|
251
|
+
"""`[OPTIONAL] MATCH path, path, ... [WHERE where]`"""
|
|
252
|
+
|
|
253
|
+
optional: bool
|
|
254
|
+
paths: tuple[PathPattern, ...]
|
|
255
|
+
where: Expression | None
|
|
256
|
+
span: Span
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
@dataclass(frozen=True, slots=True)
|
|
260
|
+
class ReturnItem:
|
|
261
|
+
expression: Expression
|
|
262
|
+
alias: Identifier | None
|
|
263
|
+
span: Span
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
@dataclass(frozen=True, slots=True)
|
|
267
|
+
class SortSpecification:
|
|
268
|
+
expression: Expression
|
|
269
|
+
descending: bool
|
|
270
|
+
span: Span
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
@dataclass(frozen=True, slots=True)
|
|
274
|
+
class ReturnStatement:
|
|
275
|
+
"""`RETURN [DISTINCT] items [GROUP BY ...] [ORDER BY ...] [OFFSET n] [LIMIT n]`
|
|
276
|
+
|
|
277
|
+
`offset` and `limit` are an `IntegerLiteral` or a `Parameter`. `SKIP` is
|
|
278
|
+
read as `OFFSET`.
|
|
279
|
+
"""
|
|
280
|
+
|
|
281
|
+
distinct: bool
|
|
282
|
+
items: tuple[ReturnItem, ...]
|
|
283
|
+
group_by: tuple[Identifier, ...]
|
|
284
|
+
order_by: tuple[SortSpecification, ...]
|
|
285
|
+
offset: IntegerLiteral | Parameter | None
|
|
286
|
+
limit: IntegerLiteral | Parameter | None
|
|
287
|
+
span: Span
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
@dataclass(frozen=True, slots=True)
|
|
291
|
+
class Query:
|
|
292
|
+
"""`[USE graph] match_statements RETURN ...`"""
|
|
293
|
+
|
|
294
|
+
graph: Identifier | None
|
|
295
|
+
match_statements: tuple[MatchStatement, ...]
|
|
296
|
+
return_statement: ReturnStatement
|
|
297
|
+
span: Span
|
gqlparser/errors.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""The two errors gqlparser raises.
|
|
2
|
+
|
|
3
|
+
They are siblings, not parent and child: "not valid GQL" and "valid GQL this
|
|
4
|
+
release does not support" are different answers, and a caller must be able to
|
|
5
|
+
tell them apart without inspecting the message.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class GqlError(Exception):
|
|
10
|
+
"""Base for gqlparser errors. Never raised itself."""
|
|
11
|
+
|
|
12
|
+
def __init__(self, message: str, line: int, column: int):
|
|
13
|
+
super().__init__(f"{message} (line {line}, column {column})")
|
|
14
|
+
self.message = message
|
|
15
|
+
self.line = line
|
|
16
|
+
self.column = column
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class GqlSyntaxError(GqlError):
|
|
20
|
+
"""The text is not valid GQL."""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class GqlNotSupportedError(GqlError):
|
|
24
|
+
"""The text uses a construct outside the subset this release supports."""
|
gqlparser/lexer.py
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"""GQL lexer: text to tokens, each with its source position.
|
|
2
|
+
|
|
3
|
+
The lexer knows GQL's lexical elements, not its grammar, so it leaves two
|
|
4
|
+
questions to the parser:
|
|
5
|
+
|
|
6
|
+
- whether a word is a keyword: keywords are case-insensitive, and the parser
|
|
7
|
+
knows which word it expects where;
|
|
8
|
+
- what double-quoted text means: GQL uses it for both strings and delimited
|
|
9
|
+
identifiers, so the token records only the quote form it saw.
|
|
10
|
+
|
|
11
|
+
Anything the lexer does not handle yet raises `GqlNotSupportedError`. It never
|
|
12
|
+
skips text it does not understand.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
from enum import Enum
|
|
17
|
+
|
|
18
|
+
from gqlparser.ast import Span
|
|
19
|
+
from gqlparser.errors import GqlNotSupportedError
|
|
20
|
+
from gqlparser.errors import GqlSyntaxError
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class TokenKind(Enum):
|
|
24
|
+
WORD = "word" # a regular identifier or a keyword, as written
|
|
25
|
+
SINGLE_QUOTED = "single_quoted" # '...'
|
|
26
|
+
DOUBLE_QUOTED = "double_quoted" # "...": a string or an identifier, by context
|
|
27
|
+
ACCENT_QUOTED = "accent_quoted" # `...`
|
|
28
|
+
INTEGER = "integer" # 42
|
|
29
|
+
EXACT_NUMBER = "exact_number" # 1.5 .5 1.
|
|
30
|
+
APPROXIMATE_NUMBER = "approximate_number" # 1e3 1.5E-2
|
|
31
|
+
PARAMETER = "parameter" # $name
|
|
32
|
+
PUNCTUATION = "punctuation" # ( -> <= ...
|
|
33
|
+
END = "end" # end of input
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True, slots=True)
|
|
37
|
+
class Token:
|
|
38
|
+
"""One token.
|
|
39
|
+
|
|
40
|
+
`value` is decoded: the word as written, quoted text with its doubled
|
|
41
|
+
quotes collapsed, the number's digits, a parameter's name without the `$`,
|
|
42
|
+
or the punctuation itself. `span` covers the token's raw source text.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
kind: TokenKind
|
|
46
|
+
value: str
|
|
47
|
+
span: Span
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
_DIGITS = "0123456789"
|
|
51
|
+
_LINE_BREAKS = "\n\r\u0085
"
|
|
52
|
+
_PUNCTUATION_2 = ("<=", ">=", "<>", "||", "->", "<-")
|
|
53
|
+
_PUNCTUATION_1 = "()[]{},.:;=<>+-*/%|&!?~^"
|
|
54
|
+
_QUOTES = {
|
|
55
|
+
"'": (TokenKind.SINGLE_QUOTED, "single-quoted text"),
|
|
56
|
+
'"': (TokenKind.DOUBLE_QUOTED, "double-quoted text"),
|
|
57
|
+
"`": (TokenKind.ACCENT_QUOTED, "accent-quoted text"),
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _is_identifier_start(ch: str) -> bool:
|
|
62
|
+
# shortcut: Python's identifier rules (XID_Start, plus `_`), so a GQL identifier
|
|
63
|
+
# starting with connector punctuation other than `_` (e.g. U+203F) is refused;
|
|
64
|
+
# upgrade if anyone writes one.
|
|
65
|
+
return ch.isidentifier()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _is_identifier_part(ch: str) -> bool:
|
|
69
|
+
return ("a" + ch).isidentifier()
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def tokenize(text: str) -> list[Token]:
|
|
73
|
+
"""Return the tokens of `text`, ending with a single END token."""
|
|
74
|
+
return _Lexer(text).run()
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class _Lexer:
|
|
78
|
+
__slots__ = ("text", "pos", "line", "column", "tokens")
|
|
79
|
+
|
|
80
|
+
def __init__(self, text: str):
|
|
81
|
+
self.text = text
|
|
82
|
+
self.pos = 0
|
|
83
|
+
self.line = 1
|
|
84
|
+
self.column = 1
|
|
85
|
+
self.tokens: list[Token] = []
|
|
86
|
+
|
|
87
|
+
def run(self) -> list[Token]:
|
|
88
|
+
text = self.text
|
|
89
|
+
while True:
|
|
90
|
+
self._skip_whitespace_and_comments()
|
|
91
|
+
if self.pos >= len(text):
|
|
92
|
+
end = len(text)
|
|
93
|
+
self.tokens.append(Token(TokenKind.END, "", Span(end, end, self.line, self.column)))
|
|
94
|
+
return self.tokens
|
|
95
|
+
|
|
96
|
+
ch = text[self.pos]
|
|
97
|
+
following = text[self.pos + 1] if self.pos + 1 < len(text) else ""
|
|
98
|
+
|
|
99
|
+
if ch in _QUOTES:
|
|
100
|
+
self._quoted(ch)
|
|
101
|
+
elif ch in _DIGITS or (ch == "." and following in _DIGITS and following):
|
|
102
|
+
self._number()
|
|
103
|
+
elif ch == "$":
|
|
104
|
+
self._parameter()
|
|
105
|
+
elif ch == "@" and following in _QUOTES:
|
|
106
|
+
raise GqlNotSupportedError(
|
|
107
|
+
"the no-escape prefix `@` on quoted text is not supported yet",
|
|
108
|
+
self.line,
|
|
109
|
+
self.column,
|
|
110
|
+
)
|
|
111
|
+
elif _is_identifier_start(ch):
|
|
112
|
+
self._word()
|
|
113
|
+
elif text.startswith(_PUNCTUATION_2, self.pos):
|
|
114
|
+
self._punctuation(2)
|
|
115
|
+
elif ch in _PUNCTUATION_1:
|
|
116
|
+
self._punctuation(1)
|
|
117
|
+
else:
|
|
118
|
+
raise GqlSyntaxError(f"unexpected character {ch!r}", self.line, self.column)
|
|
119
|
+
|
|
120
|
+
def _advance(self, count: int) -> None:
|
|
121
|
+
"""Move forward `count` characters, keeping line and column current."""
|
|
122
|
+
text = self.text
|
|
123
|
+
end = self.pos + count
|
|
124
|
+
while self.pos < end:
|
|
125
|
+
ch = text[self.pos]
|
|
126
|
+
self.pos += 1
|
|
127
|
+
if ch == "\r" and self.pos < len(text) and text[self.pos] == "\n":
|
|
128
|
+
continue # \r\n is one line break: the \n completes it
|
|
129
|
+
if ch in _LINE_BREAKS:
|
|
130
|
+
self.line += 1
|
|
131
|
+
self.column = 1
|
|
132
|
+
else:
|
|
133
|
+
self.column += 1
|
|
134
|
+
|
|
135
|
+
def _emit(self, kind: TokenKind, value: str, start: int, line: int, column: int) -> None:
|
|
136
|
+
self.tokens.append(Token(kind, value, Span(start, self.pos, line, column)))
|
|
137
|
+
|
|
138
|
+
def _skip_whitespace_and_comments(self) -> None:
|
|
139
|
+
text = self.text
|
|
140
|
+
while self.pos < len(text):
|
|
141
|
+
ch = text[self.pos]
|
|
142
|
+
if ch.isspace():
|
|
143
|
+
self._advance(1)
|
|
144
|
+
elif text.startswith("//", self.pos):
|
|
145
|
+
end = self.pos
|
|
146
|
+
while end < len(text) and text[end] not in _LINE_BREAKS:
|
|
147
|
+
end += 1
|
|
148
|
+
self._advance(end - self.pos)
|
|
149
|
+
elif text.startswith("/*", self.pos):
|
|
150
|
+
close = text.find("*/", self.pos + 2)
|
|
151
|
+
if close < 0:
|
|
152
|
+
raise GqlSyntaxError("unterminated `/*` comment", self.line, self.column)
|
|
153
|
+
self._advance(close + 2 - self.pos)
|
|
154
|
+
elif text.startswith("--", self.pos):
|
|
155
|
+
# GQL reads `--` as a comment, so Cypher's `-->` would silently drop the
|
|
156
|
+
# rest of the line. Refused until supported deliberately.
|
|
157
|
+
raise GqlNotSupportedError(
|
|
158
|
+
"`--` comments are not supported yet; use `//` or `/* */`. "
|
|
159
|
+
"(An edge is written `->` or `-[...]->`, not `-->`.)",
|
|
160
|
+
self.line,
|
|
161
|
+
self.column,
|
|
162
|
+
)
|
|
163
|
+
else:
|
|
164
|
+
return
|
|
165
|
+
|
|
166
|
+
def _quoted(self, quote: str) -> None:
|
|
167
|
+
kind, description = _QUOTES[quote]
|
|
168
|
+
start, line, column = self.pos, self.line, self.column
|
|
169
|
+
text = self.text
|
|
170
|
+
chars = []
|
|
171
|
+
i = self.pos + 1
|
|
172
|
+
while True:
|
|
173
|
+
if i >= len(text):
|
|
174
|
+
raise GqlSyntaxError(f"unterminated {description}", line, column)
|
|
175
|
+
ch = text[i]
|
|
176
|
+
if ch == quote:
|
|
177
|
+
if i + 1 < len(text) and text[i + 1] == quote:
|
|
178
|
+
chars.append(quote) # a doubled quote is one literal quote
|
|
179
|
+
i += 2
|
|
180
|
+
continue
|
|
181
|
+
i += 1
|
|
182
|
+
break
|
|
183
|
+
if ch == "\\":
|
|
184
|
+
self._advance(i - self.pos)
|
|
185
|
+
raise GqlNotSupportedError(
|
|
186
|
+
f"backslash escapes in {description} are not supported yet",
|
|
187
|
+
self.line,
|
|
188
|
+
self.column,
|
|
189
|
+
)
|
|
190
|
+
chars.append(ch)
|
|
191
|
+
i += 1
|
|
192
|
+
self._advance(i - self.pos)
|
|
193
|
+
self._emit(kind, "".join(chars), start, line, column)
|
|
194
|
+
|
|
195
|
+
def _digits_end(self, i: int) -> int:
|
|
196
|
+
text = self.text
|
|
197
|
+
while i < len(text) and text[i] in _DIGITS:
|
|
198
|
+
i += 1
|
|
199
|
+
return i
|
|
200
|
+
|
|
201
|
+
def _number(self) -> None:
|
|
202
|
+
start, line, column = self.pos, self.line, self.column
|
|
203
|
+
text = self.text
|
|
204
|
+
kind = TokenKind.INTEGER
|
|
205
|
+
i = self._digits_end(self.pos)
|
|
206
|
+
if i < len(text) and text[i] == ".":
|
|
207
|
+
kind = TokenKind.EXACT_NUMBER
|
|
208
|
+
i = self._digits_end(i + 1)
|
|
209
|
+
if i < len(text) and text[i] in "eE":
|
|
210
|
+
j = i + 1
|
|
211
|
+
if j < len(text) and text[j] in "+-":
|
|
212
|
+
j += 1
|
|
213
|
+
if j < len(text) and text[j] in _DIGITS:
|
|
214
|
+
kind = TokenKind.APPROXIMATE_NUMBER
|
|
215
|
+
i = self._digits_end(j)
|
|
216
|
+
if i < len(text) and _is_identifier_part(text[i]):
|
|
217
|
+
self._refuse_number_tail(text[start:i], i)
|
|
218
|
+
self._advance(i - self.pos)
|
|
219
|
+
self._emit(kind, text[start:i], start, line, column)
|
|
220
|
+
|
|
221
|
+
def _refuse_number_tail(self, number: str, i: int) -> None:
|
|
222
|
+
"""A number runs straight into identifier characters: say why it is refused."""
|
|
223
|
+
text = self.text
|
|
224
|
+
tail = text[i]
|
|
225
|
+
tail_is_one_letter = i + 1 >= len(text) or not _is_identifier_part(text[i + 1])
|
|
226
|
+
self._advance(i - self.pos)
|
|
227
|
+
if tail == "_":
|
|
228
|
+
reason = "digit separators (`_`) in numbers are not supported yet"
|
|
229
|
+
elif number == "0" and tail in "xXoObB":
|
|
230
|
+
reason = "hexadecimal, octal and binary numbers are not supported yet"
|
|
231
|
+
elif tail in "mMfFdD" and tail_is_one_letter:
|
|
232
|
+
reason = f"the number suffix `{tail}` is not supported yet"
|
|
233
|
+
else:
|
|
234
|
+
raise GqlSyntaxError(
|
|
235
|
+
f"a number cannot run straight into {tail!r}", self.line, self.column
|
|
236
|
+
)
|
|
237
|
+
raise GqlNotSupportedError(reason, self.line, self.column)
|
|
238
|
+
|
|
239
|
+
def _parameter(self) -> None:
|
|
240
|
+
start, line, column = self.pos, self.line, self.column
|
|
241
|
+
text = self.text
|
|
242
|
+
i = self.pos + 1
|
|
243
|
+
if i < len(text) and text[i] == "$":
|
|
244
|
+
raise GqlNotSupportedError(
|
|
245
|
+
"substituted parameters (`$$name`) are not supported yet", line, column
|
|
246
|
+
)
|
|
247
|
+
if i < len(text) and text[i] in _QUOTES:
|
|
248
|
+
raise GqlNotSupportedError(
|
|
249
|
+
"quoted parameter names are not supported yet", line, column
|
|
250
|
+
)
|
|
251
|
+
if i >= len(text) or not _is_identifier_start(text[i]):
|
|
252
|
+
raise GqlSyntaxError("expected a parameter name after `$`", line, column)
|
|
253
|
+
j = i + 1
|
|
254
|
+
while j < len(text) and _is_identifier_part(text[j]):
|
|
255
|
+
j += 1
|
|
256
|
+
self._advance(j - self.pos)
|
|
257
|
+
self._emit(TokenKind.PARAMETER, text[i:j], start, line, column)
|
|
258
|
+
|
|
259
|
+
def _word(self) -> None:
|
|
260
|
+
start, line, column = self.pos, self.line, self.column
|
|
261
|
+
text = self.text
|
|
262
|
+
i = self.pos + 1
|
|
263
|
+
while i < len(text) and _is_identifier_part(text[i]):
|
|
264
|
+
i += 1
|
|
265
|
+
self._advance(i - self.pos)
|
|
266
|
+
self._emit(TokenKind.WORD, text[start:i], start, line, column)
|
|
267
|
+
|
|
268
|
+
def _punctuation(self, length: int) -> None:
|
|
269
|
+
start, line, column = self.pos, self.line, self.column
|
|
270
|
+
self._advance(length)
|
|
271
|
+
self._emit(TokenKind.PUNCTUATION, self.text[start : self.pos], start, line, column)
|