| 71888 |      1 | (*  Title:      Pure/System/scala_compiler.ML
 | 
| 71881 |      2 |     Author:     Makarius
 | 
|  |      3 | 
 | 
| 71888 |      4 | Scala compiler operations.
 | 
| 71881 |      5 | *)
 | 
|  |      6 | 
 | 
| 71888 |      7 | signature SCALA_COMPILER =
 | 
| 71881 |      8 | sig
 | 
| 75654 |      9 |   val toplevel: string -> unit
 | 
| 71906 |     10 |   val static_check: string * Position.T -> unit
 | 
| 71881 |     11 | end;
 | 
|  |     12 | 
 | 
| 71888 |     13 | structure Scala_Compiler: SCALA_COMPILER =
 | 
| 71881 |     14 | struct
 | 
|  |     15 | 
 | 
| 71887 |     16 | (* check declaration *)
 | 
|  |     17 | 
 | 
| 75654 |     18 | fun toplevel source =
 | 
| 71881 |     19 |   let val errors =
 | 
| 75654 |     20 |     \<^scala>\<open>scala_toplevel\<close> source
 | 
| 71881 |     21 |     |> YXML.parse_body
 | 
|  |     22 |     |> let open XML.Decode in list string end
 | 
|  |     23 |   in if null errors then () else error (cat_lines errors) end;
 | 
|  |     24 | 
 | 
| 71906 |     25 | fun static_check (source, pos) =
 | 
| 75654 |     26 |   toplevel ("class __Dummy__ { __dummy__ => " ^ source ^ " }")
 | 
| 71906 |     27 |     handle ERROR msg => error (msg ^ Position.here pos);
 | 
| 71888 |     28 | 
 | 
| 71887 |     29 | 
 | 
|  |     30 | (* antiquotations *)
 | 
|  |     31 | 
 | 
|  |     32 | local
 | 
|  |     33 | 
 | 
|  |     34 | fun make_list bg en = space_implode "," #> enclose bg en;
 | 
|  |     35 | 
 | 
|  |     36 | fun print_args [] = ""
 | 
|  |     37 |   | print_args xs = make_list "(" ")" xs;
 | 
|  |     38 | 
 | 
|  |     39 | fun print_types [] = ""
 | 
|  |     40 |   | print_types Ts = make_list "[" "]" Ts;
 | 
|  |     41 | 
 | 
|  |     42 | fun print_class (c, Ts) = c ^ print_types Ts;
 | 
|  |     43 | 
 | 
|  |     44 | val types =
 | 
|  |     45 |   Scan.optional (Parse.$$$ "[" |-- Parse.list1 Parse.name --| Parse.$$$ "]") [];
 | 
|  |     46 | 
 | 
| 71905 |     47 | val class =
 | 
|  |     48 |   Scan.option
 | 
|  |     49 |     (Parse.$$$ "(" |-- Parse.!!! (Parse.$$$ "in" |-- Parse.name -- types  --| Parse.$$$ ")"));
 | 
| 71887 |     50 | 
 | 
|  |     51 | val arguments =
 | 
|  |     52 |   (Parse.nat >> (fn n => replicate n "_") ||
 | 
| 71905 |     53 |     Parse.list (Parse.underscore || Parse.name >> (fn T => "_ : " ^ T))) >> print_args;
 | 
| 71887 |     54 | 
 | 
| 71905 |     55 | val args = Scan.optional (Parse.$$$ "(" |-- arguments --| Parse.$$$ ")") " _";
 | 
| 71887 |     56 | 
 | 
| 71900 |     57 | fun scala_name name =
 | 
| 74790 |     58 |   Latex.macro "isatt" (Latex.string (Latex.output_ascii_breakable "." name));
 | 
| 71900 |     59 | 
 | 
| 71887 |     60 | in
 | 
|  |     61 | 
 | 
|  |     62 | val _ =
 | 
|  |     63 |   Theory.setup
 | 
| 73761 |     64 |     (Document_Output.antiquotation_verbatim_embedded \<^binding>\<open>scala\<close>
 | 
| 74563 |     65 |       (Scan.lift Parse.embedded_position)
 | 
| 71906 |     66 |       (fn _ => fn (s, pos) => (static_check (s, pos); s)) #>
 | 
| 71891 |     67 | 
 | 
| 73761 |     68 |     Document_Output.antiquotation_raw_embedded \<^binding>\<open>scala_type\<close>
 | 
| 74563 |     69 |       (Scan.lift (Parse.embedded_position -- (types >> print_types)))
 | 
| 71906 |     70 |       (fn _ => fn ((t, pos), type_args) =>
 | 
|  |     71 |         (static_check ("type _Test_" ^ type_args ^ " = " ^ t ^ type_args, pos);
 | 
| 71900 |     72 |           scala_name (t ^ type_args))) #>
 | 
| 71887 |     73 | 
 | 
| 73761 |     74 |     Document_Output.antiquotation_raw_embedded \<^binding>\<open>scala_object\<close>
 | 
| 74563 |     75 |       (Scan.lift Parse.embedded_position)
 | 
| 71906 |     76 |       (fn _ => fn (x, pos) => (static_check ("val _test_ = " ^ x, pos); scala_name x)) #>
 | 
| 71887 |     77 | 
 | 
| 73761 |     78 |     Document_Output.antiquotation_raw_embedded \<^binding>\<open>scala_method\<close>
 | 
| 74563 |     79 |       (Scan.lift (class -- Parse.embedded_position -- types -- args))
 | 
| 71906 |     80 |       (fn _ => fn (((class_context, (method, pos)), method_types), method_args) =>
 | 
| 71887 |     81 |         let
 | 
| 71905 |     82 |           val class_types = (case class_context of SOME (_, Ts) => Ts | NONE => []);
 | 
| 71887 |     83 |           val def = "def _test_" ^ print_types (merge (op =) (method_types, class_types));
 | 
|  |     84 |           val def_context =
 | 
| 71905 |     85 |             (case class_context of
 | 
| 71887 |     86 |               NONE => def ^ " = "
 | 
|  |     87 |             | SOME c => def ^ "(_this_ : " ^ print_class c ^ ") = _this_.");
 | 
|  |     88 |           val source = def_context ^ method ^ method_args;
 | 
| 71906 |     89 |           val _ = static_check (source, pos);
 | 
| 71905 |     90 |           val text = (case class_context of NONE => method | SOME c => print_class c ^ "." ^ method);
 | 
|  |     91 |         in scala_name text end));
 | 
| 71887 |     92 | 
 | 
| 71881 |     93 | end;
 | 
| 71887 |     94 | 
 | 
|  |     95 | end;
 |