Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

add API for parsing impl/interface from a raw string #643

Merged
merged 2 commits into from
Sep 19, 2022
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
38 changes: 38 additions & 0 deletions src/res_driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ let setup ~filename ~forPrinter () =
let mode = if forPrinter then Res_parser.Default else ParseForTypeChecker in
Res_parser.make ~mode src filename

let setupFromSource ~displayFilename ~source ~forPrinter () =
let mode = if forPrinter then Res_parser.Default else ParseForTypeChecker in
Res_parser.make ~mode source displayFilename

let parsingEngine =
{
parseImplementation =
Expand Down Expand Up @@ -82,6 +86,40 @@ let parsingEngine =
Res_diagnostics.printReport diagnostics source);
}

let parseImplementationFromSource ~forPrinter ~displayFilename ~source =
let engine = setupFromSource ~displayFilename ~source ~forPrinter () in
let structure = Res_core.parseImplementation engine in
let invalid, diagnostics =
match engine.diagnostics with
| [] as diagnostics -> (false, diagnostics)
| _ as diagnostics -> (true, diagnostics)
in
{
filename = engine.scanner.filename;
source = engine.scanner.src;
parsetree = structure;
diagnostics;
invalid;
comments = List.rev engine.comments;
}

let parseInterfaceFromSource ~forPrinter ~displayFilename ~source =
let engine = setupFromSource ~displayFilename ~source ~forPrinter () in
let signature = Res_core.parseSpecification engine in
let invalid, diagnostics =
match engine.diagnostics with
| [] as diagnostics -> (false, diagnostics)
| _ as diagnostics -> (true, diagnostics)
in
{
filename = engine.scanner.filename;
source = engine.scanner.src;
parsetree = signature;
diagnostics;
invalid;
comments = List.rev engine.comments;
}

let printEngine =
{
printImplementation =
Expand Down
14 changes: 14 additions & 0 deletions src/res_driver.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ type 'diagnostics parsingEngine = {
stringOfDiagnostics: source:string -> filename:string -> 'diagnostics -> unit;
}

val parseImplementationFromSource :
forPrinter:bool ->
displayFilename:string ->
source:string ->
(Parsetree.structure, Res_diagnostics.t list) parseResult
[@@live]

val parseInterfaceFromSource :
forPrinter:bool ->
displayFilename:string ->
source:string ->
(Parsetree.signature, Res_diagnostics.t list) parseResult
[@@live]

type printEngine = {
printImplementation:
width:int ->
Expand Down