interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / protocol / synapse / claims / src / 30_parse.om

410 lines · 6602 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Strict ledger-line scanner. A line is accepted only in the exact canonical form this tool writes
3note (fixed key order, no spaces, canonical escapes), so "parse ok" also means "re-serialises to itself".
4note Results go to gp cells (offsets into the line); the cursor is gp[ppos], the line length gp[pln].
5
6const qseq ulong 0
7const qts ulong 1
8const qop ulong 2
9const qid ulong 3
10const qag ulong 4
11const qagl ulong 5
12const qse ulong 6
13const qsel ulong 7
14const qkind ulong 8
15const qtg ulong 9
16const qtgl ulong 10
17const qmode ulong 11
18const qttl ulong 12
19const qexp ulong 13
20const qst ulong 14
21const qin ulong 15
22const qinl ulong 16
23const qev ulong 17
24const qevl ulong 18
25const qor ulong 19
26const qorl ulong 20
27const qprev ulong 21
28const qhpos ulong 22
29const qhash ulong 23
30const qsig ulong 24
31const ppos ulong 30
32const pln ulong 31
33const pso ulong 32
34const psl ulong 33
35
36text kv "{\"v\":1,\"seq\":"
37text kts ",\"ts\":"
38text kop ",\"op\":"
39text kid ",\"id\":"
40text kag ",\"agent\":"
41text kse ",\"session\":"
42text kkind ",\"kind\":"
43text ktg ",\"target\":"
44text kmode ",\"mode\":"
45text kttl ",\"ttl_s\":"
46text kexp ",\"expires\":"
47text kst ",\"state\":"
48text kin ",\"intent\":"
49text kev ",\"evidence\":["
50text kor ",\"origin\":"
51text kprev ",\"prev\":"
52text khash ",\"hash\":"
53text tu00 "00"
54text ksig ",\"sig\":"
55
56fn sc_at s edit uchar * to ulong
57p = get gp ppos
58if ge p get gp pln
59ret 256
60end
61ret ulong get s p
62end
63
64fn sc_adv k ulong to void
65put gp ppos add get gp ppos k
66end
67
68fn sc_lit s edit uchar * t view uchar * to bool
69p = get gp ppos
70if gt add p len t get gp pln
71ret false
72end
73for i ulong len t
74if ne get s add p i get t i
75ret false
76end
77end
78sc_adv len t
79ret true
80end
81
82note sc_uint: 1..19 digits, no leading zero (except "0"); value or tbad
83fn sc_uint s edit uchar * to ulong
84v = ulong 0
85k = ulong 0
86while true bound 25
87c = sc_at s
88if or lt c 48 gt c 57
89break
90end
91if and eq k 1 eq v 0
92ret tbad
93end
94if ge k 19
95ret tbad
96end
97v = add mul v 10 sub c 48
98k = add k 1
99sc_adv 1
100end
101if eq k 0
102ret tbad
103end
104ret v
105end
106
107note sc_sf: quoted safe string (sf_ok charset, 1..256) -> gp[pso], gp[psl]
108fn sc_sf s edit uchar * to bool
109if ne sc_at s 34
110ret false
111end
112sc_adv 1
113st = get gp ppos
114k = ulong 0
115while true bound 300
116c = sc_at s
117if eq c 34
118break
119end
120if or or lt c 33 gt c 126 eq c 92
121ret false
122end
123if ge k 256
124ret false
125end
126k = add k 1
127sc_adv 1
128end
129if eq k 0
130ret false
131end
132sc_adv 1
133put gp pso st
134put gp psl k
135ret true
136end
137
138note sc_txt: quoted JSON string in this tool's canonical escaping (\" \\ \n \t \r, other controls
139note \u00xx lowercase); raw bytes >= 0x20 except quote/backslash. Span (escaped) -> gp[pso], gp[psl].
140fn sc_txt s edit uchar * to bool
141if ne sc_at s 34
142ret false
143end
144sc_adv 1
145st = get gp ppos
146while true bound 70000
147c = sc_at s
148if eq c 256
149ret false
150end
151if eq c 34
152put gp pso st
153put gp psl sub get gp ppos st
154sc_adv 1
155ret true
156end
157if lt c 32
158ret false
159end
160if eq c 92
161sc_adv 1
162d = sc_at s
163if or or or eq d 34 eq d 92 eq d 110 or eq d 116 eq d 114
164sc_adv 1
165continue
166end
167if ne d 117
168ret false
169end
170sc_adv 1
171if eq sc_lit s tu00 false
172ret false
173end
174h = sc_at s
175sc_adv 1
176l = sc_at s
177sc_adv 1
178if or gt h 49 lt h 48
179ret false
180end
181if eq hexv uchar and l 255 16
182ret false
183end
184if gt l 255
185ret false
186end
187x = add mul sub h 48 16 hexv uchar l
188if or or eq x 10 eq x 9 eq x 13
189ret false
190end
191continue
192end
193sc_adv 1
194end
195ret false
196end
197
198
199note sc_hex: quoted string of exactly k lowercase hex digits -> offset of first digit
200fn sc_hex s edit uchar * k ulong to ulong
201if ne sc_at s 34
202ret tbad
203end
204sc_adv 1
205st = get gp ppos
206for i ulong k
207c = sc_at s
208if gt c 255
209ret tbad
210end
211if eq hexv uchar c 16
212ret tbad
213end
214sc_adv 1
215end
216if ne sc_at s 34
217ret tbad
218end
219sc_adv 1
220ret st
221end
222
223note sc_word: quoted word from wall[base .. base+cnt) -> its wall index, or tbad
224fn sc_word s edit uchar * base ulong cnt ulong to ulong
225if eq sc_sf s false
226ret tbad
227end
228ret tw_find part s get gp pso get gp psl wall base cnt
229end
230
231fn sc_ts s edit uchar * to ulong
232if ne sc_at s 34
233ret tbad
234end
235sc_adv 1
236p = get gp ppos
237if gt add p 21 get gp pln
238ret tbad
239end
240v = ts_parse s p
241if eq v tbad
242ret tbad
243end
244sc_adv 20
245if ne sc_at s 34
246ret tbad
247end
248sc_adv 1
249ret v
250end
251
252note evidence list after `"evidence":[`: zero or more sf strings joined by commas, then `]`
253fn sc_evl s edit uchar * to bool
254st = get gp ppos
255if eq sc_at s 93
256put gp qev st
257put gp qevl 0
258sc_adv 1
259ret true
260end
261while true bound 70000
262if eq sc_sf s false
263ret false
264end
265c = sc_at s
266if eq c 93
267put gp qev st
268put gp qevl sub get gp ppos st
269sc_adv 1
270ret true
271end
272if ne c 44
273ret false
274end
275sc_adv 1
276end
277ret false
278end
279
280fn sc_put q ulong v ulong to bool
281put gp q v
282ret ne v tbad
283end
284
285fn sc_sfk s edit uchar * t view uchar * q ulong to bool
286if eq sc_lit s t false
287ret false
288end
289if eq sc_sf s false
290ret false
291end
292put gp q get gp pso
293put gp add q 1 get gp psl
294ret true
295end
296
297note pl_head: v .. ttl_s
298fn pl_head s edit uchar * to bool
299if eq sc_lit s kv false
300ret false
301end
302if eq sc_put qseq sc_uint s false
303ret false
304end
305if eq sc_lit s kts false
306ret false
307end
308if eq sc_put qts sc_ts s false
309ret false
310end
311if eq sc_lit s kop false
312ret false
313end
314if eq sc_put qop sc_word s 0 5 false
315ret false
316end
317if eq sc_lit s kid false
318ret false
319end
320if eq sc_put qid sc_hex s 16 false
321ret false
322end
323if eq sc_sfk s kag qag false
324ret false
325end
326if eq sc_sfk s kse qse false
327ret false
328end
329if eq sc_lit s kkind false
330ret false
331end
332if eq sc_put qkind sc_word s 13 5 false
333ret false
334end
335if eq sc_sfk s ktg qtg false
336ret false
337end
338if eq sc_lit s kmode false
339ret false
340end
341if eq sc_put qmode sc_word s 18 2 false
342ret false
343end
344if eq sc_lit s kttl false
345ret false
346end
347ret sc_put qttl sc_uint s
348end
349
350note pl_line: whole-line scan; true when s[0..n) is a canonical event line
351fn pl_line s edit uchar * n ulong to bool
352put gp ppos 0
353put gp pln n
354if eq pl_head s false
355ret false
356end
357if eq sc_lit s kexp false
358ret false
359end
360if eq sc_put qexp sc_ts s false
361ret false
362end
363if eq sc_lit s kst false
364ret false
365end
366if eq sc_put qst sc_word s 5 8 false
367ret false
368end
369if eq sc_lit s kin false
370ret false
371end
372if eq sc_txt s false
373ret false
374end
375put gp qin get gp pso
376put gp qinl get gp psl
377if eq sc_lit s kev false
378ret false
379end
380if eq sc_evl s false
381ret false
382end
383if eq sc_sfk s kor qor false
384ret false
385end
386if eq sc_lit s kprev false
387ret false
388end
389if eq sc_put qprev sc_hex s 64 false
390ret false
391end
392put gp qhpos get gp ppos
393if eq sc_lit s khash false
394ret false
395end
396if eq sc_put qhash sc_hex s 64 false
397ret false
398end
399if eq sc_lit s ksig false
400ret false
401end
402if eq sc_put qsig sc_hex s 128 false
403ret false
404end
405if ne sc_at s 125
406ret false
407end
408sc_adv 1
409ret eq get gp ppos n
410end