Skip to content

Commit 9911fe2

Browse files
committed
Merge pull request #197 from WebAssembly/lexstream
Do streaming lexing
2 parents 7ad01ba + bc6f78c commit 9911fe2

File tree

3 files changed

+48
-45
lines changed

3 files changed

+48
-45
lines changed

ml-proto/host/load.ml

Lines changed: 0 additions & 11 deletions
This file was deleted.

ml-proto/host/main.ml

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
let name = "wasm"
2-
let version = "0.1"
2+
let version = "0.2"
33

4-
let load file =
5-
let f = open_in file in
6-
let size = in_channel_length f + 1 in
7-
let buf = Bytes.create size in
8-
let rec loop () =
9-
let len = input f buf 0 size in
10-
let source = Bytes.sub_string buf 0 len in
11-
if len == 0 then source else source ^ loop ()
12-
in
13-
let source = loop () in
14-
close_in f;
15-
source
4+
let banner () =
5+
print_endline (name ^ " " ^ version ^ " spec interpreter")
166

17-
let parse name source =
18-
let lexbuf = Lexing.from_string source in
7+
let parse name lexbuf start =
198
lexbuf.Lexing.lex_curr_p <-
209
{lexbuf.Lexing.lex_curr_p with Lexing.pos_fname = name};
21-
try Parser.script Lexer.token lexbuf with Script.Syntax (region, s) ->
10+
try start Lexer.token lexbuf with Script.Syntax (region, s) ->
2211
let region' = if region <> Source.no_region then region else
2312
{Source.left = Lexer.convert_pos lexbuf.Lexing.lex_start_p;
2413
Source.right = Lexer.convert_pos lexbuf.Lexing.lex_curr_p} in
@@ -29,10 +18,9 @@ let error at category msg =
2918
prerr_endline (Source.string_of_region at ^ ": " ^ msg);
3019
false
3120

32-
let process file source =
21+
let process file lexbuf start =
3322
try
34-
Script.trace "Parsing...";
35-
let script = parse file source in
23+
let script = parse file lexbuf start in
3624
Script.trace "Desugaring...";
3725
let script' = Script.desugar script in
3826
Script.trace "Running...";
@@ -48,21 +36,44 @@ let process file source =
4836

4937
let process_file file =
5038
Script.trace ("Loading (" ^ file ^ ")...");
51-
let source = load file in
52-
if not (process file source) then exit 1
39+
let ic = open_in file in
40+
try
41+
let lexbuf = Lexing.from_channel ic in
42+
Script.trace "Parsing...";
43+
let success = process file lexbuf Parser.script in
44+
close_in ic;
45+
if not success then exit 1
46+
with exn -> close_in ic; raise exn
47+
48+
let continuing = ref false
49+
50+
let lexbuf_stdin buf len =
51+
let prompt = if !continuing then " " else "> " in
52+
print_string prompt; flush_all ();
53+
continuing := true;
54+
let rec loop i =
55+
if i = len then i else
56+
let ch = input_char stdin in
57+
Bytes.set buf i ch;
58+
if ch = '\n' then i + 1 else loop (i + 1)
59+
in
60+
let n = loop 0 in
61+
if n = 1 then continuing := false else Script.trace "Parsing...";
62+
n
5363

5464
let rec process_stdin () =
55-
print_string (name ^ "> "); flush_all ();
56-
match try Some (input_line stdin) with End_of_file -> None with
57-
| None ->
65+
banner ();
66+
let lexbuf = Lexing.from_function lexbuf_stdin in
67+
let rec loop () =
68+
let success = process "stdin" lexbuf Parser.script1 in
69+
if not success then Lexing.flush_input lexbuf;
70+
if Lexing.(lexbuf.lex_curr_pos >= lexbuf.lex_buffer_len - 1) then
71+
continuing := false;
72+
loop ()
73+
in
74+
try loop () with End_of_file ->
5875
print_endline "";
5976
Script.trace "Bye."
60-
| Some source ->
61-
ignore (process "stdin" source);
62-
process_stdin ()
63-
64-
let greet () =
65-
print_endline ("Version " ^ version)
6677

6778
let usage = "Usage: " ^ name ^ " [option] [file ...]"
6879
let argspec = Arg.align
@@ -72,7 +83,7 @@ let argspec = Arg.align
7283
"-s", Arg.Set Flags.print_sig, " show module signatures";
7384
"-d", Arg.Set Flags.dry, " dry, do not run program";
7485
"-t", Arg.Set Flags.trace, " trace execution";
75-
"-v", Arg.Unit greet, " show version"
86+
"-v", Arg.Unit banner, " show version"
7687
]
7788

7889
let () =
@@ -84,7 +95,7 @@ let () =
8495
List.iter process_file !files;
8596
if !Flags.interactive then process_stdin ()
8697
with exn ->
87-
flush stdout;
98+
flush_all ();
8899
prerr_endline
89100
(Sys.argv.(0) ^ ": uncaught exception " ^ Printexc.to_string exn);
90101
Printexc.print_backtrace stderr;

ml-proto/host/parser.mly

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,9 @@ let implicit_decl c t at =
186186
%nonassoc LOW
187187
%nonassoc VAR
188188

189-
%start script
189+
%start script script1
190190
%type<Script.script> script
191+
%type<Script.script> script1
191192

192193
%%
193194

@@ -487,5 +488,7 @@ const_list :
487488
script :
488489
| cmd_list EOF { $1 }
489490
;
490-
491+
script1 :
492+
| cmd { [$1] }
493+
;
491494
%%

0 commit comments

Comments
 (0)