@yar.ua/numerals
Version:
Number to text - Inflector for Ukrainian numerals
48 lines (47 loc) • 1.26 kB
JavaScript
import { SyntaxEdge } from "./edge.js";
import { DummyLexeme } from "../lexeme/dummy.js";
export class SyntaxNode {
constructor(lexeme) {
this.lexeme = lexeme;
this.edges = [];
}
add_child(lexeme, rel) {
const child = new SyntaxNode(lexeme);
return this.add_child_node(child, rel);
}
add_child_node(child, rel) {
const edge = new SyntaxEdge(this, child, rel);
this.edges.push(edge);
return child;
}
agree() {
this.edges
.slice()
.reverse()
.forEach((edge) => edge.agree());
}
lexemes() {
return [this.lexeme, ...this.edges.flatMap((e) => e.child.lexemes())];
}
text() {
return this.lexemes()
.map((l) => l.text())
.reverse()
.join(" ");
}
toObject() {
return {
kind: "SyntaxNode",
lexeme: this.lexeme.toObject(),
children: this.edges.map((e) => e.toObject()),
};
}
}
export class DummySyntaxNode extends SyntaxNode {
constructor(form, persistent_form) {
super(new DummyLexeme(form, persistent_form));
}
lexemes() {
return this.edges.flatMap((e) => e.child.lexemes());
}
}