Skip to content

Commit

Permalink
Better traces
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Nov 8, 2022
1 parent 56c0dc1 commit 105c7da
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 20 deletions.
47 changes: 42 additions & 5 deletions compiler/tests-jsoo/gh_1307.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,56 @@ let test content =
print_endline "failure"

let%expect_test "parsing" =
let (_ : bool) = Parsing.set_trace false in
let (old : bool) = Parsing.set_trace true in
test "a";
[%expect {|
[%expect
{|
input: "a"
State 0: shift to state 1
State 1: read token TOKEN
State 1: shift to state 3
State 3: read token EOF
Discarding state 3
Discarding state 1
No more states to discard
Stdlib.Parsing.Parse_error
failure |}];
test "aa";
[%expect {|
[%expect
{|
input: "aa"
State 0: shift to state 1
State 1: read token TOKEN
State 1: shift to state 3
State 3: read token TOKEN
State 3: shift to state 6
State 6: reduce by rule 3
State 5: read token EOF
State 5: shift to state 8
State 8: reduce by rule 1
State 4: reduce by rule 4
0
success |}];
test "aaa";
[%expect {|
[%expect
{|
input: "aaa"
State 0: shift to state 1
State 1: read token TOKEN
State 1: shift to state 3
State 3: read token TOKEN
State 3: shift to state 6
State 6: reduce by rule 3
State 5: read token TOKEN
Recovering in state 5
State 5: shift to state 7
State 7: shift to state 3
State 3: read token EOF
Discarding state 3
Discarding state 7
Recovering in state 5
State 3: shift to state 7
Stdlib.Parsing.Parse_error
failure |}]
failure |}];
let (_ : bool) = Parsing.set_trace old in
()
81 changes: 76 additions & 5 deletions compiler/tests-jsoo/test_parsing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,89 @@ let parse s =
with Calc_lexer.Eof -> print_endline "EOF"

let%expect_test "parsing" =
let (old : bool) = Parsing.set_trace false in
let (old : bool) = Parsing.set_trace true in
parse "1+2*3";
[%expect {|
[%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 {|
[%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 {|
[%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 {|
[%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";
Expand Down
37 changes: 27 additions & 10 deletions runtime/parsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var caml_parser_trace = 0;

//Provides: caml_parse_engine
//Requires: caml_lex_array, caml_parser_trace,caml_jsstring_of_string
//Requires: caml_ml_output, caml_ml_string_length, caml_string_of_jsbytes
//Requires: caml_jsbytes_of_string, MlBytes
function caml_parse_engine(tables, env, cmd, arg)
{
var ERRCODE = 256;
Expand Down Expand Up @@ -79,6 +81,12 @@ function caml_parse_engine(tables, env, cmd, arg)
var tbl_names_const = 15;
var tbl_names_block = 16;


function log(x) {
var s = caml_string_of_jsbytes(x + "\n");
caml_ml_output(2, s, 0, caml_ml_string_length(s));
}

function token_name(names, number)
{
var str = caml_jsstring_of_string(names);
Expand All @@ -87,15 +95,24 @@ function caml_parse_engine(tables, env, cmd, arg)
return str.split('\x00')[number];
}

function print_token(tok)
function print_token(state, tok)
{
var token;
var token, kind;
if (tok instanceof Array) {
token = token_name(tables[tbl_names_block], tok[0]);
if (typeof tok[1] == "number")
kind = "" + tok[1];
else if (typeof tok[1] == "string")
kind = tok[1]
else if (tok[1] instanceof MlBytes)
kind = caml_jsbytes_of_string(tok[1])
else
kind = "_"
log("State " + state + ": read token " + token + "(" + kind + ")");
} else {
token = token_name(tables[tbl_names_const], tok);
log("State " + state + ": read token " + token);
}
console.log("State", state, ": read token", token);
}

if (!tables.dgoto) {
Expand Down Expand Up @@ -140,7 +157,7 @@ function caml_parse_engine(tables, env, cmd, arg)
env[env_curr_char] = tables[tbl_transl_const][arg + 1];
env[env_lval] = 0;
}
if (caml_parser_trace) print_token (arg);
if (caml_parser_trace) print_token (state, arg);
// Fall through

case 7://testshift:
Expand Down Expand Up @@ -173,14 +190,14 @@ function caml_parse_engine(tables, env, cmd, arg)
if (n1 != 0 && n2 >= 0 && n2 <= tables[tbl_tablesize] &&
tables.check[n2] == ERRCODE) {
if (caml_parser_trace)
console.log("Recovering in state", state1);
log("Recovering in state " + state1);
cmd = shift_recover; break next;
} else {
if (caml_parser_trace)
console.log("Discarding state", state1);
log("Discarding state " + state1);
if (sp <= env[env_stackbase]) {
if (caml_parser_trace)
console.log("No more states to discard");
log("No more states to discard");
return RAISE_PARSE_ERROR;
}
/* The ML code raises Parse_error */
Expand All @@ -191,7 +208,7 @@ function caml_parse_engine(tables, env, cmd, arg)
if (env[env_curr_char] == 0)
return RAISE_PARSE_ERROR; /* The ML code raises Parse_error */
if (caml_parser_trace)
console.log("Discarding last token read");
log("Discarding last token read");
env[env_curr_char] = -1;
cmd = loop; break;
}
Expand All @@ -202,7 +219,7 @@ function caml_parse_engine(tables, env, cmd, arg)
// Fall through
case 9://shift_recover:
if (caml_parser_trace)
console.log("State", state, ": shift to state ", tables.table[n2]);
log("State " + state + ": shift to state " + tables.table[n2]);
state = tables.table[n2];
sp++;
if (sp >= env[env_stacksize]) {
Expand All @@ -221,7 +238,7 @@ function caml_parse_engine(tables, env, cmd, arg)

case 10://reduce:
if (caml_parser_trace)
console.log("State", state, ": reduce by rule", n);
log("State " + state + ": reduce by rule " + n);
var m = tables.len[n];
env[env_asp] = sp;
env[env_rule_number] = n;
Expand Down

0 comments on commit 105c7da

Please sign in to comment.