我正在创建一个基于python的DSL,只是为了好玩。现在,它只是将文本编译成一个直接执行python代码的AST。我的意思是,如果我写:
a = int(read("input something: "))
b = a**2
它将其转换为如下树:
Program:
VariableSet a:
Call:
VariableGet int
Call:
VariableGet read
Literal("input something: ")
VariableSet b:
BinaryExpession **:
VariableGet a
Literal(2)
每个节点都将使用如下代码执行:
class VariableSet(object):
def __init__(self, name, expr):
self.name = name
self.expr = expr
def __call__(self, scope):
value = self.expr(scope)
scope.put(self.name, value)
return value
它确实很有效。我想,当某些指令抛出异常时,异常堆栈回溯指向DSL源代码,而不是python AST代码。现在,我看到的只有:
Traceback (most recent call last):
File "parser.py", line 164, in <module>
parse(data)(scope)
File "/home/juanplopes/Projects/my-parser/ast.py", line 158, in __call__
result = expr(scope)
File "/home/juanplopes/Projects/my-parser/ast.py", line 63, in __call__
value = self.expr(scope)
File "/home/juanplopes/Projects/my-parser/ast.py", line 81, in __call__
return self.method(scope)(*[arg(scope) for arg in self.args])
File "/home/juanplopes/Projects/my-parser/ast.py", line 81, in __call__
return self.method(scope)(*[arg(scope) for arg in self.args])
File "parser.py", line 153, in read
raise Exception('some exception')
Exception: some exception
有没有办法自定义如何在python中生成回溯?
答案 0 :(得分:3)
是的,但这样做很难看。
Jinja是一个模板引擎。它处理回溯,以便它们指向模板错误 也许您可以将其转换为您的代码,以便它指向DSL错误。