Skip to content
This repository has been archived by the owner on Jun 13, 2020. It is now read-only.

Commit

Permalink
Finish factoring out Parse.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkloos committed Jun 30, 2016
1 parent e48075d commit 56edde0
Showing 1 changed file with 1 addition and 44 deletions.
45 changes: 1 addition & 44 deletions webidl.ml
Original file line number Diff line number Diff line change
@@ -1,47 +1,4 @@
let run_parser lexbuf =
let module I = Idlparser.MenhirInterpreter in
(* Stolen from CompCert *)
let state checkpoint =
match Lazy.force (I.stack checkpoint) with
| MenhirLib.General.Nil -> 0
| MenhirLib.General.Cons(I.Element (s, _, _, _), _) -> I.number s
and last_token = ref Idlparser.EOF
in let lexer lexbuf =
let token = Idllexer.read lexbuf in
last_token := token; token
in
I.loop_handle (fun result -> result)
(function I.Rejected -> failwith "Parser rejected input"
| I.HandlingError e ->
let s = state e in
Format.eprintf "%a: %s. Last token: %a@."
Misc.pp_position lexbuf.Lexing.lex_start_p
(try IdlparserOutput.message s with Not_found -> "no errror message found")
TokenPrinter.pp_token !last_token
;
raise Idlparser.Error
| _ -> failwith "Unexpected state in failure handler!"
)
(I.lexer_lexbuf_to_supplier lexer lexbuf)
(Idlparser.Incremental.main lexbuf.Lexing.lex_curr_p)

let read_ast_from_lexbuf ?fname lexbuf =
begin match fname with
| Some fname ->
let open Lexing in
lexbuf.lex_start_p <- { lexbuf.lex_start_p with pos_fname = fname };
lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = fname }
| None -> ()
end;
run_parser lexbuf

let read_ast_from_channel ?fname channel =
try
let ast = read_ast_from_lexbuf ?fname (Lexing.from_channel channel) in
close_in channel; ast
with e -> close_in channel; raise e

let read_ast_from_file file = read_ast_from_channel ~fname:file (open_in file)
open Parse

let parse_ast ast = ast |> AstSimplify.cleanup |> AstParse.translate_definitions
let parse_from_lexbuf x = x |> read_ast_from_lexbuf |> parse_ast
Expand Down

0 comments on commit 56edde0

Please sign in to comment.