c# 4.0 - Access Channels in ANTLR 4 and Parse them separately -
i have included comments in separate channel in antlr 4. in case channel 2.
this lexer grammar.
comment: '/*' .*? '*/' -> channel(2) ; i want access channel 2 , parse on channel accumulate comments. included in parsing grammar below
comment :comment ; in program
string s = " paring string" antlrinputstream input = new antlrinputstream(s); csslexer lexer = new csslexer(input); commontokenstream tokens = new commontokenstream(lexer,2); then want parsing on tokens
var xr = parser.comment().getrulecontexts<commentcontext>(); because want information commentcontext object such start.column etc.
edit:
this improved question
to more specific, want tokens in channel 2 , parse them using comment grammar comments list(ireadonly<commentcontext>) can iterate through each of these , access information such as, start line, start column, end line end column, , token text.
commontokenstream tokens = new commontokenstream(lexer,2); this not giving me tokens in channel 2. , thing discovered until these tokens passed arguments parser construct xparser parser = new xparser(tokens);
then can access the tokens calling gettokens().in tokes can see there comments identified tokens , in channel 2. though commenttokenstrem species channel number above. contains tokens.
what reason of not able access tokens until parser object created using tokens?
i want
commenttokenstremin channel 2 , pass xparser object creation parse these tokens usingcommentgrammar. best way of doing in antlr 4 api?
commontokenstream internally tracks tokens channel. thing won't see when call gettokens() lexer rules -> skip action executed (no token created rules).
you can @ tokens on channel 2 using tokenstream.lt , intstream.consume methods.
java example
commontokenstream cts = new commontokenstream(tokensource, 2); list<token> tokens = new arraylist<token>(); while (cts.la(1) != eof) { tokens.add(cts.lt(1)); cts.consume(); } c# example:
commontokenstream cts = new commontokenstream(tokensource, 2); ilist<itoken> tokens = new list<itoken>(); while (cts.la(1) != eof) { tokens.add(cts.lt(1)); cts.consume(); }
Comments
Post a Comment