Skip to content

Commit a1ea0bb

Browse files
committed
Add options '--strip' and '--normalize' to further facilitate the comparison
of two versions of the same grammar.
1 parent e063ec5 commit a1ea0bb

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

src/gen/bin/Ocaml_tree_sitter_main.ml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type to_js_conf = {
2525
output_path: string option;
2626
sort_choices: bool;
2727
sort_rules: bool;
28+
strip: bool;
2829
}
2930

3031
type cmd_conf =
@@ -66,6 +67,7 @@ let to_js (conf : to_js_conf) =
6667
To_JS.run
6768
~sort_choices:conf.sort_choices
6869
~sort_rules:conf.sort_rules
70+
~strip:conf.strip
6971
conf.input_path conf.output_path
7072

7173
let run conf =
@@ -179,6 +181,23 @@ let to_js_cmd =
179181
in
180182
Arg.value (Arg.flag info) in
181183

184+
let strip_term : bool Term.t =
185+
let info = Arg.info ["strip"]
186+
~doc:"Remove elements that don't affect the generated OCaml types \
187+
such as precedences."
188+
in
189+
Arg.value (Arg.flag info) in
190+
191+
let normalize_term : bool Term.t =
192+
let info = Arg.info ["normalize"]
193+
~doc:"Shorthand for all the options that rearrange the grammar \
194+
so as to make them easier to compare while ignoring the parts \
195+
that don't affect the generated types in file 'CST.ml'. \
196+
It currently combines --sort-choices, --sort-rules, \
197+
and --strip."
198+
in
199+
Arg.value (Arg.flag info) in
200+
182201
let doc =
183202
"recover a tree-sitter grammar.js from grammar.json" in
184203

@@ -191,15 +210,22 @@ let to_js_cmd =
191210
https://github.com/returntocorp/ocaml-tree-sitter/issues.";
192211
] in
193212
let info = Term.info ~doc ~man "to-js" in
194-
let config input_path output_path sort_choices sort_rules =
195-
To_JS { input_path; output_path; sort_choices; sort_rules }
213+
let config input_path output_path sort_choices sort_rules strip normalize =
214+
let sort_choices, sort_rules, strip =
215+
normalize || sort_choices,
216+
normalize || sort_rules,
217+
normalize || strip
218+
in
219+
To_JS { input_path; output_path; sort_choices; sort_rules; strip }
196220
in
197221
let cmdline_term = Term.(
198222
const config
199223
$ input_path_term
200224
$ output_path_term
201225
$ sort_choices_term
202-
$ sort_rules_term) in
226+
$ sort_rules_term
227+
$ strip_term
228+
$ normalize_term) in
203229
(cmdline_term, info)
204230

205231
let gen_cmd =

src/gen/lib/To_JS.ml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,50 @@ let pp_grammar ~sort_choices ~sort_rules (x : grammar) : Indent.t =
224224
Line "});";
225225
]
226226

227-
let run ~sort_choices ~sort_rules input_path output_path =
227+
let rec strip (body : rule_body) =
228+
match body with
229+
| SYMBOL _
230+
| STRING _
231+
| PATTERN _
232+
| BLANK -> body
233+
| REPEAT x -> REPEAT (strip x)
234+
| REPEAT1 x -> REPEAT1 (strip x)
235+
| CHOICE xs -> CHOICE (List.map strip xs)
236+
| SEQ xs -> SEQ (List.map strip xs)
237+
| PREC (_prec_value, x) -> strip x
238+
| PREC_DYNAMIC (_n, x) -> strip x
239+
| PREC_LEFT (_opt_prec_value, x) -> strip x
240+
| PREC_RIGHT (_opt_prec_value, x) -> strip x
241+
| ALIAS x -> strip x.content
242+
| FIELD (_name, x) -> strip x
243+
| IMMEDIATE_TOKEN x -> strip x
244+
| TOKEN x -> strip x
245+
246+
(*
247+
Eliminate all the constructs that don't affect the structure of the
248+
tree as reflected by the types in the generated file CST.ml.
249+
*)
250+
let strip_grammar (grammar : grammar) =
251+
{
252+
grammar with
253+
extras = List.map strip grammar.extras;
254+
inline = [];
255+
conflicts = [];
256+
precedences = [];
257+
externals = (* is this useful to keep? *) List.map strip grammar.externals;
258+
supertypes = [];
259+
rules = List.map (fun (name, body) -> (name, strip body)) grammar.rules;
260+
}
261+
262+
let run ~sort_choices ~sort_rules ~strip input_path output_path =
228263
let grammar =
229264
match input_path with
230265
| None ->
231266
Atdgen_runtime.Util.Json.from_channel Tree_sitter_j.read_grammar stdin
232267
| Some file ->
233268
Atdgen_runtime.Util.Json.from_file Tree_sitter_j.read_grammar file
234269
in
270+
let grammar = if strip then strip_grammar grammar else grammar in
235271
let tree = pp_grammar ~sort_choices ~sort_rules grammar in
236272
match output_path with
237273
| None -> Indent.to_channel stdout tree

src/gen/lib/To_JS.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
val run :
1717
sort_choices:bool ->
1818
sort_rules:bool ->
19+
strip:bool ->
1920
string option -> string option -> unit

0 commit comments

Comments
 (0)