Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental command for extracting (string) contents from extension points #974

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading