syncraft 0.1.15__tar.gz → 0.1.16__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.
- {syncraft-0.1.15 → syncraft-0.1.16}/PKG-INFO +1 -1
- {syncraft-0.1.15 → syncraft-0.1.16}/pyproject.toml +1 -1
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft/algebra.py +1 -11
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft/generator.py +25 -18
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft.egg-info/PKG-INFO +1 -1
- {syncraft-0.1.15 → syncraft-0.1.16}/tests/test_bimap.py +82 -4
- {syncraft-0.1.15 → syncraft-0.1.16}/LICENSE +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/README.md +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/setup.cfg +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft/__init__.py +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft/ast.py +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft/cmd.py +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft/diagnostic.py +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft/dsl.py +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft/parser.py +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft/py.typed +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft/sqlite3.py +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft.egg-info/SOURCES.txt +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft.egg-info/dependency_links.txt +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft.egg-info/requires.txt +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/syncraft.egg-info/top_level.txt +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/tests/test_parse.py +0 -0
- {syncraft-0.1.15 → syncraft-0.1.16}/tests/test_until.py +0 -0
|
@@ -170,17 +170,7 @@ class ThenResult(Generic[A, B], StructuralResult):
|
|
|
170
170
|
right: B
|
|
171
171
|
def bimap(self, ctx: Any) -> Tuple[Any, Callable[[Any], StructuralResult]]:
|
|
172
172
|
def branch(b: Any) -> Tuple[Any, Callable[[Any], StructuralResult]]:
|
|
173
|
-
if isinstance(b,
|
|
174
|
-
value, backward = b.bimap(ctx)
|
|
175
|
-
if isinstance(value, tuple):
|
|
176
|
-
x, y = ThenResult.flat(value)
|
|
177
|
-
return x, lambda data: ThenResult(self.kind, y(data), self.right)
|
|
178
|
-
else:
|
|
179
|
-
return value, backward
|
|
180
|
-
elif isinstance(b, StructuralResult):
|
|
181
|
-
return b.bimap(ctx)
|
|
182
|
-
else:
|
|
183
|
-
return b, lambda x: x
|
|
173
|
+
return b.bimap(ctx) if isinstance(b, StructuralResult) else (b, lambda x: x)
|
|
184
174
|
match self.kind:
|
|
185
175
|
case ThenKind.BOTH:
|
|
186
176
|
left_value, left_bmap = branch(self.left)
|
|
@@ -147,24 +147,31 @@ class TokenGen(TokenSpec):
|
|
|
147
147
|
@dataclass(frozen=True)
|
|
148
148
|
class Generator(Algebra[GenResult[T], GenState[T]]):
|
|
149
149
|
def flat_map(self, f: Callable[[GenResult[T]], Algebra[B, GenState[T]]]) -> Algebra[B, GenState[T]]:
|
|
150
|
-
def flat_map_run(
|
|
151
|
-
wrapper =
|
|
152
|
-
input =
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
150
|
+
def flat_map_run(original: GenState[T], use_cache:bool) -> Either[Any, Tuple[B, GenState[T]]]:
|
|
151
|
+
wrapper = original.wrapper()
|
|
152
|
+
input = original if not original.is_named else original.down(0) # If the input is named, we need to go down to the first child
|
|
153
|
+
try:
|
|
154
|
+
lft = input.left()
|
|
155
|
+
match self.run(lft, use_cache=use_cache):
|
|
156
|
+
case Left(error):
|
|
157
|
+
return Left(error)
|
|
158
|
+
case Right((value, next_input)):
|
|
159
|
+
r = input.right()
|
|
160
|
+
match f(value).run(r, use_cache):
|
|
161
|
+
case Left(e):
|
|
162
|
+
return Left(e)
|
|
163
|
+
case Right((result, next_input)):
|
|
164
|
+
return Right((wrapper(result), next_input))
|
|
165
|
+
raise ValueError("flat_map should always return a value or an error.")
|
|
166
|
+
except Exception as e:
|
|
167
|
+
return Left(Error(
|
|
168
|
+
message=str(e),
|
|
169
|
+
this=self,
|
|
170
|
+
state=original,
|
|
171
|
+
error=e
|
|
172
|
+
))
|
|
173
|
+
return Generator(run_f = flat_map_run, name=self.name) # type: ignore
|
|
174
|
+
|
|
168
175
|
|
|
169
176
|
def many(self, *, at_least: int, at_most: Optional[int]) -> Algebra[ManyResult[GenResult[T]], GenState[T]]:
|
|
170
177
|
assert at_least > 0, "at_least must be greater than 0"
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
|
-
from syncraft.
|
|
3
|
-
from syncraft.
|
|
2
|
+
from syncraft.algebra import NamedResult
|
|
3
|
+
from syncraft.parser import literal, parse, Parser
|
|
4
4
|
import syncraft.generator as gen
|
|
5
|
-
from typing import Any
|
|
6
5
|
from rich import print
|
|
7
6
|
|
|
8
7
|
|
|
@@ -85,4 +84,83 @@ def test5_nested_then_many() -> None:
|
|
|
85
84
|
# assert ast == generated
|
|
86
85
|
value, bmap = generated.bimap(None)
|
|
87
86
|
print(value)
|
|
88
|
-
assert bmap(value) == generated
|
|
87
|
+
assert bmap(value) == generated
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_then_flatten():
|
|
92
|
+
A, B, C = literal("a"), literal("b"), literal("c")
|
|
93
|
+
syntax = A + (B + C)
|
|
94
|
+
sql = "a b c"
|
|
95
|
+
ast = parse(syntax(Parser), sql, dialect='sqlite')
|
|
96
|
+
print(ast)
|
|
97
|
+
generated = gen.generate(syntax(gen.Generator), ast)
|
|
98
|
+
assert ast == generated
|
|
99
|
+
value, bmap = ast.bimap(None)
|
|
100
|
+
assert bmap(value) == ast
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def test_named_in_then():
|
|
105
|
+
A = literal("a").bind("first")
|
|
106
|
+
B = literal("b").bind("second")
|
|
107
|
+
C = literal("c").bind("third")
|
|
108
|
+
syntax = A + B + C
|
|
109
|
+
sql = "a b c"
|
|
110
|
+
ast = parse(syntax(Parser), sql, dialect='sqlite')
|
|
111
|
+
print(ast)
|
|
112
|
+
generated = gen.generate(syntax(gen.Generator), ast)
|
|
113
|
+
assert ast == generated
|
|
114
|
+
value, bmap = ast.bimap(None)
|
|
115
|
+
assert isinstance(value, tuple)
|
|
116
|
+
print(value)
|
|
117
|
+
assert set(x.name for x in value if isinstance(x, NamedResult)) == {"first", "second", "third"}
|
|
118
|
+
assert bmap(value) == ast
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def test_named_in_many():
|
|
122
|
+
A = literal("x").bind("x")
|
|
123
|
+
syntax = A.many()
|
|
124
|
+
sql = "x x x"
|
|
125
|
+
ast = parse(syntax(Parser), sql, dialect='sqlite')
|
|
126
|
+
print(ast)
|
|
127
|
+
generated = gen.generate(syntax(gen.Generator), ast)
|
|
128
|
+
assert ast == generated
|
|
129
|
+
value, bmap = ast.bimap(None)
|
|
130
|
+
assert isinstance(value, list)
|
|
131
|
+
assert all(isinstance(v, NamedResult) for v in value if isinstance(v, NamedResult))
|
|
132
|
+
assert bmap(value) == ast
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def test_named_in_or():
|
|
136
|
+
A = literal("a").bind("a")
|
|
137
|
+
B = literal("b").bind("b")
|
|
138
|
+
syntax = A | B
|
|
139
|
+
sql = "b"
|
|
140
|
+
ast = parse(syntax(Parser), sql, dialect='sqlite')
|
|
141
|
+
print(ast)
|
|
142
|
+
generated = gen.generate(syntax(gen.Generator), ast)
|
|
143
|
+
assert ast == generated
|
|
144
|
+
value, bmap = ast.bimap(None)
|
|
145
|
+
assert isinstance(value, NamedResult)
|
|
146
|
+
assert value.name == "b"
|
|
147
|
+
assert bmap(value) == ast
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def test_deep_mix():
|
|
154
|
+
A = literal("a").bind("a")
|
|
155
|
+
B = literal("b")
|
|
156
|
+
C = literal("c").bind("c")
|
|
157
|
+
syntax = ((A + B) | C).many() + B
|
|
158
|
+
sql = "a b a b c b"
|
|
159
|
+
ast = parse(syntax(Parser), sql, dialect='sqlite')
|
|
160
|
+
print(ast)
|
|
161
|
+
generated = gen.generate(syntax(gen.Generator), ast)
|
|
162
|
+
print('---' * 40)
|
|
163
|
+
print(generated)
|
|
164
|
+
assert ast == generated
|
|
165
|
+
value, bmap = ast.bimap(None)
|
|
166
|
+
assert bmap(value) == ast
|
|
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
|