开发者

Returning multiple values in ANTLR rule

开发者 https://www.devze.com 2023-01-07 13:05 出处:网络
I have an ANTLR rule like this receive returns[Evaluator e,String message] : RECEIVE FILENAME {$e= new ReceiveEvaluator($FILENAME.text);}

I have an ANTLR rule like this

receive returns[Evaluator e,String message]
  : RECEIVE FILENAME {$e= new ReceiveEvaluator($FILENAME.text);}
  ;

I have added a new return message and I want to put the file c开发者_如何转开发ontent in that. One way I could do is make the evaluator return the String when I walk the tree by calling the evaluate() method.

I was wondering if I could do it strightaway here - but am not aware how to set multiple return values and access them later.

Thanks Hari


Here's how to set- and use multiple return values:

parse
  :  r=receive {
       Evaluator e = $r.evaluator;
       String m = $r.message;
     }
  ;

receive returns[Evaluator evaluator, String message]
  :  RECEIVE f=FILENAME {
       $evaluator = new ReceiveEvaluator($f.text);
       $message = "Some message here...";
     }
  ;
0

精彩评论

暂无评论...
验证码 换一张
取 消