|
| 1 | +__all__ = ["parse", "unparse"] |
| 2 | + |
| 3 | +from antlr4 import InputStream, CommonTokenStream |
| 4 | +from .DspLexer import DspLexer |
| 5 | +from .DspParser import DspParser |
| 6 | +from .DspVisitor import DspVisitor |
| 7 | +from .DsLexer import DsLexer |
| 8 | +from .DsParser import DsParser |
| 9 | +from .DsVisitor import DsVisitor |
| 10 | + |
| 11 | + |
| 12 | +class ParseVisitor(DspVisitor): |
| 13 | + def visitRule_pool(self, ctx): |
| 14 | + return "\n\n".join(self.visit(r) for r in ctx.rule_()) |
| 15 | + |
| 16 | + def visitRule(self, ctx): |
| 17 | + result = [self.visit(t) for t in ctx.term()] |
| 18 | + if len(result) == 1: |
| 19 | + return f"----\n{result[0]}" |
| 20 | + else: |
| 21 | + conclusion = result.pop() |
| 22 | + length = max(len(premise) for premise in result) |
| 23 | + result.append("-" * max(length, 4)) |
| 24 | + result.append(conclusion) |
| 25 | + return "\n".join(result) |
| 26 | + |
| 27 | + def visitSymbol(self, ctx): |
| 28 | + return ctx.SYMBOL().getText() |
| 29 | + |
| 30 | + def visitParentheses(self, ctx): |
| 31 | + return self.visit(ctx.term()) |
| 32 | + |
| 33 | + def visitSubscript(self, ctx): |
| 34 | + return f"(subscript {' '.join(self.visit(t) for t in ctx.term())})" |
| 35 | + |
| 36 | + def visitFunction(self, ctx): |
| 37 | + return f"(function {' '.join(self.visit(t) for t in ctx.term())})" |
| 38 | + |
| 39 | + def visitUnary(self, ctx): |
| 40 | + return f"(unary {ctx.getChild(0).getText()} {self.visit(ctx.term())})" |
| 41 | + |
| 42 | + def visitBinary(self, ctx): |
| 43 | + return f"(binary {ctx.getChild(1).getText()} {self.visit(ctx.term(0))} {self.visit(ctx.term(1))})" |
| 44 | + |
| 45 | + |
| 46 | +class UnparseVisitor(DsVisitor): |
| 47 | + def visitRule_pool(self, ctx): |
| 48 | + return "\n".join(self.visit(r) for r in ctx.rule_()) |
| 49 | + |
| 50 | + def visitRule(self, ctx): |
| 51 | + result = [self.visit(t) for t in ctx.term()] |
| 52 | + conclusion = result.pop() |
| 53 | + return ", ".join(result) + " -> " + conclusion |
| 54 | + |
| 55 | + def visitSymbol(self, ctx): |
| 56 | + return ctx.SYMBOL().getText() |
| 57 | + |
| 58 | + def visitSubscript(self, ctx): |
| 59 | + return f"{self.visit(ctx.term(0))}[{', '.join(self.visit(t) for t in ctx.term()[1:])}]" |
| 60 | + |
| 61 | + def visitFunction(self, ctx): |
| 62 | + return f"{self.visit(ctx.term(0))}({', '.join(self.visit(t) for t in ctx.term()[1:])})" |
| 63 | + |
| 64 | + def visitUnary(self, ctx): |
| 65 | + return f"({ctx.getChild(0).getText()} {self.visit(ctx.term())})" |
| 66 | + |
| 67 | + def visitBinary(self, ctx): |
| 68 | + return f"({self.visit(ctx.term(0))} {ctx.getChild(1).getText()} {self.visit(ctx.term(1))})" |
| 69 | + |
| 70 | + |
| 71 | +def parse(input): |
| 72 | + chars = InputStream(input) |
| 73 | + lexer = DspLexer(chars) |
| 74 | + tokens = CommonTokenStream(lexer) |
| 75 | + parser = DspParser(tokens) |
| 76 | + tree = parser.rule_pool() |
| 77 | + visitor = ParseVisitor() |
| 78 | + return visitor.visit(tree) |
| 79 | + |
| 80 | + |
| 81 | +def unparse(input): |
| 82 | + chars = InputStream(input) |
| 83 | + lexer = DsLexer(chars) |
| 84 | + tokens = CommonTokenStream(lexer) |
| 85 | + parser = DsParser(tokens) |
| 86 | + tree = parser.rule_pool() |
| 87 | + visitor = UnparseVisitor() |
| 88 | + return visitor.visit(tree) |
0 commit comments