src/Pure/General/symbol.ML
author wenzelm
Wed Jan 31 22:14:53 2001 +0100 (2001-01-31)
changeset 11010 2c6559297be3
parent 10953 ea024d025463
child 12116 4027b15377a5
permissions -rw-r--r--
added strip_blanks;
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@8806
     6
Generalized characters, independent of encoding.
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@6272
    38
  val input: string -> string
wenzelm@10953
    39
  val default_indent: string * int -> string
wenzelm@10953
    40
  val add_mode: string -> (string -> string * real) * (string * int -> string) -> unit
wenzelm@6692
    41
  val isabelle_fontN: string
wenzelm@6692
    42
  val symbolsN: string
wenzelm@6692
    43
  val xsymbolsN: string
wenzelm@10923
    44
  val plain_output: string -> string
wenzelm@6272
    45
  val output: string -> string
wenzelm@6272
    46
  val output_width: string -> string * real
wenzelm@10953
    47
  val indent: string * int -> string
wenzelm@6116
    48
end;
wenzelm@6116
    49
wenzelm@6116
    50
structure Symbol: SYMBOL =
wenzelm@6116
    51
struct
wenzelm@6116
    52
wenzelm@6116
    53
wenzelm@6272
    54
(** generalized characters **)
wenzelm@6272
    55
wenzelm@6272
    56
(*symbols, which are considered the smallest entities of any Isabelle
wenzelm@6272
    57
  string, may be of the following form:
wenzelm@6272
    58
    (a) ASCII symbols: a
wenzelm@6272
    59
    (b) printable symbols: \<ident>
wenzelm@6272
    60
    (c) control symbols: \<^ident>
wenzelm@6272
    61
wenzelm@6272
    62
  input may include non-ASCII characters according to isabelle-0 encoding;
wenzelm@6272
    63
  output is subject to the print_mode variable (default: verbatim);
wenzelm@6272
    64
*)
wenzelm@6272
    65
wenzelm@6272
    66
type symbol = string;
wenzelm@6272
    67
wenzelm@6272
    68
val space = " ";
wenzelm@10953
    69
fun spaces k = Library.replicate_string k space;
wenzelm@6857
    70
val sync = "\\<^sync>";
wenzelm@10747
    71
val malformed = "\\<^malformed>";
wenzelm@6272
    72
val eof = "";
wenzelm@6272
    73
wenzelm@6272
    74
wenzelm@6272
    75
(* kinds *)
wenzelm@6272
    76
wenzelm@6857
    77
fun is_sync s = s = sync;
wenzelm@6857
    78
fun not_sync s = s <> sync;
wenzelm@6857
    79
wenzelm@6272
    80
fun is_eof s = s = eof;
wenzelm@6272
    81
fun not_eof s = s <> eof;
wenzelm@6272
    82
val stopper = (eof, is_eof);
wenzelm@6272
    83
wenzelm@6272
    84
fun is_ascii s = size s = 1 andalso ord s < 128;
wenzelm@6272
    85
wenzelm@6272
    86
fun is_letter s =
wenzelm@6272
    87
  size s = 1 andalso
wenzelm@6272
    88
   (ord "A" <= ord s andalso ord s <= ord "Z" orelse
wenzelm@6272
    89
    ord "a" <= ord s andalso ord s <= ord "z");
wenzelm@6272
    90
wenzelm@6272
    91
fun is_digit s =
wenzelm@6272
    92
  size s = 1 andalso ord "0" <= ord s andalso ord s <= ord "9";
wenzelm@6272
    93
wenzelm@6272
    94
fun is_quasi_letter "_" = true
wenzelm@6272
    95
  | is_quasi_letter "'" = true
wenzelm@6272
    96
  | is_quasi_letter s = is_letter s;
wenzelm@6272
    97
wenzelm@6272
    98
val is_blank =
wenzelm@6272
    99
  fn " " => true | "\t" => true | "\n" => true | "\^L" => true
wenzelm@6272
   100
    | "\160" => true | "\\<spacespace>" => true
