Skip to content

Commit e6de49a

Browse files
committed
Tests: tests for parsing
1 parent cd52f81 commit e6de49a

File tree

8 files changed

+160
-1
lines changed

8 files changed

+160
-1
lines changed

compiler/tests-jsoo/calc_input.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1+2*3
2+
(1+2)*3
3+
-10-1
4+
63/2*-3

compiler/tests-jsoo/calc_lexer.mll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
open Calc_parser (* The type token is defined in calc_parser.mli *)
3+
exception Eof
4+
}
5+
6+
rule token = parse
7+
[' ' '\t' '\r'] { token lexbuf } (* skip blanks *)
8+
| ['\n' ] { EOL }
9+
| ['0'-'9']+ as lxm { INT(int_of_string lxm) }
10+
| '+' { PLUS }
11+
| '-' { MINUS }
12+
| '*' { TIMES }
13+
| '/' { DIV }
14+
| '(' { LPAREN }
15+
| ')' { RPAREN }
16+
| eof { raise Eof }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
%token <int> INT
2+
%token PLUS MINUS TIMES DIV
3+
%token LPAREN RPAREN
4+
%token EOL
5+
%left PLUS MINUS /* lowest precedence */
6+
%left TIMES DIV /* medium precedence */
7+
%nonassoc UMINUS /* highest precedence */
8+
%start main /* the entry point */
9+
%type <int> main
10+
%%
11+
main:
12+
expr EOL { $1 }
13+
;
14+
expr:
15+
INT { $1 }
16+
| LPAREN expr RPAREN { $2 }
17+
| expr PLUS expr { $1 + $3 }
18+
| expr MINUS expr { $1 - $3 }
19+
| expr TIMES expr { $1 * $3 }
20+
| expr DIV expr { $1 / $3 }
21+
| MINUS expr %prec UMINUS { - $2 }
22+
;

compiler/tests-jsoo/dune

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(library
22
(name jsoo_testsuite)
3-
(libraries unix)
3+
(libraries unix compiler-libs.common)
44
(foreign_stubs
55
(language c)
66
(names bigarray_stubs))
@@ -9,3 +9,11 @@
99
(modes js native))
1010
(preprocess
1111
(pps ppx_expect)))
12+
13+
(ocamlyacc calc_parser)
14+
15+
(ocamllex calc_lexer)
16+
17+
(ocamlyacc parser_1307)
18+
19+
(ocamllex lexer_1307)

compiler/tests-jsoo/gh_1307.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
let test content =
2+
Printf.printf "input: %S\n" content;
3+
flush_all ();
4+
match Parser_1307.root Lexer_1307.token (Lexing.from_string content) with
5+
| n ->
6+
Printf.printf "%d\n" n;
7+
print_endline "success"
8+
| exception e ->
9+
print_endline (Printexc.to_string e);
10+
print_endline "failure"
11+
12+
let%expect_test "parsing" =
13+
let (_ : bool) = Parsing.set_trace false in
14+
test "a";
15+
[%expect {|
16+
input: "a"
17+
Stdlib.Parsing.Parse_error
18+
failure |}];
19+
test "aa";
20+
[%expect {|
21+
input: "aa"
22+
0
23+
success |}];
24+
test "aaa";
25+
[%expect {|
26+
input: "aaa"
27+
Stdlib.Parsing.Parse_error
28+
failure |}]

compiler/tests-jsoo/lexer_1307.mll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
open Parser_1307
3+
}
4+
5+
rule token = parse
6+
| _ { TOKEN }
7+
| eof { EOF }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
%{
3+
4+
%}
5+
6+
%token EOF
7+
%token TOKEN
8+
%start root
9+
%type <int> root
10+
%%
11+
12+
root:
13+
| expr EOF { 0 }
14+
| expr error root { 1 }
15+
16+
expr:
17+
TOKEN TOKEN { 1 }
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
(* Js_of_ocaml tests
2+
* http://www.ocsigen.org/js_of_ocaml/
3+
* Copyright (C) 2022 Hugo Heuzard
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, with linking exception;
8+
* either version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18+
*)
19+
20+
let parse s =
21+
try
22+
let lexbuf = Lexing.from_string s in
23+
while true do
24+
let result = Calc_parser.main Calc_lexer.token lexbuf in
25+
print_int result;
26+
print_newline ();
27+
flush stdout
28+
done
29+
with Calc_lexer.Eof -> print_endline "EOF"
30+
31+
let%expect_test "parsing" =
32+
let (old : bool) = Parsing.set_trace false in
33+
parse "1+2*3";
34+
[%expect
35+
{|
36+
EOF |}];
37+
parse "(1+2)*3";
38+
[%expect
39+
{|
40+
EOF |}];
41+
parse "-10-1";
42+
[%expect
43+
{|
44+
EOF |}];
45+
parse "63/2*-3";
46+
[%expect
47+
{|
48+
EOF |}];
49+
let (_ : bool) = Parsing.set_trace old in
50+
parse "1+2*3";
51+
[%expect {| EOF |}];
52+
parse "(1+2)*3";
53+
[%expect {| EOF |}];
54+
parse "-10-1";
55+
[%expect {| EOF |}];
56+
parse "63/2*-3";
57+
[%expect {| EOF |}]

0 commit comments

Comments
 (0)