sedlex icon indicating copy to clipboard operation
sedlex copied to clipboard

Nested matches using the same lexbuf produce misleading error

Open sheijk opened this issue 11 years ago • 1 comments

When nesting a match%sedlex inside another one the error message is pretty confusing:

let nested str =
  let buf = Sedlexing.Latin1.from_string str in
  match%sedlex buf with
    | "0" ->
       print_endline "it's a 0!"
    | "foo" ->
       begin
         match %sedlex buf with
           | "bar" -> print_endline "it's a generic example"
           | _ -> print_endline "bad foo"
       end
    | _ ->
       print_endline "invalid input"

This produces a generic error message

File "src_test/sedlex_test.ml", line 8, characters 16-22:
Uninterpreted extension 'sedlex'.

The problem seems to be reusing buf. If a different variable is used it compiles and works:

let nested str =
  let buf = Sedlexing.Latin1.from_string str in
  match%sedlex buf with
    | "0" ->
       print_endline "it's a 0!"
    | "foo" ->
       begin
         let buf2 = buf in
         match %sedlex buf2 with
           | "bar" -> print_endline "it's a generic example"
           | _ -> print_endline "bad foo"
       end
    | _ ->
       print_endline "invalid input"

sheijk avatar Oct 26 '14 14:10 sheijk

The problem is actually that ppx_sedlex.ml applies super#expr instead of this#expr to immediate sub-expressions (actions).

alainfrisch avatar Oct 27 '14 17:10 alainfrisch

@toots, can you close this one. automatic closing didn't work

hhugo avatar Feb 17 '23 15:02 hhugo