context.getText() excludes spaces in ANTLR4

一曲冷凌霜 提交于 2019-12-02 16:11:22

问题


The getText() returns the complete statement excluding the spaces between the words. One way of considering the spaces is to include them in grammar. But, is there any other way to get the complete String with the spaces considered.


回答1:


Yes, there is (assuming here you are using ParserRuleContext.getText(). The idea is to ask the input char stream for a range of characters. The position values are stored in the start and stop tokens of the context.

Here's some code (converted from C++, so it might not be 100% correct):

string sourceTextForContext(ParseTree context) {
  Token startToken = (context.start instanceof TerminalNode) ? (TerminalNode)(start).getSymbol() : (ParserRuleContext)(start).start;
  Token stopToken = (context.stop instanceof TerminalNode) ? (TerminalNode)(stop).getSymbol() : (ParserRuleContext)(stop).start;

  CharStream cs = start.getTokenSource().getInputStream();
  int stopIndex = stop != null ? stop.getStopIndex() : -1;
  return cs.getText(new Interval(start.getStartIndex(), stopIndex));
}

Since this retrieval function uses the absolute char indexes, it doesn't count in any possible whitespace rule.



来源:https://stackoverflow.com/questions/50443728/context-gettext-excludes-spaces-in-antlr4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!