src/Pure/ML/ml_syntax.ML
author wenzelm
Sat, 30 Mar 2019 20:54:47 +0100
changeset 70015 c8e08d8ffb93
parent 69281 599b6d0d199b
child 71456 09c850e82258
permissions -rw-r--r--
clarified signature: more explicit type Path.binding; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24582
57599da58045 ML_Lex.keywords;
wenzelm
parents: 24574
diff changeset
     1
(*  Title:      Pure/ML/ml_syntax.ML
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     3
62528
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
     4
Concrete ML syntax for basic values.
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     5
*)
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     6
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     7
signature ML_SYNTAX =
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     8
sig
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
     9
  val reserved_names: string list
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    10
  val reserved: Name.context
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    11
  val is_reserved: string -> bool
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    12
  val is_identifier: string -> bool
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    13
  val atomic: string -> string
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    14
  val print_int: int -> string
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    15
  val print_pair: ('a -> string) -> ('b -> string) -> 'a * 'b -> string
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    16
  val print_list: ('a -> string) -> 'a list -> string
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    17
  val print_option: ('a -> string) -> 'a option -> string
69207
ae2074acbaa8 clarified signature;
wenzelm
parents: 65933
diff changeset
    18
  val print_symbol_char: Symbol.symbol -> string
ae2074acbaa8 clarified signature;
wenzelm
parents: 65933
diff changeset
    19
  val print_symbol: Symbol.symbol -> string
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    20
  val print_string: string -> string
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    21
  val print_strings: string list -> string
69281
599b6d0d199b tuned signature;
wenzelm
parents: 69207
diff changeset
    22
  val print_path: Path.T -> string
30230
a2094a8c1672 added print_properties, print_position (again);
wenzelm
parents: 29606
diff changeset
    23
  val print_properties: Properties.T -> string
a2094a8c1672 added print_properties, print_position (again);
wenzelm
parents: 29606
diff changeset
    24
  val print_position: Position.T -> string
58978
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 56184
diff changeset
    25
  val print_range: Position.range -> string
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 69281
diff changeset
    26
  val print_path_binding: Path.binding -> string
30523
4007ea1ddac2 added make_binding;
wenzelm
parents: 30230
diff changeset
    27
  val make_binding: string * Position.T -> string
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    28
  val print_indexname: indexname -> string
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    29
  val print_class: class -> string
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    30
  val print_sort: sort -> string
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    31
  val print_typ: typ -> string
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    32
  val print_term: term -> string
41415
23533273220a tuned ML toplevel pp for type string: observe depth limit;
wenzelm
parents: 40626
diff changeset
    33
  val pretty_string: int -> string -> Pretty.T
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    34
end;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    35
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    36
structure ML_Syntax: ML_SYNTAX =
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    37
struct
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    38
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    39
(* reserved words *)
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    40
50238
98d35a7368bd more uniform Symbol.is_ascii_identifier in ML/Scala;
wenzelm
parents: 43845
diff changeset
    41
val reserved_names = filter Symbol.is_ascii_identifier ML_Lex.keywords;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    42
val reserved = Name.make_context reserved_names;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    43
val is_reserved = Name.is_declared reserved;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    44
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    45
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    46
(* identifiers *)
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    47
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    48
fun is_identifier name =
50238
98d35a7368bd more uniform Symbol.is_ascii_identifier in ML/Scala;
wenzelm
parents: 43845
diff changeset
    49
  not (is_reserved name) andalso Symbol.is_ascii_identifier name;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    50
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    51
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    52
(* literal output -- unformatted *)
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    53
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    54
val atomic = enclose "(" ")";
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    55
41491
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 41415
diff changeset
    56
val print_int = string_of_int;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    57
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    58
fun print_pair f1 f2 (x, y) = "(" ^ f1 x ^ ", " ^ f2 y ^ ")";
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    59
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    60
fun print_list f = enclose "[" "]" o commas o map f;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    61
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    62
fun print_option f NONE = "NONE"
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    63
  | print_option f (SOME x) = "SOME (" ^ f x ^ ")";
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    64
69207
ae2074acbaa8 clarified signature;
wenzelm
parents: 65933
diff changeset
    65
fun print_symbol_char s =
62528
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    66
  if Symbol.is_char s then
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    67
    (case ord s of
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    68
      34 => "\\\""
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    69
    | 92 => "\\\\"
65933
f3e4f9e6c485 more correct and complete output of control characters;
wenzelm
parents: 62819
diff changeset
    70
    | 7 => "\\a"
f3e4f9e6c485 more correct and complete output of control characters;
wenzelm
parents: 62819
diff changeset
    71
    | 8 => "\\b"
62528
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    72
    | 9 => "\\t"
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    73
    | 10 => "\\n"
65933
f3e4f9e6c485 more correct and complete output of control characters;
wenzelm
parents: 62819
diff changeset
    74
    | 11 => "\\v"
f3e4f9e6c485 more correct and complete output of control characters;
wenzelm
parents: 62819
diff changeset
    75
    | 12 => "\\f"
62528
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    76
    | 13 => "\\r"
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    77
    | c =>
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    78
        if c < 32 then "\\^" ^ chr (c + 64)
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    79
        else if c < 127 then s
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    80
        else "\\" ^ string_of_int c)
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    81
  else error ("Bad character: " ^ quote s);
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    82
69207
ae2074acbaa8 clarified signature;
wenzelm
parents: 65933
diff changeset
    83
fun print_symbol s =
ae2074acbaa8 clarified signature;
wenzelm
parents: 65933
diff changeset
    84
  if Symbol.is_char s then print_symbol_char s
ae2074acbaa8 clarified signature;
wenzelm
parents: 65933
diff changeset
    85
  else if Symbol.is_utf8 s then translate_string print_symbol_char s
62528
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 58978
diff changeset
    86
  else s;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    87
69207
ae2074acbaa8 clarified signature;
wenzelm
parents: 65933
diff changeset
    88
val print_string = quote o implode o map print_symbol o Symbol.explode;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    89
val print_strings = print_list print_string;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
    90
69281
599b6d0d199b tuned signature;
wenzelm
parents: 69207
diff changeset
    91
fun print_path path =
599b6d0d199b tuned signature;
wenzelm
parents: 69207
diff changeset
    92
  "Path.explode " ^ print_string (Path.implode path);
599b6d0d199b tuned signature;
wenzelm
parents: 69207
diff changeset
    93
30230
a2094a8c1672 added print_properties, print_position (again);
wenzelm
parents: 29606
diff changeset
    94
val print_properties = print_list (print_pair print_string print_string);
58978
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 56184
diff changeset
    95
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 56184
diff changeset
    96
fun print_position pos =
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 56184
diff changeset
    97
  "Position.of_properties " ^ print_properties (Position.properties_of pos);
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 56184
diff changeset
    98
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 56184
diff changeset
    99
fun print_range range =
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 56184
diff changeset
   100
  "Position.range_of_properties " ^ print_properties (Position.properties_of_range range);
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 56184
diff changeset
   101
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 69281
diff changeset
   102
fun print_path_binding binding =
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 69281
diff changeset
   103
  "Path.binding " ^ print_pair print_path print_position (Path.dest_binding binding);
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 69281
diff changeset
   104
58978
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 56184
diff changeset
   105
fun make_binding (name, pos) =
e42da880c61e more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents: 56184
diff changeset
   106
  "Binding.make " ^ print_pair print_string print_position (name, pos);
30230
a2094a8c1672 added print_properties, print_position (again);
wenzelm
parents: 29606
diff changeset
   107
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   108
val print_indexname = print_pair print_string print_int;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   109
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   110
val print_class = print_string;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   111
val print_sort = print_list print_class;
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   112
56184
a2bd40830593 more robust machine-generated ML sources: constructors for typ and term sometimes occur elsewhere;
wenzelm
parents: 50238
diff changeset
   113
fun print_typ (Type arg) = "Term.Type " ^ print_pair print_string (print_list print_typ) arg
a2bd40830593 more robust machine-generated ML sources: constructors for typ and term sometimes occur elsewhere;
wenzelm
parents: 50238
diff changeset
   114
  | print_typ (TFree arg) = "Term.TFree " ^ print_pair print_string print_sort arg
a2bd40830593 more robust machine-generated ML sources: constructors for typ and term sometimes occur elsewhere;
wenzelm
parents: 50238
diff changeset
   115
  | print_typ (TVar arg) = "Term.TVar " ^ print_pair print_indexname print_sort arg;
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   116
56184
a2bd40830593 more robust machine-generated ML sources: constructors for typ and term sometimes occur elsewhere;
wenzelm
parents: 50238
diff changeset
   117
fun print_term (Const arg) = "Term.Const " ^ print_pair print_string print_typ arg
a2bd40830593 more robust machine-generated ML sources: constructors for typ and term sometimes occur elsewhere;
wenzelm
parents: 50238
diff changeset
   118
  | print_term (Free arg) = "Term.Free " ^ print_pair print_string print_typ arg
a2bd40830593 more robust machine-generated ML sources: constructors for typ and term sometimes occur elsewhere;
wenzelm
parents: 50238
diff changeset
   119
  | print_term (Var arg) = "Term.Var " ^ print_pair print_indexname print_typ arg
a2bd40830593 more robust machine-generated ML sources: constructors for typ and term sometimes occur elsewhere;
wenzelm
parents: 50238
diff changeset
   120
  | print_term (Bound i) = "Term.Bound " ^ print_int i
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   121
  | print_term (Abs (s, T, t)) =
56184
a2bd40830593 more robust machine-generated ML sources: constructors for typ and term sometimes occur elsewhere;
wenzelm
parents: 50238
diff changeset
   122
      "Term.Abs (" ^ print_string s ^ ", " ^ print_typ T ^ ", " ^ print_term t ^ ")"
a2bd40830593 more robust machine-generated ML sources: constructors for typ and term sometimes occur elsewhere;
wenzelm
parents: 50238
diff changeset
   123
  | print_term (t1 $ t2) = "Term.$ " ^ print_pair print_term print_term (t1, t2);
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   124
37535
75de61a479e3 ML pretty printing of type string according to (quasi-abstract) YXML markup and symbols (including UTF8);
wenzelm
parents: 31543
diff changeset
   125
75de61a479e3 ML pretty printing of type string according to (quasi-abstract) YXML markup and symbols (including UTF8);
wenzelm
parents: 31543
diff changeset
   126
(* toplevel pretty printing *)
75de61a479e3 ML pretty printing of type string according to (quasi-abstract) YXML markup and symbols (including UTF8);
wenzelm
parents: 31543
diff changeset
   127
41415
23533273220a tuned ML toplevel pp for type string: observe depth limit;
wenzelm
parents: 40626
diff changeset
   128
fun pretty_string max_len str =
37535
75de61a479e3 ML pretty printing of type string according to (quasi-abstract) YXML markup and symbols (including UTF8);
wenzelm
parents: 31543
diff changeset
   129
  let
41415
23533273220a tuned ML toplevel pp for type string: observe depth limit;
wenzelm
parents: 40626
diff changeset
   130
    val body =
23533273220a tuned ML toplevel pp for type string: observe depth limit;
wenzelm
parents: 40626
diff changeset
   131
      maps (fn XML.Elem _ => ["<markup>"] | XML.Text s => Symbol.explode s) (YXML.parse_body str)
23533273220a tuned ML toplevel pp for type string: observe depth limit;
wenzelm
parents: 40626
diff changeset
   132
        handle Fail _ => Symbol.explode str;
23533273220a tuned ML toplevel pp for type string: observe depth limit;
wenzelm
parents: 40626
diff changeset
   133
    val body' =
23533273220a tuned ML toplevel pp for type string: observe depth limit;
wenzelm
parents: 40626
diff changeset
   134
      if length body <= max_len then body
42047
a7a4e04b5386 pretty_string: proper handling of negative max_len;
wenzelm
parents: 41491
diff changeset
   135
      else take (Int.max (max_len, 0)) body @ ["..."];
69207
ae2074acbaa8 clarified signature;
wenzelm
parents: 65933
diff changeset
   136
  in Pretty.str (quote (implode (map print_symbol body'))) end;
37535
75de61a479e3 ML pretty printing of type string according to (quasi-abstract) YXML markup and symbols (including UTF8);
wenzelm
parents: 31543
diff changeset
   137
62663
bea354f6ff21 clarified modules;
wenzelm
parents: 62528
diff changeset
   138
val _ =
62819
d3ff367a16a0 careful export of type-dependent functions, without losing their special status;
wenzelm
parents: 62663
diff changeset
   139
  ML_system_pp (fn depth => fn _ => fn str =>
62663
bea354f6ff21 clarified modules;
wenzelm
parents: 62528
diff changeset
   140
    Pretty.to_polyml (pretty_string (FixedInt.toInt (depth * 100)) str));
bea354f6ff21 clarified modules;
wenzelm
parents: 62528
diff changeset
   141
24574
e840872e9c7c moved ML_XXX.ML files to Pure/ML;
wenzelm
parents:
diff changeset
   142
end;