Skip to content

Commit

Permalink
Add experimental command for extracting (string) contents from extens…
Browse files Browse the repository at this point in the history
…ion points
  • Loading branch information
zth committed Apr 28, 2024
1 parent 1d7dfb7 commit 19db752
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tools/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
## master

#### :rocket: New Feature

- _internal_ Add experimental command for extracting (string) contents from extension points.

## 0.5.0

#### :rocket: New Feature
Expand Down
6 changes: 6 additions & 0 deletions tools/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ let main () =
done;
Sys.argv.(len - 1) <- "";
Reanalyze.cli ()
| "extract-embedded" :: extPointNames :: filename :: _ ->
logAndExit
(Ok
(Tools.extractEmbedded
~extensionPoints:(extPointNames |> String.split_on_char ',')
~filename))
| ["-h"] | ["--help"] -> logAndExit (Ok help)
| ["-v"] | ["--version"] -> logAndExit (Ok version)
| _ -> logAndExit (Error help)
Expand Down
39 changes: 39 additions & 0 deletions tools/src/tools.ml
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,42 @@ let extractDocs ~entryPointFile ~debug =
in

result

let extractEmbedded ~extensionPoints ~filename =
let {Res_driver.parsetree = structure} =
Res_driver.parsingEngine.parseImplementation ~forPrinter:false ~filename
in
let content = ref [] in
let append item = content := item :: !content in
let extension (iterator : Ast_iterator.iterator) (ext : Parsetree.extension) =
(match ext with
| ( {txt},
PStr
[
{
pstr_desc =
Pstr_eval
( {
pexp_loc;
pexp_desc = Pexp_constant (Pconst_string (contents, _));
},
_ );
};
] )
when extensionPoints |> List.exists (fun v -> v = txt) ->
append (pexp_loc, txt, contents)
| _ -> ());
Ast_iterator.default_iterator.extension iterator ext
in
let iterator = {Ast_iterator.default_iterator with extension} in
iterator.structure iterator structure;
let open Analysis.Protocol in
!content
|> List.map (fun (loc, extensionName, contents) ->
stringifyObject
[
("extensionName", Some extensionName);
("contents", Some contents);
("loc", Some (Analysis.Utils.cmtLocToRange loc |> stringifyRange));
])
|> array

0 comments on commit 19db752

Please sign in to comment.