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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
open Js_of_ocaml_compiler
open Stdlib
let exe =
match Sys.os_type with
| "Cygwin" | "Win32" -> fun x -> x ^ ".exe"
| "Unix" | _ -> fun x -> x
let node = try Sys.getenv "NODE" with Not_found -> exe "node"
let failure_expected = ref false
let progress = ref false
let verbose = ref false
let flags, files =
Sys.argv
|> Array.to_list
|> List.tl
|> List.partition ~f:(fun x -> Char.equal (String.get x 0) '-')
let rec ls_files path =
match Unix.lstat path with
| { st_kind = S_DIR; _ } ->
Sys.readdir path
|> Array.to_list
|> List.concat_map ~f:(fun name -> ls_files (Filename.concat path name))
| { st_kind = S_REG; _ } -> [ path ]
| { st_kind = S_CHR | S_BLK | S_LNK | S_FIFO | S_SOCK; _ } -> []
let files =
List.concat_map files ~f:(fun path ->
let l = ls_files path in
List.filter l ~f:(fun name -> Filename.check_suffix name ".js"))
let () = if !verbose then Printf.eprintf "Found %d files\n%!" (List.length files)
let () =
List.iter flags ~f:(function
| "--fail" -> failure_expected := true
| "-p" | "--progress" -> progress := true
| "-v" | "--verbose" -> verbose := true
| f -> failwith ("unrecognised flag " ^ f))
type error =
| Diff of Javascript.program * Javascript.program
| Print_parse of Parse_info.t * string
| Parse of Parse_info.t * string
| Parse_warning of Parse_js.Lexer.error list
| Tok_missmatch of string
let unsupported = ref []
let negative = ref []
let noStrict = ref []
let fail = ref []
let pass = ref []
let normalize_string s =
let l = String.length s in
let b = Buffer.create (String.length s + 2) in
for i = 0 to l - 1 do
let c = s.[i] in
match c with
| '\000' when i = l - 1 || not (Char.is_num s.[i + 1]) -> Buffer.add_string b "\\0"
| '\b' -> Buffer.add_string b "\\b"
| '\t' -> Buffer.add_string b "\\t"
| '\n' -> Buffer.add_string b "\\n"
(* This escape sequence is not supported by IE < 9
| '\011' -> "\\v"
*)
| '\012' -> Buffer.add_string b "\\f"
(* https://github.com/ocsigen/js_of_ocaml/issues/898 *)
| '/' when i > 0 && Char.equal s.[i - 1] '<' -> Buffer.add_string b "\\/"
| '\r' -> Buffer.add_string b "\\r"
| '\000' .. '\031' | '\127' ->
Buffer.add_string b "\\x";
Buffer.add_char_hex b c
| _ -> Buffer.add_char b c
done;
Buffer.contents b
class clean_loc =
object
inherit Js_traverse.map as super
method! parse_info _ = Parse_info.zero
method! loc _ = N
method! expression e =
match e with
| EStr (Utf8 x) -> EStr (Utf8_string.of_string_exn (normalize_string x))
| e -> super#expression e
end
let clean_loc = new clean_loc
let clean_loc p = clean_loc#program p
let p_to_string p =
let buffer = Buffer.create 100 in
let pp = Pretty_print.to_buffer buffer in
let _ = Js_output.program pp p in
Buffer.contents buffer
let accepted_by_node file =
let ((ic, oc, ec) as ic_oc) =
Unix.open_process_full (Printf.sprintf "%s --check %s" node file) [||]
in
let pid = Unix.process_full_pid ic_oc in
let _pid, status = Unix.waitpid [] pid in
close_in ic;
close_out oc;
close_in ec;
match status with
| WEXITED 0 -> true
| WEXITED _ -> false
| WSIGNALED _ | WSTOPPED _ -> assert false
let should_ignore_feature = function
| "import-assertions" | "import-attributes" | "decorators" -> true
| _ -> false
let should_ignore_flag = function
| "noStrict" -> true
| _ -> false
let token_equal : Js_token.t -> Js_token.t -> bool =
fun a b ->
match a, b with
| T_SEMICOLON, T_VIRTUAL_SEMICOLON | T_VIRTUAL_SEMICOLON, T_SEMICOLON -> true
| T_DECR_NB, T_DECR | T_DECR, T_DECR_NB | T_INCR_NB, T_INCR | T_INCR, T_INCR_NB -> true
| T_IDENTIFIER (Utf8 a, _), T_IDENTIFIER (Utf8 b, _) -> String.equal a b
| T_STRING (Utf8 a, _), T_STRING (Utf8 b, _) ->
String.equal a b || String.equal (normalize_string a) (normalize_string b)
| a, T_IDENTIFIER (Utf8 b, _) when Poly.(Some a = Js_token.is_keyword b) -> true
| T_IDENTIFIER (Utf8 a, _), b when Poly.(Some b = Js_token.is_keyword a) -> true
| a, b -> Poly.(a = b)
let rec check_toks
(a : (Js_token.t * _) list)
(stack_a : Js_token.t list)
(b : (Js_token.t * _) list)
(stack_b : Js_token.t list) : _ =
match a, b with
| ( ( ( TComment _
| T_EOF
| T_LPAREN
| T_LPAREN_ARROW
| T_RPAREN
| T_SEMICOLON
| T_VIRTUAL_SEMICOLON
| T_COMMA )
, _ )
:: a
, b ) -> check_toks a stack_a b stack_b
| ( a
, ( ( TComment _
| T_EOF
| T_LPAREN
| T_LPAREN_ARROW
| T_RPAREN
| T_SEMICOLON
| T_VIRTUAL_SEMICOLON
| T_COMMA )
, _ )
:: b ) -> check_toks a stack_a b stack_b
| (ta, loc_a) :: ra, (tb, _loc_b) :: rb ->
if token_equal ta tb
then check_toks ra stack_a rb stack_b
else
Error
(Printf.sprintf
"token mismatch (%d:%d) %s <> %s\n"
(Loc.line loc_a)
(Loc.column loc_a)
(Js_token.to_string ta)
(Js_token.to_string tb))
| [], [] -> Ok ()
| [], (tb, _) :: _rb ->
Error (Printf.sprintf "token mismatch <EOF> vs %s\n" (Js_token.to_string tb))
| (ta, _) :: _ra, [] ->
Error (Printf.sprintf "token mismatch %s vs <EOF>\n" (Js_token.to_string ta))
let to_file p =
let s = p_to_string p in
let f, oc = Filename.open_temp_file "as" "WE" in
output_string oc s;
output_string oc "\n";
close_out oc;
f
let patdiff p1 p2 =
ignore (Sys.command (Printf.sprintf "patdiff %s %s" (to_file p1) (to_file p2)));
flush_all ()
let print_loc_error (pi : Parse_info.t) c =
List.iteri (String.split_on_char ~sep:'\n' c) ~f:(fun i c ->
let diff = abs (i + 1 - pi.line) in
if diff = 0
then (
let b = Buffer.create (String.length c) in
String.fold_utf_8 c () ~f:(fun () i u ->
if i = pi.col then Buffer.add_utf_8_uchar b (Uchar.of_int 0x274C);
Buffer.add_utf_8_uchar b u);
Printf.eprintf "%s\n" (Buffer.contents b))
else if diff < 5
then Printf.eprintf "%s\n" c
else ());
Printf.eprintf "\n"
let () =
let negative_r = Str.regexp_string "negative:" in
let features_r = Str.regexp {|features: ?\[\([a-zA-Z. ,-]*\)\]|} in
let flags_r = Str.regexp {|flags: ?\[\([a-zA-Z. ,-]*\)\]|} in
let total = List.length files in
List.iteri files ~f:(fun i filename ->
let () = if !progress then Printf.eprintf "%d/%d\r%!" i total in
let ic = open_in_bin filename in
let content = In_channel.input_all ic in
let errors = ref [] in
let add r = r := (filename, content) :: !r in
close_in ic;
let mode =
match Str.search_forward negative_r content 0 with
| _ -> `Negative
| exception Not_found -> (
let features =
match Str.search_forward features_r content 0 with
| _ ->
String.split_on_char ~sep:',' (Str.matched_group 1 content)
|> List.map ~f:String.trim
| exception Not_found -> []
in
let flags =
match Str.search_forward flags_r content 0 with
| _ ->
String.split_on_char ~sep:',' (Str.matched_group 1 content)
|> List.map ~f:String.trim
| exception Not_found -> []
in
match List.find_opt ~f:should_ignore_feature features with
| Some f -> `Unsupported f
| None -> (
match List.find_opt ~f:should_ignore_flag flags with
| Some f -> `Unsupported f
| None -> `Ok))
in
match mode with
| `Negative -> negative := filename :: !negative
| `Unsupported "noStrict" -> noStrict := filename :: !noStrict
| `Unsupported _ -> unsupported := (filename, content) :: !unsupported
| `Ok -> (
try
let p1, toks1 =
Parse_js.Lexer.of_string
~report_error:(fun e -> errors := e :: !errors)
~filename
content
|> Parse_js.parse'
in
let p1 = List.concat_map p1 ~f:snd in
match List.rev !errors with
| [] -> (
let s = p_to_string p1 in
try
let p2, toks2 =
Parse_js.Lexer.of_string
~report_error:(fun e -> errors := e :: !errors)
~filename
s
|> Parse_js.parse'
in
let p2 = List.concat_map p2 ~f:snd in
match
Poly.(clean_loc p1 = clean_loc p2), check_toks toks1 [] toks2 []
with
| true, Error s when false ->
fail := (Tok_missmatch s, filename) :: !fail
| true, _ -> add pass
| false, _ -> fail := (Diff (p1, p2), filename) :: !fail
with Parse_js.Parsing_error loc ->
if not (accepted_by_node filename)
then add unsupported
else fail := (Print_parse (loc, s), filename) :: !fail)
| l ->
if accepted_by_node filename
then fail := (Parse_warning l, filename) :: !fail
with
| Parse_js.Parsing_error loc ->
if not (accepted_by_node filename)
then add unsupported
else fail := (Parse (loc, content), filename) :: !fail
| e ->
Printf.eprintf "Unexpected error %s\n%s\n" filename (Printexc.to_string e)));
Printf.printf "Summary:\n";
Printf.printf " invalid : %d\n" (List.length !negative);
Printf.printf " not scrict : %d\n" (List.length !noStrict);
Printf.printf " skip : %d\n" (List.length !unsupported);
Printf.printf " fail : %d\n" (List.length !fail);
Printf.printf " pass : %d\n" (List.length !pass);
flush_all ();
let l = !fail in
if !failure_expected
then (
List.iter !pass ~f:(fun (f, c) ->
Printf.printf "succeded to parse %s\n" f;
Printf.printf "%s\n\n" c);
match !pass with
| [] -> exit 0
| _ -> exit 1)
else (
List.iter l ~f:(fun (reason, f) ->
match reason with
| Parse (pi, content) ->
Printf.eprintf "<ERROR>: Parse_error: %s:%d:%d\n" f pi.line pi.col;
print_loc_error pi content
| Print_parse (pi, content) ->
Printf.eprintf "<ERROR>: Parse_error(roundtrip): %s:%d:%d\n" f pi.line pi.col;
print_loc_error pi content
| Diff (p1, p2) ->
Printf.eprintf "<ERROR>: Diff: %s\n%!" f;
patdiff p1 p2
| Parse_warning l ->
Printf.eprintf "<ERROR>: Lexer warning: %s\n" f;
List.iter l ~f:Parse_js.Lexer.print_error
| Tok_missmatch s ->
Printf.eprintf "<ERROR>: Tok mismatch: %s\n" f;
Printf.eprintf "%s\n" s);
match !fail with
| [] -> exit 0
| _ -> exit 1)
|