1
votes

My input file is having multiple JavaDoc style comments (/** ..... */), I need to read only the very first commented section and skip the rest all commented sections.

Input.txt

/** 
  @Description("Hi there")
  @Input("String")

 */

/**
 * This is the a
 * commented section that we
 * don't want to read.
 */

/**
 * This is the another
 * commented section that we
 * don't want to read.
 */

My Lexer Grammar is as below:-

lexer grammar AnnotationLexer;

ANNOTATION_START
 : '/**' -> mode(INSIDE), skip
 ;

IGNORE
 : . -> skip
 ;

mode INSIDE;


KEY : '@' [a-zA-Z]+ ;


STRING: '"' (~'"' | ',')* '"' ;


ANNOTATION_END
 : '*/' -> mode(DEFAULT_MODE), skip
 ;

IGNORE_INSIDE
 : [ \t\r\n] -> skip
1
Would it not help, if you were creating another mode when you already seen the first javadoc comment? - Gábor Bakos
Gabor, I tried, but it didn't help, might be I'm missing something, I added another mode to skip everything but no luck. ANOTHER_START : '/**' mode(ANOTHERMODE)-> skip; ANOTHER_END : '/' -> skip; mode ANOTHERMODE; ANOTHER_IGNORE : . -> skip; it would be very kind, if you can help me to point what i'm doing wrong. - Abhishek-M

1 Answers

1
votes

Here is my try (I have not tried it though). I am afraid it will not be satisfactory unless you really only read just javadocs and nothing else:

lexer grammar AnnotationLexer;

ANNOTATION_START
 : '/**' -> mode(INSIDE), skip
 ;

IGNORE
 : . -> skip
 ;

mode INSIDE;


KEY : '@' [a-zA-Z]+ ;


STRING: '"' (~'"' | ',')* '"' ;


ANNOTATION_END
 : '*/' -> mode(READ_JAVADOC), skip
 ;

IGNORE_INSIDE
 : [ \t\r\n] -> skip

mode READ_JAVADOC;

JAVADOC_START_AFTER_FIRST
 : '/**' skip
 ;

IGNORE_INSIDE_AFTER_FIRST
 : [ \t\r\n] -> skip
 ;

JAVADOC_END_AFTER_FIRST
 : '*/' skip
 ;

Practically this way you have to create all lexer rules two times. Probably it is better to use semantic predicates in this case (with mutable member fields for the state describing how many javadoc were read) instead of modes.