src/Pure/General/symbol.ML
author wenzelm
Wed Jan 13 15:18:02 1999 +0100 (1999-01-13)
changeset 6118 caa439435666
parent 6116 8ba2f25610f7
child 6166 a56aaad7ff2d
permissions -rw-r--r--
fixed titles;
wenzelm@6118
     1
(*  Title:      Pure/General/symbol.ML
wenzelm@6116
     2
    ID:         $Id$
wenzelm@6116
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@6116
     4
wenzelm@6116
     5
Generalized characters.
wenzelm@6116
     6
*)
wenzelm@6116
     7
wenzelm@6116
     8
signature SYMBOL =
wenzelm@6116
     9
sig
wenzelm@6116
    10
  type symbol
wenzelm@6116
    11
  val space: symbol
wenzelm@6116
    12
  val eof: symbol
wenzelm@6116
    13
  val is_eof: symbol -> bool
wenzelm@6116
    14
  val not_eof: symbol -> bool
wenzelm@6116
    15
  val stopper: symbol * (symbol -> bool)
wenzelm@6116
    16
  val is_ascii: symbol -> bool
wenzelm@6116
    17
  val is_letter: symbol -> bool
wenzelm@6116
    18
  val is_digit: symbol -> bool
wenzelm@6116
    19
  val is_quasi_letter: symbol -> bool
wenzelm@6116
    20
  val is_letdig: symbol -> bool
wenzelm@6116
    21
  val is_blank: symbol -> bool
wenzelm@6116
    22
  val is_printable: symbol -> bool
wenzelm@6116
    23
  val beginning: symbol list -> string
wenzelm@6116
    24
  val scan: string list -> symbol * string list
wenzelm@6116
    25
  val explode: string -> symbol list
wenzelm@6116
    26
  val length: symbol list -> int
wenzelm@6116
    27
  val size: string -> int
wenzelm@6116
    28
  val input: string -> string
wenzelm@6116
    29
  val output: string -> string
