Significant parsing errors with FSyacc

I am using fsyacc / fslex from the F # Power Pack to parse some source code.

To detect errors, I use the following code:

use inputChannel = new StreamReader(File.OpenRead tempFileName) let lexbuf = Lexing.LexBuffer<_>.FromTextReader inputChannel let ast = try Parser.start Lexer.tokenize lexbuf with e -> let pos = lexbuf.EndPos let line = pos.Line let column = pos.Column let message = e.Message let lastToken = new System.String(lexbuf.Lexeme) printf "Parse failed at line %d, column %d:\n" line column printf "Last loken: %s" lastToken printf "\n" exit 1 

But when this code gives an error message while parsing a multi-line source file, I get the wrong row and column position:

 Parse failed at line 0, column 10899: 

How can I correctly get the line number on which an error occurred?

+4
source share
1 answer

During vocabulary, you need to manually increase the line number using a rule, for example

 ... let newline = ('\n' | '\r' '\n') rule tokenize = parse | newline { lexbuf.EndPos <- lexbuf.EndPos.NextLine; tokenize lexbuf } ... 
+3
source

All Articles