Meaningful errors during parsing with FSyacc

2019-02-10 22:05发布

问题:

I'm using fsyacc/fslex from 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 throws the error message on parsing multi-line source file, I'm getting wrong line and column position:

Parse failed at line 0, column 10899:

How can I get line number correctly on which error has been occurred?

回答1:

During lexing, you need to manually increment the line number with a rule like

...
let newline = ('\n' | '\r' '\n')

rule tokenize = parse
| newline    { lexbuf.EndPos <- lexbuf.EndPos.NextLine; tokenize lexbuf }
...