Reputation: 435
Calling the getText()-Method on a Context-Object when parsing content with an antlr generated parser returns in case that all WS-Token are ignored in the parser (which might be the case quite often) returns a concatenation of the Token without any whitespace between them. E.g.: Parsing a Statement like
Hello beautiful World !
with the parsing rules
stmnt: 'Hello' 'beautiful' 'World' '!' ;
WS : ( '\t' | ' ' | '\r' | '\n')+ -> skip;
and then calling getText() on the Context when processing the stmnt-Rule would return the following String: "HellobeautifulWorld!"
My idea is to create a method, that iterates through the Context and concatenates all the tokens together with ws between them. Does anyone know if there is a method to create a not context-specific solution for this?
Upvotes: 1
Views: 123
Reputation: 170178
Replace -> skip
with -> channel(HIDDEN)
:
WS : ( '\t' | ' ' | '\r' | '\n')+ -> channel(HIDDEN);
This will cause the spaces to be placed on the HIDDEN
channel while the parser is fed from the DEFAULT
channel. However, getText()
will use both channels, preserving the spaces when called.
Upvotes: 1