-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml_only_lex.ml
44 lines (40 loc) · 1.69 KB
/
yaml_only_lex.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
open Core.Std
open Yaml_parser
open Lexing
let flatten_lexer lexer =
let queue = Queue.create () in
fun lexbuf ->
if Queue.is_empty queue then
List.iter (List.rev (lexer lexbuf)) ~f:(fun t -> Queue.enqueue queue t);
match Queue.dequeue queue with
|Some x -> x
|None -> print_endline "error in lexer"; EOF
let lex_and_print lexbuf =
let lexer = flatten_lexer Yaml_lexer.read in
let rec lex_and_print_rec lexbuf =
let token = lexer lexbuf in
begin
match token with
|EOF -> print_endline "EOF"
|STRING s -> print_endline ("STRING " ^ s)
|INDENT -> print_endline "INDENT"
|HYPHEN -> print_endline "HYPHEN"
|EMPTY_LINE -> print_endline "EMPTY_LINE"
|DEDENT -> print_endline "DEDENT"
|COMMENT c-> print_endline ("COMMENT " ^ c)
|COLON -> print_endline "COLON"
end;
if token = EOF then () else lex_and_print_rec lexbuf
in try lex_and_print_rec lexbuf with
|_ -> print_endline "error"
let loop filename () =
let inx = In_channel.create filename in
let lexbuf = Lexing.from_channel inx in
lexbuf.lex_curr_p <- {lexbuf.lex_curr_p with pos_fname = filename };
lex_and_print lexbuf;
In_channel.close inx
let () =
Command.basic ~summary:"Parse and display YAML"
Command.Spec.(empty +> anon ("filename" %: file))
loop
|> Command.run