src/Pure/General/symbol.ML
author schirmer
Mon Jan 26 10:34:02 2004 +0100 (2004-01-26)
changeset 14361 ad2f5da643b4
parent 14234 9590df3c5f2a
child 14557 31ae4a47267c
permissions -rw-r--r--
* Support for raw latex output in control symbols: \<^raw...>
* Symbols may only start with one backslash: \<...>. \\<...> is no longer
accepted by the scanner.
- Adapted some Isar-theories to fit to this policy
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>
schirmer@14361
    62
    (c) control symbols: \<^ctrlident>
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
skalberg@14171
    88
local
skalberg@14171
    89
    fun wrap s = "\\<" ^ s ^ ">"
skalberg@14171
    90
skalberg@14171
    91
    val cal_letters =
skalberg@14171
    92
	["A","B","C","D","E","F","G","H","I","J","K","L","M",
skalberg@14171
    93
	 "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
skalberg@14171
    94
skalberg@14171
    95
    val small_letters =
skalberg@14171
    96
	["a","b","c","d","e","f","g","h","i","j","k","l","m",
skalberg@14171
    97
	 "n","o","p","q","r","s","t","u","v","w","x","y","z"]
skalberg@14171
    98
skalberg@14171
    99
    val goth_letters =
skalberg@14171
   100
	["AA","BB","CC","DD","EE","FF","GG","HH","II","JJ","KK","LL","MM",
skalberg@14171
   101
	 "NN","OO","PP","QQ","RR","SS","TT","UU","VV","WW","XX","YY","ZZ",
skalberg@14171
   102
	 "aa","bb","cc","dd","ee","ff","gg","hh","ii","jj","kk","ll","mm",
skalberg@14171
   103
	 "nn","oo","pp","qq","rr","ss","tt","uu","vv","ww","xx","yy","zz"]
skalberg@14171
   104
skalberg@14171
   105
    val greek_letters =
skalberg@14171
   106
	["alpha","beta","gamma","delta","epsilon","zeta","eta","theta",
skalberg@14171
   107
	 "iota","kappa",(*"lambda",*)"mu","nu","xi","pi","rho","sigma","tau",
skalberg@14171
   108
	 "upsilon","phi","psi","omega","Gamma","Delta","Theta","Lambda",
skalberg@14171
   109
	 "Xi","Pi","Sigma","Upsilon","Phi","Psi","Omega"]
skalberg@14171
   110
skalberg@14171
   111
    val bbb_letters = ["bool","complex","nat","rat","real","int"]
skalberg@14171
   112
kleing@14234
   113
    val control_letters = ["^isub", "^isup"]
kleing@14232
   114
 
skalberg@14171
   115
    val pre_letters =
skalberg@14171
   116
	cal_letters   @
skalberg@14171
   117
	small_letters @
skalberg@14171
   118
	goth_letters  @
kleing@14232
   119
	greek_letters @
kleing@14232
   120
	control_letters
kleing@14232
   121
skalberg@14171
   122
in
kleing@14232
   123
val ext_letters = map wrap pre_letters 
skalberg@14173
   124
skalberg@14171
   125
fun is_ext_letter s = String.isPrefix "\\<" s andalso s mem ext_letters
skalberg@14171
   126
end
skalberg@14171
   127
     
wenzelm@6272
   128
fun is_letter s =
skalberg@14171
   129
    (size s = 1 andalso
skalberg@14171
   130
     (ord "A" <= ord s andalso ord s <= ord "Z" orelse
skalberg@14171
   131
      ord "a" <= ord s andalso ord s <= ord "z"))
skalberg@14171
   132
    orelse is_ext_letter s
wenzelm@6272
   133
wenzelm@6272
   134
fun is_digit s =
skalberg@14173
   135
    size s = 1 andalso ord "0" <= ord s andalso ord s <= ord "9"
wenzelm@6272
   136
wenzelm@12904
   137
fun is_quasi "_" = true
wenzelm@12904
   138
  | is_quasi "'" = true
wenzelm@12904
   139
  | is_quasi _ = false;
wenzelm@12904
   140
wenzelm@12904
   141
fun is_quasi_letter s = is_quasi s orelse is_letter s;
wenzelm@6272
   142
wenzelm@6272
   143
val is_blank =
paulson@14221
   144
  fn " " => true | "\t" => true | "\r" => true | "\n" => true | "\^L" => true
wenzelm@6272
   145
    | "\160" => true | "\\<spacespace>" => true
wenzelm@6272
   146
    | _ => false;
wenzelm@6272
   147
wenzelm@12904
   148
fun is_letdig s = is_quasi_letter s orelse is_digit s;
wenzelm@6272
   149
wenzelm@8230
   150
fun is_symbolic s =
skalberg@14171
   151
  size s > 2 andalso nth_elem_string (2, s) <> "^"
skalberg@14173
   152
  andalso not (is_ext_letter s);
wenzelm@8230
   153
wenzelm@6272
   154
fun is_printable s =
wenzelm@6272
   155
  size s = 1 andalso ord space <= ord s andalso ord s <= ord "~" orelse
skalberg@14173
   156
  is_ext_letter s orelse
wenzelm@8230
   157
  is_symbolic s;
wenzelm@8230
   158
schirmer@14361
   159
fun is_ctrl_letter s =
schirmer@14361
   160
  size s = 1 andalso ord space <= ord s andalso ord s <= ord "~" andalso s <> ">";
schirmer@14361
   161
paulson@13559
   162
fun is_identifier s =
paulson@13559
   163
  case (explode s) of
paulson@13559
   164
      [] => false
paulson@13559
   165
    | c::cs => is_letter c andalso forall is_letdig cs;
wenzelm@6272
   166
wenzelm@10738
   167
fun sym_length ss = foldl (fn (n, s) =>
wenzelm@10738
   168
  (if not (is_printable s) then 0 else
wenzelm@10738
   169
    (case Library.try String.substring (s, 2, 4) of
wenzelm@10738
   170
      Some s' => if s' = "long" orelse s' = "Long" then 2 else 1
wenzelm@10738
   171
    | None => 1)) + n) (0, ss);
wenzelm@6272
   172
wenzelm@11010
   173
fun strip_blanks s =
wenzelm@11010
   174
  implode (#1 (Library.take_suffix is_blank (#2 (Library.take_prefix is_blank (explode s)))));
wenzelm@11010
   175
wenzelm@6272
   176
wenzelm@6272
   177
(* beginning *)
wenzelm@6272
   178
wenzelm@6272
   179
val smash_blanks = map (fn s => if is_blank s then space else s);
wenzelm@6272
   180
wenzelm@6272
   181
fun beginning raw_ss =
wenzelm@6272
   182
  let
wenzelm@6272
   183
    val (all_ss, _) = take_suffix is_blank raw_ss;
wenzelm@6272
   184
    val dots = if length all_ss > 10 then " ..." else "";
wenzelm@6272
   185
    val (ss, _) = take_suffix is_blank (take (10, all_ss));
wenzelm@6272
   186
  in implode (smash_blanks ss) ^ dots end;
wenzelm@6272
   187
wenzelm@6272
   188
wenzelm@6272
   189
wenzelm@8998
   190
(** scanning through symbols **)
wenzelm@6640
   191
wenzelm@6640
   192
fun scanner msg scan chs =
wenzelm@6640
   193
  let
wenzelm@6640
   194
    fun err_msg cs = msg ^ ": " ^ beginning cs;
wenzelm@6640
   195
    val fin_scan = Scan.error (Scan.finite stopper (!! (fn (cs, _) => err_msg cs) scan));
wenzelm@6640
   196
  in
wenzelm@6640
   197
    (case fin_scan chs of
wenzelm@6640
   198
      (result, []) => result
wenzelm@6640
   199
    | (_, rest) => error (err_msg rest))
wenzelm@6640
   200
  end;
wenzelm@6640
   201
wenzelm@6640
   202
wenzelm@6640
   203
wenzelm@6272
   204
(** symbol input **)
wenzelm@6116
   205
wenzelm@6116
   206
(* scan *)
wenzelm@6116
   207
wenzelm@6116
   208
val scan_id = Scan.one is_letter ^^ (Scan.any is_letdig >> implode);
schirmer@14361
   209
val scan_ctrlid = Scan.one is_letter ^^ (Scan.any is_ctrl_letter >> implode);
wenzelm@6116
   210
wenzelm@6116
   211
val scan =
schirmer@14361
   212
  $$ "\\" ^^ $$ "<" ^^
wenzelm@6116
   213
    !! (fn (cs, _) => "Malformed symbolic character specification: \\" ^ "<" ^ beginning cs)
schirmer@14361
   214
       ((($$ "^" ^^ scan_ctrlid) || scan_id) ^^ $$ ">") ||
wenzelm@6116
   215
  Scan.one not_eof;
wenzelm@6116
   216
wenzelm@6116
   217
(* source *)
wenzelm@6116
   218
wenzelm@10747
   219
val recover = Scan.any ((not o is_blank) andf not_eof) >> K [malformed];
wenzelm@6116
   220
wenzelm@6116
   221
fun source do_recover src =
wenzelm@6116
   222
  Source.source stopper (Scan.bulk scan) (if do_recover then Some recover else None) src;
wenzelm@6116
   223
wenzelm@6116
   224
wenzelm@6116
   225
(* explode *)
wenzelm@6116
   226
wenzelm@6116
   227
fun no_syms [] = true
wenzelm@6116
   228
  | no_syms ("\\" :: "<" :: _) = false
wenzelm@12116
   229
  | no_syms (_ :: cs) = no_syms cs;
wenzelm@6116
   230
wenzelm@6116
   231
fun sym_explode str =
wenzelm@6116
   232
  let val chs = explode str in
wenzelm@6116
   233
    if no_syms chs then chs     (*tune trivial case*)
wenzelm@12116
   234
    else the (Scan.read stopper (Scan.repeat scan) chs)
wenzelm@6116
   235
  end;
wenzelm@6116
   236
wenzelm@6116
   237
wenzelm@12904
   238
(* bump_string -- increment suffix of lowercase letters like a base 26 number *)
wenzelm@12904
   239
wenzelm@12904
   240
fun bump_string str =
wenzelm@12904
   241
  let
wenzelm@12904
   242
    fun bump [] = ["a"]
wenzelm@12904
   243
      | bump ("z" :: ss) = "a" :: bump ss
wenzelm@12904
   244
      | bump (s :: ss) =
wenzelm@12904
   245
          if size s = 1 andalso ord "a" <= ord s andalso ord s < ord "z"
wenzelm@12904
   246
          then chr (ord s + 1) :: ss
wenzelm@12904
   247
          else "a" :: s :: ss;
wenzelm@12904
   248
    val (cs, qs) = Library.take_suffix is_quasi (sym_explode str);
wenzelm@12904
   249
  in implode (rev (bump (rev cs)) @ qs) end;
wenzelm@12904
   250
wenzelm@6272
   251
wenzelm@6272
   252
(** symbol output **)
wenzelm@6272
   253
wenzelm@10953
   254
(* default *)
wenzelm@6272
   255
wenzelm@6272
   256
fun string_size s = (s, real (size s));
wenzelm@6272
   257
berghofe@13730
   258
val escape = Scan.repeat
berghofe@13730
   259
  ((($$ "\\" >> K "\\\\") ^^ Scan.optional ($$ "\\" >> K "\\\\") "" ^^ $$ "<" ^^
berghofe@13730
   260
      Scan.optional ($$ "^") "" ^^ scan_id ^^ $$ ">") ||
berghofe@13730
   261
   Scan.one not_eof) >> implode;
berghofe@13730
   262
wenzelm@6272
   263
fun default_output s =
wenzelm@6320
   264
  if not (exists_string (equal "\\") s) then string_size s
berghofe@13730
   265
  else string_size (fst (Scan.finite stopper escape (explode s)));
wenzelm@6272
   266
wenzelm@10953
   267
fun default_indent (_: string, k) = spaces k;
wenzelm@6272
   268
wenzelm@10953
   269
wenzelm@6272
   270
(* maintain modes *)
wenzelm@6272
   271
wenzelm@6692
   272
val symbolsN = "symbols";
wenzelm@6692
   273
val xsymbolsN = "xsymbols";
wenzelm@6692
   274
wenzelm@12116
   275
val modes =
wenzelm@12116
   276
  ref (Symtab.empty: ((string -> string * real) * (string * int -> string)) Symtab.table);
wenzelm@6272
   277
wenzelm@6272
   278
fun lookup_mode name = Symtab.lookup (! modes, name);
wenzelm@6272
   279
wenzelm@10953
   280
fun add_mode name m =
wenzelm@6272
   281
 (if is_none (lookup_mode name) then ()
wenzelm@6320
   282
  else warning ("Redeclaration of symbol print mode " ^ quote name);
wenzelm@10953
   283
  modes := Symtab.update ((name, m), ! modes));
wenzelm@10953
   284
wenzelm@10953
   285
fun get_mode () =
wenzelm@10953
   286
  if_none (get_first lookup_mode (! print_mode)) (default_output, default_indent);
wenzelm@6272
   287
wenzelm@6272
   288
wenzelm@6272
   289
(* mode output *)
wenzelm@6272
   290
wenzelm@10953
   291
fun output_width x = #1 (get_mode ()) x;
wenzelm@6272
   292
val output = #1 o output_width;
wenzelm@10923
   293
val plain_output = #1 o default_output;
wenzelm@6272
   294
wenzelm@10953
   295
fun indent x = #2 (get_mode ()) x;
wenzelm@10953
   296
wenzelm@6272
   297
wenzelm@6116
   298
(*final declarations of this structure!*)
wenzelm@6272
   299
val length = sym_length;
wenzelm@6116
   300
val explode = sym_explode;
wenzelm@6272
   301
wenzelm@6116
   302
wenzelm@6116
   303
end;