src/Pure/General/symbol.ML
author wenzelm
Fri Dec 14 11:52:54 2001 +0100 (2001-12-14)
changeset 12498 3b0091bf06e8
parent 12116 4027b15377a5
child 12904 c208d71702d1
permissions -rw-r--r--
changed Thm.varifyT';
wenzelm@6118
     1
(*  Title:      Pure/General/symbol.ML
wenzelm@6116
     2
    ID:         $Id$
wenzelm@6116
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8806
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@6116
     5
wenzelm@12116
     6
Generalized characters with infinitely many named symbols.
wenzelm@6116
     7
*)
wenzelm@6116
     8
wenzelm@6116
     9
signature SYMBOL =
wenzelm@6116
    10
sig
wenzelm@6116
    11
  type symbol
wenzelm@6116
    12
  val space: symbol
wenzelm@10953
    13
  val spaces: int -> symbol
wenzelm@6857
    14
  val sync: symbol
wenzelm@6857
    15
  val is_sync: symbol -> bool
wenzelm@6857
    16
  val not_sync: symbol -> bool
wenzelm@10747
    17
  val malformed: symbol
wenzelm@6116
    18
  val eof: symbol
wenzelm@6116
    19
  val is_eof: symbol -> bool
wenzelm@6116
    20
  val not_eof: symbol -> bool
wenzelm@6116
    21
  val stopper: symbol * (symbol -> bool)
wenzelm@6116
    22
  val is_ascii: symbol -> bool
wenzelm@6116
    23
  val is_letter: symbol -> bool
wenzelm@6116
    24
  val is_digit: symbol -> bool
wenzelm@6116
    25
  val is_quasi_letter: symbol -> bool
wenzelm@6116
    26
  val is_letdig: symbol -> bool
wenzelm@6116
    27
  val is_blank: symbol -> bool
wenzelm@8230
    28
  val is_symbolic: symbol -> bool
wenzelm@6116
    29
  val is_printable: symbol -> bool
wenzelm@6272
    30
  val length: symbol list -> int
wenzelm@11010
    31
  val strip_blanks: string -> string
wenzelm@6116
    32
  val beginning: symbol list -> string
wenzelm@6116
    33
  val scan: string list -> symbol * string list
