4

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?

Evgeny Gavrin
  • 7,627
  • 1
  • 22
  • 27

1 Answers1

3

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 }
...
Stephen Swensen
  • 22,107
  • 9
  • 81
  • 136
  • Yes, I have such rule for lexer. – Evgeny Gavrin Oct 28 '11 at 13:23
  • Hmm, maybe your newline case is not matching for some reason - it's hard to debug fslex lexing, but I would double check that your actually newlines are correct for your newline regex. And maybe you can add a printf statement to see if you are actually going into the newline case (maybe you never reach it because of an earlier rule consuming the newline token?) – Stephen Swensen Oct 28 '11 at 13:28
  • Yeah, you were right. I corrupted newlines before parsing. Thank you :) – Evgeny Gavrin Oct 28 '11 at 13:48