wenzelm@6272
   101
    | _ => false;
wenzelm@6272
   102
wenzelm@6272
   103
val is_letdig = is_quasi_letter orf is_digit;
wenzelm@6272
   104
wenzelm@8230
   105
fun is_symbolic s =
wenzelm@8230
   106
  size s > 2 andalso nth_elem_string (2, s) <> "^";
wenzelm@8230
   107
wenzelm@6272
   108
fun is_printable s =
wenzelm@6272
   109
  size s = 1 andalso ord space <= ord s andalso ord s <= ord "~" orelse
wenzelm@8230
   110
  is_symbolic s;
wenzelm@8230
   111
wenzelm@6272
   112
wenzelm@10738
   113
fun sym_length ss = foldl (fn (n, s) =>
wenzelm@10738
   114
  (if not (is_printable s) then 0 else
wenzelm@10738
   115
    (case Library.try String.substring (s, 2, 4) of
wenzelm@10738
   116
      Some s' => if s' = "long" orelse s' = "Long" then 2 else 1
wenzelm@10738
   117
    | None => 1)) + n) (0, ss);
wenzelm@6272
   118
wenzelm@11010
   119
fun strip_blanks s =
wenzelm@11010
   120
  implode (#1 (Library.take_suffix is_blank (#2 (Library.take_prefix is_blank (explode s)))));
wenzelm@11010
   121
wenzelm@6272
   122
wenzelm@6272
   123
(* beginning *)
wenzelm@6272
   124
wenzelm@6272
   125
val smash_blanks = map (fn s => if is_blank s then space else s);
wenzelm@6272
   126
wenzelm@6272
   127
fun beginning raw_ss =
wenzelm@6272
   128
  let
wenzelm@6272
   129
    val (all_ss, _) = take_suffix is_blank raw_ss;
wenzelm@6272
   130
    val dots = if length all_ss > 10 then " ..." else "";
wenzelm@6272
   131
    val (ss, _) = take_suffix is_blank (take (10, all_ss));
wenzelm@6272
   132
  in implode (smash_blanks ss) ^ dots end;
wenzelm@6272
   133
wenzelm@6272
   134
wenzelm@6272
   135
wenzelm@6272
   136
(** isabelle-0 encoding table **)
wenzelm@6116
   137
wenzelm@6116
   138
val enc_start = 160;
wenzelm@6116
   139
val enc_end = 255;
wenzelm@6116
   140
wenzelm@6116
   141
val enc_vector =
wenzelm@6116
   142
[
wenzelm@6116
   143
(* GENERATED TEXT FOLLOWS - Do not edit! *)
oheimb@6166
   144
  "\\<spacespace>",
wenzelm@6116
   145
  "\\<Gamma>",
wenzelm@6116
   146
  "\\<Delta>",
wenzelm@6116
   147
  "\\<Theta>",
wenzelm@6116
   148
  "\\<Lambda>",
wenzelm@6116
   149
  "\\<Pi>",
wenzelm@6116
   150
  "\\<Sigma>",
wenzelm@6116
   151
  "\\<Phi>",
wenzelm@6116
   152
  "\\<Psi>",
wenzelm@6116
   153
  "\\<Omega>",
wenzelm@6116
   154
  "\\<alpha>",
wenzelm@6116
   155
  "\\<beta>",
wenzelm@6116
   156
  "\\<gamma>",
wenzelm@6116
   157
  "\\<delta>",
wenzelm@6116
   158
  "\\<epsilon>",
wenzelm@6116
   159
  "\\<zeta>",
wenzelm@6116
   160
  "\\<eta>",
wenzelm@6116
   161
  "\\<theta>",
wenzelm@6116
   162
  "\\<kappa>",
wenzelm@6116
   163
  "\\<lambda>",
wenzelm@6116
   164
  "\\<mu>",
wenzelm@6116
   165
  "\\<nu>",
wenzelm@6116
   166
  "\\<xi>",
wenzelm@6116
   167
  "\\<pi>",
wenzelm@6116
   168
  "\\<rho>",
wenzelm@6116
   169
  "\\<sigma>",
wenzelm@6116
   170
  "\\<tau>",
wenzelm@6116
   171
  "\\<phi>",
wenzelm@6116
   172
  "\\<chi>",
wenzelm@6116
   173
  "\\<psi>",
wenzelm@6116
   174
  "\\<omega>",
wenzelm@6116
   175
  "\\<not>",
wenzelm@6116
   176
  "\\<and>",
wenzelm@6116
   177
  "\\<or>",
wenzelm@6116
   178
  "\\<forall>",
wenzelm@6116
   179
  "\\<exists>",
wenzelm@6116
   180
  "\\<And>",
wenzelm@6116
   181
  "\\<lceil>",
wenzelm@6116
   182
  "\\<rceil>",
wenzelm@6116
   183
  "\\<lfloor>",
wenzelm@6116
   184
  "\\<rfloor>",
wenzelm@6116
   185
  "\\<turnstile>",
wenzelm@6116
   186
  "\\<Turnstile>",
wenzelm@6116
   187
  "\\<lbrakk>",
wenzelm@6116
   188
  "\\<rbrakk>",
wenzelm@6116
   189
  "\\<cdot>",
wenzelm@6116
   190
  "\\<in>",
wenzelm@6116
   191
  "\\<subseteq>",
wenzelm@6116
   192
  "\\<inter>",
wenzelm@6116
   193
  "\\<union>",
wenzelm@6116
   194
  "\\<Inter>",
wenzelm@6116
   195
  "\\<Union>",
wenzelm@6116
   196
  "\\<sqinter>",
wenzelm@6116
   197
  "\\<squnion>",
wenzelm@6116
   198
  "\\<Sqinter>",
wenzelm@6116
   199
  "\\<Squnion>",
wenzelm@6116
   200
  "\\<bottom>",
wenzelm@6116
   201
  "\\<doteq>",
wenzelm@6116
   202
  "\\<equiv>",
wenzelm@6116
   203
  "\\<noteq>",
wenzelm@6116
   204
  "\\<sqsubset>",
wenzelm@6116
   205
  "\\<sqsubseteq>",
wenzelm@6116
   206
  "\\<prec>",
wenzelm@6116
   207
  "\\<preceq>",
wenzelm@6116
   208
  "\\<succ>",
wenzelm@6116
   209
  "\\<approx>",
wenzelm@6116
   210
  "\\<sim>",
wenzelm@6116
   211
  "\\<simeq>",
wenzelm@6116
   212
  "\\<le>",
wenzelm@6116
   213
  "\\<Colon>",
wenzelm@6116
   214
  "\\<leftarrow>",
wenzelm@6116
   215
  "\\<midarrow>",
wenzelm@6116
   216
  "\\<rightarrow>",
wenzelm@6116
   217
  "\\<Leftarrow>",
wenzelm@6116
   218
  "\\<Midarrow>",
wenzelm@6116
   219
  "\\<Rightarrow>",
wenzelm@9961
   220
  "\\<frown>",
wenzelm@6116
   221
  "\\<mapsto>",
wenzelm@6116
   222
  "\\<leadsto>",
wenzelm@6116
   223
  "\\<up>",
wenzelm@6116
   224
  "\\<down>",
wenzelm@6116
   225
  "\\<notin>",
wenzelm@6116
   226
  "\\<times>",
wenzelm@6116
   227
  "\\<oplus>",
wenzelm@6116
   228
  "\\<ominus>",
wenzelm@6116
   229
  "\\<otimes>",
wenzelm@6116
   230
  "\\<oslash>",
wenzelm@6116
   231
  "\\<subset>",
wenzelm@6116
   232
  "\\<infinity>",
wenzelm@6116
   233
  "\\<box>",
wenzelm@6116
   234
  "\\<diamond>",
wenzelm@6116
   235
  "\\<circ>",
wenzelm@6116
   236
  "\\<bullet>",
wenzelm@6116
   237
  "\\<parallel>",
wenzelm@6116
   238
  "\\<surd>",
wenzelm@6116
   239
  "\\<copyright>"
wenzelm@6116
   240
(* END OF GENERATED TEXT *)
wenzelm@6116
   241
];
wenzelm@6116
   242
wenzelm@6116
   243
val enc_rel = enc_vector ~~ map chr (enc_start upto enc_end);
wenzelm@6116
   244
wenzelm@6116
   245
val char_tab = Symtab.make enc_rel;
wenzelm@6116
   246
val symbol_tab = Symtab.make (map swap enc_rel);
wenzelm@6116
   247
wenzelm@6116
   248
fun lookup_symbol c =
wenzelm@6116
   249
  if ord c < enc_start then None
wenzelm@6116
   250
  else Symtab.lookup (symbol_tab, c);
wenzelm@6116
   251
wenzelm@6116
   252
wenzelm@6116
   253
(* encode / decode *)
wenzelm@6116
   254
wenzelm@6116
   255
fun char s = if_none (Symtab.lookup (char_tab, s)) s;
wenzelm@6116
   256
fun symbol c = if_none (lookup_symbol c) c;
wenzelm@6116
   257
wenzelm@6116
   258
fun symbol' c =
wenzelm@6116
   259
  (case lookup_symbol c of
wenzelm@6116
   260
    None => c
wenzelm@6116
   261
  | Some s => "\\" ^ s);
wenzelm@6116
   262
wenzelm@6116
   263
wenzelm@6116
   264
wenzelm@8998
   265
(** scanning through symbols **)
wenzelm@6640
   266
wenzelm@6640
   267
fun scanner msg scan chs =
wenzelm@6640
   268
  let
wenzelm@6640
   269
    fun err_msg cs = msg ^ ": " ^ beginning cs;
wenzelm@6640
   270
    val fin_scan = Scan.error (Scan.finite stopper (!! (fn (cs, _) => err_msg cs) scan));
wenzelm@6640
   271
  in
wenzelm@6640
   272
    (case fin_scan chs of
wenzelm@6640
   273
      (result, []) => result
wenzelm@6640
   274
    | (_, rest) => error (err_msg rest))
wenzelm@6640
   275
  end;
wenzelm@6640
   276
wenzelm@6640
   277
wenzelm@6640
   278
wenzelm@6272
   279
(** symbol input **)
wenzelm@6116
   280
wenzelm@6116
   281
(* scan *)
wenzelm@6116
   282
wenzelm@6116
   283
val scan_id = Scan.one is_letter ^^ (Scan.any is_letdig >> implode);
wenzelm@6116
   284
wenzelm@6116
   285
val scan =
wenzelm@6116
   286
  ($$ "\\" --| Scan.optional ($$ "\\") "") ^^ $$ "<" ^^
wenzelm@6116
   287
    !! (fn (cs, _) => "Malformed symbolic character specification: \\" ^ "<" ^ beginning cs)
wenzelm@6116
   288
      (Scan.optional ($$ "^") "" ^^ scan_id ^^ $$ ">") ||
wenzelm@6116
   289
  Scan.one not_eof;
wenzelm@6116
   290
wenzelm@6116
   291
wenzelm@6116
   292
(* source *)
wenzelm@6116
   293
wenzelm@10747
   294
val recover = Scan.any ((not o is_blank) andf not_eof) >> K [malformed];
wenzelm@6116
   295
wenzelm@6116
   296
fun source do_recover src =
wenzelm@6116
   297
  Source.source stopper (Scan.bulk scan) (if do_recover then Some recover else None) src;
wenzelm@6116
   298
wenzelm@6116
   299
wenzelm@6116
   300
(* explode *)
wenzelm@6116
   301
wenzelm@6116
   302
fun no_syms [] = true
wenzelm@6116
   303
  | no_syms ("\\" :: "<" :: _) = false
wenzelm@6116
   304
  | no_syms (c :: cs) = ord c < enc_start andalso no_syms cs;
wenzelm@6116
   305
wenzelm@6116
   306
fun sym_explode str =
wenzelm@6116
   307
  let val chs = explode str in
wenzelm@6116
   308
    if no_syms chs then chs     (*tune trivial case*)
wenzelm@6116
   309
    else map symbol (the (Scan.read stopper (Scan.repeat scan) chs))
wenzelm@6116
   310
  end;
wenzelm@6116
   311
wenzelm@6116
   312
wenzelm@6272
   313
(* input *)
wenzelm@6116
   314
wenzelm@6116
   315
fun input str =
wenzelm@6116
   316
  let val chs = explode str in
wenzelm@6116
   317
    if forall (fn c => ord c < enc_start) chs then str
wenzelm@6116
   318
    else implode (map symbol' chs)
wenzelm@6116
   319
  end;
wenzelm@6116
   320
wenzelm@6116
   321
wenzelm@6272
   322
wenzelm@6272
   323
(** symbol output **)
wenzelm@6272
   324
wenzelm@10953
   325
(* default *)
wenzelm@6272
   326
wenzelm@6272
   327
fun string_size s = (s, real (size s));
wenzelm@6272
   328
wenzelm@6272
   329
fun default_output s =
wenzelm@6320
   330
  if not (exists_string (equal "\\") s) then string_size s
wenzelm@10738
   331
  else string_size (implode (map (fn "\\" => "\\\\" | c => c) (explode s)));    (*sic!*)
wenzelm@6272
   332
wenzelm@10953
   333
fun default_indent (_: string, k) = spaces k;
wenzelm@6272
   334
wenzelm@10953
   335
wenzelm@10953
   336
(* isabelle_font *)
wenzelm@6272
   337
wenzelm@6272
   338
fun isabelle_font_output s =
wenzelm@6272
   339
  let val cs = sym_explode s
wenzelm@6272
   340
  in (implode (map char cs), real (sym_length cs)) end;
wenzelm@6272
   341
wenzelm@10953
   342
val isabelle_font_indent = default_indent;
wenzelm@10953
   343
wenzelm@6272
   344
wenzelm@6272
   345
(* maintain modes *)
wenzelm@6272
   346
wenzelm@6692
   347
val isabelle_fontN = "isabelle_font";
wenzelm@6692
   348
val symbolsN = "symbols";
wenzelm@6692
   349
val xsymbolsN = "xsymbols";
wenzelm@6692
   350
wenzelm@10953
   351
val modes = ref (Symtab.make [(isabelle_fontN, (isabelle_font_output, isabelle_font_indent))]);
wenzelm@6272
   352
wenzelm@6272
   353
fun lookup_mode name = Symtab.lookup (! modes, name);
wenzelm@6272
   354
wenzelm@10953
   355
fun add_mode name m =
wenzelm@6272
   356
 (if is_none (lookup_mode name) then ()
wenzelm@6320
   357
  else warning ("Redeclaration of symbol print mode " ^ quote name);
wenzelm@10953
   358
  modes := Symtab.update ((name, m), ! modes));
wenzelm@10953
   359
wenzelm@10953
   360
fun get_mode () =
wenzelm@10953
   361
  if_none (get_first lookup_mode (! print_mode)) (default_output, default_indent);
wenzelm@6272
   362
wenzelm@6272
   363
wenzelm@6272
   364
(* mode output *)
wenzelm@6272
   365
wenzelm@10953
   366
fun output_width x = #1 (get_mode ()) x;
wenzelm@6272
   367
val output = #1 o output_width;
wenzelm@10923
   368
val plain_output = #1 o default_output;
wenzelm@6272
   369
wenzelm@10953
   370
fun indent x = #2 (get_mode ()) x;
wenzelm@10953
   371
wenzelm@6272
   372
wenzelm@6116
   373
(*final declarations of this structure!*)
wenzelm@6272
   374
val length = sym_length;
wenzelm@6116
   375
val explode = sym_explode;
wenzelm@6272
   376
wenzelm@6116
   377
wenzelm@6116
   378
end;