File: test_parsing.ml

package info (click to toggle)
js-of-ocaml 5.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 32,020 kB
  • sloc: ml: 91,250; javascript: 57,289; ansic: 315; makefile: 271; lisp: 23; sh: 6; perl: 4
file content (130 lines) | stat: -rw-r--r-- 4,060 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(* Js_of_ocaml tests
 * http://www.ocsigen.org/js_of_ocaml/
 * Copyright (C) 2022 Hugo Heuzard
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, with linking exception;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)

(* see https://github.com/ocaml/ocaml/pull/12046 *)
external flush_stdout_stderr : unit -> unit = "flush_stdout_stderr"

let parse s =
  flush_stdout_stderr ();
  try
    let lexbuf = Lexing.from_string s in
    while true do
      let result = Calc_parser.main Calc_lexer.token lexbuf in
      flush_stdout_stderr ();
      print_int result;
      print_newline ()
    done
  with Calc_lexer.Eof ->
    flush_stdout_stderr ();
    print_endline "EOF"

let%expect_test "parsing" =
  let (old : bool) = Parsing.set_trace true in
  parse "1+2*3";
  [%expect
    {|
       State 0: shift to state 1
       State 1: read token INT(1)
       State 1: shift to state 3
       State 3: reduce by rule 2
       State 7: read token PLUS
       State 7: shift to state 10
       State 10: read token INT(2)
       State 10: shift to state 3
       State 3: reduce by rule 2
       State 16: read token TIMES
       State 16: shift to state 12
       State 12: read token INT(3)
       State 12: shift to state 3
       State 3: reduce by rule 2
       State 18: reduce by rule 6
       EOF |}];
  parse "(1+2)*3";
  [%expect
    {|
       State 0: shift to state 1
       State 1: read token LPAREN
       State 1: shift to state 5
       State 5: read token INT(1)
       State 5: shift to state 3
       State 3: reduce by rule 2
       State 9: read token PLUS
       State 9: shift to state 10
       State 10: read token INT(2)
       State 10: shift to state 3
       State 3: reduce by rule 2
       State 16: read token RPAREN
       State 16: reduce by rule 4
       State 9: shift to state 15
       State 15: reduce by rule 3
       State 7: read token TIMES
       State 7: shift to state 12
       State 12: read token INT(3)
       State 12: shift to state 3
       State 3: reduce by rule 2
       State 18: reduce by rule 6
       EOF |}];
  parse "-10-1";
  [%expect
    {|
       State 0: shift to state 1
       State 1: read token MINUS
       State 1: shift to state 4
       State 4: read token INT(10)
       State 4: shift to state 3
       State 3: reduce by rule 2
       State 8: reduce by rule 8
       State 7: read token MINUS
       State 7: shift to state 11
       State 11: read token INT(1)
       State 11: shift to state 3
       State 3: reduce by rule 2
       EOF |}];
  parse "63/2*-3";
  [%expect
    {|
       State 0: shift to state 1
       State 1: read token INT(63)
       State 1: shift to state 3
       State 3: reduce by rule 2
       State 7: read token DIV
       State 7: shift to state 13
       State 13: read token INT(2)
       State 13: shift to state 3
       State 3: reduce by rule 2
       State 19: reduce by rule 7
       State 7: read token TIMES
       State 7: shift to state 12
       State 12: read token MINUS
       State 12: shift to state 4
       State 4: read token INT(3)
       State 4: shift to state 3
       State 3: reduce by rule 2
       State 8: reduce by rule 8
       State 18: reduce by rule 6
       EOF |}];
  let (_ : bool) = Parsing.set_trace old in
  parse "1+2*3";
  [%expect {| EOF |}];
  parse "(1+2)*3";
  [%expect {| EOF |}];
  parse "-10-1";
  [%expect {| EOF |}];
  parse "63/2*-3";
  [%expect {| EOF |}]