syncraft 0.2.6__tar.gz → 0.2.7__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.2.6/syncraft.egg-info → syncraft-0.2.7}/PKG-INFO +1 -1
- {syncraft-0.2.6 → syncraft-0.2.7}/pyproject.toml +1 -1
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/algebra.py +4 -4
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/syntax.py +4 -4
- {syncraft-0.2.6 → syncraft-0.2.7/syncraft.egg-info}/PKG-INFO +1 -1
- syncraft-0.2.7/tests/test_lazy.py +107 -0
- syncraft-0.2.6/tests/test_lazy.py +0 -50
- {syncraft-0.2.6 → syncraft-0.2.7}/LICENSE +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/README.md +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/setup.cfg +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/__init__.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/ast.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/cache.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/constraint.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/dev.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/finder.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/generator.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/lexer.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/parser.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/py.typed +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/sqlite3.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/utils.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft/walker.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft.egg-info/SOURCES.txt +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft.egg-info/dependency_links.txt +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft.egg-info/requires.txt +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/syncraft.egg-info/top_level.txt +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/tests/test_bimap.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/tests/test_constraint.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/tests/test_find.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/tests/test_parse.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/tests/test_to.py +0 -0
- {syncraft-0.2.6 → syncraft-0.2.7}/tests/test_walk.py +0 -0
|
@@ -103,10 +103,10 @@ class Algebra(Generic[A, S]):
|
|
|
103
103
|
cache: Cache) -> Algebra[A, S]:
|
|
104
104
|
def algebra_lazy_run(input: S, use_cache:bool) -> Generator[Incomplete[S], S, Either[Any, Tuple[A, S]]]:
|
|
105
105
|
alg = thunk()
|
|
106
|
-
print('--' * 20, "Algebra.lazy.algebra_lazy_run", '--' * 20)
|
|
107
|
-
print('thunk', thunk, id(thunk))
|
|
108
|
-
print('input', input, id(input))
|
|
109
|
-
print('alg', alg, id(alg))
|
|
106
|
+
# print('--' * 20, "Algebra.lazy.algebra_lazy_run", '--' * 20)
|
|
107
|
+
# print('thunk', thunk, id(thunk))
|
|
108
|
+
# print('input', input, id(input))
|
|
109
|
+
# print('alg', alg, id(alg))
|
|
110
110
|
result = yield from alg.run(input, use_cache)
|
|
111
111
|
return result
|
|
112
112
|
return cls(algebra_lazy_run, name=cls.__name__ + '.lazy', cache=cache)
|
|
@@ -566,10 +566,10 @@ def lazy(thunk: Callable[[], Syntax[A, S]]) -> Syntax[A, S]:
|
|
|
566
566
|
algebra: Optional[Algebra[A, S]] = None
|
|
567
567
|
def syntax_lazy_run(cls: Type[Algebra[Any, S]], cache: Cache) -> Algebra[A, S]:
|
|
568
568
|
nonlocal syntax, algebra
|
|
569
|
-
print('==' * 20, 'Syntax.lazy.syntax_lazy_run', '==' * 20)
|
|
570
|
-
print('thunk', thunk, id(thunk))
|
|
571
|
-
print('syntax', syntax, id(syntax))
|
|
572
|
-
print('algebra', algebra, id(algebra))
|
|
569
|
+
# print('==' * 20, 'Syntax.lazy.syntax_lazy_run', '==' * 20)
|
|
570
|
+
# print('thunk', thunk, id(thunk))
|
|
571
|
+
# print('syntax', syntax, id(syntax))
|
|
572
|
+
# print('algebra', algebra, id(algebra))
|
|
573
573
|
if syntax is None:
|
|
574
574
|
syntax = thunk()
|
|
575
575
|
def algebra_lazy_f():
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from syncraft.parser import token
|
|
3
|
+
import pytest
|
|
4
|
+
from syncraft.walker import walk
|
|
5
|
+
from syncraft.ast import Nothing
|
|
6
|
+
from syncraft.syntax import lazy, literal, regex
|
|
7
|
+
from syncraft.parser import parse
|
|
8
|
+
from syncraft.generator import TokenGen
|
|
9
|
+
from syncraft.cache import RecursionError
|
|
10
|
+
from rich import print
|
|
11
|
+
|
|
12
|
+
def test_simple_recursion()->None:
|
|
13
|
+
A = lazy(lambda: literal('a') + ~A | literal('a'))
|
|
14
|
+
v, s = parse(A, 'a a a', dialect='sqlite')
|
|
15
|
+
print(v)
|
|
16
|
+
ast1, inv = v.bimap()
|
|
17
|
+
print(ast1)
|
|
18
|
+
assert ast1 == (
|
|
19
|
+
TokenGen.from_string('a'),
|
|
20
|
+
(
|
|
21
|
+
TokenGen.from_string('a'),
|
|
22
|
+
(
|
|
23
|
+
TokenGen.from_string('a'),
|
|
24
|
+
Nothing()
|
|
25
|
+
)
|
|
26
|
+
)
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
def test_direct_recursion()->None:
|
|
30
|
+
Expr1 = lazy(lambda: literal('a') + ~Expr1)
|
|
31
|
+
v, s = parse(Expr1, 'a a a', dialect='sqlite')
|
|
32
|
+
x, _ = v.bimap()
|
|
33
|
+
assert x == (
|
|
34
|
+
TokenGen.from_string('a'),
|
|
35
|
+
(
|
|
36
|
+
TokenGen.from_string('a'),
|
|
37
|
+
(
|
|
38
|
+
TokenGen.from_string('a'),
|
|
39
|
+
Nothing()
|
|
40
|
+
)
|
|
41
|
+
)
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_mutual_recursion()->None:
|
|
46
|
+
A = lazy(lambda: literal('a') + B)
|
|
47
|
+
B = lazy(lambda: (literal('b') + A) | (literal('c')))
|
|
48
|
+
v, s = parse(A, 'a b a b a c', dialect='sqlite')
|
|
49
|
+
print('--' * 20, "test_mutual_recursion", '--' * 20)
|
|
50
|
+
print(v)
|
|
51
|
+
ast1, inv = v.bimap()
|
|
52
|
+
print(ast1)
|
|
53
|
+
assert ast1 == (
|
|
54
|
+
TokenGen.from_string('a'),
|
|
55
|
+
(
|
|
56
|
+
TokenGen.from_string('b'),
|
|
57
|
+
TokenGen.from_string('a'),
|
|
58
|
+
(
|
|
59
|
+
TokenGen.from_string('b'),
|
|
60
|
+
TokenGen.from_string('a'),
|
|
61
|
+
TokenGen.from_string('c')
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_recursion() -> None:
|
|
69
|
+
A = literal('a')
|
|
70
|
+
B = literal('b')
|
|
71
|
+
L = lazy(lambda: literal("if") >> (A | B) // literal('then'))
|
|
72
|
+
|
|
73
|
+
def parens():
|
|
74
|
+
return A + ~lazy(parens) + B
|
|
75
|
+
p_code = 'a a b b'
|
|
76
|
+
LL = parens() | L
|
|
77
|
+
|
|
78
|
+
v, s = parse(LL, p_code, dialect='sqlite')
|
|
79
|
+
ast1, inv = v.bimap()
|
|
80
|
+
print(v)
|
|
81
|
+
print(ast1)
|
|
82
|
+
assert ast1 == (
|
|
83
|
+
TokenGen.from_string('a'),
|
|
84
|
+
(
|
|
85
|
+
TokenGen.from_string('a'),
|
|
86
|
+
Nothing(),
|
|
87
|
+
TokenGen.from_string('b')
|
|
88
|
+
),
|
|
89
|
+
TokenGen.from_string('b')
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
def test_direct_left_recursion()->None:
|
|
93
|
+
Term = literal('n')
|
|
94
|
+
Expr = lazy(lambda: Expr + literal('+') + Term | Term)
|
|
95
|
+
with pytest.raises(RecursionError):
|
|
96
|
+
v, s = parse(Expr, 'n+n+n', dialect='sqlite')
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_indirect_left_recursion()->None:
|
|
101
|
+
NUMBER = regex(r'\d+').map(int)
|
|
102
|
+
PLUS = token(text='+')
|
|
103
|
+
STAR = token(text='*')
|
|
104
|
+
A = lazy(lambda: (B >> PLUS >> A) | B)
|
|
105
|
+
B = lazy(lambda: (A >> STAR >> NUMBER) | NUMBER)
|
|
106
|
+
with pytest.raises(RecursionError):
|
|
107
|
+
v, s = parse(A, '1 + 2 * 3', dialect='sqlite')
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
from syncraft.parser import token
|
|
3
|
-
|
|
4
|
-
from syncraft.walker import walk
|
|
5
|
-
from syncraft.ast import Nothing
|
|
6
|
-
from syncraft.syntax import lazy, literal, regex
|
|
7
|
-
from syncraft.parser import parse
|
|
8
|
-
from syncraft.generator import TokenGen
|
|
9
|
-
|
|
10
|
-
from rich import print
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def test_recursion() -> None:
|
|
14
|
-
A = literal('a')
|
|
15
|
-
B = literal('b')
|
|
16
|
-
L = lazy(lambda: literal("if") >> (A | B) // literal('then'))
|
|
17
|
-
|
|
18
|
-
def parens():
|
|
19
|
-
return A + ~lazy(parens) + B
|
|
20
|
-
p_code = 'a a b b'
|
|
21
|
-
LL = parens() | L
|
|
22
|
-
|
|
23
|
-
v, s = parse(LL, p_code, dialect='sqlite')
|
|
24
|
-
ast1, inv = v.bimap()
|
|
25
|
-
print(v)
|
|
26
|
-
print(ast1)
|
|
27
|
-
assert ast1 == (
|
|
28
|
-
TokenGen.from_string('a'),
|
|
29
|
-
(
|
|
30
|
-
TokenGen.from_string('a'),
|
|
31
|
-
Nothing(),
|
|
32
|
-
TokenGen.from_string('b')
|
|
33
|
-
),
|
|
34
|
-
TokenGen.from_string('b')
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
def test_direct_left_recursion()->None:
|
|
38
|
-
Term = literal('n')
|
|
39
|
-
Expr = lazy(lambda: Expr + literal('+') + Term | Term)
|
|
40
|
-
v, s = parse(Expr, 'n+n+n', dialect='sqlite')
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
def test_indirect_left_recursion()->None:
|
|
45
|
-
NUMBER = regex(r'\d+').map(int)
|
|
46
|
-
PLUS = token(text='+')
|
|
47
|
-
STAR = token(text='*')
|
|
48
|
-
A = lazy(lambda: (B >> PLUS >> A) | B)
|
|
49
|
-
B = lazy(lambda: (A >> STAR >> NUMBER) | NUMBER)
|
|
50
|
-
v, s = parse(A, '1 + 2 * 3', dialect='sqlite')
|
|
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
|
|
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
|