Work in progress on generic parser

This commit is contained in:
2026-05-04 22:24:48 +02:00
parent eef2630af7
commit 3047649372
2 changed files with 144 additions and 0 deletions

View File

@@ -0,0 +1,136 @@
import { Row_Based_Table } from '@efforting.tech/table';
import { load_raster_table } from '@efforting.tech/table/raster-table';
import { RegExp_Tokenizer } from '@efforting.tech/parsing/regexp-dispatch';
import { RegExp_Token_Parsing_Rule, Parser } from '@efforting.tech/parsing/generic-parsing';
function load_table(raster) {
const table = load_raster_table(raster, Row_Based_Table);
table.replace_all_cells(({cell}) => cell.trim());
return table;
}
const logic_ops = load_table(`
name symbol
---- ------
AND ∧
OR
XOR ⊕
NAND ↑
NOR ↓
XNOR ⊙
IMPLIES →
IFF ↔
NOT ¬
`);
const generic_ops = load_table(`
name symbol
---- ------
PLUS +
HYPHEN -
DOT ·
ASTERISK *
CROSS ×
SLASH /
CARET ^
UNDERSCORE _
PERCENT %
`);
const punctuation = load_table(`
name symbol
---- ------
COMMA ,
SEMI_COLON ;
COLON :
PERIOD .
`);
const grouping = load_table(`
name left right
---- ---- -----
PARENTESIS ( )
SQUARE_BRACKET [ ]
CURLY_BRACE { }
ANGLE_BRACKET ⟨ ⟩
DOUBLE_ARROW_BRACKET « »
`);
const greek_chars = load_table(`
name lower upper
---- ----- -----
ALPHA α Α
BETA β Β
GAMMA γ Γ
DELTA δ Δ
EPSILON ε Ε
ZETA ζ Ζ
ETA η Η
THETA θ Θ
IOTA ι Ι
KAPPA κ Κ
LAMBDA λ Λ
MU μ Μ
NU ν Ν
XI ξ Ξ
OMICRON ο Ο
PI π Π
RHO ρ Ρ
SIGMA σ Σ
TAU τ Τ
UPSILON υ Υ
PHI φ Φ
CHI χ Χ
PSI ψ Ψ
OMEGA ω Ω
`);
const rt = new RegExp_Tokenizer();
for (const { name, left, right } of grouping.iter_objects()) {
rt.add_rules(new RegExp_Token_Parsing_Rule(RegExp.escape(left),
(tokenizer, match) => tokenizer.enter_sub_tokenizer(undefined,
(tokenizer, value) => tokenizer.push_token(
{kind: 'grouping', name, value}
)
), `LEFT_${name}`
));
rt.add_rules(new RegExp_Token_Parsing_Rule(RegExp.escape(right),
(tokenizer, match) => tokenizer.leave_sub_tokenizer(), `RIGHT_${name}`)
);
}
for (const table of [logic_ops, generic_ops, punctuation]) {
for (const { name, symbol } of table.iter_objects()) {
rt.add_rules(new RegExp_Token_Parsing_Rule(RegExp.escape(symbol),
(tokenizer, match) => tokenizer.push_token(match.value), name)
);
}
}
for (const { name, lower, upper } of greek_chars.iter_objects()) {
rt.add_rules(new RegExp_Token_Parsing_Rule(RegExp.escape(lower),
(tokenizer, match) => tokenizer.push_token(match.value), `LOWER_${name}`)
);
rt.add_rules(new RegExp_Token_Parsing_Rule(RegExp.escape(upper),
(tokenizer, match) => tokenizer.push_token(match.value), `UPPER_${name}`)
);
}
console.log(rt.rules.at(-1));

View File

@@ -146,12 +146,20 @@ export class Row_Based_Table {
} }
//TODO: Implement map and other functions expected by collections
*[Symbol.iterator]() { *[Symbol.iterator]() {
for (const index of this.rows.keys()) { for (const index of this.rows.keys()) {
yield new Table_Row_Reference(this, index); yield new Table_Row_Reference(this, index);
} }
} }
*iter_objects() {
for (const index of this.rows.keys()) {
yield new Table_Row_Reference(this, index).object;
}
}
} }