src/Pure/ML/ml_compiler_polyml-5.3.ML
author wenzelm
Fri, 17 Sep 2010 15:51:11 +0200
changeset 39439 1c294d150ded
parent 39240 a0c0698e56c0
child 39440 4c2547af5909
permissions -rw-r--r--
eliminated markup "location" in favour of more explicit "no_report", which is actually deleted from messages;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML/ml_compiler_polyml-5.3.ML
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     3
33538
edf497b5b5d2 setup for official Poly/ML 5.3.0, which is now the default;
wenzelm
parents: 32738
diff changeset
     4
Advanced runtime compilation for Poly/ML 5.3.0.
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     5
*)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     6
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     7
structure ML_Compiler: ML_COMPILER =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     8
struct
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
     9
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    10
(* source locations *)
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    11
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    12
fun position_of (loc: PolyML.location) =
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    13
  let
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    14
    val {file = text, startLine = line, startPosition = offset,
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    15
      endLine = end_line, endPosition = end_offset} = loc;
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    16
    val loc_props =
31434
1f9b6a5dc8cc more robust treatment of bootstrap source positions;
wenzelm
parents: 31429
diff changeset
    17
      (case YXML.parse text of
38228
ada3ab6b9085 simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
wenzelm
parents: 37209
diff changeset
    18
        XML.Elem ((e, atts), _) => if e = Markup.positionN then atts else []
31434
1f9b6a5dc8cc more robust treatment of bootstrap source positions;
wenzelm
parents: 31429
diff changeset
    19
      | XML.Text s => Position.file_name s);
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    20
  in
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    21
    Position.value Markup.lineN line @
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    22
    Position.value Markup.offsetN offset @
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    23
    Position.value Markup.end_lineN end_line @
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    24
    Position.value Markup.end_offsetN end_offset @
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    25
    loc_props
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    26
  end |> Position.of_properties;
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    27
39240
a0c0698e56c0 ML_Compiler.eval: reported positions need to contain offset, to avoid displaced reports due to synthesized line numbers;
wenzelm
parents: 39232
diff changeset
    28
fun offset_position_of loc =
a0c0698e56c0 ML_Compiler.eval: reported positions need to contain offset, to avoid displaced reports due to synthesized line numbers;
wenzelm
parents: 39232
diff changeset
    29
  let val pos = position_of loc
a0c0698e56c0 ML_Compiler.eval: reported positions need to contain offset, to avoid displaced reports due to synthesized line numbers;
wenzelm
parents: 39232
diff changeset
    30
  in if is_some (Position.offset_of pos) then pos else Position.none end;
a0c0698e56c0 ML_Compiler.eval: reported positions need to contain offset, to avoid displaced reports due to synthesized line numbers;
wenzelm
parents: 39232
diff changeset
    31
31477
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
    32
fun exn_position exn =
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    33
  (case PolyML.exceptionLocation exn of
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    34
    NONE => Position.none
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    35
  | SOME loc => position_of loc);
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    36
38874
4a4d34d2f97b more careful treatment of nested multi-exceptions, collapsing cumulatively empty list to Interrupt;
wenzelm
parents: 38720
diff changeset
    37
val exn_messages = Runtime.exn_messages exn_position;
31477
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
    38
val exn_message = Runtime.exn_message exn_position;
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
    39
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    40
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    41
(* parse trees *)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    42
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    43
fun report_parse_tree depth space =
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    44
  let
38720
7f8bc335e203 ML_Context.eval: produce antiquotation environment preferably in invisible context, to avoid displaced report messages from ML_Compiler;
wenzelm
parents: 38228
diff changeset
    45
    val report_text =
7f8bc335e203 ML_Context.eval: produce antiquotation environment preferably in invisible context, to avoid displaced report messages from ML_Compiler;
wenzelm
parents: 38228
diff changeset
    46
      (case Context.thread_data () of
7f8bc335e203 ML_Context.eval: produce antiquotation environment preferably in invisible context, to avoid displaced report messages from ML_Compiler;
wenzelm
parents: 38228
diff changeset
    47
        SOME (Context.Proof ctxt) => Context_Position.report_text ctxt
7f8bc335e203 ML_Context.eval: produce antiquotation environment preferably in invisible context, to avoid displaced report messages from ML_Compiler;
wenzelm
parents: 38228
diff changeset
    48
      | _ => Position.report_text);
