As you can see, Lexing.lexeme lexbuf gives only last character of annotation i.e. ‘)’
It should also print full annotation string as first print statement. I want to do some operation on this lexbuf further.
But, I do not understand this behavior of lexbuf. Could you please tell me what is wrong in my code?
lexbuf is modified in-place by the lexer. After the call to parse_and_include_annotation, the call to Lexing.lexeme lexbuf will return the last lexeme seen by the lexer, to wit: “)”.
OK, is it not possible to modify lexbuf structure? I mean, is it possible to modify lexbuf.Lexing.lex_curr_p somehow to modify the lexeme output to include full annotation text?
As a general remark, the lexbuf structure is not meant to be modified by the user – the lexing engine assumes it is the only one modifying it. You already have access to the full annotation text (annot), and I don’t see what would be gained by extracting this again from the lexbuf.
Maybe it would help the discussion if you explained what you are really trying to achieve.
Well, you would need to get the location (using Lexing.lexeme_{start,end}_p) at when you start/finish extracting the annotation text (in the parse_and_return_annotation rule) and then propagate these two locations back to the calling function.
But really, it would be much simpler if you used a parser separate from the lexer for this. Both parser generators ocamlyacc and menhir offer a simple way to keep track of token locations.