-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathparser.ml
75 lines (64 loc) · 2.21 KB
/
parser.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
include Nice_parser.Make(struct
type result = Ast.sexp
type token = Menhir_parser.token
exception ParseError = Menhir_parser.Error
let parse = Menhir_parser.sexp_eof
include Lexer
end)
(*===========================================================================*)
(* TESTS *)
(* https://dune.readthedocs.io/en/stable/tests.html#inline-expectation-tests *)
(*===========================================================================*)
let%test_module _ = (module struct
let () = Printexc.record_backtrace false
let%expect_test "atom" =
parse_string "this_is_AN_At0m"
|> Printf.printf !"%{sexp:Ast.sexp}";
[%expect{| (Atom this_is_AN_At0m) |}]
let%expect_test "whitespace" =
parse_string " \t\n this_also_is_an_atom \t"
|> Printf.printf !"%{sexp:Ast.sexp}";
[%expect{| (Atom this_also_is_an_atom) |}]
let%expect_test "illegal atom" =
parse_string " *$-# "
|> Printf.printf !"%{sexp:Ast.sexp}";
[%expect.unreachable]
[@@expect.uncaught_exn {|
("Nice_parser.Make(P).LexError(\"[lexer] unexpected character: '*'\", _)")
|}]
let%expect_test "empty list" =
parse_string "()"
|> Printf.printf !"%{sexp:Ast.sexp}";
[%expect{| (List ()) |}]
let%expect_test "complex example" =
{|
(
(
list in a list (* comment within a list *)
(list in a list in a list)
42 is the answer to all questions
(* (this S-expression
(has been commented out)
)
(* Block comments *) can be "nested" *)
)
)
|}
|> parse_string
|> Printf.printf !"%{sexp:Ast.sexp}";
[%expect{|
(List
((List
((Atom list) (Atom in) (Atom a) (Atom list)
(List
((Atom list) (Atom in) (Atom a) (Atom list) (Atom in) (Atom a)
(Atom list)))
(Atom 42) (Atom is) (Atom the) (Atom answer) (Atom to) (Atom all)
(Atom questions)))))
|}]
let%expect_test "illegal list" =
parse_string "this aint a list"
|> Printf.printf !"%{sexp:Ast.sexp}";
[%expect.unreachable]
[@@expect.uncaught_exn {| ("Nice_parser.Make(P).ParseError(_, _)") |}]
end)