Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/tests-jsoo/calc_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1+2*3
(1+2)*3
-10-1
63/2*-3
16 changes: 16 additions & 0 deletions compiler/tests-jsoo/calc_lexer.mll
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
open Calc_parser (* The type token is defined in calc_parser.mli *)
exception Eof
}

rule token = parse
[' ' '\t' '\r'] { token lexbuf } (* skip blanks *)
| ['\n' ] { EOL }
| ['0'-'9']+ as lxm { INT(int_of_string lxm) }
| '+' { PLUS }
| '-' { MINUS }
| '*' { TIMES }
| '/' { DIV }
| '(' { LPAREN }
| ')' { RPAREN }
| eof { raise Eof }
22 changes: 22 additions & 0 deletions compiler/tests-jsoo/calc_parser.mly
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
%token <int> INT
%token PLUS MINUS TIMES DIV
%token LPAREN RPAREN
%token EOL
%left PLUS MINUS /* lowest precedence */
%left TIMES DIV /* medium precedence */
%nonassoc UMINUS /* highest precedence */
%start main /* the entry point */
%type <int> main
%%
main:
expr EOL { $1 }
;
expr:
INT { $1 }
| LPAREN expr RPAREN { $2 }
| expr PLUS expr { $1 + $3 }
| expr MINUS expr { $1 - $3 }
| expr TIMES expr { $1 * $3 }
| expr DIV expr { $1 / $3 }
| MINUS expr %prec UMINUS { - $2 }
;
10 changes: 9 additions & 1 deletion compiler/tests-jsoo/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(library
(name jsoo_testsuite)
(libraries unix)
(libraries unix compiler-libs.common)
(foreign_stubs
(language c)
(names bigarray_stubs))
Expand All @@ -9,3 +9,11 @@
(modes js native))
(preprocess
(pps ppx_expect)))

(ocamlyacc calc_parser)

(ocamllex calc_lexer)

(ocamlyacc parser_1307)

(ocamllex lexer_1307)
28 changes: 28 additions & 0 deletions compiler/tests-jsoo/gh_1307.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
let test content =
Printf.printf "input: %S\n" content;
flush_all ();
match Parser_1307.root Lexer_1307.token (Lexing.from_string content) with
| n ->
Printf.printf "%d\n" n;
print_endline "success"
| exception e ->
print_endline (Printexc.to_string e);
print_endline "failure"

let%expect_test "parsing" =
let (_ : bool) = Parsing.set_trace false in
test "a";
[%expect {|
input: "a"
Stdlib.Parsing.Parse_error
failure |}];
test "aa";
[%expect {|
input: "aa"
0
success |}];
test "aaa";
[%expect {|
input: "aaa"
Stdlib.Parsing.Parse_error
failure |}]
7 changes: 7 additions & 0 deletions compiler/tests-jsoo/lexer_1307.mll
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
open Parser_1307
}

rule token = parse
| _ { TOKEN }
| eof { EOF }
17 changes: 17 additions & 0 deletions compiler/tests-jsoo/parser_1307.mly
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

%{

%}

%token EOF
%token TOKEN
%start root
%type <int> root
%%

root:
| expr EOF { 0 }
| expr error root { 1 }

expr:
TOKEN TOKEN { 1 }
53 changes: 53 additions & 0 deletions compiler/tests-jsoo/test_parsing.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(* 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.
*)

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

let%expect_test "parsing" =
let (old : bool) = Parsing.set_trace false in
parse "1+2*3";
[%expect {|
EOF |}];
parse "(1+2)*3";
[%expect {|
EOF |}];
parse "-10-1";
[%expect {|
EOF |}];
parse "63/2*-3";
[%expect {|
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 |}]