src/Pure/General/symbol.ML
author wenzelm
Tue Jun 06 13:13:25 2017 +0200 (2017-06-06)
changeset 66020 a31760eee09d
parent 64275 ac2abc987cf9
child 67373 17f9fa98abab
permissions -rw-r--r--
discontinued obsolete print mode;
wenzelm@6118
     1
(*  Title:      Pure/General/symbol.ML
wenzelm@63936
     2
    Author:     Makarius
wenzelm@6116
     3
paulson@21897
     4
Generalized characters with infinitely many named symbols.
wenzelm@6116
     5
*)
wenzelm@6116
     6
wenzelm@6116
     7
signature SYMBOL =
wenzelm@6116
     8
sig
wenzelm@40509
     9
  type symbol = string
wenzelm@63936
    10
  val explode: string -> symbol list
wenzelm@61865
    11
  val spaces: int -> string
wenzelm@26524
    12
  val STX: symbol
wenzelm@26524
    13
  val DEL: symbol
wenzelm@62210
    14
  val open_: symbol
wenzelm@62210
    15
  val close: symbol
wenzelm@6116
    16
  val space: symbol
wenzelm@62804
    17
  val is_space: symbol -> bool
wenzelm@61579
    18
  val comment: symbol
wenzelm@14678
    19
  val is_char: symbol -> bool
wenzelm@37533
    20
  val is_utf8: symbol -> bool
wenzelm@14678
    21
  val is_symbolic: symbol -> bool
wenzelm@55033
    22
  val is_symbolic_char: symbol -> bool
wenzelm@14678
    23
  val is_printable: symbol -> bool
wenzelm@61470
    24
  val is_control: symbol -> bool
wenzelm@6116
    25
  val eof: symbol
wenzelm@6116
    26
  val is_eof: symbol -> bool
wenzelm@27766
    27
  val not_eof: symbol -> bool
wenzelm@27732
    28
  val stopper: symbol Scan.stopper
wenzelm@40523
    29
  val is_malformed: symbol -> bool
wenzelm@40523
    30
  val malformed_msg: symbol -> string
wenzelm@6116
    31
  val is_ascii: symbol -> bool
wenzelm@14678
    32
  val is_ascii_letter: symbol -> bool
wenzelm@14678
    33
  val is_ascii_digit: symbol -> bool
wenzelm@24580
    34
  val is_ascii_hex: symbol -> bool
wenzelm@14678
    35
  val is_ascii_quasi: symbol -> bool
wenzelm@14678
    36
  val is_ascii_blank: symbol -> bool
wenzelm@34095
    37
  val is_ascii_control: symbol -> bool
wenzelm@50236
    38
  val is_ascii_letdig: symbol -> bool
wenzelm@20200
    39
  val is_ascii_lower: symbol -> bool
wenzelm@20200
    40
  val is_ascii_upper: symbol -> bool
wenzelm@20200
    41
  val to_ascii_lower: symbol -> symbol
wenzelm@20200
    42
  val to_ascii_upper: symbol -> symbol
wenzelm@50238
    43
  val is_ascii_identifier: string -> bool
wenzelm@50236
    44
  val scan_ascii_id: string list -> string * string list
wenzelm@40523
    45
  datatype sym =
wenzelm@63936
    46
    Char of string | UTF8 of string | Sym of string | Control of string | Malformed of string | EOF
wenzelm@14873
    47
  val decode: symbol -> sym
wenzelm@14678
    48
  datatype kind = Letter | Digit | Quasi | Blank | Other
wenzelm@14678
    49
  val kind: symbol -> kind
wenzelm@6116
    50
  val is_letter: symbol -> bool
wenzelm@6116
    51
  val is_digit: symbol -> bool
wenzelm@12904
    52
  val is_quasi: symbol -> bool
wenzelm@14678
    53
  val is_blank: symbol -> bool
wenzelm@47850
    54
  val is_block_ctrl: symbol -> bool
wenzelm@6116
    55
  val is_quasi_letter: symbol -> bool
wenzelm@6116
    56
  val is_letdig: symbol -> bool
wenzelm@14728
    57
  val beginning: int -> symbol list -> string
