Skip to content

Commit

Permalink
Fix extra dumpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mjambon committed Sep 10, 2024
1 parent 31b0c2a commit 837731c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
29 changes: 20 additions & 9 deletions src/gen/lib/Codegen_boilerplate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ let gen ~cst_module_name ~is_extra grammar =
|> Codegen_util.interleave [Line ""]
|> List.flatten

let generate_dumper grammar =
sprintf "\
let dump_tree root =
map_%s () root
|> Tree_sitter_run.Raw_tree.to_channel stdout
"
(trans grammar.entrypoint)

let generate_map_extra grammar =
let cases =
grammar.extras
Expand All @@ -211,18 +220,20 @@ let generate_map_extra grammar =
|> String.concat ""
in
sprintf "\
let map_extra (env : env) (x : CST.extra) =
match x with
%s"
cases

let generate_dumper grammar =
sprintf "\
let dump_tree root =
map_%s () root
|> Tree_sitter_run.Raw_tree.to_channel stdout
let generate_extra_dumper grammar =
match grammar.extras with
| [] ->
"\
let dump_extras (extras : CST.extras) = ()
"
| _ ->
sprintf "\
%s
let dump_extras (extras : CST.extras) =
List.iter (fun extra ->
Expand All @@ -237,8 +248,7 @@ let dump_extras (extras : CST.extras) =
Tree_sitter_run.Raw_tree.to_channel stdout raw_tree
) extras
"
(trans grammar.entrypoint)
(generate_map_extra grammar)
(generate_map_extra grammar)

let make_is_extra grammar =
let tbl = Hashtbl.create 100 in
Expand All @@ -253,3 +263,4 @@ let generate ~cst_module_name grammar =
make_header grammar
^ Indent.to_string tree
^ generate_dumper grammar
^ generate_extra_dumper grammar
2 changes: 1 addition & 1 deletion test/extras/check-test-output
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
set -eu

if ! grep Complex_extra test.out/ok/complex_extra.cst > /dev/null; then
if ! grep 'complex_extra:' test.out/ok/complex_extra.cst > /dev/null; then
echo "Extra 'complex_extra' is missing from test.out/ok/complex_extra.cst"
exit 1
fi
5 changes: 2 additions & 3 deletions test/implicit-extra/check-test-output
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ for actual in $targets; do
diff -u "$expected" "$actual"
done

echo "Checking parse results."

diff example-parse.expected <(./parse test/ok/example)
echo "Checking parse results: example-parse.expected"
diff -u example-parse.expected <(./parse test/ok/example)
40 changes: 16 additions & 24 deletions test/implicit-extra/example-parse.expected
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,22 @@ Recovered typed CST:
]
)
Extras:
| Period
| "0:0-0:1"
| "."
| Comma
| "0:1-0:2"
| ","
| Period
| "0:6-0:7"
| "."
| Comma
| "0:7-0:8"
| ","
| Comma
| "0:12-0:13"
| ","
| Period
| "0:13-0:14"
| "."
| Period
| "0:18-0:19"
| "."
| Comma
| "0:19-0:20"
| ","
period:
"."
comma:
","
period:
"."
comma:
","
comma:
","
period:
"."
period:
"."
comma:
","
total lines: 2
error lines: 0
error count: 0
Expand Down

0 comments on commit 837731c

Please sign in to comment.