jumbl 0.2.1.dev1__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.
- jumbl/__init__.py +35 -0
- jumbl/_ast_nodes.py +2089 -0
- jumbl/_comprehension.py +179 -0
- jumbl/_config.py +657 -0
- jumbl/_eval.py +249 -0
- jumbl/_expression.py +271 -0
- jumbl/_fstring.py +238 -0
- jumbl/_lark_parser.py +12 -0
- jumbl/_legacy.py +93 -0
- jumbl/_make_static.py +86 -0
- jumbl/_substitute.py +52 -0
- jumbl/_transformer.bak.py +755 -0
- jumbl/_transformer.py +2415 -0
- jumbl/_utils.py +46 -0
- jumbl/api.py +236 -0
- jumbl/ast_nodes.bak.py +676 -0
- jumbl/grammar.lark +302 -0
- jumbl/unparser.bak.py +345 -0
- jumbl-0.2.1.dev1.dist-info/METADATA +331 -0
- jumbl-0.2.1.dev1.dist-info/RECORD +23 -0
- jumbl-0.2.1.dev1.dist-info/WHEEL +4 -0
- jumbl-0.2.1.dev1.dist-info/licenses/LICENSE +201 -0
- jumbl-0.2.1.dev1.dist-info/licenses/NOTICE +7 -0
jumbl/_comprehension.py
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# jumbl/_comprehension.py
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
from typing import Iterator, Any
|
|
4
|
+
import re
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__) # put this with your other imports
|
|
9
|
+
logger.setLevel(logging.DEBUG) # caller can override
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
logger.debug("[_comprehension] module loaded")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# ④ evaluate list- and dict-comprehension strings
|
|
16
|
+
list_comp_pattern = re.compile(r"^\s*\[.*\bfor\b.*\]\s*$")
|
|
17
|
+
dict_comp_pattern = re.compile(r"^\s*\{.*\bfor\b.*\}\s*$")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _eval_comprehensions(obj: Any) -> Any:
|
|
21
|
+
logger.debug(f"[_eval_comprehensions] entering: obj={obj}")
|
|
22
|
+
if isinstance(obj, dict):
|
|
23
|
+
return {k: _eval_comprehensions(v) for k, v in obj.items()}
|
|
24
|
+
if isinstance(obj, list):
|
|
25
|
+
return [_eval_comprehensions(v) for v in obj]
|
|
26
|
+
if isinstance(obj, str):
|
|
27
|
+
if list_comp_pattern.match(obj):
|
|
28
|
+
try:
|
|
29
|
+
return eval(obj)
|
|
30
|
+
except Exception:
|
|
31
|
+
return obj
|
|
32
|
+
if dict_comp_pattern.match(obj):
|
|
33
|
+
python_syntax = re.sub(r"=(?=[^}]*\bfor\b)", ":", obj)
|
|
34
|
+
try:
|
|
35
|
+
# THIS DOES NOT USE THE SAFE_EVAL METHOD
|
|
36
|
+
# CAUTION SHOULD BE EXERCISED UNTIL PATCHED.
|
|
37
|
+
return eval(python_syntax)
|
|
38
|
+
except Exception:
|
|
39
|
+
return obj
|
|
40
|
+
return obj
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _evaluate_comprehension(node, global_env, context=None):
|
|
44
|
+
from ._ast_nodes import ListComprehensionNode
|
|
45
|
+
|
|
46
|
+
logger.debug(
|
|
47
|
+
(
|
|
48
|
+
f"[_evaluate_comprehension] entering: node={node}, "
|
|
49
|
+
f"global_env_keys={list(global_env.keys())}, context={context}"
|
|
50
|
+
)
|
|
51
|
+
)
|
|
52
|
+
results = []
|
|
53
|
+
for env in iter_environments(node.clauses, global_env, {}, context or {}):
|
|
54
|
+
logger.debug(f"[_evaluate_comprehension] iteration env={env}")
|
|
55
|
+
# resolve header_expr (for lists) or pair (for dicts)
|
|
56
|
+
if hasattr(node, "header_expr"):
|
|
57
|
+
logger.debug(
|
|
58
|
+
(
|
|
59
|
+
f"[_evaluate_comprehension] resolving header_expr: "
|
|
60
|
+
f"{node.header_expr}"
|
|
61
|
+
)
|
|
62
|
+
)
|
|
63
|
+
node.header_expr.resolve(global_env, env)
|
|
64
|
+
logger.debug(
|
|
65
|
+
(
|
|
66
|
+
f"[_evaluate_comprehension] resolved "
|
|
67
|
+
f"header_expr.resolved={node.header_expr.resolved}"
|
|
68
|
+
)
|
|
69
|
+
)
|
|
70
|
+
results.append(node.header_expr.resolved)
|
|
71
|
+
else:
|
|
72
|
+
logger.debug(
|
|
73
|
+
f"[_evaluate_comprehension] resolving pair: {node.pair}"
|
|
74
|
+
)
|
|
75
|
+
node.pair.resolve(global_env, env)
|
|
76
|
+
k, v = node.pair.resolved
|
|
77
|
+
logger.debug(
|
|
78
|
+
(
|
|
79
|
+
f"[_evaluate_comprehension] resolved pair -> key={k}, "
|
|
80
|
+
f"value={v}"
|
|
81
|
+
)
|
|
82
|
+
)
|
|
83
|
+
results.append((k, v))
|
|
84
|
+
# for dict comprehensions, you’d turn results into a dict
|
|
85
|
+
if isinstance(node, ListComprehensionNode):
|
|
86
|
+
logger.debug(
|
|
87
|
+
(
|
|
88
|
+
f"[_evaluate_comprehension] returning list of length "
|
|
89
|
+
f"{len(results)}"
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
return results
|
|
93
|
+
else:
|
|
94
|
+
result_dict = dict(results)
|
|
95
|
+
logger.debug(
|
|
96
|
+
(
|
|
97
|
+
f"[_evaluate_comprehension] returning dict of size "
|
|
98
|
+
f"{len(result_dict)}"
|
|
99
|
+
)
|
|
100
|
+
)
|
|
101
|
+
return result_dict
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
# ────────────────────────────────────────────────────────────────
|
|
105
|
+
# _comprehension.py · function iter_environments │
|
|
106
|
+
# ────────────────────────────────────────────────────────────────
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def iter_environments(
|
|
110
|
+
clauses, global_env, local_env, context
|
|
111
|
+
) -> Iterator[dict]:
|
|
112
|
+
from ._ast_nodes import AliasClauseNode, BaseNode
|
|
113
|
+
|
|
114
|
+
clause_list = clauses.clauses
|
|
115
|
+
|
|
116
|
+
def _rec(idx: int, env: dict):
|
|
117
|
+
logger.debug(f"{idx} {env}")
|
|
118
|
+
if idx == len(clause_list):
|
|
119
|
+
yield env
|
|
120
|
+
return
|
|
121
|
+
|
|
122
|
+
clause = clause_list[idx]
|
|
123
|
+
iterable = clause.iterable
|
|
124
|
+
|
|
125
|
+
# evaluate the iterable
|
|
126
|
+
# -------------------------------------------------
|
|
127
|
+
if (
|
|
128
|
+
context is not None
|
|
129
|
+
and hasattr(iterable, "render")
|
|
130
|
+
and type(iterable).render is not BaseNode.render
|
|
131
|
+
):
|
|
132
|
+
iterable_vals = iterable.render(
|
|
133
|
+
global_env, {**local_env, **env}, context
|
|
134
|
+
)
|
|
135
|
+
else:
|
|
136
|
+
if hasattr(iterable, "resolve"):
|
|
137
|
+
iterable.resolve(global_env, {**local_env, **env})
|
|
138
|
+
iterable_vals = iterable.resolved
|
|
139
|
+
else:
|
|
140
|
+
iterable_vals = iterable
|
|
141
|
+
iterable_vals = iterable_vals or []
|
|
142
|
+
|
|
143
|
+
# main loop
|
|
144
|
+
# -------------------------------------------------------------
|
|
145
|
+
logger.debug(f"iterable_vals: {iterable_vals}")
|
|
146
|
+
for item in iterable_vals:
|
|
147
|
+
new_env = dict(env)
|
|
148
|
+
bound_any = False
|
|
149
|
+
|
|
150
|
+
for var in getattr(clause, "loop_vars", []):
|
|
151
|
+
bound_any = True
|
|
152
|
+
if isinstance(var, AliasClauseNode):
|
|
153
|
+
alias_name = var.resolve(global_env, new_env) # ✔ fixed
|
|
154
|
+
new_env[alias_name] = item
|
|
155
|
+
else:
|
|
156
|
+
name = getattr(var, "value", str(var))
|
|
157
|
+
new_env[name] = item
|
|
158
|
+
|
|
159
|
+
if not bound_any:
|
|
160
|
+
continue
|
|
161
|
+
|
|
162
|
+
# conditions
|
|
163
|
+
# --------------------------------------------------------
|
|
164
|
+
passed = True
|
|
165
|
+
for cond in getattr(clause, "conditions", []):
|
|
166
|
+
cond_val = (
|
|
167
|
+
cond.render(global_env, new_env, context)
|
|
168
|
+
if hasattr(cond, "render") and context
|
|
169
|
+
else (cond.resolve(global_env, new_env) or cond.evaluate())
|
|
170
|
+
)
|
|
171
|
+
if not cond_val:
|
|
172
|
+
passed = False
|
|
173
|
+
break
|
|
174
|
+
if not passed:
|
|
175
|
+
continue
|
|
176
|
+
|
|
177
|
+
yield from _rec(idx + 1, new_env)
|
|
178
|
+
|
|
179
|
+
yield from _rec(0, {})
|