wenzelm@6116
    30
  val source: bool -> (string, 'a) Source.source ->
wenzelm@6116
    31
    (symbol, (string, 'a) Source.source) Source.source
wenzelm@6116
    32
end;
wenzelm@6116
    33
wenzelm@6116
    34
structure Symbol: SYMBOL =
wenzelm@6116
    35
struct
wenzelm@6116
    36
wenzelm@6116
    37
wenzelm@6116
    38
(** encoding table (isabelle-0) **)
wenzelm@6116
    39
wenzelm@6116
    40
val enc_start = 160;
wenzelm@6116
    41
val enc_end = 255;
wenzelm@6116
    42
wenzelm@6116
    43
val enc_vector =
wenzelm@6116
    44
[
wenzelm@6116
    45
(* GENERATED TEXT FOLLOWS - Do not edit! *)
wenzelm@6116
    46
  "\\<space2>",
wenzelm@6116
    47
  "\\<Gamma>",
wenzelm@6116
    48
  "\\<Delta>",
wenzelm@6116
    49
  "\\<Theta>",
wenzelm@6116
    50
  "\\<Lambda>",
wenzelm@6116
    51
  "\\<Pi>",
wenzelm@6116
    52
  "\\<Sigma>",
wenzelm@6116
    53
  "\\<Phi>",
wenzelm@6116
    54
  "\\<Psi>",
wenzelm@6116
    55
  "\\<Omega>",
wenzelm@6116
    56
  "\\<alpha>",
wenzelm@6116
    57
  "\\<beta>",
wenzelm@6116
    58
  "\\<gamma>",
wenzelm@6116
    59
  "\\<delta>",
wenzelm@6116
    60
  "\\<epsilon>",
wenzelm@6116
    61
  "\\<zeta>",
wenzelm@6116
    62
  "\\<eta>",
wenzelm@6116
    63
  "\\<theta>",
wenzelm@6116
    64
  "\\<kappa>",
wenzelm@6116
    65
  "\\<lambda>",
wenzelm@6116
    66
  "\\<mu>",
wenzelm@6116
    67
  "\\<nu>",
wenzelm@6116
    68
  "\\<xi>",
wenzelm@6116
    69
  "\\<pi>",
wenzelm@6116
    70
  "\\<rho>",
wenzelm@6116
    71
  "\\<sigma>",
wenzelm@6116
    72
  "\\<tau>",
wenzelm@6116
    73
  "\\<phi>",
wenzelm@6116
    74
  "\\<chi>",
wenzelm@6116
    75
  "\\<psi>",
wenzelm@6116
    76
  "\\<omega>",
wenzelm@6116
    77
  "\\<not>",
wenzelm@6116
    78
  "\\<and>",
wenzelm@6116
    79
  "\\<or>",
wenzelm@6116
    80
  "\\<forall>",
wenzelm@6116
    81
  "\\<exists>",
wenzelm@6116
    82
  "\\<And>",
wenzelm@6116
    83
  "\\<lceil>",
wenzelm@6116
    84
  "\\<rceil>",
wenzelm@6116
    85
  "\\<lfloor>",
wenzelm@6116
    86
  "\\<rfloor>",
wenzelm@6116
    87
  "\\<turnstile>",
wenzelm@6116
    88
  "\\<Turnstile>",
wenzelm@6116
    89
  "\\<lbrakk>",
wenzelm@6116
    90
  "\\<rbrakk>",
wenzelm@6116
    91
  "\\<cdot>",
wenzelm@6116
    92
  "\\<in>",
wenzelm@6116
    93
  "\\<subseteq>",
wenzelm@6116
    94
  "\\<inter>",
wenzelm@6116
    95
  "\\<union>",
wenzelm@6116
    96
  "\\<Inter>",
wenzelm@6116
    97
  "\\<Union>",
wenzelm@6116
    98
  "\\<sqinter>",
wenzelm@6116
    99
  "\\<squnion>",
wenzelm@6116
   100
  "\\<Sqinter>",
wenzelm@6116
   101
  "\\<Squnion>",
wenzelm@6116
   102
  "\\<bottom>",
wenzelm@6116
   103
  "\\<doteq>",
wenzelm@6116
   104
  "\\<equiv>",
wenzelm@6116
   105
  "\\<noteq>",
wenzelm@6116
   106
  "\\<sqsubset>",
wenzelm@6116
   107
  "\\<sqsubseteq>",
wenzelm@6116
   108
  "\\<prec>",
wenzelm@6116
   109
  "\\<preceq>",
wenzelm@6116
   110
  "\\<succ>",
wenzelm@6116
   111
  "\\<approx>",
wenzelm@6116
   112
  "\\<sim>",
wenzelm@6116
   113
  "\\<simeq>",
wenzelm@6116
   114
  "\\<le>",
wenzelm@6116
   115
  "\\<Colon>",
wenzelm@6116
   116
  "\\<leftarrow>",
wenzelm@6116
   117
  "\\<midarrow>",
wenzelm@6116
   118
  "\\<rightarrow>",
wenzelm@6116
   119
  "\\<Leftarrow>",
wenzelm@6116
   120
  "\\<Midarrow>",
wenzelm@6116
   121
  "\\<Rightarrow>",
wenzelm@6116
   122
  "\\<bow>",
wenzelm@6116
   123
  "\\<mapsto>",
wenzelm@6116
   124
  "\\<leadsto>",
wenzelm@6116
   125
  "\\<up>",
wenzelm@6116
   126
  "\\<down>",
wenzelm@6116
   127
  "\\<notin>",
wenzelm@6116
   128
  "\\<times>",
wenzelm@6116
   129
  "\\<oplus>",
wenzelm@6116
   130
  "\\<ominus>",
wenzelm@6116
   131
  "\\<otimes>",
wenzelm@6116
   132
  "\\<oslash>",
wenzelm@6116
   133
  "\\<subset>",
wenzelm@6116
   134
  "\\<infinity>",
wenzelm@6116
   135
  "\\<box>",
wenzelm@6116
   136
  "\\<diamond>",
wenzelm@6116
   137
  "\\<circ>",
wenzelm@6116
   138
  "\\<bullet>",
wenzelm@6116
   139
  "\\<parallel>",
wenzelm@6116
   140
  "\\<surd>",
wenzelm@6116
   141
  "\\<copyright>"
wenzelm@6116
   142
(* END OF GENERATED TEXT *)
wenzelm@6116
   143
];
wenzelm@6116
   144
wenzelm@6116
   145
val enc_rel = enc_vector ~~ map chr (enc_start upto enc_end);
wenzelm@6116
   146
wenzelm@6116
   147
val char_tab = Symtab.make enc_rel;
wenzelm@6116
   148
val symbol_tab = Symtab.make (map swap enc_rel);
wenzelm@6116
   149
wenzelm@6116
   150
fun lookup_symbol c =
wenzelm@6116
   151
  if ord c < enc_start then None
wenzelm@6116
   152
  else Symtab.lookup (symbol_tab, c);
wenzelm@6116
   153
wenzelm@6116
   154
wenzelm@6116
   155
(* encode / decode *)
wenzelm@6116
   156
wenzelm@6116
   157
fun char s = if_none (Symtab.lookup (char_tab, s)) s;
wenzelm@6116
   158
fun symbol c = if_none (lookup_symbol c) c;
wenzelm@6116
   159
wenzelm@6116
   160
fun symbol' c =
wenzelm@6116
   161
  (case lookup_symbol c of
wenzelm@6116
   162
    None => c
wenzelm@6116
   163
  | Some s => "\\" ^ s);
wenzelm@6116
   164
wenzelm@6116
   165
wenzelm@6116
   166
wenzelm@6116
   167
(** type symbol **)
wenzelm@6116
   168
wenzelm@6116
   169
type symbol = string;
wenzelm@6116
   170
wenzelm@6116
   171
val space = " ";
wenzelm@6116
   172
val eof = "";
wenzelm@6116
   173
wenzelm@6116
   174
wenzelm@6116
   175
(* kinds *)
wenzelm@6116
   176
wenzelm@6116
   177
fun is_eof s = s = eof;
wenzelm@6116
   178
fun not_eof s = s <> eof;
wenzelm@6116
   179
val stopper = (eof, is_eof);
wenzelm@6116
   180
wenzelm@6116
   181
fun is_ascii s = size s = 1 andalso ord s < 128;
wenzelm@6116
   182
wenzelm@6116
   183
fun is_letter s =
wenzelm@6116
   184
  size s = 1 andalso
wenzelm@6116
   185
   (ord "A" <= ord s andalso ord s <= ord "Z" orelse
wenzelm@6116
   186
    ord "a" <= ord s andalso ord s <= ord "z");
wenzelm@6116
   187
wenzelm@6116
   188
fun is_digit s =
wenzelm@6116
   189
  size s = 1 andalso ord "0" <= ord s andalso ord s <= ord "9";
wenzelm@6116
   190
wenzelm@6116
   191
fun is_quasi_letter "_" = true
wenzelm@6116
   192
  | is_quasi_letter "'" = true
wenzelm@6116
   193
  | is_quasi_letter s = is_letter s;
wenzelm@6116
   194
wenzelm@6116
   195
val is_blank =
wenzelm@6116
   196
  fn " " => true | "\t" => true | "\n" => true | "\^L" => true
wenzelm@6116
   197
    | "\160" => true | "\\<space2>" => true
wenzelm@6116
   198
    | _ => false;
wenzelm@6116
   199
wenzelm@6116
   200
val is_letdig = is_quasi_letter orf is_digit;
wenzelm@6116
   201
wenzelm@6116
   202
fun is_printable s =
wenzelm@6116
   203
  size s = 1 andalso ord space <= ord s andalso ord s <= ord "~" orelse
wenzelm@6116
   204
  size s > 2 andalso nth_elem (2, explode s) <> "^";
wenzelm@6116
   205
wenzelm@6116
   206
wenzelm@6116
   207
(* beginning *)
wenzelm@6116
   208
wenzelm@6116
   209
val smash_blanks = map (fn s => if is_blank s then space else s);
wenzelm@6116
   210
wenzelm@6116
   211
fun beginning raw_ss =
wenzelm@6116
   212
  let
wenzelm@6116
   213
    val (all_ss, _) = take_suffix is_blank raw_ss;
wenzelm@6116
   214
    val dots = if length all_ss > 10 then " ..." else "";
wenzelm@6116
   215
    val (ss, _) = take_suffix is_blank (take (10, all_ss));
wenzelm@6116
   216
  in implode (smash_blanks ss) ^ dots end;
wenzelm@6116
   217
wenzelm@6116
   218
wenzelm@6116
   219
(* scan *)
wenzelm@6116
   220
wenzelm@6116
   221
val scan_id = Scan.one is_letter ^^ (Scan.any is_letdig >> implode);
wenzelm@6116
   222
wenzelm@6116
   223
val scan =
wenzelm@6116
   224
  ($$ "\\" --| Scan.optional ($$ "\\") "") ^^ $$ "<" ^^
wenzelm@6116
   225
    !! (fn (cs, _) => "Malformed symbolic character specification: \\" ^ "<" ^ beginning cs)
wenzelm@6116
   226
      (Scan.optional ($$ "^") "" ^^ scan_id ^^ $$ ">") ||
wenzelm@6116
   227
  Scan.one not_eof;
wenzelm@6116
   228
wenzelm@6116
   229
wenzelm@6116
   230
(* source *)
wenzelm@6116
   231
wenzelm@6116
   232
val recover = Scan.any1 ((not o is_blank) andf not_eof);
wenzelm@6116
   233
wenzelm@6116
   234
fun source do_recover src =
wenzelm@6116
   235
  Source.source stopper (Scan.bulk scan) (if do_recover then Some recover else None) src;
wenzelm@6116
   236
wenzelm@6116
   237
wenzelm@6116
   238
(* explode *)
wenzelm@6116
   239
wenzelm@6116
   240
fun no_syms [] = true
wenzelm@6116
   241
  | no_syms ("\\" :: "<" :: _) = false
wenzelm@6116
   242
  | no_syms (c :: cs) = ord c < enc_start andalso no_syms cs;
wenzelm@6116
   243
wenzelm@6116
   244
fun sym_explode str =
wenzelm@6116
   245
  let val chs = explode str in
wenzelm@6116
   246
    if no_syms chs then chs     (*tune trivial case*)
wenzelm@6116
   247
    else map symbol (the (Scan.read stopper (Scan.repeat scan) chs))
wenzelm@6116
   248
  end;
wenzelm@6116
   249
wenzelm@6116
   250
wenzelm@6116
   251
(* printable length *)
wenzelm@6116
   252
wenzelm@6116
   253
fun sym_length ss = foldl (fn (n, c) => if is_printable c then n + 1 else n) (0, ss);
wenzelm@6116
   254
val sym_size = sym_length o sym_explode;
wenzelm@6116
   255
wenzelm@6116
   256
wenzelm@6116
   257
(* input / output *)
wenzelm@6116
   258
wenzelm@6116
   259
fun input str =
wenzelm@6116
   260
  let val chs = explode str in
wenzelm@6116
   261
    if forall (fn c => ord c < enc_start) chs then str
wenzelm@6116
   262
    else implode (map symbol' chs)
wenzelm@6116
   263
  end;
wenzelm@6116
   264
wenzelm@6116
   265
val output = implode o map char o sym_explode;
wenzelm@6116
   266
wenzelm@6116
   267
wenzelm@6116
   268
(*final declarations of this structure!*)
wenzelm@6116
   269
val explode = sym_explode;
wenzelm@6116
   270
val length = sym_length;
wenzelm@6116
   271
val size = sym_size;
wenzelm@6116
   272
wenzelm@6116
   273
wenzelm@6116
   274
end;