Skip to content

Commit 0648afc

Browse files
committed
Update bnf binding for js.
1 parent 2bc8ab8 commit 0648afc

3 files changed

Lines changed: 107 additions & 208 deletions

File tree

bnf/atsds_bnf/index.js

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,107 @@
1-
/**
2-
* BNF Parser and Unparsers for DS
3-
*
4-
* This package provides bidirectional conversion between:
5-
* - Ds: The lisp-like syntax currently used in DS
6-
* - Dsp: A traditional readable syntax
7-
*/
8-
9-
export { unparse } from './unparse.js';
10-
export { parse } from './parse.js';
1+
import {
2+
InputStream,
3+
CommonTokenStream
4+
} from "antlr4";
5+
import DspLexer from "./DspLexer.js";
6+
import DspParser from "./DspParser.js";
7+
import DspVisitor from "./DspVisitor.js";
8+
import DsLexer from "./DsLexer.js";
9+
import DsParser from "./DsParser.js";
10+
import DsVisitor from "./DsVisitor.js";
11+
12+
class ParseVisitor extends DspVisitor {
13+
visitRule_pool(ctx) {
14+
return ctx.rule_().map(r => this.visit(r)).join("\n\n");
15+
}
16+
17+
visitRule(ctx) {
18+
const result = ctx.term().map(t => this.visit(t));
19+
if (result.length === 1) {
20+
return `----\n${result[0]}`;
21+
} else {
22+
const conclusion = result.pop();
23+
const length = Math.max(...result.map(premise => premise.length));
24+
result.push("-".repeat(Math.max(length, 4)));
25+
result.push(conclusion);
26+
return result.join("\n");
27+
}
28+
}
29+
30+
visitSymbol(ctx) {
31+
return ctx.SYMBOL().getText();
32+
}
33+
34+
visitParentheses(ctx) {
35+
return this.visit(ctx.term());
36+
}
37+
38+
visitSubscript(ctx) {
39+
return `(subscript ${ctx.term().map(t => this.visit(t)).join(" ")})`;
40+
}
41+
42+
visitFunction(ctx) {
43+
return `(function ${ctx.term().map(t => this.visit(t)).join(" ")})`;
44+
}
45+
46+
visitUnary(ctx) {
47+
return `(unary ${ctx.getChild(0).getText()} ${this.visit(ctx.term())})`;
48+
}
49+
50+
visitBinary(ctx) {
51+
return `(binary ${ctx.getChild(1).getText()} ${this.visit(ctx.term(0))} ${this.visit(ctx.term(1))})`;
52+
}
53+
}
54+
55+
56+
class UnparseVisitor extends DsVisitor {
57+
visitRule_pool(ctx) {
58+
return ctx.rule_().map(r => this.visit(r)).join("\n");
59+
}
60+
61+
visitRule(ctx) {
62+
const result = ctx.term().map(t => this.visit(t));
63+
const conclusion = result.pop();
64+
const length = Math.max(...result.map(premise => premise.length));
65+
return result.join(", ") + " -> " + conclusion;
66+
}
67+
68+
visitSymbol(ctx) {
69+
return ctx.SYMBOL().getText();
70+
}
71+
72+
visitSubscript(ctx) {
73+
return `${this.visit(ctx.term(0))}[${ctx.term().slice(1).map(t => this.visit(t)).join(", ")}]`;
74+
}
75+
76+
visitFunction(ctx) {
77+
return `${this.visit(ctx.term(0))}(${ctx.term().slice(1).map(t => this.visit(t)).join(", ")})`;
78+
}
79+
80+
visitUnary(ctx) {
81+
return `(${ctx.getChild(0).getText()} ${this.visit(ctx.term())})`;
82+
}
83+
84+
visitBinary(ctx) {
85+
return `(${this.visit(ctx.term(0))} ${ctx.getChild(1).getText()} ${this.visit(ctx.term(1))})`;
86+
}
87+
}
88+
89+
export function parse(input) {
90+
const chars = new InputStream(input);
91+
const lexer = new DspLexer(chars);
92+
const tokens = new CommonTokenStream(lexer);
93+
const parser = new DspParser(tokens);
94+
const tree = parser.rule_pool();
95+
const visitor = new ParseVisitor();
96+
return visitor.visit(tree);
97+
}
98+
99+
export function unparse(input) {
100+
const chars = new InputStream(input);
101+
const lexer = new DsLexer(chars);
102+
const tokens = new CommonTokenStream(lexer);
103+
const parser = new DsParser(tokens);
104+
const tree = parser.rule_pool();
105+
const visitor = new UnparseVisitor();
106+
return visitor.visit(tree);
107+
}

bnf/atsds_bnf/parse.js

Lines changed: 0 additions & 111 deletions
This file was deleted.

bnf/atsds_bnf/unparse.js

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)