Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove forward ref in the lexer #168

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ocamllex read)
(ocamllex read lexer_utils)

(rule
(targets t.ml)
Expand Down Expand Up @@ -111,7 +111,7 @@
(library
(name yojson)
(public_name yojson)
(modules yojson t basic safe raw common codec)
(modules yojson t basic safe raw common codec lexer_utils)
(synopsis "JSON parsing and printing")
(libraries seq)
(flags
Expand Down
9 changes: 9 additions & 0 deletions lib/lexer_utils.mll
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rule read_junk buf n = parse
| eof { () }
| _ {
if n <= 0 then ()
else begin
Buffer.add_char buf (Lexing.lexeme_char lexbuf 0);
read_junk buf (n - 1) lexbuf
end
}
22 changes: 3 additions & 19 deletions lib/read.mll
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@
(sprintf "%s '%s'" descr (Lexing.lexeme lexbuf))
v lexbuf

let read_junk = ref (fun _ -> assert false)

let long_error descr v lexbuf =
let junk = Lexing.lexeme lexbuf in
let extra_junk = !read_junk lexbuf in
let buf = Buffer.create 32 in
let () = Lexer_utils.read_junk buf 32 lexbuf in
let extra_junk = Buffer.contents buf in
custom_error
(sprintf "%s '%s%s'" descr junk extra_junk)
v lexbuf
Expand Down Expand Up @@ -169,16 +169,6 @@ let hex = [ '0'-'9' 'a'-'f' 'A'-'F' ]

let ident = ['a'-'z' 'A'-'Z' '_']['a'-'z' 'A'-'Z' '_' '0'-'9']*

let optjunk4 = (eof | _ (eof | _ (eof | _ (eof | _))))
let optjunk8 = (eof | _ (eof | _ (eof | _ (eof | _ (eof | optjunk4)))))
let optjunk12 = (eof | _ (eof | _ (eof | _ (eof | _ (eof | optjunk8)))))
let optjunk16 = (eof | _ (eof | _ (eof | _ (eof | _ (eof | optjunk12)))))
let optjunk20 = (eof | _ (eof | _ (eof | _ (eof | _ (eof | optjunk16)))))
let optjunk24 = (eof | _ (eof | _ (eof | _ (eof | _ (eof | optjunk20)))))
let optjunk28 = (eof | _ (eof | _ (eof | _ (eof | _ (eof | optjunk24)))))
let optjunk32 = (eof | _ (eof | _ (eof | _ (eof | _ (eof | optjunk28)))))
let junk = optjunk32

rule read_json v = parse
| "true" { `Bool true }
| "false" { `Bool false }
Expand Down Expand Up @@ -1045,17 +1035,11 @@ and finish_buffer_comment v = parse
finish_buffer_comment v lexbuf }
| _ { add_lexeme v.buf lexbuf; finish_buffer_comment v lexbuf }

and junk = parse
junk { Lexing.lexeme lexbuf }

{
let _ = (read_json : lexer_state -> Lexing.lexbuf -> t)

let read_t = read_json

let () =
read_junk := junk

let read_int8 v lexbuf =
let n = read_int v lexbuf in
if n < 0 || n > 255 then
Expand Down