7f8bc335e203 ML_Context.eval: produce antiquotation environment preferably in invisible context, to avoid displaced report messages from ML_Compiler;
wenzelm
parents: 38228
diff changeset
    49
31475
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    50
    fun report_decl markup loc decl =
39240
a0c0698e56c0 ML_Compiler.eval: reported positions need to contain offset, to avoid displaced reports due to synthesized line numbers;
wenzelm
parents: 39232
diff changeset
    51
      report_text Markup.ML_ref (offset_position_of loc)
31475
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    52
        (Markup.markup (Markup.properties (Position.properties_of (position_of decl)) markup) "");
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    53
    fun report loc (PolyML.PTtype types) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    54
          PolyML.NameSpace.displayTypeExpression (types, depth, space)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    55
          |> pretty_ml |> Pretty.from_ML |> Pretty.string_of
39240
a0c0698e56c0 ML_Compiler.eval: reported positions need to contain offset, to avoid displaced reports due to synthesized line numbers;
wenzelm
parents: 39232
diff changeset
    56
          |> report_text Markup.ML_typing (offset_position_of loc)
31475
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    57
      | report loc (PolyML.PTdeclaredAt decl) = report_decl Markup.ML_def loc decl
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    58
      | report loc (PolyML.PTopenedAt decl) = report_decl Markup.ML_open loc decl
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
    59
      | report loc (PolyML.PTstructureAt decl) = report_decl Markup.ML_struct loc decl
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    60
      | report _ (PolyML.PTnextSibling tree) = report_tree (tree ())
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    61
      | report _ (PolyML.PTfirstChild tree) = report_tree (tree ())
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    62
      | report _ _ = ()
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    63
    and report_tree (loc, props) = List.app (report loc) props;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    64
  in report_tree end;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    65
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    66
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    67
(* eval ML source tokens *)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    68
31756
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    69
val offset_of = the_default 0 o Position.offset_of;
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    70
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    71
fun eval verbose pos toks =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    72
  let
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    73
    val _ = Secure.secure_mltext ();
39227
4985f4706c6d clarified -- inlined ML_Env.local_context;
wenzelm
parents: 38874
diff changeset
    74
    val space = ML_Env.name_space;
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    75
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    76
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    77
    (* input *)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    78
31437
70309dc3deac removed unused location_of;
wenzelm
parents: 31434
diff changeset
    79
    val location_props =
