java - ANTLR4 get ID (get correct token text) -


here grammar:

        grammar text;          prog: description+;          description:              type='dat' colon  time colon ';'         ;          time:                type='before ' id               | type='after ' id         ;           string : '"' ('""'|~'"')* '"' ; // quote-quote escaped quote           line_comment             : '//' (~('\n'|'\r'))* -> skip;          comment : '/*' .*? '*/' -> skip;         le: '<';         minus: '-';         gr: '>';           colon      : ':' ;         hash: '#';         eq: '=';         semi: ';';         space: ' ';         comma: ',';          and:  [aa][nn][dd];         number: [0-9];         id: [a-za-z][a-za-z0-9]+;         ws  :   [ \t\n\r]+ -> channel(hidden);         any_char : . ;  

and corresponding listener function:

  public void enterdescription(anamparser.descriptioncontext ctx) {        string id = ctx.time().id().tostring();       system.out.println("id " + id);    } 

if syntax this:

dat:before somethingelse; 

the string id not contain "somethingelse" "[somethingelse]"

replacing line string id = ctx.time().id().tostring(); string id = ctx.time().id().getstring();

does not change behavoir.

what correct way access content of id?

i did oversimplify example bit:

time:            type='before ' id           | type='after ' id          | type='between ' id ' , ' id     ; 

is whole story.

so seems time().id() gives array type='before ' , type="'after '.

so correct access id is:

string id = ctx.time().id(0).tostring(); 

and third type ('between ') access is

string id2 = ctx.time().id(1).tostring(); 

this works fine.

'


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -