Auto-detect syntax based on file extension

This commit is contained in:
Georges Dupéron 2019-06-10 22:16:08 +02:00
parent a75c0ac061
commit 6bb5a0f731
2 changed files with 18 additions and 2 deletions

View File

@ -68,9 +68,9 @@ let syntax =
let open Arg in let open Arg in
let info = let info =
let docv = "SYNTAX" in let docv = "SYNTAX" in
let doc = "$(docv) is the syntax that will be used. Currently supported syntaxes are \"pascaligo\" and \"cameligo\". \"pascaligo\" is the default." in let doc = "$(docv) is the syntax that will be used. Currently supported syntaxes are \"pascaligo\" and \"cameligo\". By default, the syntax is guessed from the extension (.ligo and .mligo, respectively)." in
info ~docv ~doc ["syntax" ; "s"] in info ~docv ~doc ["syntax" ; "s"] in
value @@ opt string "pascaligo" info value @@ opt string "auto" info
let compile_file = let compile_file =
let f source entry_point syntax = let f source entry_point syntax =

View File

@ -95,7 +95,22 @@ let parsify_expression_ligodity = fun source ->
Simplify.Ligodity.simpl_expression raw in Simplify.Ligodity.simpl_expression raw in
ok simplified ok simplified
let detect_syntax = fun syntax source ->
if String.equal syntax "auto" then
begin
let subr s n =
String.sub s (String.length s - n) n in
if String.equal (subr source 5) ".ligo"
then ok "pascaligo"
else if String.equal (subr source 6) ".mligo"
then ok "cameligo"
else simple_fail "cannot auto-detect syntax, pleas use -s name_of_syntax"
end
else
ok syntax
let parsify = fun syntax source -> let parsify = fun syntax source ->
let%bind syntax = detect_syntax syntax source in
let%bind parsify = match syntax with let%bind parsify = match syntax with
| "pascaligo" -> ok parsify_pascaligo | "pascaligo" -> ok parsify_pascaligo
| "cameligo" -> ok parsify_ligodity | "cameligo" -> ok parsify_ligodity
@ -104,6 +119,7 @@ let parsify = fun syntax source ->
parsify source parsify source
let parsify_expression = fun syntax source -> let parsify_expression = fun syntax source ->
let%bind syntax = detect_syntax syntax source in
let%bind parsify = match syntax with let%bind parsify = match syntax with
| "pascaligo" -> ok parsify_expression_pascaligo | "pascaligo" -> ok parsify_expression_pascaligo
| "cameligo" -> ok parsify_expression_ligodity | "cameligo" -> ok parsify_expression_ligodity