문제

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?

도움이 되었습니까?

해결책

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 }
...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top