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