wenzelm@50237
    58
  val esc: symbol -> string
wenzelm@50237
    59
  val escape: string -> string
wenzelm@50237
    60
  val scanner: string -> (string list -> 'a * string list) -> symbol list -> 'a
wenzelm@50162
    61
  val split_words: symbol list -> string list
wenzelm@50162
    62
  val explode_words: string -> string list
wenzelm@61435
    63
  val trim_blanks: string -> string
wenzelm@14678
    64
  val bump_init: string -> string
wenzelm@12904
    65
  val bump_string: string -> string
wenzelm@14678
    66
  val length: symbol list -> int
wenzelm@40131
    67
  val output: string -> Output.output * int
wenzelm@6116
    68
end;
wenzelm@6116
    69
wenzelm@6116
    70
structure Symbol: SYMBOL =
wenzelm@6116
    71
struct
wenzelm@6116
    72
wenzelm@14678
    73
(** type symbol **)
wenzelm@6272
    74
wenzelm@14678
    75
(*Symbols, which are considered the smallest entities of any Isabelle
wenzelm@6272
    76
  string, may be of the following form:
wenzelm@14678
    77
wenzelm@63936
    78
    (1) ASCII: a
wenzelm@63936
    79
    (2) UTF-8: รค
wenzelm@17823
    80
    (2) regular symbols: \<ident>
wenzelm@14834
    81
    (3) control symbols: \<^ident>
wenzelm@6272
    82
wenzelm@14678
    83
  Output is subject to the print_mode variable (default: verbatim),
wenzelm@14678
    84
  actual interpretation in display is up to front-end tools.
wenzelm@6272
    85
*)
wenzelm@6272
    86
wenzelm@6272
    87
type symbol = string;
wenzelm@6272
    88
wenzelm@26524
    89
val STX = chr 2;
wenzelm@26524
    90
val DEL = chr 127;
wenzelm@26524
    91
wenzelm@62529
    92
val open_ = "\<open>";
wenzelm@62529
    93
val close = "\<close>";
wenzelm@62210
    94
wenzelm@26524
    95
val space = chr 32;
wenzelm@62804
    96
fun is_space s = s = space;
wenzelm@17063
    97
wenzelm@61865
    98
local
wenzelm@61865
    99
  val small_spaces = Vector.tabulate (65, fn i => replicate_string i space);
wenzelm@61865
   100
in
wenzelm@61865
   101
  fun spaces n =
wenzelm@61865
   102
    if n < 64 then Vector.sub (small_spaces, n)
wenzelm@61865
   103
    else
wenzelm@61865
   104
      replicate_string (n div 64) (Vector.sub (small_spaces, 64)) ^
wenzelm@61865
   105
        Vector.sub (small_spaces, n mod 64);
wenzelm@61865
   106
end;
wenzelm@61865
   107
wenzelm@62529
   108
val comment = "\<comment>";
wenzelm@61579
   109
wenzelm@14678
   110
fun is_char s = size s = 1;
wenzelm@14678
   111
wenzelm@37533
   112
fun is_utf8 s = size s > 0 andalso forall_string (fn c => ord c >= 128) s;
wenzelm@37533
   113
wenzelm@55033
   114
fun raw_symbolic s =
wenzelm@62877
   115
  String.isPrefix "\092<" s andalso String.isSuffix ">" s andalso not (String.isPrefix "\092<^" s);
wenzelm@55033
   116
wenzelm@14678
   117
fun is_symbolic s =
wenzelm@62210
   118
  s <> open_ andalso s <> close andalso raw_symbolic s;
wenzelm@55033
   119
wenzelm@55033
   120
val is_symbolic_char = member (op =) (raw_explode "!#$%&*+-/<=>?@^_|~");
wenzelm@14678
   121
wenzelm@14678
   122
fun is_printable s =
wenzelm@64275
   123
  if is_char s then 32 <= ord s andalso ord s <= Char.ord #"~"
wenzelm@55033
   124
  else is_utf8 s orelse raw_symbolic s;
wenzelm@26632
   125
wenzelm@61470
   126
fun is_control s =
wenzelm@62877
   127
  String.isPrefix "\092<^" s andalso String.isSuffix ">" s;
wenzelm@61470
   128
wenzelm@6272
   129
wenzelm@14678
   130
(* input source control *)
wenzelm@6272
   131
wenzelm@14678
   132
val eof = "";
wenzelm@6272
   133
fun is_eof s = s = eof;
wenzelm@6272
   134
fun not_eof s = s <> eof;
wenzelm@27732
   135
val stopper = Scan.stopper (K eof) is_eof;
wenzelm@6272
   136
wenzelm@48774
   137
fun is_malformed s =
wenzelm@62877
   138
  String.isPrefix "\092<" s andalso not (String.isSuffix ">" s)
wenzelm@62877
   139
  orelse s = "\092<>" orelse s = "\092<^>";
wenzelm@48774
   140
wenzelm@40523
   141
fun malformed_msg s = "Malformed symbolic character: " ^ quote s;
wenzelm@14678
   142
wenzelm@14678
   143
wenzelm@43418
   144
(* ASCII symbols *)
wenzelm@14678
   145
wenzelm@14678
   146
fun is_ascii s = is_char s andalso ord s < 128;
wenzelm@14678
   147
wenzelm@14678
   148
fun is_ascii_letter s =
wenzelm@14678
   149
  is_char s andalso
wenzelm@64275
   150
   (Char.ord #"A" <= ord s andalso ord s <= Char.ord #"Z" orelse
wenzelm@64275
   151
    Char.ord #"a" <= ord s andalso ord s <= Char.ord #"z");
wenzelm@14678
   152
wenzelm@14678
   153
fun is_ascii_digit s =
wenzelm@64275
   154
  is_char s andalso Char.ord #"0" <= ord s andalso ord s <= Char.ord #"9";
wenzelm@14678
   155
wenzelm@24580
   156
fun is_ascii_hex s =
wenzelm@24580
   157
  is_char s andalso
wenzelm@64275
   158
   (Char.ord #"0" <= ord s andalso ord s <= Char.ord #"9" orelse
wenzelm@64275
   159
    Char.ord #"A" <= ord s andalso ord s <= Char.ord #"F" orelse
wenzelm@64275
   160
    Char.ord #"a" <= ord s andalso ord s <= Char.ord #"f");
wenzelm@24580
   161
wenzelm@14678
   162
fun is_ascii_quasi "_" = true
wenzelm@14678
   163
  | is_ascii_quasi "'" = true
wenzelm@14678
   164
  | is_ascii_quasi _ = false;
wenzelm@14678
   165
wenzelm@14678
   166
val is_ascii_blank =
wenzelm@43845
   167
  fn " " => true | "\t" => true | "\n" => true | "\^K" => true | "\f" => true | "\^M" => true
wenzelm@14678
   168
    | _ => false;
wenzelm@14678
   169
wenzelm@34095
   170
fun is_ascii_control s = is_char s andalso ord s < 32 andalso not (is_ascii_blank s);
wenzelm@34095
   171
wenzelm@50236
   172
fun is_ascii_letdig s = is_ascii_letter s orelse is_ascii_digit s orelse is_ascii_quasi s;
wenzelm@50236
   173
wenzelm@64275
   174
fun is_ascii_lower s = is_char s andalso (Char.ord #"a" <= ord s andalso ord s <= Char.ord #"z");
wenzelm@64275
   175
fun is_ascii_upper s = is_char s andalso (Char.ord #"A" <= ord s andalso ord s <= Char.ord #"Z");
wenzelm@20200
   176
wenzelm@64275
   177
fun to_ascii_lower s = if is_ascii_upper s then chr (ord s + Char.ord #"a" - Char.ord #"A") else s;
wenzelm@64275
   178
fun to_ascii_upper s = if is_ascii_lower s then chr (ord s + Char.ord #"A" - Char.ord #"a") else s;
wenzelm@20200
   179
wenzelm@50238
   180
fun is_ascii_identifier s =
wenzelm@50238
   181
  size s > 0 andalso is_ascii_letter (String.substring (s, 0, 1)) andalso
wenzelm@50238
   182
  forall_string is_ascii_letdig s;
wenzelm@50238
   183
wenzelm@50236
   184
val scan_ascii_id = Scan.one is_ascii_letter ^^ (Scan.many is_ascii_letdig >> implode);
wenzelm@50236
   185
wenzelm@14678
   186
wenzelm@14956
   187
(* diagnostics *)
wenzelm@14956
   188
wenzelm@14956
   189
fun beginning n cs =
wenzelm@14956
   190
  let
haftmann@33955
   191
    val drop_blanks = #1 o take_suffix is_ascii_blank;
wenzelm@14956
   192
    val all_cs = drop_blanks cs;
wenzelm@14956
   193
    val dots = if length all_cs > n then " ..." else "";
wenzelm@14956
   194
  in
haftmann@33955
   195
    (drop_blanks (take n all_cs)
wenzelm@14956
   196
      |> map (fn c => if is_ascii_blank c then space else c)
wenzelm@14956
   197
      |> implode) ^ dots
wenzelm@14956
   198
  end;
wenzelm@14956
   199
wenzelm@14956
   200
wenzelm@14873
   201
(* symbol variants *)
wenzelm@14873
   202
wenzelm@37533
   203
datatype sym =
wenzelm@63936
   204
  Char of string | UTF8 of string | Sym of string | Control of string | Malformed of string | EOF;
wenzelm@14873
   205
wenzelm@14873
   206
fun decode s =
wenzelm@43485
   207
  if s = "" then EOF
wenzelm@43485
   208
  else if is_char s then Char s
wenzelm@37533
   209
  else if is_utf8 s then UTF8 s
wenzelm@40523
   210
  else if is_malformed s then Malformed s
wenzelm@61475
   211
  else if is_control s then Control (String.substring (s, 3, size s - 4))
wenzelm@40523
   212
  else Sym (String.substring (s, 2, size s - 3));
wenzelm@14873
   213
wenzelm@14873
   214
wenzelm@14678
   215
(* standard symbol kinds *)
wenzelm@14678
   216
skalberg@14171
   217
local
wenzelm@50235
   218
  val letter_symbols =
wenzelm@50235
   219
    Symtab.make_set [
wenzelm@62529
   220
      "\<A>",
wenzelm@62529
   221
      "\<B>",
wenzelm@62529
   222
      "\<C>",
wenzelm@62529
   223
      "\<D>",
wenzelm@62529
   224
      "\<E>",
wenzelm@62529
   225
      "\<F>",
wenzelm@62529
   226
      "\<G>",
wenzelm@62529
   227
      "\<H>",
wenzelm@62529
   228
      "\<I>",
wenzelm@62529
   229
      "\<J>",
wenzelm@62529
   230
      "\<K>",
wenzelm@62529
   231
      "\<L>",
wenzelm@62529
   232
      "\<M>",
wenzelm@62529
   233
      "\<N>",
wenzelm@62529
   234
      "\<O>",
wenzelm@62529
   235
      "\<P>",
wenzelm@62529
   236
      "\<Q>",
wenzelm@62529
   237
      "\<R>",
wenzelm@62529
   238
      "\<S>",
wenzelm@62529
   239
      "\<T>",
wenzelm@62529
   240
      "\<U>",
wenzelm@62529
   241
      "\<V>",
wenzelm@62529
   242
      "\<W>",
wenzelm@62529
   243
      "\<X>",
wenzelm@62529
   244
      "\<Y>",
wenzelm@62529
   245
      "\<Z>",
wenzelm@62529
   246
      "\<a>",
wenzelm@62529
   247
      "\<b>",
wenzelm@62529
   248
      "\<c>",
wenzelm@62529
   249
      "\<d>",
wenzelm@62529
   250
      "\<e>",
wenzelm@62529
   251
      "\<f>",
wenzelm@62529
   252
      "\<g>",
wenzelm@62529
   253
      "\<h>",
wenzelm@62529
   254
      "\<i>",
wenzelm@62529
   255
      "\<j>",
wenzelm@62529
   256
      "\<k>",
wenzelm@62529
   257
      "\<l>",
wenzelm@62529
   258
      "\<m>",
wenzelm@62529
   259
      "\<n>",
wenzelm@62529
   260
      "\<o>",
wenzelm@62529
   261
      "\<p>",
wenzelm@62529
   262
      "\<q>",
wenzelm@62529
   263
      "\<r>",
wenzelm@62529
   264
      "\<s>",
wenzelm@62529
   265
      "\<t>",
wenzelm@62529
   266
      "\<u>",
wenzelm@62529
   267
      "\<v>",
wenzelm@62529
   268
      "\<w>",
wenzelm@62529
   269
      "\<x>",
wenzelm@62529
   270
      "\<y>",
wenzelm@62529
   271
      "\<z>",
wenzelm@62529
   272
      "\<AA>",
wenzelm@62529
   273
      "\<BB>",
wenzelm@62529
   274
      "\<CC>",
wenzelm@62529
   275
      "\<DD>",
wenzelm@62529
   276
      "\<EE>",
wenzelm@62529
   277
      "\<FF>",
wenzelm@62529
   278
      "\<GG>",
wenzelm@62529
   279
      "\<HH>",
wenzelm@62529
   280
      "\<II>",
wenzelm@62529
   281
      "\<JJ>",
wenzelm@62529
   282
      "\<KK>",
wenzelm@62529
   283
      "\<LL>",
wenzelm@62529
   284
      "\<MM>",
wenzelm@62529
   285
      "\<NN>",
wenzelm@62529
   286
      "\<OO>",
wenzelm@62529
   287
      "\<PP>",
wenzelm@62529
   288
      "\<QQ>",
wenzelm@62529
   289
      "\<RR>",
wenzelm@62529
   290
      "\<SS>",
wenzelm@62529
   291
      "\<TT>",
wenzelm@62529
   292
      "\<UU>",
wenzelm@62529
   293
      "\<VV>",
wenzelm@62529
   294
      "\<WW>",
wenzelm@62529
   295
      "\<XX>",
wenzelm@62529
   296
      "\<YY>",
wenzelm@62529
   297
      "\<ZZ>",
wenzelm@62529
   298
      "\<aa>",
wenzelm@62529
   299
      "\<bb>",
wenzelm@62529
   300
      "\<cc>",
wenzelm@62529
   301
      "\<dd>",
wenzelm@62529
   302
      "\<ee>",
wenzelm@62529
   303
      "\<ff>",
wenzelm@62529
   304
      "\<gg>",
wenzelm@62529
   305
      "\<hh>",
wenzelm@62529
   306
      "\<ii>",
wenzelm@62529
   307
      "\<jj>",
wenzelm@62529
   308
      "\<kk>",
wenzelm@62529
   309
      "\<ll>",
wenzelm@62529
   310
      "\<mm>",
wenzelm@62529
   311
      "\<nn>",
wenzelm@62529
   312
      "\<oo>",
wenzelm@62529
   313
      "\<pp>",
wenzelm@62529
   314
      "\<qq>",
wenzelm@62529
   315
      "\<rr>",
wenzelm@62529
   316
      "\<ss>",
wenzelm@62529
   317
      "\<tt>",
wenzelm@62529
   318
      "\<uu>",
wenzelm@62529
   319
      "\<vv>",
wenzelm@62529
   320
      "\<ww>",
wenzelm@62529
   321
      "\<xx>",
wenzelm@62529
   322
      "\<yy>",
wenzelm@62529
   323
      "\<zz>",
wenzelm@62529
   324
      "\<alpha>",
wenzelm@62529
   325
      "\<beta>",
wenzelm@62529
   326
      "\<gamma>",
wenzelm@62529
   327
      "\<delta>",
wenzelm@62529
   328
      "\<epsilon>",
wenzelm@62529
   329
      "\<zeta>",
wenzelm@62529
   330
      "\<eta>",
wenzelm@62529
   331
      "\<theta>",
wenzelm@62529
   332
      "\<iota>",
wenzelm@62529
   333
      "\<kappa>",
wenzelm@62529
   334
      (*"\<lambda>", sic!*)
wenzelm@62529
   335
      "\<mu>",
wenzelm@62529
   336
      "\<nu>",
wenzelm@62529
   337
      "\<xi>",
wenzelm@62529
   338
      "\<pi>",
wenzelm@62529
   339
      "\<rho>",
wenzelm@62529
   340
      "\<sigma>",
wenzelm@62529
   341
      "\<tau>",
wenzelm@62529
   342
      "\<upsilon>",
wenzelm@62529
   343
      "\<phi>",
wenzelm@62529
   344
      "\<chi>",
wenzelm@62529
   345
      "\<psi>",
wenzelm@62529
   346
      "\<omega>",
wenzelm@62529
   347
      "\<Gamma>",
wenzelm@62529
   348
      "\<Delta>",
wenzelm@62529
   349
      "\<Theta>",
wenzelm@62529
   350
      "\<Lambda>",
wenzelm@62529
   351
      "\<Xi>",
wenzelm@62529
   352
      "\<Pi>",
wenzelm@62529
   353
      "\<Sigma>",
wenzelm@62529
   354
      "\<Upsilon>",
wenzelm@62529
   355
      "\<Phi>",
wenzelm@62529
   356
      "\<Psi>",
wenzelm@62529
   357
      "\<Omega>"
wenzelm@50235
   358
    ];
skalberg@14171
   359
in
wenzelm@50242
   360
wenzelm@50242
   361
val is_letter_symbol = Symtab.defined letter_symbols;
wenzelm@50242
   362
wenzelm@14678
   363
end;
skalberg@14173
   364
wenzelm@50242
   365
datatype kind = Letter | Digit | Quasi | Blank | Other;
wenzelm@50242
   366
wenzelm@50242
   367
fun kind s =
wenzelm@50242
   368
  if is_ascii_letter s then Letter
wenzelm@50242
   369
  else if is_ascii_digit s then Digit
wenzelm@50242
   370
  else if is_ascii_quasi s then Quasi
wenzelm@50242
   371
  else if is_ascii_blank s then Blank
wenzelm@50242
   372
  else if is_char s then Other
wenzelm@50242
   373
  else if is_letter_symbol s then Letter
wenzelm@50242
   374
  else Other;
wenzelm@50242
   375
wenzelm@14678
   376
fun is_letter s = kind s = Letter;
wenzelm@14678
   377
fun is_digit s = kind s = Digit;
wenzelm@14678
   378
fun is_quasi s = kind s = Quasi;
wenzelm@14678
   379
fun is_blank s = kind s = Blank;
wenzelm@6272
   380
wenzelm@62529
   381
val is_block_ctrl = member (op =) ["\<^bsub>", "\<^esub>", "\<^bsup>", "\<^esup>"];
wenzelm@47850
   382
wenzelm@14678
   383
fun is_quasi_letter s = let val k = kind s in k = Letter orelse k = Quasi end;
wenzelm@14678
   384
fun is_letdig s = let val k = kind s in k = Letter orelse k = Digit orelse k = Quasi end;
wenzelm@11010
   385
wenzelm@6272
   386
wenzelm@50237
   387
(* escape *)
wenzelm@50237
   388
wenzelm@50237
   389
val esc = fn s =>
wenzelm@50237
   390
  if is_char s then s
wenzelm@50237
   391
  else if is_utf8 s then translate_string (fn c => "\\" ^ string_of_int (ord c)) s
wenzelm@50237
   392
  else "\\" ^ s;
wenzelm@50237
   393
wenzelm@63936
   394
val escape = implode o map esc o Symbol.explode;
wenzelm@50237
   395
wenzelm@50237
   396
wenzelm@50237
   397
wenzelm@50237
   398
(** scanning through symbols **)
wenzelm@50237
   399
wenzelm@50237
   400
(* scanner *)
wenzelm@50237
   401
wenzelm@50237
   402
fun scanner msg scan syms =
wenzelm@50237
   403
  let
wenzelm@50237
   404
    fun message (ss, NONE) = (fn () => msg ^ ": " ^ quote (beginning 10 ss))
wenzelm@50237
   405
      | message (ss, SOME msg') = (fn () => msg ^ ", " ^ msg' () ^ ": " ^ quote (beginning 10 ss));
wenzelm@50237
   406
    val finite_scan = Scan.error (Scan.finite stopper (!! message scan));
wenzelm@50237
   407
  in
wenzelm@50237
   408
    (case finite_scan syms of
wenzelm@50237
   409
      (result, []) => result
wenzelm@50237
   410
    | (_, rest) => error (message (rest, NONE) ()))
wenzelm@50237
   411
  end;
wenzelm@50237
   412
wenzelm@50237
   413
wenzelm@50162
   414
(* space-separated words *)
wenzelm@50162
   415
wenzelm@50162
   416
val scan_word =
wenzelm@50162
   417
  Scan.many1 is_ascii_blank >> K NONE ||
wenzelm@50162
   418
  Scan.many1 (fn s => not (is_ascii_blank s) andalso not_eof s) >> (SOME o implode);
wenzelm@50162
   419
wenzelm@50162
   420
val split_words = scanner "Bad text" (Scan.repeat scan_word >> map_filter I);
wenzelm@50162
   421
wenzelm@63936
   422
val explode_words = split_words o Symbol.explode;
wenzelm@50162
   423
wenzelm@50162
   424
wenzelm@14678
   425
(* blanks *)
wenzelm@14678
   426
wenzelm@63936
   427
val trim_blanks = Symbol.explode #> trim is_blank #> implode;
wenzelm@14678
   428
wenzelm@14678
   429
wenzelm@14678
   430
(* bump string -- treat as base 26 or base 1 numbers *)
wenzelm@14678
   431
wenzelm@62529
   432
fun symbolic_end (_ :: "\<^sub>" :: _) = true
wenzelm@53198
   433
  | symbolic_end ("'" :: ss) = symbolic_end ss
wenzelm@55033
   434
  | symbolic_end (s :: _) = raw_symbolic s
wenzelm@14908
   435
  | symbolic_end [] = false;
wenzelm@14678
   436
wenzelm@14678
   437
fun bump_init str =
wenzelm@63936
   438
  if symbolic_end (rev (Symbol.explode str)) then str ^ "'"
wenzelm@14678
   439
  else str ^ "a";
wenzelm@12904
   440
wenzelm@12904
   441
fun bump_string str =
wenzelm@12904
   442
  let
wenzelm@12904
   443
    fun bump [] = ["a"]
wenzelm@12904
   444
      | bump ("z" :: ss) = "a" :: bump ss
wenzelm@12904
   445
      | bump (s :: ss) =
wenzelm@64275
   446
          if is_char s andalso Char.ord #"a" <= ord s andalso ord s < Char.ord #"z"
wenzelm@12904
   447
          then chr (ord s + 1) :: ss
wenzelm@12904
   448
          else "a" :: s :: ss;
wenzelm@14678
   449
wenzelm@63936
   450
    val (ss, qs) = apfst rev (take_suffix is_quasi (Symbol.explode str));
wenzelm@14908
   451
    val ss' = if symbolic_end ss then "'" :: ss else bump ss;
wenzelm@14678
   452
  in implode (rev ss' @ qs) end;
wenzelm@14678
   453
wenzelm@12904
   454
wenzelm@6272
   455
wenzelm@29324
   456
(** symbol output **)
wenzelm@14977
   457
wenzelm@29324
   458
(* length *)
wenzelm@6272
   459
wenzelm@14678
   460
fun sym_len s =
wenzelm@24593
   461
  if not (is_printable s) then (0: int)
wenzelm@62877
   462
  else if String.isPrefix "\092<long" s then 2
wenzelm@62877
   463
  else if String.isPrefix "\092<Long" s then 2
wenzelm@14678
   464
  else 1;
wenzelm@14678
   465
wenzelm@19473
   466
fun sym_length ss = fold (fn s => fn n => sym_len s + n) ss 0;
wenzelm@14678
   467
wenzelm@63936
   468
fun output s = (s, sym_length (Symbol.explode s));
wenzelm@29324
   469
wenzelm@29324
   470
wenzelm@6116
   471
(*final declarations of this structure!*)
wenzelm@63936
   472
val explode = Symbol.explode;
wenzelm@6272
   473
val length = sym_length;
wenzelm@6116
   474
wenzelm@6116
   475
end;