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