expr
returns[v: number]:
a = expr op = ('*' | '/') b = expr {
if ( $op.type==MUL ) $v = $a.v * $b.v;
else $v = $a.v / $b.v;
}
| a = expr op = ('+' | '-') b = expr {
if ( $op.type==ADD ) $v = $a.v + $b.v;
else $v = $a.v - $b.v;
}
| INT {$v = Number.parseInt($INT.text,10);}
| ID {$v = this.memory[$ID.text];}
| '(' e = expr ')' {$v = $e.v;};
MUL: '*'; // assigns token name to '*' used above in grammar
DIV: '/';
ADD: '+';
SUB: '-';
The generated code from is flagged by Typescript on the naked MULand ADDin the actions, suggesting "Cannot find name 'ADD'. Did you mean the static member 'ActionExprParser.ADD'?"
We might make actions including this sort of code work by declaring constants for ADD and MUL in the outermost scope of the generated parser, or perhaps even a const enum (with a consistant name!)
In ActionExpr.g4, I've got the following:
The generated code from is flagged by Typescript on the naked
MUL
andADD
in the actions, suggesting "Cannot find name 'ADD'. Did you mean the static member 'ActionExprParser.ADD'?"We might make actions including this sort of code work by declaring constants for ADD and MUL in the outermost scope of the generated parser, or perhaps even a const enum (with a consistant name!)