src/Pure/General/symbol.ML
author wenzelm
Wed Feb 20 00:55:42 2002 +0100 (2002-02-20)
changeset 12904 c208d71702d1
parent 12116 4027b15377a5
child 13559 51c3ac47d127
permissions -rw-r--r--
added is_quasi;
added bump_string (clarified version of old Library.bump_string);
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
wenzelm@8230
    29
  val is_symbolic: symbol -> bool
wenzelm@6116
    30
  val is_printable: symbol -> bool
wenzelm@6272
    31
  val length: symbol list -> int
wenzelm@11010
    32
  val strip_blanks: string -> string
wenzelm@6116
    33
  val beginning: symbol list -> string
wenzelm@6116
    34
  val scan: string list -> symbol * string list
wenzelm@6640
    35
  val scanner: string -> (symbol list -> 'a * symbol list) -> symbol list -> 'a
wenzelm@6116
    36
  val source: bool -> (string, 'a) Source.source ->
wenzelm@6116
    37
    (symbol, (string, 'a) Source.source) Source.source
wenzelm@6272
    38
  val explode: string -> symbol list
wenzelm@12904
    39
  val bump_string: string -> string
wenzelm@10953
    40
  val default_indent: string * int -> string
wenzelm@10953
    41
  val add_mode: string -> (string -> string * real) * (string * int -> string) -> unit
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@12116
    62
  output is subject to the print_mode variable (default: verbatim),
wenzelm@12116
    63
  actual interpretation in display is up to front-end tools;
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@12904
    94
fun is_quasi "_" = true
wenzelm@12904
    95
  | is_quasi "'" = true
wenzelm@12904
    96
  | is_quasi _ = false;
wenzelm@12904
    97
wenzelm@12904
    98
fun is_quasi_letter s = is_quasi s orelse is_letter s;
wenzelm@6272
    99
wenzelm@6272
   100
val is_blank =
wenzelm@6272
   101
  fn " " => true | "\t" => true | "\n" => true | "\^L" => true
wenzelm@6272
   102
    | "\160" => true | "\\<spacespace>" => true
wenzelm@6272
   103
    | _ => false;
wenzelm@6272
   104
wenzelm@12904
   105
fun is_letdig s = is_quasi_letter s orelse is_digit s;
wenzelm@6272
   106
wenzelm@8230
   107
fun is_symbolic s =
wenzelm@8230
   108
  size s > 2 andalso nth_elem_string (2, s) <> "^";
wenzelm@8230
   109
wenzelm@6272
   110
fun is_printable s =
wenzelm@6272
   111
  size s = 1 andalso ord space <= ord s andalso ord s <= ord "~" orelse
wenzelm@8230
   112
  is_symbolic s;
wenzelm@8230
   113
wenzelm@6272
   114
wenzelm@10738
   115
fun sym_length ss = foldl (fn (n, s) =>
wenzelm@10738
   116
  (if not (is_printable s) then 0 else
wenzelm@10738
   117
    (case Library.try String.substring (s, 2, 4) of
wenzelm@10738
   118
      Some s' => if s' = "long" orelse s' = "Long" then 2 else 1
wenzelm@10738
   119
    | None => 1)) + n) (0, ss);
wenzelm@6272
   120
wenzelm@11010
   121
fun strip_blanks s =
wenzelm@11010
   122
  implode (#1 (Library.take_suffix is_blank (#2 (Library.take_prefix is_blank (explode s)))));
wenzelm@11010
   123
wenzelm@6272
   124
wenzelm@6272
   125
(* beginning *)
wenzelm@6272
   126
wenzelm@6272
   127
val smash_blanks = map (fn s => if is_blank s then space else s);
wenzelm@6272
   128
wenzelm@6272
   129
fun beginning raw_ss =
wenzelm@6272
   130
  let
wenzelm@6272
   131
    val (all_ss, _) = take_suffix is_blank raw_ss;
wenzelm@6272
   132
    val dots = if length all_ss > 10 then " ..." else "";
wenzelm@6272
   133
    val (ss, _) = take_suffix is_blank (take (10, all_ss));
wenzelm@6272
   134
  in implode (smash_blanks ss) ^ dots end;
wenzelm@6272
   135
wenzelm@6272
   136
wenzelm@6272
   137
wenzelm@8998
   138
(** scanning through symbols **)
wenzelm@6640
   139
wenzelm@6640
   140
fun scanner msg scan chs =
wenzelm@6640
   141
  let
wenzelm@6640
   142
    fun err_msg cs = msg ^ ": " ^ beginning cs;
wenzelm@6640
   143
    val fin_scan = Scan.error (Scan.finite stopper (!! (fn (cs, _) => err_msg cs) scan));
wenzelm@6640
   144
  in
wenzelm@6640
   145
    (case fin_scan chs of
wenzelm@6640
   146
      (result, []) => result
wenzelm@6640
   147
    | (_, rest) => error (err_msg rest))
wenzelm@6640
   148
  end;
wenzelm@6640
   149
wenzelm@6640
   150
wenzelm@6640
   151
wenzelm@6272
   152
(** symbol input **)
wenzelm@6116
   153
wenzelm@6116
   154
(* scan *)
wenzelm@6116
   155
wenzelm@6116
   156
val scan_id = Scan.one is_letter ^^ (Scan.any is_letdig >> implode);
wenzelm@6116
   157
wenzelm@6116
   158
val scan =
wenzelm@6116
   159
  ($$ "\\" --| Scan.optional ($$ "\\") "") ^^ $$ "<" ^^
wenzelm@6116
   160
    !! (fn (cs, _) => "Malformed symbolic character specification: \\" ^ "<" ^ beginning cs)
wenzelm@6116
   161
      (Scan.optional ($$ "^") "" ^^ scan_id ^^ $$ ">") ||
wenzelm@6116
   162
  Scan.one not_eof;
wenzelm@6116
   163
wenzelm@6116
   164
wenzelm@6116
   165
(* source *)
wenzelm@6116
   166
wenzelm@10747
   167
val recover = Scan.any ((not o is_blank) andf not_eof) >> K [malformed];
wenzelm@6116
   168
wenzelm@6116
   169
fun source do_recover src =
wenzelm@6116
   170
  Source.source stopper (Scan.bulk scan) (if do_recover then Some recover else None) src;
wenzelm@6116
   171
wenzelm@6116
   172
wenzelm@6116
   173
(* explode *)
wenzelm@6116
   174
wenzelm@6116
   175
fun no_syms [] = true
wenzelm@6116
   176
  | no_syms ("\\" :: "<" :: _) = false
wenzelm@12116
   177
  | no_syms (_ :: cs) = no_syms cs;
wenzelm@6116
   178
wenzelm@6116
   179
fun sym_explode str =
wenzelm@6116
   180
  let val chs = explode str in
wenzelm@6116
   181
    if no_syms chs then chs     (*tune trivial case*)
wenzelm@12116
   182
    else the (Scan.read stopper (Scan.repeat scan) chs)
wenzelm@6116
   183
  end;
wenzelm@6116
   184
wenzelm@6116
   185
wenzelm@12904
   186
(* bump_string -- increment suffix of lowercase letters like a base 26 number *)
wenzelm@12904
   187
wenzelm@12904
   188
fun bump_string str =
wenzelm@12904
   189
  let
wenzelm@12904
   190
    fun bump [] = ["a"]
wenzelm@12904
   191
      | bump ("z" :: ss) = "a" :: bump ss
wenzelm@12904
   192
      | bump (s :: ss) =
wenzelm@12904
   193
          if size s = 1 andalso ord "a" <= ord s andalso ord s < ord "z"
wenzelm@12904
   194
          then chr (ord s + 1) :: ss
wenzelm@12904
   195
          else "a" :: s :: ss;
wenzelm@12904
   196
    val (cs, qs) = Library.take_suffix is_quasi (sym_explode str);
wenzelm@12904
   197
  in implode (rev (bump (rev cs)) @ qs) end;
wenzelm@12904
   198
wenzelm@6272
   199
wenzelm@6272
   200
(** symbol output **)
wenzelm@6272
   201
wenzelm@10953
   202
(* default *)
wenzelm@6272
   203
wenzelm@6272
   204
fun string_size s = (s, real (size s));
wenzelm@6272
   205
wenzelm@6272
   206
fun default_output s =
wenzelm@6320
   207
  if not (exists_string (equal "\\") s) then string_size s
wenzelm@10738
   208
  else string_size (implode (map (fn "\\" => "\\\\" | c => c) (explode s)));    (*sic!*)
wenzelm@6272
   209
wenzelm@10953
   210
fun default_indent (_: string, k) = spaces k;
wenzelm@6272
   211
wenzelm@10953
   212
wenzelm@6272
   213
(* maintain modes *)
wenzelm@6272
   214
wenzelm@6692
   215
val symbolsN = "symbols";
wenzelm@6692
   216
val xsymbolsN = "xsymbols";
wenzelm@6692
   217
wenzelm@12116
   218
val modes =
wenzelm@12116
   219
  ref (Symtab.empty: ((string -> string * real) * (string * int -> string)) Symtab.table);
wenzelm@6272
   220
wenzelm@6272
   221
fun lookup_mode name = Symtab.lookup (! modes, name);
wenzelm@6272
   222
wenzelm@10953
   223
fun add_mode name m =
wenzelm@6272
   224
 (if is_none (lookup_mode name) then ()
wenzelm@6320
   225
  else warning ("Redeclaration of symbol print mode " ^ quote name);
wenzelm@10953
   226
  modes := Symtab.update ((name, m), ! modes));
wenzelm@10953
   227
wenzelm@10953
   228
fun get_mode () =
wenzelm@10953
   229
  if_none (get_first lookup_mode (! print_mode)) (default_output, default_indent);
wenzelm@6272
   230
wenzelm@6272
   231
wenzelm@6272
   232
(* mode output *)
wenzelm@6272
   233
wenzelm@10953
   234
fun output_width x = #1 (get_mode ()) x;
wenzelm@6272
   235
val output = #1 o output_width;
wenzelm@10923
   236
val plain_output = #1 o default_output;
wenzelm@6272
   237
wenzelm@10953
   238
fun indent x = #2 (get_mode ()) x;
wenzelm@10953
   239
wenzelm@6272
   240
wenzelm@6116
   241
(*final declarations of this structure!*)
wenzelm@6272
   242
val length = sym_length;
wenzelm@6116
   243
val explode = sym_explode;
wenzelm@6272
   244
wenzelm@6116
   245
wenzelm@6116
   246
end;