diff --git a/compiler/tests-jsoo/calc_input.txt b/compiler/tests-jsoo/calc_input.txt new file mode 100644 index 0000000000..fffe1520d7 --- /dev/null +++ b/compiler/tests-jsoo/calc_input.txt @@ -0,0 +1,4 @@ +1+2*3 +(1+2)*3 +-10-1 +63/2*-3 diff --git a/compiler/tests-jsoo/calc_lexer.mll b/compiler/tests-jsoo/calc_lexer.mll new file mode 100644 index 0000000000..4986ea12a5 --- /dev/null +++ b/compiler/tests-jsoo/calc_lexer.mll @@ -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 } diff --git a/compiler/tests-jsoo/calc_parser.mly b/compiler/tests-jsoo/calc_parser.mly new file mode 100644 index 0000000000..63ce87d334 --- /dev/null +++ b/compiler/tests-jsoo/calc_parser.mly @@ -0,0 +1,22 @@ +%token 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 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 } +; diff --git a/compiler/tests-jsoo/dune b/compiler/tests-jsoo/dune index bccadcff50..cacba550c5 100644 --- a/compiler/tests-jsoo/dune +++ b/compiler/tests-jsoo/dune @@ -1,6 +1,6 @@ (library (name jsoo_testsuite) - (libraries unix) + (libraries unix compiler-libs.common) (foreign_stubs (language c) (names bigarray_stubs)) @@ -9,3 +9,11 @@ (modes js native)) (preprocess (pps ppx_expect))) + +(ocamlyacc calc_parser) + +(ocamllex calc_lexer) + +(ocamlyacc parser_1307) + +(ocamllex lexer_1307) diff --git a/compiler/tests-jsoo/gh_1307.ml b/compiler/tests-jsoo/gh_1307.ml new file mode 100644 index 0000000000..a1c148553b --- /dev/null +++ b/compiler/tests-jsoo/gh_1307.ml @@ -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 |}] diff --git a/compiler/tests-jsoo/lexer_1307.mll b/compiler/tests-jsoo/lexer_1307.mll new file mode 100644 index 0000000000..3c7fb1df3a --- /dev/null +++ b/compiler/tests-jsoo/lexer_1307.mll @@ -0,0 +1,7 @@ +{ + open Parser_1307 +} + +rule token = parse +| _ { TOKEN } +| eof { EOF } \ No newline at end of file diff --git a/compiler/tests-jsoo/parser_1307.mly b/compiler/tests-jsoo/parser_1307.mly new file mode 100644 index 0000000000..321951fa2d --- /dev/null +++ b/compiler/tests-jsoo/parser_1307.mly @@ -0,0 +1,17 @@ + +%{ + +%} + +%token EOF +%token TOKEN +%start root +%type root +%% + +root: +| expr EOF { 0 } +| expr error root { 1 } + +expr: + TOKEN TOKEN { 1 } diff --git a/compiler/tests-jsoo/test_parsing.ml b/compiler/tests-jsoo/test_parsing.ml new file mode 100644 index 0000000000..65fb29a99c --- /dev/null +++ b/compiler/tests-jsoo/test_parsing.ml @@ -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 |}]