javascript中antlr4访问者中的自定义上下文变量
我想用javascript变量中的值替换输入中的变量。变量名可能是一个具有中间或结尾函数的对象的路径:<代码>对象>属性。 其语法如下所示:javascript中antlr4访问者中的自定义上下文变量,javascript,antlr,antlr4,visitor,Javascript,Antlr,Antlr4,Visitor,我想用javascript变量中的值替换输入中的变量。变量名可能是一个具有中间或结尾函数的对象的路径:对象>属性。 其语法如下所示: funParameter: '(' value? (', ' value)* ')' ; value: INT | BOOL | STRING | variable; varname: VAR ; variable: (varname | '{' value '}') funParameter? ('.' variable)* ; variable的访问者函数
funParameter: '(' value? (', ' value)* ')' ;
value: INT | BOOL | STRING | variable;
varname: VAR ;
variable: (varname | '{' value '}') funParameter? ('.' variable)* ;
variable
的访问者函数(涉及customValue
的所有内容都是我想象中的方法):
问题是,我不知道如何将JS变量的当前值向下传递到解析树。当计算object
时,我需要向下传递访问者中的JS对象,以便在下一个visitVariable
中,很明显我想要返回object.property
的值。
如有任何提示,我们将不胜感激 它可能有点被黑客攻击,但是文档中描述的函数
getParent()
,在ctx
-对象中不可用。但是我找到了ctx.parentCtx
。现在我只是分配ctx.customValue='somevalue'
,并且可以通过ctx.parentCtx.customValue
从子对象访问它,它可能有点被黑客攻击,但是文档中描述的getParent()
函数在ctx
对象中不可用。但是我找到了ctx.parentCtx
。现在我只分配ctx.customValue='somevalue'
,并可以通过ctx.parentCtx.customValue
从子级访问它
visitVariable(ctx) {
var key, val;
if(ctx.value()) {
key = this.visit(ctx.value());
} else if(ctx.varname()) {
key = ctx.varname().getText();
}
if(ctx.funParameter()) {
//idea:
val = this.callFunction(key, this.visit(ctx.funParameter()), ctx.customValue); //pass value to function call
} else {
//idea:
if(ctx.customValue) {
val = ctx.customValue[key]; //Get value from parent
} else {
val = this.vars[key]; //Get value from JS-variables
}
}
if(ctx.variable()) {
//idea:
val = this.visit(ctx.variable(), {customValue:val});
}
return val;
}