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