syncraft 0.1.16__tar.gz → 0.1.18__tar.gz
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.
Potentially problematic release.
This version of syncraft might be problematic. Click here for more details.
- {syncraft-0.1.16 → syncraft-0.1.18}/PKG-INFO +1 -1
- {syncraft-0.1.16 → syncraft-0.1.18}/pyproject.toml +1 -1
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft/ast.py +2 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft/generator.py +4 -1
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft.egg-info/PKG-INFO +1 -1
- {syncraft-0.1.16 → syncraft-0.1.18}/tests/test_bimap.py +108 -1
- {syncraft-0.1.16 → syncraft-0.1.18}/LICENSE +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/README.md +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/setup.cfg +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft/__init__.py +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft/algebra.py +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft/cmd.py +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft/diagnostic.py +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft/dsl.py +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft/parser.py +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft/py.typed +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft/sqlite3.py +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft.egg-info/SOURCES.txt +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft.egg-info/dependency_links.txt +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft.egg-info/requires.txt +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/syncraft.egg-info/top_level.txt +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/tests/test_parse.py +0 -0
- {syncraft-0.1.16 → syncraft-0.1.18}/tests/test_until.py +0 -0
|
@@ -111,6 +111,7 @@ def token_type_from_string(token_type: Optional[TokenType], text: str, case_sens
|
|
|
111
111
|
|
|
112
112
|
@dataclass(frozen=True)
|
|
113
113
|
class TokenGen(TokenSpec):
|
|
114
|
+
|
|
114
115
|
def __str__(self) -> str:
|
|
115
116
|
tt = self.token_type.name if self.token_type else ""
|
|
116
117
|
txt = self.text if self.text else ""
|
|
@@ -141,7 +142,9 @@ class TokenGen(TokenSpec):
|
|
|
141
142
|
self.case_sensitive),
|
|
142
143
|
text=text)
|
|
143
144
|
|
|
144
|
-
|
|
145
|
+
@staticmethod
|
|
146
|
+
def from_string(string: str)->Token:
|
|
147
|
+
return Token(token_type=token_type_from_string(None, string, case_sensitive=False), text=string)
|
|
145
148
|
|
|
146
149
|
|
|
147
150
|
@dataclass(frozen=True)
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
|
-
from syncraft.algebra import NamedResult
|
|
2
|
+
from syncraft.algebra import NamedResult, Error, ManyResult, OrResult, ThenResult, ThenKind
|
|
3
3
|
from syncraft.parser import literal, parse, Parser
|
|
4
4
|
import syncraft.generator as gen
|
|
5
|
+
from syncraft.generator import TokenGen
|
|
5
6
|
from rich import print
|
|
6
7
|
|
|
7
8
|
|
|
@@ -164,3 +165,109 @@ def test_deep_mix():
|
|
|
164
165
|
assert ast == generated
|
|
165
166
|
value, bmap = ast.bimap(None)
|
|
166
167
|
assert bmap(value) == ast
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def test_empty_many() -> None:
|
|
171
|
+
A = literal("a")
|
|
172
|
+
syntax = A.many() # This should allow empty matches
|
|
173
|
+
sql = ""
|
|
174
|
+
ast = parse(syntax(Parser), sql, dialect="sqlite")
|
|
175
|
+
assert isinstance(ast, Error)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def test_backtracking_many() -> None:
|
|
179
|
+
A = literal("a")
|
|
180
|
+
B = literal("b")
|
|
181
|
+
syntax = (A.many() + B) # must not eat the final "a" needed for B
|
|
182
|
+
sql = "a a a a b"
|
|
183
|
+
ast = parse(syntax(Parser), sql, dialect="sqlite")
|
|
184
|
+
value, bmap = ast.bimap(None)
|
|
185
|
+
assert value[-1] == TokenGen.from_string("b")
|
|
186
|
+
|
|
187
|
+
def test_deep_nesting() -> None:
|
|
188
|
+
A = literal("a")
|
|
189
|
+
syntax = A
|
|
190
|
+
for _ in range(100):
|
|
191
|
+
syntax = syntax + A
|
|
192
|
+
sql = " " .join("a" for _ in range(101))
|
|
193
|
+
ast = parse(syntax(Parser), sql, dialect="sqlite")
|
|
194
|
+
assert ast is not None
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def test_nested_many() -> None:
|
|
198
|
+
A = literal("a")
|
|
199
|
+
syntax = (A.many().many()) # groups of groups of "a"
|
|
200
|
+
sql = "a a a"
|
|
201
|
+
ast = parse(syntax(Parser), sql, dialect="sqlite")
|
|
202
|
+
assert isinstance(ast.focus, ManyResult)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_named_many() -> None:
|
|
206
|
+
A = literal("a").bind("alpha")
|
|
207
|
+
syntax = A.many()
|
|
208
|
+
sql = "a a"
|
|
209
|
+
ast = parse(syntax(Parser), sql, dialect="sqlite")
|
|
210
|
+
# Expect [NamedResult("alpha", "a"), NamedResult("alpha", "a")]
|
|
211
|
+
flattened, _ = ast.bimap(None)
|
|
212
|
+
assert all(isinstance(x, NamedResult) for x in flattened)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def test_or_named() -> None:
|
|
216
|
+
A = literal("a").bind("x")
|
|
217
|
+
B = literal("b").bind("y")
|
|
218
|
+
syntax = A | B
|
|
219
|
+
sql = "b"
|
|
220
|
+
ast = parse(syntax(Parser), sql, dialect="sqlite")
|
|
221
|
+
# Either NamedResult("y", "b") or just "b", depending on your design
|
|
222
|
+
assert isinstance(ast.focus, OrResult)
|
|
223
|
+
value, _ = ast.bimap(None)
|
|
224
|
+
assert value == NamedResult(name="y", value=TokenGen.from_string("b"))
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def test_then_associativity() -> None:
|
|
228
|
+
A = literal("a")
|
|
229
|
+
B = literal("b")
|
|
230
|
+
C = literal("c")
|
|
231
|
+
syntax = A + B + C
|
|
232
|
+
sql = "a b c"
|
|
233
|
+
ast = parse(syntax(Parser), sql, dialect="sqlite")
|
|
234
|
+
# Should be ThenResult(ThenResult(A,B),C)
|
|
235
|
+
assert ast.focus == ThenResult(kind=ThenKind.BOTH,
|
|
236
|
+
left=ThenResult(kind=ThenKind.BOTH,
|
|
237
|
+
left=TokenGen.from_string('a'),
|
|
238
|
+
right=TokenGen.from_string('b')),
|
|
239
|
+
right=TokenGen.from_string('c'))
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def test_ambiguous() -> None:
|
|
243
|
+
A = literal("a")
|
|
244
|
+
B = literal("a") + literal("b")
|
|
245
|
+
syntax = A | B
|
|
246
|
+
sql = "a"
|
|
247
|
+
ast = parse(syntax(Parser), sql, dialect="sqlite")
|
|
248
|
+
# Does it prefer A (shorter) or B (fails)? Depends on design.
|
|
249
|
+
assert ast.focus == OrResult(TokenGen.from_string("a"))
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def test_combo() -> None:
|
|
253
|
+
A = literal("a").bind("a")
|
|
254
|
+
B = literal("b")
|
|
255
|
+
C = literal("c").bind("c")
|
|
256
|
+
syntax = ((A + B).many() | C) + B
|
|
257
|
+
sql = "a b a b c b"
|
|
258
|
+
# Should fail, as we discussed earlier
|
|
259
|
+
ast = parse(syntax(Parser), sql, dialect="sqlite")
|
|
260
|
+
assert isinstance(ast, Error)
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def test_optional():
|
|
264
|
+
A = literal("a").bind("a")
|
|
265
|
+
syntax = A.optional()
|
|
266
|
+
ast1 = parse(syntax(Parser), "", dialect="sqlite")
|
|
267
|
+
v1, _ = ast1.bimap(None)
|
|
268
|
+
assert v1 is None
|
|
269
|
+
ast2 = parse(syntax(Parser), "a", dialect="sqlite")
|
|
270
|
+
v2, _ = ast2.bimap(None)
|
|
271
|
+
assert v2 == NamedResult(name='a', value=TokenGen.from_string('a'))
|
|
272
|
+
|
|
273
|
+
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|