wenzelm@6640
    34
  val scanner: string -> (symbol list -> 'a * symbol list) -> symbol list -> 'a
wenzelm@6116
    35
  val source: bool -> (string, 'a) Source.source ->
wenzelm@6116
    36
    (symbol, (string, 'a) Source.source) Source.source
wenzelm@6272
    37
  val explode: string -> symbol list
wenzelm@10953
    38
  val default_indent: string * int -> string
wenzelm@10953
    39
  val add_mode: string -> (string -> string * real) * (string * int -> string) -> unit
wenzelm@6692
    40
  val symbolsN: string
wenzelm@6692
    41
  val xsymbolsN: string
wenzelm@10923
    42
  val plain_output: string -> string
wenzelm@6272
    43
  val output: string -> string
wenzelm@6272
    44
  val output_width: string -> string * real
wenzelm@10953
    45
  val indent: string * int -> string
wenzelm@6116
    46
end;
wenzelm@6116
    47
wenzelm@6116
    48
structure Symbol: SYMBOL =
wenzelm@6116
    49
struct
wenzelm@6116
    50
wenzelm@6116
    51
wenzelm@6272
    52
(** generalized characters **)
wenzelm@6272
    53
wenzelm@6272
    54
(*symbols, which are considered the smallest entities of any Isabelle
wenzelm@6272
    55
  string, may be of the following form:
wenzelm@6272
    56
    (a) ASCII symbols: a
wenzelm@6272
    57
    (b) printable symbols: \<ident>
wenzelm@6272
    58
    (c) control symbols: \<^ident>
wenzelm@6272
    59
wenzelm@12116
    60
  output is subject to the print_mode variable (default: verbatim),
wenzelm@12116
    61
  actual interpretation in display is up to front-end tools;
wenzelm@6272
    62
*)
wenzelm@6272
    63
wenzelm@6272
    64
type symbol = string;
wenzelm@6272
    65
wenzelm@6272
    66
val space = " ";
wenzelm@10953
    67
fun spaces k = Library.replicate_string k space;
wenzelm@6857
    68
val sync = "\\<^sync>";
wenzelm@10747
    69
val malformed = "\\<^malformed>";
wenzelm@6272
    70
val eof = "";
wenzelm@6272
    71
wenzelm@6272
    72
wenzelm@6272
    73
(* kinds *)
wenzelm@6272
    74
wenzelm@6857
    75
fun is_sync s = s = sync;
wenzelm@6857
    76
fun not_sync s = s <> sync;
wenzelm@6857
    77
wenzelm@6272
    78
fun is_eof s = s = eof;
wenzelm@6272
    79
fun not_eof s = s <> eof;
wenzelm@6272
    80
val stopper = (eof, is_eof);
wenzelm@6272
    81
wenzelm@6272
    82
fun is_ascii s = size s = 1 andalso ord s < 128;
wenzelm@6272
    83
wenzelm@6272
    84
fun is_letter s =
wenzelm@6272
    85
  size s = 1 andalso
wenzelm@6272
    86
   (ord "A" <= ord s andalso ord s <= ord "Z" orelse
wenzelm@6272
    87
    ord "a" <= ord s andalso ord s <= ord "z");
wenzelm@6272
    88
wenzelm@6272
    89
fun is_digit s =
wenzelm@6272
    90
  size s = 1 andalso ord "0" <= ord s andalso ord s <= ord "9";
wenzelm@6272
    91
wenzelm@6272
    92
fun is_quasi_letter "_" = true
wenzelm@6272
    93
  | is_quasi_letter "'" = true
wenzelm@6272
    94
  | is_quasi_letter s = is_letter s;
wenzelm@6272
    95
wenzelm@6272
    96
val is_blank =
wenzelm@6272
    97
  fn " " => true | "\t" => true | "\n" => true | "\^L" => true
wenzelm@6272
    98
    | "\160" => true | "\\<spacespace>" => true
wenzelm@6272
    99
    | _ => false;
wenzelm@6272
   100
wenzelm@6272
   101
val is_letdig = is_quasi_letter orf is_digit;
wenzelm@6272
   102
wenzelm@8230
   103
fun is_symbolic s =
wenzelm@8230
   104
  size s > 2 andalso nth_elem_string (2, s) <> "^";
wenzelm@8230
   105
wenzelm@6272
   106
fun is_printable s =
wenzelm@6272
   107
  size s = 1 andalso ord space <= ord s andalso ord s <= ord "~" orelse
wenzelm@8230
   108
  is_symbolic s;
wenzelm@8230
   109
wenzelm@6272
   110
wenzelm@10738
   111
fun sym_length ss = foldl (fn (n, s) =>
wenzelm@10738
   112
  (if not (is_printable s) then 0 else
wenzelm@10738
   113
    (case Library.try String.substring (s, 2, 4) of
wenzelm@10738
   114
      Some s' => if s' = "long" orelse s' = "Long" then 2 else 1
wenzelm@10738
   115
    | None => 1)) + n) (0, ss);
wenzelm@6272
   116
wenzelm@11010
   117
fun strip_blanks s =
wenzelm@11010
   118
  implode (#1 (Library.take_suffix is_blank (#2 (Library.take_prefix is_blank (explode s)))));
wenzelm@11010
   119
wenzelm@6272
   120
wenzelm@6272
   121
(* beginning *)
wenzelm@6272
   122
wenzelm@6272
   123
val smash_blanks = map (fn s => if is_blank s then space else s);
wenzelm@6272
   124
wenzelm@6272
   125
fun beginning raw_ss =
wenzelm@6272
   126
  let
wenzelm@6272
   127
    val (all_ss, _) = take_suffix is_blank raw_ss;
wenzelm@6272
   128
    val dots = if length all_ss > 10 then " ..." else "";
wenzelm@6272
   129
    val (ss, _) = take_suffix is_blank (take (10, all_ss));
wenzelm@6272
   130
  in implode (smash_blanks ss) ^ dots end;
wenzelm@6272
   131
wenzelm@6272
   132
wenzelm@6272
   133
wenzelm@8998
   134
(** scanning through symbols **)
wenzelm@6640
   135
wenzelm@6640
   136
fun scanner msg scan chs =
wenzelm@6640
   137
  let
wenzelm@6640
   138
    fun err_msg cs = msg ^ ": " ^ beginning cs;
wenzelm@6640
   139
    val fin_scan = Scan.error (Scan.finite stopper (!! (fn (cs, _) => err_msg cs) scan));
wenzelm@6640
   140
  in
wenzelm@6640
   141
    (case fin_scan chs of
wenzelm@6640
   142
      (result, []) => result
wenzelm@6640
   143
    | (_, rest) => error (err_msg rest))
wenzelm@6640
   144
  end;
wenzelm@6640
   145
wenzelm@6640
   146
wenzelm@6640
   147
wenzelm@6272
   148
(** symbol input **)
wenzelm@6116
   149
wenzelm@6116
   150
(* scan *)
wenzelm@6116
   151
wenzelm@6116
   152
val scan_id = Scan.one is_letter ^^ (Scan.any is_letdig >> implode);
wenzelm@6116
   153
wenzelm@6116
   154
val scan =
wenzelm@6116
   155
  ($$ "\\" --| Scan.optional ($$ "\\") "") ^^ $$ "<" ^^
wenzelm@6116
   156
    !! (fn (cs, _) => "Malformed symbolic character specification: \\" ^ "<" ^ beginning cs)
wenzelm@6116
   157
      (Scan.optional ($$ "^") "" ^^ scan_id ^^ $$ ">") ||
wenzelm@6116
   158
  Scan.one not_eof;
wenzelm@6116
   159
wenzelm@6116
   160
wenzelm@6116
   161
(* source *)
wenzelm@6116
   162
wenzelm@10747
   163
val recover = Scan.any ((not o is_blank) andf not_eof) >> K [malformed];
wenzelm@6116
   164
wenzelm@6116
   165
fun source do_recover src =
wenzelm@6116
   166
  Source.source stopper (Scan.bulk scan) (if do_recover then Some recover else None) src;
wenzelm@6116
   167
wenzelm@6116
   168
wenzelm@6116
   169
(* explode *)
wenzelm@6116
   170
wenzelm@6116
   171
fun no_syms [] = true
wenzelm@6116
   172
  | no_syms ("\\" :: "<" :: _) = false
wenzelm@12116
   173
  | no_syms (_ :: cs) = no_syms cs;
wenzelm@6116
   174
wenzelm@6116
   175
fun sym_explode str =
wenzelm@6116
   176
  let val chs = explode str in
wenzelm@6116
   177
    if no_syms chs then chs     (*tune trivial case*)
wenzelm@12116
   178
    else the (Scan.read stopper (Scan.repeat scan) chs)
wenzelm@6116
   179
  end;
wenzelm@6116
   180
wenzelm@6116
   181
wenzelm@6272
   182
wenzelm@6272
   183
(** symbol output **)
wenzelm@6272
   184
wenzelm@10953
   185
(* default *)
wenzelm@6272
   186
wenzelm@6272
   187
fun string_size s = (s, real (size s));
wenzelm@6272
   188
wenzelm@6272
   189
fun default_output s =
wenzelm@6320
   190
  if not (exists_string (equal "\\") s) then string_size s
wenzelm@10738
   191
  else string_size (implode (map (fn "\\" => "\\\\" | c => c) (explode s)));    (*sic!*)
wenzelm@6272
   192
wenzelm@10953
   193
fun default_indent (_: string, k) = spaces k;
wenzelm@6272
   194
wenzelm@10953
   195
wenzelm@6272
   196
(* maintain modes *)
wenzelm@6272
   197
wenzelm@6692
   198
val symbolsN = "symbols";
wenzelm@6692
   199
val xsymbolsN = "xsymbols";
wenzelm@6692
   200
wenzelm@12116
   201
val modes =
wenzelm@12116
   202
  ref (Symtab.empty: ((string -> string * real) * (string * int -> string)) Symtab.table);
wenzelm@6272
   203
wenzelm@6272
   204
fun lookup_mode name = Symtab.lookup (! modes, name);
wenzelm@6272
   205
wenzelm@10953
   206
fun add_mode name m =
wenzelm@6272
   207
 (if is_none (lookup_mode name) then ()
wenzelm@6320
   208
  else warning ("Redeclaration of symbol print mode " ^ quote name);
wenzelm@10953
   209
  modes := Symtab.update ((name, m), ! modes));
wenzelm@10953
   210
wenzelm@10953
   211
fun get_mode () =
wenzelm@10953
   212
  if_none (get_first lookup_mode (! print_mode)) (default_output, default_indent);
wenzelm@6272
   213
wenzelm@6272
   214
wenzelm@6272
   215
(* mode output *)
wenzelm@6272
   216
wenzelm@10953
   217
fun output_width x = #1 (get_mode ()) x;
wenzelm@6272
   218
val output = #1 o output_width;
wenzelm@10923
   219
val plain_output = #1 o default_output;
wenzelm@6272
   220
wenzelm@10953
   221
fun indent x = #2 (get_mode ()) x;
wenzelm@10953
   222
wenzelm@6272
   223
wenzelm@6116
   224
(*final declarations of this structure!*)
wenzelm@6272
   225
val length = sym_length;
wenzelm@6116
   226
val explode = sym_explode;
wenzelm@6272
   227
wenzelm@6116
   228
wenzelm@6116
   229
end;