mirror of
https://github.com/CatalaLang/catala.git
synced 2024-09-20 00:41:05 +03:00
Clean language options
This commit is contained in:
parent
60aa306a3f
commit
32527855f0
@ -45,13 +45,14 @@ let language =
|
||||
Arg.(
|
||||
value
|
||||
& opt (some string) None
|
||||
& info [ "l"; "language" ] ~docv:"LANG" ~doc:"Input language among: en, fr, nv (default nv)")
|
||||
& info [ "l"; "language" ] ~docv:"LANG"
|
||||
~doc:"Input language among: en, fr, non-verbose (default non-verbose)")
|
||||
|
||||
type language_option = [ `Fr | `En | `NonVerbose ]
|
||||
type frontend_lang = [ `Fr | `En | `NonVerbose ]
|
||||
|
||||
type reduced_lang_option = [ `Fr | `En ]
|
||||
type backend_lang = [ `Fr | `En ]
|
||||
|
||||
let reduce_lang (lang : language_option) : reduced_lang_option =
|
||||
let to_backend_lang (lang : frontend_lang) : backend_lang =
|
||||
match lang with `En | `NonVerbose -> `En | `Fr -> `Fr
|
||||
|
||||
let output =
|
||||
|
@ -27,6 +27,7 @@ let driver (source_file : string) (debug : bool) (wrap_weaved_output : bool)
|
||||
| Some l ->
|
||||
if l = "fr" then `Fr
|
||||
else if l = "en" then `En
|
||||
else if l = "non-verbose" then `NonVerbose
|
||||
else begin
|
||||
Cli.error_print (Printf.sprintf "The selected language (%s) is not supported by Catala" l);
|
||||
exit 1
|
||||
@ -62,14 +63,15 @@ let driver (source_file : string) (debug : bool) (wrap_weaved_output : bool)
|
||||
(String.concat "\\\n" program.program_source_files);
|
||||
0
|
||||
| Cli.Latex | Cli.Html -> (
|
||||
let language : Cli.backend_lang = Cli.to_backend_lang language in
|
||||
Cli.debug_print
|
||||
(Printf.sprintf "Weaving literate program into %s"
|
||||
(match backend with Cli.Latex -> "LaTeX" | Cli.Html -> "HTML" | _ -> assert false));
|
||||
try
|
||||
let weaved_output =
|
||||
match backend with
|
||||
| Cli.Latex -> Latex.ast_to_latex program (Cli.reduce_lang language)
|
||||
| Cli.Html -> Html.ast_to_html program pygmentize_loc (Cli.reduce_lang language)
|
||||
| Cli.Latex -> Latex.ast_to_latex program language
|
||||
| Cli.Html -> Html.ast_to_html program pygmentize_loc language
|
||||
| _ -> assert false
|
||||
in
|
||||
let output_file =
|
||||
@ -105,14 +107,15 @@ let driver (source_file : string) (debug : bool) (wrap_weaved_output : bool)
|
||||
let prgm = Firstpass.translate_program_to_scope ctxt program in
|
||||
Uid.UidMap.iter
|
||||
(fun _uid scope ->
|
||||
Printf.printf "Execution of scope %d\n" scope.Scope.scope_uid;
|
||||
Cli.debug_print (Printf.sprintf "Execution of scope %d\n" scope.Scope.scope_uid);
|
||||
let exec_ctxt = Interpreter.execute_scope ctxt Interpreter.empty_exec_ctxt prgm scope in
|
||||
Uid.UidMap.iter
|
||||
(fun uid value ->
|
||||
Printf.printf "Var %s:\t%s\n" (Uid.get_ident uid)
|
||||
(Lambda.print_term ((value, Pos.no_pos), TDummy)))
|
||||
Cli.debug_print
|
||||
(Printf.sprintf "Var %s:\t%s\n" (Uid.get_ident uid)
|
||||
(Lambda.print_term ((value, Pos.no_pos), TDummy))))
|
||||
exec_ctxt;
|
||||
Printf.printf "\n")
|
||||
Cli.debug_print "\n")
|
||||
prgm;
|
||||
0
|
||||
with Errors.ContextError msg | Errors.DefaultConflict msg ->
|
||||
|
@ -23,8 +23,7 @@ module C = Cli
|
||||
let pre_html (s : string) = s
|
||||
|
||||
let wrap_html (code : string) (source_files : string list) (custom_pygments : string option)
|
||||
(language : Cli.language_option) : string =
|
||||
let language = C.reduce_lang language in
|
||||
(language : Cli.backend_lang) : string =
|
||||
let pygments = match custom_pygments with Some p -> p | None -> "pygmentize" in
|
||||
let css_file = Filename.temp_file "catala_css_pygments" "" in
|
||||
let pygments_args = [| "-f"; "html"; "-S"; "colorful"; "-a"; ".catala-code" |] in
|
||||
@ -82,7 +81,7 @@ let wrap_html (code : string) (source_files : string list) (custom_pygments : st
|
||||
source_files))
|
||||
code
|
||||
|
||||
let pygmentize_code (c : string Pos.marked) (language : C.reduced_lang_option)
|
||||
let pygmentize_code (c : string Pos.marked) (language : C.backend_lang)
|
||||
(custom_pygments : string option) : string =
|
||||
C.debug_print (Printf.sprintf "Pygmenting the code chunk %s" (Pos.to_string (Pos.get_position c)));
|
||||
let temp_file_in = Filename.temp_file "catala_html_pygments" "in" in
|
||||
@ -121,7 +120,7 @@ let pygmentize_code (c : string Pos.marked) (language : C.reduced_lang_option)
|
||||
type program_state = InsideArticle | OutsideArticle
|
||||
|
||||
let program_item_to_html (i : A.program_item) (custom_pygments : string option)
|
||||
(language : C.reduced_lang_option) (state : program_state) : string * program_state =
|
||||
(language : C.backend_lang) (state : program_state) : string * program_state =
|
||||
let closing_div =
|
||||
(* First we terminate the div of the previous article if need be *)
|
||||
match (i, state) with
|
||||
@ -177,8 +176,8 @@ let program_item_to_html (i : A.program_item) (custom_pygments : string option)
|
||||
in
|
||||
(closing_div ^ item_string, new_state)
|
||||
|
||||
let ast_to_html (program : A.program) (custom_pygments : string option)
|
||||
(language : C.reduced_lang_option) : string =
|
||||
let ast_to_html (program : A.program) (custom_pygments : string option) (language : C.backend_lang)
|
||||
: string =
|
||||
let i_s, _ =
|
||||
List.fold_left
|
||||
(fun (acc, state) i ->
|
||||
|
@ -32,8 +32,7 @@ let pre_latexify (s : string) =
|
||||
s
|
||||
|
||||
let wrap_latex (code : string) (source_files : string list) (custom_pygments : string option)
|
||||
(language : Cli.language_option) =
|
||||
let language = C.reduce_lang language in
|
||||
(language : C.backend_lang) =
|
||||
Printf.sprintf
|
||||
"\\documentclass[11pt, a4paper]{article}\n\n\
|
||||
\\usepackage[T1]{fontenc}\n\
|
||||
@ -122,7 +121,7 @@ let math_syms_replace (c : string) : string =
|
||||
in
|
||||
R.substitute ~rex:syms ~subst:syms2cmd c
|
||||
|
||||
let program_item_to_latex (i : A.program_item) (language : C.reduced_lang_option) : string =
|
||||
let program_item_to_latex (i : A.program_item) (language : C.backend_lang) : string =
|
||||
match i with
|
||||
| A.LawHeading (title, precedence) ->
|
||||
P.sprintf "\\%ssection*{%s}"
|
||||
@ -164,5 +163,5 @@ let program_item_to_latex (i : A.program_item) (language : C.reduced_lang_option
|
||||
file label
|
||||
| A.LawInclude (A.CatalaFile _ | A.LegislativeText _) -> ""
|
||||
|
||||
let ast_to_latex (program : A.program) (language : C.reduced_lang_option) : string =
|
||||
let ast_to_latex (program : A.program) (language : C.backend_lang) : string =
|
||||
String.concat "\n\n" (List.map (fun i -> program_item_to_latex i language) program.program_items)
|
||||
|
@ -130,8 +130,8 @@ let sedlex_with_menhir (lexer' : lexbuf -> Parser.token) (token_list : (string *
|
||||
with Sedlexing.MalFormed | Sedlexing.InvalidCodepoint _ ->
|
||||
Errors.lexer_error (lexing_positions lexbuf) (Utf8.lexeme lexbuf)
|
||||
|
||||
let rec parse_source_files (source_files : string list) (language : Cli.language_option) :
|
||||
Ast.program =
|
||||
let rec parse_source_files (source_files : string list) (language : Cli.frontend_lang) : Ast.program
|
||||
=
|
||||
match source_files with
|
||||
| [] -> { program_items = []; program_source_files = [] }
|
||||
| source_file :: rest -> (
|
||||
|
Loading…
Reference in New Issue
Block a user