caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] lexer errors
@ 2012-06-23  1:14 Jacques Le Normand
  2012-06-23  6:57 ` Mehdi Dogguy
  0 siblings, 1 reply; 2+ messages in thread
From: Jacques Le Normand @ 2012-06-23  1:14 UTC (permalink / raw)
  To: caml-list

How can I get some decent lexer error reporting?

Here's my program:


{
  open Lexing
}

rule lex = parse
  | "foo" {()}
  | "\n" {lex lexbuf}

{
  let main () =
    let lexbuf = from_channel stdin in
      try
	let rec loop () =
	  let x = lex lexbuf in
	    loop x
	in
	  loop ()
      with
	| _ ->
	    let x = lexbuf.Lexing.lex_curr_p in
	      Printf.printf
		"line %d, character %d\n"
		x.pos_lnum (x.pos_cnum - x.pos_bol)

  let _ = main ()
}

running:

	ocamlc -I ~/ocaml/ ~/ocaml/test_lexer2.ml -o ~/ocaml/test.o
	echo foo > ~/temp.txt
	echo bar >> ~/temp.txt
	~/ocaml/test.o < ~/temp.txt

outputs the incorrect line:

       line 1, character 4

(Should be line 2, character 1)

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] lexer errors
  2012-06-23  1:14 [Caml-list] lexer errors Jacques Le Normand
@ 2012-06-23  6:57 ` Mehdi Dogguy
  0 siblings, 0 replies; 2+ messages in thread
From: Mehdi Dogguy @ 2012-06-23  6:57 UTC (permalink / raw)
  To: Jacques Le Normand; +Cc: caml-list

On 06/23/2012 03:14 AM, Jacques Le Normand wrote:
> rule lex = parse
>   | "foo" {()}
>   | "\n" {lex lexbuf}
> 

You should add "new_line lexbuf" before re-calling "lex".


> (Should be line 2, character 1)
> 

In fact, it should be "line 2, character 0" since "bar" is really
at the beginning of the line.

Best,

-- 
Mehdi Dogguy مهدي الدڤي
http://dogguy.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-06-23  7:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-23  1:14 [Caml-list] lexer errors Jacques Le Normand
2012-06-23  6:57 ` Mehdi Dogguy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).