|
| 1 | +import { InputStream, CommonTokenStream } from "antlr4"; |
| 2 | +import DspLexer from "./DspLexer.js"; |
| 3 | +import DspParser from "./DspParser.js"; |
| 4 | +import DspVisitor from "./DspVisitor.js"; |
| 5 | +import DsLexer from "./DsLexer.js"; |
| 6 | +import DsParser from "./DsParser.js"; |
| 7 | +import DsVisitor from "./DsVisitor.js"; |
| 8 | + |
| 9 | +class ParseVisitor extends DspVisitor { |
| 10 | + visitRule_pool(ctx: any): string { |
| 11 | + return ctx |
| 12 | + .rule_() |
| 13 | + .map((r: any) => this.visit(r)) |
| 14 | + .join("\n\n"); |
| 15 | + } |
| 16 | + |
| 17 | + visitRule(ctx: any): string { |
| 18 | + const result = ctx.term().map((t: any) => 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: string) => 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: any): string { |
| 31 | + return ctx.SYMBOL().getText(); |
| 32 | + } |
| 33 | + |
| 34 | + visitParentheses(ctx: any): string { |
| 35 | + return this.visit(ctx.term()); |
| 36 | + } |
| 37 | + |
| 38 | + visitSubscript(ctx: any): string { |
| 39 | + return `(subscript ${ctx |
| 40 | + .term() |
| 41 | + .map((t: any) => this.visit(t)) |
| 42 | + .join(" ")})`; |
| 43 | + } |
| 44 | + |
| 45 | + visitFunction(ctx: any): string { |
| 46 | + return `(function ${ctx |
| 47 | + .term() |
| 48 | + .map((t: any) => this.visit(t)) |
| 49 | + .join(" ")})`; |
| 50 | + } |
| 51 | + |
| 52 | + visitUnary(ctx: any): string { |
| 53 | + return `(unary ${ctx.getChild(0).getText()} ${this.visit(ctx.term())})`; |
| 54 | + } |
| 55 | + |
| 56 | + visitBinary(ctx: any): string { |
| 57 | + return `(binary ${ctx.getChild(1).getText()} ${this.visit(ctx.term(0))} ${this.visit(ctx.term(1))})`; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +class UnparseVisitor extends DsVisitor { |
| 62 | + visitRule_pool(ctx: any): string { |
| 63 | + return ctx |
| 64 | + .rule_() |
| 65 | + .map((r: any) => this.visit(r)) |
| 66 | + .join("\n"); |
| 67 | + } |
| 68 | + |
| 69 | + visitRule(ctx: any): string { |
| 70 | + const result = ctx.term().map((t: any) => this.visit(t)); |
| 71 | + const conclusion = result.pop(); |
| 72 | + return result.join(", ") + " -> " + conclusion; |
| 73 | + } |
| 74 | + |
| 75 | + visitSymbol(ctx: any): string { |
| 76 | + return ctx.SYMBOL().getText(); |
| 77 | + } |
| 78 | + |
| 79 | + visitSubscript(ctx: any): string { |
| 80 | + return `${this.visit(ctx.term(0))}[${ctx |
| 81 | + .term() |
| 82 | + .slice(1) |
| 83 | + .map((t: any) => this.visit(t)) |
| 84 | + .join(", ")}]`; |
| 85 | + } |
| 86 | + |
| 87 | + visitFunction(ctx: any): string { |
| 88 | + return `${this.visit(ctx.term(0))}(${ctx |
| 89 | + .term() |
| 90 | + .slice(1) |
| 91 | + .map((t: any) => this.visit(t)) |
| 92 | + .join(", ")})`; |
| 93 | + } |
| 94 | + |
| 95 | + visitUnary(ctx: any): string { |
| 96 | + return `(${ctx.getChild(1).getText()} ${this.visit(ctx.term())})`; |
| 97 | + } |
| 98 | + |
| 99 | + visitBinary(ctx: any): string { |
| 100 | + return `(${this.visit(ctx.term(0))} ${ctx.getChild(1).getText()} ${this.visit(ctx.term(1))})`; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Parse Dsp format (human-readable) to Ds format (internal S-expression) |
| 106 | + * @param input - The Dsp format string to parse |
| 107 | + * @returns The Ds format string |
| 108 | + */ |
| 109 | +export function parse(input: string): string { |
| 110 | + const chars = new InputStream(input); |
| 111 | + const lexer = new DspLexer(chars); |
| 112 | + const tokens = new CommonTokenStream(lexer); |
| 113 | + const parser = new DspParser(tokens); |
| 114 | + const tree = parser.rule_pool(); |
| 115 | + const visitor = new ParseVisitor(); |
| 116 | + return visitor.visit(tree); |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Unparse Ds format (internal S-expression) to Dsp format (human-readable) |
| 121 | + * @param input - The Ds format string to unparse |
| 122 | + * @returns The Dsp format string |
| 123 | + */ |
| 124 | +export function unparse(input: string): string { |
| 125 | + const chars = new InputStream(input); |
| 126 | + const lexer = new DsLexer(chars); |
| 127 | + const tokens = new CommonTokenStream(lexer); |
| 128 | + const parser = new DsParser(tokens); |
| 129 | + const tree = parser.rule_pool(); |
| 130 | + const visitor = new UnparseVisitor(); |
| 131 | + return visitor.visit(tree); |
| 132 | +} |
0 commit comments