32492
9d49a280f3f9 eval/location_props: always produce YXML markup, independent of print_mode;
wenzelm
parents: 32490
diff changeset
    80
      op ^ (YXML.output_markup (Markup.position |> Markup.properties
9d49a280f3f9 eval/location_props: always produce YXML markup, independent of print_mode;
wenzelm
parents: 32490
diff changeset
    81
            (filter (member (op =) [Markup.idN, Markup.fileN] o #1) (Position.properties_of pos))));
31437
70309dc3deac removed unused location_of;
wenzelm
parents: 31434
diff changeset
    82
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    83
    val input = toks |> maps (fn tok =>
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    84
      let
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
    85
        val syms = Symbol.explode (ML_Lex.check_content_of tok);
31437
70309dc3deac removed unused location_of;
wenzelm
parents: 31434
diff changeset
    86
        val (ps, _) = fold_map (fn s => fn p => (p, Position.advance s p)) syms
70309dc3deac removed unused location_of;
wenzelm
parents: 31434
diff changeset
    87
          (Position.reset_range (ML_Lex.pos_of tok));
31756
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    88
      in
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    89
        (ps ~~ syms) |> maps (fn (p, sym) =>
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    90
          map (pair (offset_of p)) (String.explode (Symbol.esc sym)))
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    91
      end);
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    92
37209
1c8cf0048934 terminate ML compiler input produced by ML_Lex.read (cf. 85e864045497);
wenzelm
parents: 33603
diff changeset
    93
    val input_buffer = Unsynchronized.ref input;
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32493
diff changeset
    94
    val line = Unsynchronized.ref (the_default 1 (Position.line_of pos));
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    95
31756
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
    96
    fun get_offset () = (case ! input_buffer of [] => 0 | (i, _) :: _ => i);
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    97
    fun get () =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    98
      (case ! input_buffer of
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
    99
        [] => NONE
31756
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
   100
      | (_, c) :: rest =>
178621145f98 eval: tuned input buffer and get_offset (which ran very hot, up to the point where AFP/FlyspeckTame would seemingly take forever);
wenzelm
parents: 31495
diff changeset
   101
          (input_buffer := rest;
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
   102
           if c = #"\n" then line := ! line + 1 else ();
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   103
           SOME c));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   104
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   105
39228
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   106
    (* output channels *)
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   107
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   108
    val writeln_buffer = Unsynchronized.ref Buffer.empty;
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   109
    fun write s = Unsynchronized.change writeln_buffer (Buffer.add s);
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   110
    fun output_writeln () = writeln (Buffer.content (! writeln_buffer));
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   111
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   112
    val warnings = Unsynchronized.ref ([]: string list);
39231
25c345302a17 avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
wenzelm
parents: 39230
diff changeset
   113
    fun warn msg = Unsynchronized.change warnings (cons msg);
39228
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   114
    fun output_warnings () = List.app warning (rev (! warnings));
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   115
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   116
    val error_buffer = Unsynchronized.ref Buffer.empty;
39231
25c345302a17 avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
wenzelm
parents: 39230
diff changeset
   117
    fun err msg = Unsynchronized.change error_buffer (Buffer.add msg #> Buffer.add "\n");
39228
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   118
    fun flush_error () = writeln (Buffer.content (! error_buffer));
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   119
    fun raise_error msg = error (Buffer.content (Buffer.add msg (! error_buffer)));
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   120
39228
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   121
    fun message {message = msg, hard, location = loc, context = _} =
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   122
      let
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   123
        val txt =
39439
1c294d150ded eliminated markup "location" in favour of more explicit "no_report", which is actually deleted from messages;
wenzelm
parents: 39240
diff changeset
   124
          Markup.markup Markup.no_report
39240
a0c0698e56c0 ML_Compiler.eval: reported positions need to contain offset, to avoid displaced reports due to synthesized line numbers;
wenzelm
parents: 39232
diff changeset
   125
            ((if hard then "Error" else "Warning") ^ Position.str_of (position_of loc) ^ ":\n") ^
39228
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   126
          Pretty.string_of (Pretty.from_ML (pretty_ml msg)) ^
39240
a0c0698e56c0 ML_Compiler.eval: reported positions need to contain offset, to avoid displaced reports due to synthesized line numbers;
wenzelm
parents: 39232
diff changeset
   127
          Markup.markup (Position.report_markup (offset_position_of loc)) "";
39231
25c345302a17 avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
wenzelm
parents: 39230
diff changeset
   128
      in if hard then err txt else warn txt end;
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   129
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   130
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   131
    (* results *)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   132
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   133
    val depth = get_print_depth ();
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   134
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   135
    fun apply_result {fixes, types, signatures, structures, functors, values} =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   136
      let
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   137
        fun display disp x =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   138
          if depth > 0 then
39228
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   139
            (disp x |> pretty_ml |> Pretty.from_ML |> Pretty.string_of |> write; write "\n")
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   140
          else ();
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   141
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   142
        fun apply_fix (a, b) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   143
          (display PolyML.NameSpace.displayFix (a, b); #enterFix space (a, b));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   144
        fun apply_type (a, b) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   145
          (display PolyML.NameSpace.displayType (b, depth, space); #enterType space (a, b));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   146
        fun apply_sig (a, b) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   147
          (display PolyML.NameSpace.displaySig (b, depth, space); #enterSig space (a, b));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   148
        fun apply_struct (a, b) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   149
          (display PolyML.NameSpace.displayStruct (b, depth, space); #enterStruct space (a, b));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   150
        fun apply_funct (a, b) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   151
          (display PolyML.NameSpace.displayFunct (b, depth, space); #enterFunct space (a, b));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   152
        fun apply_val (a, b) =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   153
          (display PolyML.NameSpace.displayVal (b, depth, space); #enterVal space (a, b));
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   154
      in
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   155
        List.app apply_fix fixes;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   156
        List.app apply_type types;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   157
        List.app apply_sig signatures;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   158
        List.app apply_struct structures;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   159
        List.app apply_funct functors;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   160
        List.app apply_val values
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   161
      end;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   162
39231
25c345302a17 avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
wenzelm
parents: 39230
diff changeset
   163
    exception STATIC_ERRORS of unit;
39230
184507f6e8d0 ML_Compiler.eval: discontinued extra "Static Errors" of raw Poly/ML;
wenzelm
parents: 39228
diff changeset
   164
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   165
    fun result_fun (phase1, phase2) () =
31477
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
   166
     ((case phase1 of
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
   167
        NONE => ()
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
   168
      | SOME parse_tree => report_parse_tree depth space parse_tree);
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
   169
      (case phase2 of
39231
25c345302a17 avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
wenzelm
parents: 39230
diff changeset
   170
        NONE => raise STATIC_ERRORS ()
31477
ae1a00e1a2f6 added exn_message (formerly in toplevel.ML);
wenzelm
parents: 31475
diff changeset
   171
      | SOME code =>
33603
3713a5208671 generalized Runtime.toplevel_error wrt. output function;
wenzelm
parents: 33538
diff changeset
   172
          apply_result
3713a5208671 generalized Runtime.toplevel_error wrt. output function;
wenzelm
parents: 33538
diff changeset
   173
            ((code
3713a5208671 generalized Runtime.toplevel_error wrt. output function;
wenzelm
parents: 33538
diff changeset
   174
              |> Runtime.debugging
39231
25c345302a17 avoid mixing of static and runtime errors in compiler output, to accomodate Proof General;
wenzelm
parents: 39230
diff changeset
   175
              |> Runtime.toplevel_error (err o exn_message)) ())));
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   176
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   177
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   178
    (* compiler invocation *)
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   179
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   180
    val parameters =
39228
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   181
     [PolyML.Compiler.CPOutStream write,
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   182
      PolyML.Compiler.CPNameSpace space,
39228
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   183
      PolyML.Compiler.CPErrorMessageProc message,
31429
8a7c0899e0b1 convert explicitly between Position.T/PolyML.location, without costly registration of tokens;
wenzelm
parents: 31333
diff changeset
   184
      PolyML.Compiler.CPLineNo (fn () => ! line),
31475
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
   185
      PolyML.Compiler.CPLineOffset get_offset,
31437
70309dc3deac removed unused location_of;
wenzelm
parents: 31434
diff changeset
   186
      PolyML.Compiler.CPFileName location_props,
31475
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
   187
      PolyML.Compiler.CPCompilerResultFun result_fun,
85e864045497 report_parse_tree: ML_open, ML_struct;
wenzelm
parents: 31437
diff changeset
   188
      PolyML.Compiler.CPPrintInAlphabeticalOrder false];
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   189
    val _ =
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   190
      (while not (List.null (! input_buffer)) do
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   191
        PolyML.compiler (get, parameters) ())
39232
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 39231
diff changeset
   192
      handle exn =>
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 39231
diff changeset
   193
        if Exn.is_interrupt exn then reraise exn
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 39231
diff changeset
   194
        else
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 39231
diff changeset
   195
          let
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 39231
diff changeset
   196
            val exn_msg =
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 39231
diff changeset
   197
              (case exn of
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 39231
diff changeset
   198
                STATIC_ERRORS () => ""
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 39231
diff changeset
   199
              | Runtime.TOPLEVEL_ERROR => ""
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 39231
diff changeset
   200
              | _ => "Exception- " ^ General.exnMessage exn ^ " raised");
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 39231
diff changeset
   201
            val _ = output_warnings ();
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 39231
diff changeset
   202
            val _ = output_writeln ();
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 39231
diff changeset
   203
          in raise_error exn_msg end;
39228
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   204
  in
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   205
    if verbose then (output_warnings (); flush_error (); output_writeln ())
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   206
    else ()
cb7264721c91 ML_Compiler.eval: more careful printing of messages and regular output, trying to accomodate Poly/ML, Proof General, Isabelle/Scala/jEdit at the same time;
wenzelm
parents: 39227
diff changeset
   207
  end;
31333
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   208
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   209
end;
fcd010617e6c added structure ML_Compiler: runtime compilation, with advanced version for Poly/ML 5.3 (formerly ML_Test);
wenzelm
parents:
diff changeset
   210