ANTLR4 using the HIDDEN channel causes errors when using skips

In my grammar, I use:

WS: [ \t\r\n]+ -> skip;

when I change this to use the HIDDEN channel:

WS: [ \t\r\n]+ -> channel(HIDDEN);

I get errors (rogue input ...), I didn’t receive when using “skip.” I thought that skipping and sending to the channel are not different when it comes to content passed to the parser.

Below you can find the code fragment in which the parser is executed:

    CharStream charStream = new ANTLRInputStream(formulaString);
    FormulaLexer lexer = new FormulaLexer(charStream);
    BufferedTokenStream tokens = new BufferedTokenStream(lexer);
    FormulaParser parser = new FormulaParser(tokens);
    ParseTree tree = parser.startRule();

    StartRuleVisitor startRuleVisitor = new StartRuleVisitor();
    startRuleVisitor.visit(tree);

    VariableVisitor variableVisitor = new VariableVisitor(tokens);
    variableVisitor.visit(tree);

And the grammar itself:

grammar Formula;


startRule
   : variable RELATION_OPERATOR integer
   ;

integer
   : DIGIT+
   ;

identifier
   : (LETTER | DIGIT) ( DIGIT | LETTER | '_' | '.')+
   ;

tableId
   : 'T_' (identifier | WILDCARD)
   ;

rowId
   : 'R_' (identifier | WILDCARD)
   ;

columnId
   : 'C_' (identifier | WILDCARD)
   ;

sheetId
   : 'S_' (identifier | WILDCARD)
   ;

variable
   : L_CURLY_BRACKET cellIdComponent (COMMA cellIdComponent)+ R_CURLY_BRACKET
   ;

cellIdComponent
   : tableId | rowId | columnId | sheetId
   ;

COMMA
   : ','
   ;

RELATION_OPERATOR
   : EQ
   ;

WILDCARD
   : 'NNN'
   ;

L_CURLY_BRACKET
   : '{'
   ;

R_CURLY_BRACKET
   : '}'
   ;


LETTER
   : ('a' .. 'z') | ('A' .. 'Z')
   ;

DIGIT
   : ('0' .. '9')
   ;


EQ
   : '='
   | 'EQ' | 'eq'
   ;


WS
   : [ \t\r\n]+ -> channel(HIDDEN)
   ;

String I'm trying to parse:

{T_C 00.01, R_010,   C_010} = 1

The output I get with the channel used (HIDDEN):

line 1:4 extraneous input ' ' expecting {'_', '.', LETTER, DIGIT}
line 1:11 extraneous input ' ' expecting {'T_', 'R_', 'C_', 'S_'}
line 1:18 extraneous input '   ' expecting {'T_', 'R_', 'C_', 'S_'}
line 1:27 extraneous input ' ' expecting RELATION_OPERATOR
line 1:29 extraneous input ' ' expecting DIGIT

But if I change the channel (HIDDEN) to “skip”, there will be no errors.

, , , , " ...", (HIDDEN), "".

, ?

+4
1

CommonTokenStream BufferedTokenStream. . BufferedTokenStream github:

{@link Token # getChannel}. , , , {@link Token # DEFAULT_CHANNEL} {@Link Token # HIDDEN_CHANNEL}, , {@link CommonTokenStream}.

+1

Source: https://habr.com/ru/post/1669837/


All Articles