interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / fts / gen_parse_tables.py

44 lines · 1892 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Generator (not product code) for the parser tables in om/fts.om.
3
4Copies the Lemon LALR tables of SQLite 3.45.0's FTS5 expression grammar (fts5parse.y as
5generated into sqlite3.c): fts5yy_action, fts5yy_lookahead, fts5yy_shift_ofst,
6fts5yy_reduce_ofst (stored +100, it is signed), fts5yy_default, fts5yyRuleInfoLhs and
7fts5yyRuleInfoNRhs (stored negated, i.e. as the RHS length). Prints Omega `const` offsets
8and one text literal `qptx`.
9
10Usage: python3 tests/fts/gen_parse_tables.py PATH/TO/sqlite3.c
11"""
12import hashlib, re, sys
13
14src = open(sys.argv[1], encoding='latin-1').read()
15
16
17def arr(decl):
18 i = src.index(decl)
19 j = src.index('{', i)
20 k = src.index('};', j)
21 txt = re.sub(r'/\*.*?\*/', '', src[j + 1:k], flags=re.S)
22 return [int(x) for x in re.findall(r'-?\d+', txt)]
23
24
25act = arr('static const fts5YYACTIONTYPE fts5yy_action[] =')
26look = arr('static const fts5YYCODETYPE fts5yy_lookahead[] =')
27sh = arr('static const unsigned char fts5yy_shift_ofst[] =')
28red = arr('static const signed char fts5yy_reduce_ofst[] =')
29dfl = arr('static const fts5YYACTIONTYPE fts5yy_default[] =')
30lhs = arr('static const fts5YYCODETYPE fts5yyRuleInfoLhs[] =')
31nrhs = arr('static const signed char fts5yyRuleInfoNRhs[] =')
32assert (len(act), len(look), len(sh), len(red), len(dfl), len(lhs), len(nrhs)) == (105, 121, 35, 18, 35, 28, 28)
33parts = [('QPA', act), ('QPL', look), ('QPS', sh), ('QPR', [v + 100 for v in red]), ('QPD', dfl),
34 ('QPH', lhs), ('QPN', [-v for v in nrhs])]
35print('note generated by tests/fts/gen_parse_tables.py from sqlite3.c sha256 %s (SQLite 3.45.0)'
36 % hashlib.sha256(src.encode('latin-1')).hexdigest())
37off = 0
38allv = []
39for name, vals in parts:
40 print('const %s ulong %d' % (name, off))
41 off += len(vals)
42 allv += vals
43print('const QPT ulong %d' % off)
44print('text qptx "' + ' '.join(str(v) for v in allv) + '"')