| author | haftmann | 
| Mon, 23 Mar 2015 19:05:14 +0100 | |
| changeset 59816 | 034b13f4efae | 
| parent 58978 | e42da880c61e | 
| child 62528 | c8c532b22947 | 
| permissions | -rw-r--r-- | 
| 24582 | 1 | (* Title: Pure/ML/ml_syntax.ML | 
| 24574 | 2 | Author: Makarius | 
| 3 | ||
| 4 | Basic ML syntax operations. | |
| 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
 | |
| 18 | val print_char: string -> string | |
| 19 | val print_string: string -> string | |
| 20 | val print_strings: string list -> string | |
| 30230 | 21 | val print_properties: Properties.T -> string | 
| 22 | val print_position: Position.T -> string | |
| 58978 
e42da880c61e
more position information, e.g. relevant for errors in generated ML source;
 wenzelm parents: 
56184diff
changeset | 23 | val print_range: Position.range -> string | 
| 30523 | 24 | val make_binding: string * Position.T -> string | 
| 24574 | 25 | val print_indexname: indexname -> string | 
| 26 | val print_class: class -> string | |
| 27 | val print_sort: sort -> string | |
| 28 | val print_typ: typ -> string | |
| 29 | val print_term: term -> string | |
| 41415 
23533273220a
tuned ML toplevel pp for type string: observe depth limit;
 wenzelm parents: 
40626diff
changeset | 30 | val pretty_string: int -> string -> Pretty.T | 
| 24574 | 31 | end; | 
| 32 | ||
| 33 | structure ML_Syntax: ML_SYNTAX = | |
| 34 | struct | |
| 35 | ||
| 36 | (* reserved words *) | |
| 37 | ||
| 50238 
98d35a7368bd
more uniform Symbol.is_ascii_identifier in ML/Scala;
 wenzelm parents: 
43845diff
changeset | 38 | val reserved_names = filter Symbol.is_ascii_identifier ML_Lex.keywords; | 
| 24574 | 39 | val reserved = Name.make_context reserved_names; | 
| 40 | val is_reserved = Name.is_declared reserved; | |
| 41 | ||
| 42 | ||
| 43 | (* identifiers *) | |
| 44 | ||
| 45 | fun is_identifier name = | |
| 50238 
98d35a7368bd
more uniform Symbol.is_ascii_identifier in ML/Scala;
 wenzelm parents: 
43845diff
changeset | 46 | not (is_reserved name) andalso Symbol.is_ascii_identifier name; | 
| 24574 | 47 | |
| 48 | ||
| 49 | (* literal output -- unformatted *) | |
| 50 | ||
| 51 | val atomic = enclose "(" ")";
 | |
| 52 | ||
| 41491 | 53 | val print_int = string_of_int; | 
| 24574 | 54 | |
| 55 | fun print_pair f1 f2 (x, y) = "(" ^ f1 x ^ ", " ^ f2 y ^ ")";
 | |
| 56 | ||
| 57 | fun print_list f = enclose "[" "]" o commas o map f; | |
| 58 | ||
| 59 | fun print_option f NONE = "NONE" | |
| 60 |   | print_option f (SOME x) = "SOME (" ^ f x ^ ")";
 | |
| 61 | ||
| 62 | fun print_char s = | |
| 31543 
5bef6c7cc819
allow Isabelle symbols within low-level ML source;
 wenzelm parents: 
30523diff
changeset | 63 | if not (Symbol.is_char s) then s | 
| 24574 | 64 | else if s = "\"" then "\\\"" | 
| 65 | else if s = "\\" then "\\\\" | |
| 39514 
d9cf3f833318
ML_Syntax.print_char: more readable output of some well-known ASCII controls -- this is relevant for ML toplevel pp;
 wenzelm parents: 
37535diff
changeset | 66 | else if s = "\t" then "\\t" | 
| 
d9cf3f833318
ML_Syntax.print_char: more readable output of some well-known ASCII controls -- this is relevant for ML toplevel pp;
 wenzelm parents: 
37535diff
changeset | 67 | else if s = "\n" then "\\n" | 
| 43845 
d89353d17f54
added File.fold_pages for streaming of large files;
 wenzelm parents: 
42290diff
changeset | 68 | else if s = "\f" then "\\f" | 
| 39514 
d9cf3f833318
ML_Syntax.print_char: more readable output of some well-known ASCII controls -- this is relevant for ML toplevel pp;
 wenzelm parents: 
37535diff
changeset | 69 | else if s = "\r" then "\\r" | 
| 24574 | 70 | else | 
| 71 | let val c = ord s in | |
| 72 | if c < 32 then "\\^" ^ chr (c + ord "@") | |
| 73 | else if c < 127 then s | |
| 74 | else "\\" ^ string_of_int c | |
| 75 | end; | |
| 76 | ||
| 31543 
5bef6c7cc819
allow Isabelle symbols within low-level ML source;
 wenzelm parents: 
30523diff
changeset | 77 | val print_string = quote o implode o map print_char o Symbol.explode; | 
| 24574 | 78 | val print_strings = print_list print_string; | 
| 79 | ||
| 30230 | 80 | 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 | 81 | |
| 
e42da880c61e
more position information, e.g. relevant for errors in generated ML source;
 wenzelm parents: 
56184diff
changeset | 82 | fun print_position pos = | 
| 
e42da880c61e
more position information, e.g. relevant for errors in generated ML source;
 wenzelm parents: 
56184diff
changeset | 83 | "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 | 84 | |
| 
e42da880c61e
more position information, e.g. relevant for errors in generated ML source;
 wenzelm parents: 
56184diff
changeset | 85 | fun print_range range = | 
| 
e42da880c61e
more position information, e.g. relevant for errors in generated ML source;
 wenzelm parents: 
56184diff
changeset | 86 | "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 | 87 | |
| 
e42da880c61e
more position information, e.g. relevant for errors in generated ML source;
 wenzelm parents: 
56184diff
changeset | 88 | fun make_binding (name, pos) = | 
| 
e42da880c61e
more position information, e.g. relevant for errors in generated ML source;
 wenzelm parents: 
56184diff
changeset | 89 | "Binding.make " ^ print_pair print_string print_position (name, pos); | 
| 30230 | 90 | |
| 24574 | 91 | val print_indexname = print_pair print_string print_int; | 
| 92 | ||
| 93 | val print_class = print_string; | |
| 94 | val print_sort = print_list print_class; | |
| 95 | ||
| 56184 
a2bd40830593
more robust machine-generated ML sources: constructors for typ and term sometimes occur elsewhere;
 wenzelm parents: 
50238diff
changeset | 96 | 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 | 97 | | 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 | 98 | | print_typ (TVar arg) = "Term.TVar " ^ print_pair print_indexname print_sort arg; | 
| 24574 | 99 | |
| 56184 
a2bd40830593
more robust machine-generated ML sources: constructors for typ and term sometimes occur elsewhere;
 wenzelm parents: 
50238diff
changeset | 100 | 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 | 101 | | 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 | 102 | | 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 | 103 | | print_term (Bound i) = "Term.Bound " ^ print_int i | 
| 24574 | 104 | | 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 | 105 |       "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 | 106 | | print_term (t1 $ t2) = "Term.$ " ^ print_pair print_term print_term (t1, t2); | 
| 24574 | 107 | |
| 37535 
75de61a479e3
ML pretty printing of type string according to (quasi-abstract) YXML markup and symbols (including UTF8);
 wenzelm parents: 
31543diff
changeset | 108 | |
| 
75de61a479e3
ML pretty printing of type string according to (quasi-abstract) YXML markup and symbols (including UTF8);
 wenzelm parents: 
31543diff
changeset | 109 | (* toplevel pretty printing *) | 
| 
75de61a479e3
ML pretty printing of type string according to (quasi-abstract) YXML markup and symbols (including UTF8);
 wenzelm parents: 
31543diff
changeset | 110 | |
| 41415 
23533273220a
tuned ML toplevel pp for type string: observe depth limit;
 wenzelm parents: 
40626diff
changeset | 111 | 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 | 112 | let | 
| 41415 
23533273220a
tuned ML toplevel pp for type string: observe depth limit;
 wenzelm parents: 
40626diff
changeset | 113 | val body = | 
| 
23533273220a
tuned ML toplevel pp for type string: observe depth limit;
 wenzelm parents: 
40626diff
changeset | 114 | 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 | 115 | handle Fail _ => Symbol.explode str; | 
| 
23533273220a
tuned ML toplevel pp for type string: observe depth limit;
 wenzelm parents: 
40626diff
changeset | 116 | val body' = | 
| 
23533273220a
tuned ML toplevel pp for type string: observe depth limit;
 wenzelm parents: 
40626diff
changeset | 117 | if length body <= max_len then body | 
| 42047 
a7a4e04b5386
pretty_string: proper handling of negative max_len;
 wenzelm parents: 
41491diff
changeset | 118 | else take (Int.max (max_len, 0)) body @ ["..."]; | 
| 41415 
23533273220a
tuned ML toplevel pp for type string: observe depth limit;
 wenzelm parents: 
40626diff
changeset | 119 | in Pretty.str (quote (implode (map print_char body'))) end; | 
| 37535 
75de61a479e3
ML pretty printing of type string according to (quasi-abstract) YXML markup and symbols (including UTF8);
 wenzelm parents: 
31543diff
changeset | 120 | |
| 24574 | 121 | end; |