json-model-compiler 2.0b0__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.
- json_model/README.md +32 -0
- json_model/__init__.py +18 -0
- json_model/analyze.py +409 -0
- json_model/clang.py +563 -0
- json_model/convert.py +430 -0
- json_model/data/clang_entry.c +21 -0
- json_model/data/clang_free.c +10 -0
- json_model/data/clang_init.c +11 -0
- json_model/data/clang_pcre2_fun.c +6 -0
- json_model/data/clang_re2_fun.c +5 -0
- json_model/data/javascript_exe.js +4 -0
- json_model/data/javascript_foot.js +9 -0
- json_model/data/javascript_free.js +9 -0
- json_model/data/javascript_head.js +1 -0
- json_model/data/javascript_init.js +11 -0
- json_model/data/javascript_main.js +5 -0
- json_model/data/plpgsql_entry.sql +16 -0
- json_model/data/python_entry.py +11 -0
- json_model/data/python_exe.py +1 -0
- json_model/data/python_free.py +6 -0
- json_model/data/python_init.py +9 -0
- json_model/data/python_main.py +4 -0
- json_model/javascript.py +310 -0
- json_model/language.py +748 -0
- json_model/model.py +1084 -0
- json_model/mtypes.py +42 -0
- json_model/objmerge.py +165 -0
- json_model/optim.py +486 -0
- json_model/plpgsql.py +422 -0
- json_model/python.py +269 -0
- json_model/recurse.py +127 -0
- json_model/resolver.py +106 -0
- json_model/runtime/META.json +30 -0
- json_model/runtime/Makefile +21 -0
- json_model/runtime/__init__.py +32 -0
- json_model/runtime/json-model.c +1054 -0
- json_model/runtime/json-model.h +183 -0
- json_model/runtime/json_model--2.0.sql +261 -0
- json_model/runtime/json_model.control +2 -0
- json_model/runtime/main.c +473 -0
- json_model/runtime/support.py +485 -0
- json_model/runtime/types.py +16 -0
- json_model/script.py +585 -0
- json_model/url_cache.py +82 -0
- json_model/utils.py +623 -0
- json_model/xstatic.py +1444 -0
- json_model_compiler-2.0b0.dist-info/METADATA +201 -0
- json_model_compiler-2.0b0.dist-info/RECORD +51 -0
- json_model_compiler-2.0b0.dist-info/WHEEL +5 -0
- json_model_compiler-2.0b0.dist-info/entry_points.txt +2 -0
- json_model_compiler-2.0b0.dist-info/top_level.txt +1 -0
json_model/javascript.py
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from .language import Language, Block, Var, PropMap, ConstList
|
|
3
|
+
from .language import JsonExpr, BoolExpr, IntExpr, PathExpr, Expr
|
|
4
|
+
from .mtypes import Jsonable, JsonScalar, Number
|
|
5
|
+
|
|
6
|
+
_ESC_TABLE = { '"': r'\"', "\\": "\\\\" }
|
|
7
|
+
|
|
8
|
+
class JavaScript(Language):
|
|
9
|
+
"""Generate JSON value checker in JavaScript.
|
|
10
|
+
|
|
11
|
+
This implementation relies on inheritance for many methods, thanks to the root
|
|
12
|
+
class heavy parameterization about operators, end-of-instruction and the like.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def __init__(self, *,
|
|
16
|
+
debug: bool = False, relib: str = "re",
|
|
17
|
+
with_path: bool = True, with_report: bool = True, with_comment: bool = True):
|
|
18
|
+
|
|
19
|
+
super().__init__(
|
|
20
|
+
"JS",
|
|
21
|
+
with_path=with_path, with_report=with_report, with_comment=with_comment,
|
|
22
|
+
not_op="!", and_op="&&", or_op="||", lcom="//",
|
|
23
|
+
true="true", false="false", null="null", relib=relib,
|
|
24
|
+
check_t="object", json_t="object", int_t="Number", float_t="Number",
|
|
25
|
+
path_t="?", str_t="String", match_t="bool",
|
|
26
|
+
eoi=";", set_caps=(type(None), bool, int, float, str)) # type: ignore
|
|
27
|
+
|
|
28
|
+
assert self._relib in ("re", "re2"), f"unsupported regex engine: {self._relib}"
|
|
29
|
+
|
|
30
|
+
self._json_esc_table = str.maketrans(_ESC_TABLE)
|
|
31
|
+
|
|
32
|
+
#
|
|
33
|
+
# file
|
|
34
|
+
#
|
|
35
|
+
def file_header(self, exe: bool = True) -> Block:
|
|
36
|
+
code: Block = self.file_load("javascript_exe.js") if exe else []
|
|
37
|
+
code += super().file_header(exe)
|
|
38
|
+
code += self.file_load("javascript_head.js")
|
|
39
|
+
code += [ f"const JSON_MODEL_VERSION = {self.esc(self._version)};" ]
|
|
40
|
+
return code
|
|
41
|
+
|
|
42
|
+
def file_footer(self, exe: bool = True) -> Block:
|
|
43
|
+
code: Block = self.file_load("javascript_foot.js")
|
|
44
|
+
if exe:
|
|
45
|
+
code += [""] + self.file_load("javascript_main.js")
|
|
46
|
+
return code
|
|
47
|
+
|
|
48
|
+
#
|
|
49
|
+
# inlined type test expressions about JSON data
|
|
50
|
+
#
|
|
51
|
+
def is_num(self, var: Var) -> BoolExpr:
|
|
52
|
+
# JS === BF
|
|
53
|
+
# 12.5 instanceof Number === false
|
|
54
|
+
# Number(12.5) instanceof Number === false # YES!
|
|
55
|
+
return f"(typeof {var} === 'number' || {var} instanceof Number)"
|
|
56
|
+
|
|
57
|
+
def is_scalar(self, var: Var) -> BoolExpr:
|
|
58
|
+
return f"({var} === null || {self.is_num(var)} || {self.is_a(var, bool)} || {self.is_a(var, str)})"
|
|
59
|
+
|
|
60
|
+
def is_def(self, var: Var) -> BoolExpr:
|
|
61
|
+
return f"{var} !== undefined"
|
|
62
|
+
|
|
63
|
+
def is_a(self, var: Var, tval: type|None, loose: bool|None = None) -> BoolExpr:
|
|
64
|
+
assert loose is None or tval in (int, float, Number)
|
|
65
|
+
if tval is None or tval is type(None):
|
|
66
|
+
return f"{var} === null"
|
|
67
|
+
elif tval is bool:
|
|
68
|
+
return f"(typeof {var} === 'boolean' || {var} instanceof Boolean)"
|
|
69
|
+
elif tval is int:
|
|
70
|
+
return self.is_num(var) + f" && Number.isInteger({var})" # type: ignore
|
|
71
|
+
elif tval is float:
|
|
72
|
+
return self.is_num(var)
|
|
73
|
+
elif tval is Number:
|
|
74
|
+
return self.is_num(var)
|
|
75
|
+
elif tval is str:
|
|
76
|
+
# JS is a heavy contestant to BF.
|
|
77
|
+
# c = "Calvin": typeof c === "string", c instanceof String === false
|
|
78
|
+
# c = new String("Calvin"): typeof c === "object, c instanceof String === true
|
|
79
|
+
return f"(typeof {var} === 'string' || {var} instanceof String)"
|
|
80
|
+
elif tval is list:
|
|
81
|
+
return f"Array.isArray({var})"
|
|
82
|
+
elif tval is dict:
|
|
83
|
+
# well, nearly everything is an object in JS, including null and arrays
|
|
84
|
+
return f"Object.prototype.toString.call({var}) === '[object Object]'"
|
|
85
|
+
else:
|
|
86
|
+
raise Exception(f"unexpected type for json: {tval.__name__}")
|
|
87
|
+
|
|
88
|
+
def _json_str(self, j) -> str:
|
|
89
|
+
return '"' + json.dumps(j).translate(self._json_esc_table) + '"'
|
|
90
|
+
|
|
91
|
+
def json_cst(self, j: Jsonable) -> JsonExpr:
|
|
92
|
+
return f"json_loads({self._json_str(j)}, JSON_DECODE_ANY|JSON_ALLOW_NUL, NULL)"
|
|
93
|
+
|
|
94
|
+
def const(self, c: Jsonable) -> Expr:
|
|
95
|
+
if isinstance(c, (list, dict)):
|
|
96
|
+
return self.json_cst(c)
|
|
97
|
+
else:
|
|
98
|
+
return super().const(c)
|
|
99
|
+
|
|
100
|
+
def obj_prop_val(self, obj: Var, prop: Expr, is_var: bool = False) -> JsonExpr:
|
|
101
|
+
return f"{obj}[{prop}]" if is_var else f"{obj}[{self.esc(prop)}]" # type: ignore
|
|
102
|
+
|
|
103
|
+
def has_prop(self, obj: Var, prop: str) -> BoolExpr:
|
|
104
|
+
return f"{obj}.hasOwnProperty({self.esc(prop)})"
|
|
105
|
+
|
|
106
|
+
# FIXME path? reporting?
|
|
107
|
+
def predef(self, var: Var, name: str, path: Var, is_str: bool = False) -> BoolExpr:
|
|
108
|
+
val = var
|
|
109
|
+
if name == "$UUID":
|
|
110
|
+
return f"runtime.jm_is_valid_uuid({val})"
|
|
111
|
+
elif name == "$DATE":
|
|
112
|
+
return f"runtime.jm_is_valid_date({val}, {path}, rep)"
|
|
113
|
+
elif name == "$TIME":
|
|
114
|
+
return f"runtime.jm_is_valid_time({val}, {path}, rep)"
|
|
115
|
+
elif name == "$DATETIME":
|
|
116
|
+
return f"runtime.jm_is_valid_datetime({val}, {path}, rep)"
|
|
117
|
+
elif name == "$REGEX":
|
|
118
|
+
return f"runtime.jm_is_valid_regex({val}, false)"
|
|
119
|
+
elif name == "$EXREG":
|
|
120
|
+
return f"runtime.jm_is_valid_regex({val}, true)"
|
|
121
|
+
elif name in ("$URL", "$URI"):
|
|
122
|
+
return f"runtime.jm_is_valid_url({val})"
|
|
123
|
+
elif name == "$EMAIL":
|
|
124
|
+
return f"runtime.jm_is_valid_email({val})"
|
|
125
|
+
else:
|
|
126
|
+
return super().predef(var, name, path, is_str)
|
|
127
|
+
|
|
128
|
+
#
|
|
129
|
+
# inlined length computation
|
|
130
|
+
#
|
|
131
|
+
def obj_len(self, var: Var) -> IntExpr: # O(n) :-/
|
|
132
|
+
return f"Object.keys({var}).length"
|
|
133
|
+
|
|
134
|
+
def arr_len(self, var: Var) -> IntExpr:
|
|
135
|
+
return f"{var}.length"
|
|
136
|
+
|
|
137
|
+
def str_len(self, var: Var) -> IntExpr:
|
|
138
|
+
return f"{var}.length"
|
|
139
|
+
|
|
140
|
+
def any_len(self, var: Var) -> IntExpr:
|
|
141
|
+
return f"_any_len({var})"
|
|
142
|
+
#
|
|
143
|
+
# misc expressions
|
|
144
|
+
#
|
|
145
|
+
def assign_prop_fun(self, fun: str, prop: str, mapname: str) -> JsonExpr:
|
|
146
|
+
return f"({fun} = {mapname}.get({prop}))"
|
|
147
|
+
|
|
148
|
+
def str_start(self, val: str, start: str) -> BoolExpr:
|
|
149
|
+
return f"{val}.startsWith({self.esc(start)})"
|
|
150
|
+
|
|
151
|
+
def str_end(self, val: str, end: str) -> BoolExpr:
|
|
152
|
+
return f"{val}.endsWith({self.esc(end)})"
|
|
153
|
+
|
|
154
|
+
def check_unique(self, val: JsonExpr, path: Var) -> BoolExpr:
|
|
155
|
+
return f"runtime.jm_array_is_unique({val}, {path}, rep)"
|
|
156
|
+
|
|
157
|
+
def check_constraint(self, op: str, vop: int|float|str, val: JsonExpr, path: Var) -> BoolExpr:
|
|
158
|
+
return f"runtime.jm_check_constraint({val}, {self.esc(op)}, {self.const(vop)}, {path}, rep)"
|
|
159
|
+
|
|
160
|
+
def nope(self) -> Block:
|
|
161
|
+
return []
|
|
162
|
+
|
|
163
|
+
def var(self, var: Var, val: Expr|None, tname: str|None) -> Block:
|
|
164
|
+
assign = f" = {val}" if val else ""
|
|
165
|
+
decl = "let " if tname else ""
|
|
166
|
+
return [ f"{decl}{var}{assign}{self._eoi}" ]
|
|
167
|
+
|
|
168
|
+
def int_var(self, var: Var, val: IntExpr|None = None, declare: bool = False) -> Block:
|
|
169
|
+
return self.var(var, val, self._int_t if declare else None)
|
|
170
|
+
|
|
171
|
+
#
|
|
172
|
+
# reporting
|
|
173
|
+
#
|
|
174
|
+
def is_reporting(self) -> BoolExpr:
|
|
175
|
+
return "rep !== null"
|
|
176
|
+
|
|
177
|
+
def report(self, msg: str, path: Var) -> Block:
|
|
178
|
+
return ([ f"rep !== null && rep.push([{self.esc(msg)}, {path}])" ]
|
|
179
|
+
if self._with_report else [])
|
|
180
|
+
|
|
181
|
+
def clean_report(self) -> Block:
|
|
182
|
+
return [ "if (rep !== null) rep.length = 0" ]
|
|
183
|
+
|
|
184
|
+
#
|
|
185
|
+
# path management
|
|
186
|
+
#
|
|
187
|
+
def path_val(self, pvar: Var, pseg: str|int, is_prop: bool) -> PathExpr:
|
|
188
|
+
# note: segment is a variable name for a prop or an integer
|
|
189
|
+
return f"{pvar} ? {pvar}.concat([{pseg}]) : null" if self._with_path else "null"
|
|
190
|
+
|
|
191
|
+
def path_lvar(self, lvar: Var, rvar: Var) -> PathExpr:
|
|
192
|
+
return f"({rvar} ? {lvar} : null)" if self._with_path else "null"
|
|
193
|
+
|
|
194
|
+
#
|
|
195
|
+
# blocks
|
|
196
|
+
#
|
|
197
|
+
def indent(self, block: Block, sep: bool = True) -> Block:
|
|
198
|
+
indented = super().indent(block, sep)
|
|
199
|
+
return ([ "{" ] + indented + [ "}" ]) if sep else indented
|
|
200
|
+
|
|
201
|
+
def int_loop(self, idx: Var, start: IntExpr, end: IntExpr, body: Block) -> Block:
|
|
202
|
+
return [
|
|
203
|
+
f"for (let {idx} = {start}; {idx} < {end}; {idx}++)"
|
|
204
|
+
] + self.indent(body)
|
|
205
|
+
|
|
206
|
+
def arr_loop(self, arr: Var, idx: Var, val: Var, body: Block) -> Block:
|
|
207
|
+
# forEach: KO, cannot break
|
|
208
|
+
return self.int_loop(idx, "0", f"{arr}.length", [ f"let {val} = {arr}[{idx}]" ] + body)
|
|
209
|
+
|
|
210
|
+
def obj_loop(self, obj: Var, key: Var, val: Var, body: Block) -> Block:
|
|
211
|
+
return [
|
|
212
|
+
f"for (const [{key}, {val}] of Object.entries({obj}))"
|
|
213
|
+
] + self.indent(body)
|
|
214
|
+
|
|
215
|
+
def if_stmt(self, cond: BoolExpr, true: Block, false: Block = []) -> Block:
|
|
216
|
+
if true and false:
|
|
217
|
+
return [ f"if ({cond})" ] + self.indent(true) + ["else"] + self.indent(false)
|
|
218
|
+
elif true:
|
|
219
|
+
return [ f"if ({cond})" ] + self.indent(true)
|
|
220
|
+
else:
|
|
221
|
+
return [ f"if (!({cond}))" ] + self.indent(false)
|
|
222
|
+
|
|
223
|
+
def mif_stmt(self, cond_true: list[tuple[BoolExpr, Block]], false: Block = []) -> Block:
|
|
224
|
+
code, op = [], "if"
|
|
225
|
+
for cond, true in cond_true:
|
|
226
|
+
code += [ f"{op} ({cond})" ]
|
|
227
|
+
code += self.indent(true)
|
|
228
|
+
op = "else if"
|
|
229
|
+
if false:
|
|
230
|
+
if op != "if":
|
|
231
|
+
code += [ "else" ]
|
|
232
|
+
code += self.indent(false)
|
|
233
|
+
else:
|
|
234
|
+
code += false
|
|
235
|
+
return code
|
|
236
|
+
|
|
237
|
+
#
|
|
238
|
+
# (Extended) Regular Expressions
|
|
239
|
+
#
|
|
240
|
+
def def_re(self, name: str, regex: str, opts: str) -> Block:
|
|
241
|
+
return [ f"const {name}_re = new runtime.RX({self.esc(regex)}, {self.esc(opts)})" ]
|
|
242
|
+
|
|
243
|
+
def sub_re(self, name: str, regex: str, opts: str) -> Block:
|
|
244
|
+
return [ f"const {name} = (s) => {name}_re.exec(s) !== null" ]
|
|
245
|
+
|
|
246
|
+
def match_var(self, var: str, val: Expr|None = None, declare: bool = False) -> Block:
|
|
247
|
+
return self.var(var, val, "Array")
|
|
248
|
+
|
|
249
|
+
def match_str_var(self, rname: str, var: str, val: str, declare: bool = False) -> Block:
|
|
250
|
+
return self.var(var, val, "String")
|
|
251
|
+
|
|
252
|
+
def match_re(self, name: str, var: str, regex: str, opts: str) -> BoolExpr:
|
|
253
|
+
return f"{name}_re.exec({var})"
|
|
254
|
+
|
|
255
|
+
def match_val(self, mname: str, rname: str, sname: str, dname: str) -> Block:
|
|
256
|
+
return [ f"{dname} = {mname}.groups[{self.esc(sname)}]" ]
|
|
257
|
+
|
|
258
|
+
def sub_strfun(self, name: str, body: Block) -> Block:
|
|
259
|
+
return [ f"function {name}(val, path, rep)" ] + self.indent(body)
|
|
260
|
+
|
|
261
|
+
#
|
|
262
|
+
# Property Map
|
|
263
|
+
#
|
|
264
|
+
def def_pmap(self, name: str, pmap: PropMap, public: bool) -> Block:
|
|
265
|
+
return [ f"var {name} = new Map()" ]
|
|
266
|
+
|
|
267
|
+
def ini_pmap(self, name: str, pmap: PropMap, public: bool) -> Block:
|
|
268
|
+
return [ f"{name}.set({self.esc(p)}, {f})" for p, f in pmap.items() ]
|
|
269
|
+
|
|
270
|
+
def sub_pmap(self, name: str, pmap: PropMap, public: bool) -> Block:
|
|
271
|
+
return []
|
|
272
|
+
|
|
273
|
+
def def_cset(self, name: str, constants: ConstList) -> Block:
|
|
274
|
+
return [ f"let {name} = new Set()" ]
|
|
275
|
+
|
|
276
|
+
def sub_cset(self, name: str, constants: ConstList) -> Block:
|
|
277
|
+
return []
|
|
278
|
+
|
|
279
|
+
def in_cset(self, name: str, var: Var, constants: ConstList) -> BoolExpr:
|
|
280
|
+
return f"{name}.has({var})"
|
|
281
|
+
|
|
282
|
+
def ini_cset(self, name: str, constants: ConstList) -> Block:
|
|
283
|
+
return [ f"{name}.add({self.const(v)})" for v in constants ]
|
|
284
|
+
|
|
285
|
+
def def_fun(self, name: str) -> Block:
|
|
286
|
+
return []
|
|
287
|
+
|
|
288
|
+
def sub_fun(self, name: str, body: Block) -> Block:
|
|
289
|
+
return [ f"function {name}(val, path, rep)" ] + self.indent(body)
|
|
290
|
+
|
|
291
|
+
def def_cmap(self, name: str, mapping: dict[JsonScalar, str]) -> Block:
|
|
292
|
+
return [ f"let {name} = new Map()" ]
|
|
293
|
+
|
|
294
|
+
def sub_cmap(self, name: str, mapping: dict[JsonScalar, str]) -> Block:
|
|
295
|
+
return [ ]
|
|
296
|
+
|
|
297
|
+
def ini_cmap(self, name: str, mapping: dict[JsonScalar, str]) -> Block:
|
|
298
|
+
return [ f"{name}.set({self.const(k)}, {m})" for k, m in mapping.items() ]
|
|
299
|
+
|
|
300
|
+
def get_cmap(self, name: str, tag: Var, ttag: type) -> Expr:
|
|
301
|
+
return f"{name}.get({tag})"
|
|
302
|
+
|
|
303
|
+
def gen_init(self, init: Block) -> Block:
|
|
304
|
+
code = [
|
|
305
|
+
"runtime.jm_set_rx(require('re2'))" if self._relib == "re2" else "runtime.jm_set_rx(RegExp)"
|
|
306
|
+
] + init
|
|
307
|
+
return self.file_subs("javascript_init.js", code)
|
|
308
|
+
|
|
309
|
+
def gen_free(self, free: Block) -> Block:
|
|
310
|
+
return self.file_subs("javascript_free.js", free)
|