cortex
aien-dev/cortex
Brought by drakestapleton
cortex / tests / fts / gen_parse_tables.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Generator (not product code) for the parser tables in om/fts.om. |
| 3 | |
| 4 | Copies the Lemon LALR tables of SQLite 3.45.0's FTS5 expression grammar (fts5parse.y as |
| 5 | generated into sqlite3.c): fts5yy_action, fts5yy_lookahead, fts5yy_shift_ofst, |
| 6 | fts5yy_reduce_ofst (stored +100, it is signed), fts5yy_default, fts5yyRuleInfoLhs and |
| 7 | fts5yyRuleInfoNRhs (stored negated, i.e. as the RHS length). Prints Omega `const` offsets |
| 8 | and one text literal `qptx`. |
| 9 | |
| 10 | Usage: python3 tests/fts/gen_parse_tables.py PATH/TO/sqlite3.c |
| 11 | """ |
| 12 | import hashlib, re, sys |
| 13 | |
| 14 | src = open(sys.argv[1], encoding='latin-1').read() |
| 15 | |
| 16 | |
| 17 | def 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 | |
| 25 | act = arr('static const fts5YYACTIONTYPE fts5yy_action[] =') |
| 26 | look = arr('static const fts5YYCODETYPE fts5yy_lookahead[] =') |
| 27 | sh = arr('static const unsigned char fts5yy_shift_ofst[] =') |
| 28 | red = arr('static const signed char fts5yy_reduce_ofst[] =') |
| 29 | dfl = arr('static const fts5YYACTIONTYPE fts5yy_default[] =') |
| 30 | lhs = arr('static const fts5YYCODETYPE fts5yyRuleInfoLhs[] =') |
| 31 | nrhs = arr('static const signed char fts5yyRuleInfoNRhs[] =') |
| 32 | assert (len(act), len(look), len(sh), len(red), len(dfl), len(lhs), len(nrhs)) == (105, 121, 35, 18, 35, 28, 28) |
| 33 | parts = [('QPA', act), ('QPL', look), ('QPS', sh), ('QPR', [v + 100 for v in red]), ('QPD', dfl), |
| 34 | ('QPH', lhs), ('QPN', [-v for v in nrhs])] |
| 35 | print('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()) |
| 37 | off = 0 |
| 38 | allv = [] |
| 39 | for name, vals in parts: |
| 40 | print('const %s ulong %d' % (name, off)) |
| 41 | off += len(vals) |
| 42 | allv += vals |
| 43 | print('const QPT ulong %d' % off) |
| 44 | print('text qptx "' + ' '.join(str(v) for v in allv) + '"') |