Skip to content

Commit 90b4a4d

Browse files
authoredApr 28, 2024··
Add experimental command for extracting (string) contents from extension points (#974)
1 parent 31899c5 commit 90b4a4d

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
 

‎tools/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
## master
1414

15+
#### :rocket: New Feature
16+
17+
- _internal_ Add experimental command for extracting (string) contents from extension points.
18+
1519
## 0.5.0
1620

1721
#### :rocket: New Feature

‎tools/bin/main.ml

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ let main () =
5050
done;
5151
Sys.argv.(len - 1) <- "";
5252
Reanalyze.cli ()
53+
| "extract-embedded" :: extPointNames :: filename :: _ ->
54+
logAndExit
55+
(Ok
56+
(Tools.extractEmbedded
57+
~extensionPoints:(extPointNames |> String.split_on_char ',')
58+
~filename))
5359
| ["-h"] | ["--help"] -> logAndExit (Ok help)
5460
| ["-v"] | ["--version"] -> logAndExit (Ok version)
5561
| _ -> logAndExit (Error help)

‎tools/src/tools.ml

+39
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,42 @@ let extractDocs ~entryPointFile ~debug =
443443
in
444444

445445
result
446+
447+
let extractEmbedded ~extensionPoints ~filename =
448+
let {Res_driver.parsetree = structure} =
449+
Res_driver.parsingEngine.parseImplementation ~forPrinter:false ~filename
450+
in
451+
let content = ref [] in
452+
let append item = content := item :: !content in
453+
let extension (iterator : Ast_iterator.iterator) (ext : Parsetree.extension) =
454+
(match ext with
455+
| ( {txt},
456+
PStr
457+
[
458+
{
459+
pstr_desc =
460+
Pstr_eval
461+
( {
462+
pexp_loc;
463+
pexp_desc = Pexp_constant (Pconst_string (contents, _));
464+
},
465+
_ );
466+
};
467+
] )
468+
when extensionPoints |> List.exists (fun v -> v = txt) ->
469+
append (pexp_loc, txt, contents)
470+
| _ -> ());
471+
Ast_iterator.default_iterator.extension iterator ext
472+
in
473+
let iterator = {Ast_iterator.default_iterator with extension} in
474+
iterator.structure iterator structure;
475+
let open Analysis.Protocol in
476+
!content
477+
|> List.map (fun (loc, extensionName, contents) ->
478+
stringifyObject
479+
[
480+
("extensionName", Some extensionName);
481+
("contents", Some contents);
482+
("loc", Some (Analysis.Utils.cmtLocToRange loc |> stringifyRange));
483+
])
484+
|> array

0 commit comments

Comments
 (0)
Please sign in to comment.