src/Pure/General/symbol.ML
author wenzelm
Fri, 23 May 2008 21:18:47 +0200
changeset 26977 e736139b553d
parent 26632 90c0b075c0d3
child 27732 8dbf5761a24a
permissions -rw-r--r--
added theory_nameN;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6118
caa439435666 fixed titles;
wenzelm
parents: 6116
diff changeset
     1
(*  Title:      Pure/General/symbol.ML
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     4
21897
d0c67d715deb fixed typo in comment
paulson
parents: 21858
diff changeset
     5
Generalized characters with infinitely many named symbols.
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     6
*)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     7
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     8
signature SYMBOL =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     9
sig
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    10
  type symbol
26524
63953bec4c98 added some ASCII control symbols;
wenzelm
parents: 25644
diff changeset
    11
  val SOH: symbol
63953bec4c98 added some ASCII control symbols;
wenzelm
parents: 25644
diff changeset
    12
  val STX: symbol
26538
d65504ffb47d replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents: 26524
diff changeset
    13
  val ENQ: symbol
d65504ffb47d replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents: 26524
diff changeset
    14
  val ACK: symbol
26524
63953bec4c98 added some ASCII control symbols;
wenzelm
parents: 25644
diff changeset
    15
  val DEL: symbol
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    16
  val space: symbol
23618
32ee8cac5c02 simplified output mode setup;
wenzelm
parents: 21897
diff changeset
    17
  val spaces: int -> string
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    18
  val is_char: symbol -> bool
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    19
  val is_symbolic: symbol -> bool
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    20
  val is_printable: symbol -> bool
26632
90c0b075c0d3 added is_utf8_trailer;
wenzelm
parents: 26538
diff changeset
    21
  val is_utf8_trailer: symbol -> bool
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    22
  val eof: symbol
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    23
  val is_eof: symbol -> bool
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    24
  val stopper: symbol * (symbol -> bool)
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    25
  val sync: symbol
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    26
  val is_sync: symbol -> bool
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    27
  val malformed: symbol
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
    28
  val end_malformed: symbol
25641
615aecd485ad added separate_chars;
wenzelm
parents: 25521
diff changeset
    29
  val separate_chars: string -> string
23784
75e6b9dd5336 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 23728
diff changeset
    30
  val is_regular: symbol -> bool
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    31
  val is_ascii: symbol -> bool
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    32
  val is_ascii_letter: symbol -> bool
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    33
  val is_ascii_digit: symbol -> bool
24580
916259859344 replaced Symbol.is_hex_letter to Symbol.is_ascii_hex;
wenzelm
parents: 23784
diff changeset
    34
  val is_ascii_hex: symbol -> bool
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    35
  val is_ascii_quasi: symbol -> bool
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    36
  val is_ascii_blank: symbol -> bool
20200
c6fb50dbbc30 added is/to_ascii_lower/upper;
wenzelm
parents: 20192
diff changeset
    37
  val is_ascii_lower: symbol -> bool
c6fb50dbbc30 added is/to_ascii_lower/upper;
wenzelm
parents: 20192
diff changeset
    38
  val is_ascii_upper: symbol -> bool
c6fb50dbbc30 added is/to_ascii_lower/upper;
wenzelm
parents: 20192
diff changeset
    39
  val to_ascii_lower: symbol -> symbol
c6fb50dbbc30 added is/to_ascii_lower/upper;
wenzelm
parents: 20192
diff changeset
    40
  val to_ascii_upper: symbol -> symbol
14834
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
    41
  val is_raw: symbol -> bool
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
    42
  val decode_raw: symbol -> string
14977
77d88064991a added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents: 14961
diff changeset
    43
  val encode_raw: string -> string
14873
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
    44
  datatype sym = Char of string | Sym of string | Ctrl of string | Raw of string
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
    45
  val decode: symbol -> sym
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    46
  datatype kind = Letter | Digit | Quasi | Blank | Other
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    47
  val kind: symbol -> kind
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    48
  val is_letter: symbol -> bool
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    49
  val is_digit: symbol -> bool
12904
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
    50
  val is_quasi: symbol -> bool
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    51
  val is_blank: symbol -> bool
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    52
  val is_quasi_letter: symbol -> bool
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    53
  val is_letdig: symbol -> bool
16138
727c5e32930e added is_ident (from Syntax/lexicon.ML);
wenzelm
parents: 16002
diff changeset
    54
  val is_ident: symbol list -> bool
14728
wenzelm
parents: 14678
diff changeset
    55
  val beginning: int -> symbol list -> string
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    56
  val scanner: string -> (string list -> 'a * string list) -> symbol list -> 'a
13730
09aeb7346d3f default_output now escapes \'s more carefully.
berghofe
parents: 13559
diff changeset
    57
  val scan_id: string list -> string * string list
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    58
  val source: bool -> (string, 'a) Source.source ->
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    59
    (symbol, (string, 'a) Source.source) Source.source
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    60
  val explode: string -> symbol list
14977
77d88064991a added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents: 14961
diff changeset
    61
  val escape: string -> string
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    62
  val strip_blanks: string -> string
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    63
  val bump_init: string -> string
12904
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
    64
  val bump_string: string -> string
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    65
  val length: symbol list -> int
6692
05c56f41e661 avoid string constants;
wenzelm
parents: 6640
diff changeset
    66
  val xsymbolsN: string
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    67
end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    68
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    69
structure Symbol: SYMBOL =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    70
struct
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    71
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    72
(** type symbol **)
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    73
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    74
(*Symbols, which are considered the smallest entities of any Isabelle
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    75
  string, may be of the following form:
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    76
14834
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
    77
    (1) ASCII symbols: a
17823
4735c07399c8 raw symbols: allow non-ASCII chars (e.g. UTF-8);
wenzelm
parents: 17756
diff changeset
    78
    (2) regular symbols: \<ident>
14834
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
    79
    (3) control symbols: \<^ident>
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
    80
    (4) raw control symbols: \<^raw:...>, where "..." may be any printable
20205
7b2958d3d575 raw symbols: disallow dot to avoid confusion in NameSpace.unpack;
wenzelm
parents: 20200
diff changeset
    81
        character (excluding ".", ">"), or \<^raw000>
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    82
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    83
  Output is subject to the print_mode variable (default: verbatim),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
    84
  actual interpretation in display is up to front-end tools.
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    85
*)
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    86
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    87
type symbol = string;
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    88
26524
63953bec4c98 added some ASCII control symbols;
wenzelm
parents: 25644
diff changeset
    89
val SOH = chr 1;
63953bec4c98 added some ASCII control symbols;
wenzelm
parents: 25644
diff changeset
    90
val STX = chr 2;
26538
d65504ffb47d replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents: 26524
diff changeset
    91
val ENQ = chr 5;
d65504ffb47d replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents: 26524
diff changeset
    92
val ACK = chr 6;
26524
63953bec4c98 added some ASCII control symbols;
wenzelm
parents: 25644
diff changeset
    93
val DEL = chr 127;
63953bec4c98 added some ASCII control symbols;
wenzelm
parents: 25644
diff changeset
    94
63953bec4c98 added some ASCII control symbols;
wenzelm
parents: 25644
diff changeset
    95
val space = chr 32;
17063
502105583951 tuned Symbol.spaces;
wenzelm
parents: 16138
diff changeset
    96
502105583951 tuned Symbol.spaces;
wenzelm
parents: 16138
diff changeset
    97
local
502105583951 tuned Symbol.spaces;
wenzelm
parents: 16138
diff changeset
    98
  val small_spaces = Vector.tabulate (65, fn i => Library.replicate_string i space);
502105583951 tuned Symbol.spaces;
wenzelm
parents: 16138
diff changeset
    99
in
502105583951 tuned Symbol.spaces;
wenzelm
parents: 16138
diff changeset
   100
  fun spaces k =
502105583951 tuned Symbol.spaces;
wenzelm
parents: 16138
diff changeset
   101
    if k < 64 then Vector.sub (small_spaces, k)
502105583951 tuned Symbol.spaces;
wenzelm
parents: 16138
diff changeset
   102
    else Library.replicate_string (k div 64) (Vector.sub (small_spaces, 64)) ^
502105583951 tuned Symbol.spaces;
wenzelm
parents: 16138
diff changeset
   103
      Vector.sub (small_spaces, k mod 64);
502105583951 tuned Symbol.spaces;
wenzelm
parents: 16138
diff changeset
   104
end;
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   105
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   106
fun is_char s = size s = 1;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   107
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   108
fun is_symbolic s =
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   109
  String.isPrefix "\\<" s andalso not (String.isPrefix "\\<^" s);
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   110
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   111
fun is_printable s =
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   112
  if is_char s then ord space <= ord s andalso ord s <= ord "~"
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   113
  else not (String.isPrefix "\\<^" s);
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   114
26632
90c0b075c0d3 added is_utf8_trailer;
wenzelm
parents: 26538
diff changeset
   115
fun is_utf8_trailer s = is_char s andalso 128 <= ord s andalso ord s < 192;
90c0b075c0d3 added is_utf8_trailer;
wenzelm
parents: 26538
diff changeset
   116
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   117
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   118
(* input source control *)
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   119
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   120
val eof = "";
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   121
fun is_eof s = s = eof;
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   122
fun not_eof s = s <> eof;
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   123
val stopper = (eof, is_eof);
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   124
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   125
val sync = "\\<^sync>";
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   126
fun is_sync s = s = sync;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   127
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   128
val malformed = "[[";
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   129
val end_malformed = "]]";
25641
615aecd485ad added separate_chars;
wenzelm
parents: 25521
diff changeset
   130
615aecd485ad added separate_chars;
wenzelm
parents: 25521
diff changeset
   131
val separate_chars = explode #> space_implode space;
615aecd485ad added separate_chars;
wenzelm
parents: 25521
diff changeset
   132
fun malformed_msg s = "Malformed symbolic character: " ^ quote (separate_chars s);
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   133
23784
75e6b9dd5336 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 23728
diff changeset
   134
fun is_regular s =
75e6b9dd5336 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 23728
diff changeset
   135
  not_eof s andalso s <> sync andalso s <> malformed andalso s <> end_malformed;
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   136
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   137
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   138
(* ascii symbols *)
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   139
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   140
fun is_ascii s = is_char s andalso ord s < 128;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   141
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   142
fun is_ascii_letter s =
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   143
  is_char s andalso
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   144
   (ord "A" <= ord s andalso ord s <= ord "Z" orelse
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   145
    ord "a" <= ord s andalso ord s <= ord "z");
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   146
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   147
fun is_ascii_digit s =
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   148
  is_char s andalso ord "0" <= ord s andalso ord s <= ord "9";
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   149
24580
916259859344 replaced Symbol.is_hex_letter to Symbol.is_ascii_hex;
wenzelm
parents: 23784
diff changeset
   150
fun is_ascii_hex s =
916259859344 replaced Symbol.is_hex_letter to Symbol.is_ascii_hex;
wenzelm
parents: 23784
diff changeset
   151
  is_char s andalso
916259859344 replaced Symbol.is_hex_letter to Symbol.is_ascii_hex;
wenzelm
parents: 23784
diff changeset
   152
   (ord "0" <= ord s andalso ord s <= ord "9" orelse
916259859344 replaced Symbol.is_hex_letter to Symbol.is_ascii_hex;
wenzelm
parents: 23784
diff changeset
   153
    ord "A" <= ord s andalso ord s <= ord "F" orelse
916259859344 replaced Symbol.is_hex_letter to Symbol.is_ascii_hex;
wenzelm
parents: 23784
diff changeset
   154
    ord "a" <= ord s andalso ord s <= ord "f");
916259859344 replaced Symbol.is_hex_letter to Symbol.is_ascii_hex;
wenzelm
parents: 23784
diff changeset
   155
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   156
fun is_ascii_quasi "_" = true
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   157
  | is_ascii_quasi "'" = true
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   158
  | is_ascii_quasi _ = false;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   159
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   160
val is_ascii_blank =
24580
916259859344 replaced Symbol.is_hex_letter to Symbol.is_ascii_hex;
wenzelm
parents: 23784
diff changeset
   161
  fn " " => true | "\t" => true | "\n" => true | "\^K" => true | "\^L" => true | "\^M" => true
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   162
    | _ => false;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   163
20200
c6fb50dbbc30 added is/to_ascii_lower/upper;
wenzelm
parents: 20192
diff changeset
   164
fun is_ascii_lower s = is_char s andalso (ord "a" <= ord s andalso ord s <= ord "z");
c6fb50dbbc30 added is/to_ascii_lower/upper;
wenzelm
parents: 20192
diff changeset
   165
fun is_ascii_upper s = is_char s andalso (ord "A" <= ord s andalso ord s <= ord "Z");
c6fb50dbbc30 added is/to_ascii_lower/upper;
wenzelm
parents: 20192
diff changeset
   166
c6fb50dbbc30 added is/to_ascii_lower/upper;
wenzelm
parents: 20192
diff changeset
   167
fun to_ascii_lower s = if is_ascii_upper s then chr (ord s + ord "a" - ord "A") else s;
c6fb50dbbc30 added is/to_ascii_lower/upper;
wenzelm
parents: 20192
diff changeset
   168
fun to_ascii_upper s = if is_ascii_lower s then chr (ord s + ord "A" - ord "a") else s;
c6fb50dbbc30 added is/to_ascii_lower/upper;
wenzelm
parents: 20192
diff changeset
   169
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   170
14956
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   171
(* encode_raw *)
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   172
20205
7b2958d3d575 raw symbols: disallow dot to avoid confusion in NameSpace.unpack;
wenzelm
parents: 20200
diff changeset
   173
fun raw_chr c =
7b2958d3d575 raw symbols: disallow dot to avoid confusion in NameSpace.unpack;
wenzelm
parents: 20200
diff changeset
   174
  ord space <= ord c andalso ord c <= ord "~" andalso c <> "." andalso c <> ">"
17823
4735c07399c8 raw symbols: allow non-ASCII chars (e.g. UTF-8);
wenzelm
parents: 17756
diff changeset
   175
  orelse ord c >= 128;
14956
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   176
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   177
fun encode_raw str =
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   178
  let
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   179
    val raw0 = enclose "\\<^raw:" ">";
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   180
    val raw1 = raw0 o implode;
14956
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   181
    val raw2 = enclose "\\<^raw" ">" o string_of_int o ord;
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   182
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   183
    fun encode cs = enc (Library.take_prefix raw_chr cs)
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   184
    and enc ([], []) = []
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   185
      | enc (cs, []) = [raw1 cs]
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   186
      | enc ([], d :: ds) = raw2 d :: encode ds
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   187
      | enc (cs, d :: ds) = raw1 cs :: raw2 d :: encode ds;
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   188
  in
14977
77d88064991a added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents: 14961
diff changeset
   189
    if exists_string (not o raw_chr) str then implode (encode (explode str))
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   190
    else raw0 str
14956
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   191
  end;
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   192
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   193
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   194
(* diagnostics *)
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   195
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   196
fun beginning n cs =
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   197
  let
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   198
    val drop_blanks = #1 o Library.take_suffix is_ascii_blank;
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   199
    val all_cs = drop_blanks cs;
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   200
    val dots = if length all_cs > n then " ..." else "";
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   201
  in
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   202
    (drop_blanks (Library.take (n, all_cs))
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   203
      |> map (fn c => if is_ascii_blank c then space else c)
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   204
      |> implode) ^ dots
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   205
  end;
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   206
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   207
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   208
(* decode_raw *)
14834
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
   209
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
   210
fun is_raw s =
17063
502105583951 tuned Symbol.spaces;
wenzelm
parents: 16138
diff changeset
   211
  String.isPrefix "\\<^raw" s andalso String.isSuffix ">" s;
14834
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
   212
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
   213
fun decode_raw s =
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   214
  if not (is_raw s) then error (malformed_msg s)
14834
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
   215
  else if String.isPrefix "\\<^raw:" s then String.substring (s, 7, size s - 8)
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
   216
  else chr (#1 (Library.read_int (explode (String.substring (s, 6, size s - 7)))));
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
   217
e47744683560 improved support for raw symbols;
wenzelm
parents: 14728
diff changeset
   218
14873
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
   219
(* symbol variants *)
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
   220
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
   221
datatype sym = Char of string | Sym of string | Ctrl of string | Raw of string;
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
   222
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
   223
fun decode s =
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
   224
  if is_char s then Char s
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
   225
  else if is_raw s then Raw (decode_raw s)
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
   226
  else if String.isPrefix "\\<^" s then Ctrl (String.substring (s, 3, size s - 4))
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
   227
  else if String.isPrefix "\\<" s then Sym (String.substring (s, 2, size s - 3))
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   228
  else error (malformed_msg s);
14873
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
   229
7efc14398e82 added datatype sym, val decode: symbol -> sym;
wenzelm
parents: 14834
diff changeset
   230
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   231
(* standard symbol kinds *)
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   232
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   233
datatype kind = Letter | Digit | Quasi | Blank | Other;
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   234
14171
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   235
local
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   236
  val symbol_kinds = Symtab.make
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   237
   [("\\<A>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   238
    ("\\<B>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   239
    ("\\<C>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   240
    ("\\<D>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   241
    ("\\<E>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   242
    ("\\<F>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   243
    ("\\<G>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   244
    ("\\<H>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   245
    ("\\<I>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   246
    ("\\<J>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   247
    ("\\<K>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   248
    ("\\<L>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   249
    ("\\<M>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   250
    ("\\<N>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   251
    ("\\<O>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   252
    ("\\<P>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   253
    ("\\<Q>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   254
    ("\\<R>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   255
    ("\\<S>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   256
    ("\\<T>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   257
    ("\\<U>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   258
    ("\\<V>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   259
    ("\\<W>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   260
    ("\\<X>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   261
    ("\\<Y>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   262
    ("\\<Z>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   263
    ("\\<a>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   264
    ("\\<b>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   265
    ("\\<c>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   266
    ("\\<d>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   267
    ("\\<e>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   268
    ("\\<f>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   269
    ("\\<g>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   270
    ("\\<h>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   271
    ("\\<i>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   272
    ("\\<j>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   273
    ("\\<k>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   274
    ("\\<l>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   275
    ("\\<m>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   276
    ("\\<n>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   277
    ("\\<o>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   278
    ("\\<p>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   279
    ("\\<q>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   280
    ("\\<r>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   281
    ("\\<s>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   282
    ("\\<t>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   283
    ("\\<u>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   284
    ("\\<v>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   285
    ("\\<w>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   286
    ("\\<x>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   287
    ("\\<y>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   288
    ("\\<z>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   289
    ("\\<AA>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   290
    ("\\<BB>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   291
    ("\\<CC>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   292
    ("\\<DD>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   293
    ("\\<EE>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   294
    ("\\<FF>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   295
    ("\\<GG>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   296
    ("\\<HH>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   297
    ("\\<II>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   298
    ("\\<JJ>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   299
    ("\\<KK>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   300
    ("\\<LL>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   301
    ("\\<MM>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   302
    ("\\<NN>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   303
    ("\\<OO>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   304
    ("\\<PP>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   305
    ("\\<QQ>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   306
    ("\\<RR>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   307
    ("\\<SS>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   308
    ("\\<TT>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   309
    ("\\<UU>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   310
    ("\\<VV>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   311
    ("\\<WW>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   312
    ("\\<XX>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   313
    ("\\<YY>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   314
    ("\\<ZZ>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   315
    ("\\<aa>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   316
    ("\\<bb>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   317
    ("\\<cc>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   318
    ("\\<dd>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   319
    ("\\<ee>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   320
    ("\\<ff>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   321
    ("\\<gg>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   322
    ("\\<hh>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   323
    ("\\<ii>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   324
    ("\\<jj>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   325
    ("\\<kk>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   326
    ("\\<ll>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   327
    ("\\<mm>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   328
    ("\\<nn>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   329
    ("\\<oo>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   330
    ("\\<pp>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   331
    ("\\<qq>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   332
    ("\\<rr>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   333
    ("\\<ss>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   334
    ("\\<tt>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   335
    ("\\<uu>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   336
    ("\\<vv>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   337
    ("\\<ww>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   338
    ("\\<xx>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   339
    ("\\<yy>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   340
    ("\\<zz>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   341
    ("\\<alpha>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   342
    ("\\<beta>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   343
    ("\\<gamma>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   344
    ("\\<delta>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   345
    ("\\<epsilon>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   346
    ("\\<zeta>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   347
    ("\\<eta>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   348
    ("\\<theta>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   349
    ("\\<iota>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   350
    ("\\<kappa>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   351
    ("\\<lambda>", Other),      (*sic!*)
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   352
    ("\\<mu>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   353
    ("\\<nu>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   354
    ("\\<xi>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   355
    ("\\<pi>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   356
    ("\\<rho>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   357
    ("\\<sigma>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   358
    ("\\<tau>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   359
    ("\\<upsilon>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   360
    ("\\<phi>", Letter),
25521
6cebd2ff3ab7 symbol chi is also a letter;
wenzelm
parents: 24593
diff changeset
   361
    ("\\<chi>", Letter),
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   362
    ("\\<psi>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   363
    ("\\<omega>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   364
    ("\\<Gamma>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   365
    ("\\<Delta>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   366
    ("\\<Theta>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   367
    ("\\<Lambda>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   368
    ("\\<Xi>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   369
    ("\\<Pi>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   370
    ("\\<Sigma>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   371
    ("\\<Upsilon>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   372
    ("\\<Phi>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   373
    ("\\<Psi>", Letter),
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   374
    ("\\<Omega>", Letter),
14961
8092a0319927 isub/isup quasi letter (again); tuned;
wenzelm
parents: 14956
diff changeset
   375
    ("\\<^isub>", Letter),
8092a0319927 isub/isup quasi letter (again); tuned;
wenzelm
parents: 14956
diff changeset
   376
    ("\\<^isup>", Letter),
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   377
    ("\\<spacespace>", Blank)];
14171
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   378
in
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   379
  fun kind s =
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   380
    if is_ascii_letter s then Letter
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   381
    else if is_ascii_digit s then Digit
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   382
    else if is_ascii_quasi s then Quasi
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   383
    else if is_ascii_blank s then Blank
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   384
    else if is_char s then Other
18939
wenzelm
parents: 17823
diff changeset
   385
    else the_default Other (Symtab.lookup symbol_kinds s);
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   386
end;
14173
a3690aeb79d4 Removed the extended digits again.
skalberg
parents: 14171
diff changeset
   387
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   388
fun is_letter s = kind s = Letter;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   389
fun is_digit s = kind s = Digit;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   390
fun is_quasi s = kind s = Quasi;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   391
fun is_blank s = kind s = Blank;
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   392
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   393
fun is_quasi_letter s = let val k = kind s in k = Letter orelse k = Quasi end;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   394
fun is_letdig s = let val k = kind s in k = Letter orelse k = Digit orelse k = Quasi end;
11010
2c6559297be3 added strip_blanks;
wenzelm
parents: 10953
diff changeset
   395
16138
727c5e32930e added is_ident (from Syntax/lexicon.ML);
wenzelm
parents: 16002
diff changeset
   396
fun is_ident [] = false
727c5e32930e added is_ident (from Syntax/lexicon.ML);
wenzelm
parents: 16002
diff changeset
   397
  | is_ident (c :: cs) = is_letter c andalso forall is_letdig cs;
727c5e32930e added is_ident (from Syntax/lexicon.ML);
wenzelm
parents: 16002
diff changeset
   398
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   399
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   400
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   401
(** symbol input **)
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   402
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   403
(* scanning through symbols *)
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   404
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   405
fun scanner msg scan chs =
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   406
  let
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14994
diff changeset
   407
    fun message (cs, NONE) = msg ^ ": " ^ quote (beginning 10 cs)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14994
diff changeset
   408
      | message (cs, SOME msg') = msg ^ ", " ^ msg' ^ ": " ^ quote (beginning 10 cs);
14961
8092a0319927 isub/isup quasi letter (again); tuned;
wenzelm
parents: 14956
diff changeset
   409
    val fin_scan = Scan.error (Scan.finite stopper (!! message scan));
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   410
  in
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   411
    (case fin_scan chs of
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   412
      (result, []) => result
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14994
diff changeset
   413
    | (_, rest) => error (message (rest, NONE)))
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   414
  end;
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   415
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21495
diff changeset
   416
val scan_id = Scan.one is_letter ^^ (Scan.many is_letdig >> implode);
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   417
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   418
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   419
(* source *)
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   420
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   421
local
14561
c53396af770e * raw control symbols are of the form \<^raw:...> now.
schirmer
parents: 14559
diff changeset
   422
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   423
fun is_plain s = s <> "\^M" andalso s <> "\\" andalso not_eof s;
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   424
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   425
val scan_encoded_newline =
17756
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17412
diff changeset
   426
  $$ "\^M" -- $$ "\n" >> K "\n" ||
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17412
diff changeset
   427
  $$ "\^M" >> K "\n" ||
14956
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   428
  $$ "\\" -- Scan.optional ($$ "\\") "" -- Scan.this_string "<^newline>" >> K "\n";
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   429
70ec4b8aef8d prevent looping of error messages involving malformed symbols;
wenzelm
parents: 14908
diff changeset
   430
val scan_raw =
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21495
diff changeset
   431
  Scan.this_string "raw:" ^^ (Scan.many raw_chr >> implode) ||
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21495
diff changeset
   432
  Scan.this_string "raw" ^^ (Scan.many1 is_ascii_digit >> implode);
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   433
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   434
val scan =
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   435
  Scan.one is_plain ||
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   436
  scan_encoded_newline ||
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   437
  (($$ "\\" --| Scan.optional ($$ "\\") "") ^^ $$ "<" ^^
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   438
    !! (fn (cs, _) => malformed_msg (beginning 10 ("\\" :: "<" :: cs)))
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   439
      (($$ "^" ^^ (scan_raw || scan_id) || scan_id) ^^ $$ ">")) ||
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   440
  Scan.one not_eof;
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   441
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   442
val recover =
25644
d30391cdd9d9 recover: not skip over "`";
wenzelm
parents: 25641
diff changeset
   443
  Scan.many (fn s => not (is_blank s) andalso s <> "\"" andalso s <> "`" andalso not_eof s)
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   444
    >> (fn ss => malformed :: ss @ [end_malformed]);
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   445
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   446
in
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   447
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   448
fun source do_recover src =
23682
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23676
diff changeset
   449
  Source.source stopper (Scan.bulk scan)
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23676
diff changeset
   450
    (if do_recover then SOME (false, K recover) else NONE) src;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   451
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   452
end;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   453
14562
980da32f4617 proper handling of lines terminated by CRLF or CR;
wenzelm
parents: 14561
diff changeset
   454
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   455
(* explode *)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   456
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   457
local
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   458
14562
980da32f4617 proper handling of lines terminated by CRLF or CR;
wenzelm
parents: 14561
diff changeset
   459
fun no_explode [] = true
980da32f4617 proper handling of lines terminated by CRLF or CR;
wenzelm
parents: 14561
diff changeset
   460
  | no_explode ("\\" :: "<" :: _) = false
17756
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17412
diff changeset
   461
  | no_explode ("\^M" :: _) = false
14562
980da32f4617 proper handling of lines terminated by CRLF or CR;
wenzelm
parents: 14561
diff changeset
   462
  | no_explode (_ :: cs) = no_explode cs;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   463
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   464
in
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   465
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   466
fun sym_explode str =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   467
  let val chs = explode str in
14562
980da32f4617 proper handling of lines terminated by CRLF or CR;
wenzelm
parents: 14561
diff changeset
   468
    if no_explode chs then chs
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   469
    else Source.exhaust (source false (Source.of_list chs))
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   470
  end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   471
23676
ea9b7e9c2301 scan: changed treatment of malformed symbols, passed to next stage;
wenzelm
parents: 23618
diff changeset
   472
end;
14994
7d8501843146 added chars_only, symbol_output;
wenzelm
parents: 14981
diff changeset
   473
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   474
14977
77d88064991a added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents: 14961
diff changeset
   475
(* escape *)
77d88064991a added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents: 14961
diff changeset
   476
77d88064991a added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents: 14961
diff changeset
   477
val escape = implode o map (fn s => if is_char s then s else "\\" ^ s) o sym_explode;
77d88064991a added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents: 14961
diff changeset
   478
77d88064991a added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents: 14961
diff changeset
   479
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   480
(* blanks *)
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   481
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   482
fun strip_blanks s =
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   483
  sym_explode s
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   484
  |> Library.take_prefix is_blank |> #2
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   485
  |> Library.take_suffix is_blank |> #1
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   486
  |> implode;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   487
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   488
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   489
(* bump string -- treat as base 26 or base 1 numbers *)
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   490
15979
wenzelm
parents: 15570
diff changeset
   491
fun symbolic_end (_ :: "\\<^isub>" :: _) = true
wenzelm
parents: 15570
diff changeset
   492
  | symbolic_end (_ :: "\\<^isup>" :: _) = true
14908
wenzelm
parents: 14873
diff changeset
   493
  | symbolic_end (s :: _) = is_symbolic s
wenzelm
parents: 14873
diff changeset
   494
  | symbolic_end [] = false;
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   495
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   496
fun bump_init str =
14908
wenzelm
parents: 14873
diff changeset
   497
  if symbolic_end (rev (sym_explode str)) then str ^ "'"
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   498
  else str ^ "a";
12904
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   499
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   500
fun bump_string str =
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   501
  let
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   502
    fun bump [] = ["a"]
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   503
      | bump ("z" :: ss) = "a" :: bump ss
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   504
      | bump (s :: ss) =
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   505
          if is_char s andalso ord "a" <= ord s andalso ord s < ord "z"
12904
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   506
          then chr (ord s + 1) :: ss
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   507
          else "a" :: s :: ss;
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   508
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   509
    val (ss, qs) = apfst rev (Library.take_suffix is_quasi (sym_explode str));
14908
wenzelm
parents: 14873
diff changeset
   510
    val ss' = if symbolic_end ss then "'" :: ss else bump ss;
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   511
  in implode (rev ss' @ qs) end;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   512
12904
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   513
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   514
23618
32ee8cac5c02 simplified output mode setup;
wenzelm
parents: 21897
diff changeset
   515
(** xsymbols **)
14977
77d88064991a added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents: 14961
diff changeset
   516
77d88064991a added escape, export encode_raw, default mode now trivial, tuned;
wenzelm
parents: 14961
diff changeset
   517
val xsymbolsN = "xsymbols";
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   518
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   519
fun sym_len s =
24593
1547ea587f5a added some int constraints (ML_Parse.fix_ints not active here);
wenzelm
parents: 24580
diff changeset
   520
  if not (is_printable s) then (0: int)
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   521
  else if String.isPrefix "\\<long" s then 2
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   522
  else if String.isPrefix "\\<Long" s then 2
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   523
  else if s = "\\<spacespace>" then 2
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   524
  else 1;
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   525
19473
wenzelm
parents: 19305
diff changeset
   526
fun sym_length ss = fold (fn s => fn n => sym_len s + n) ss 0;
14678
662b181cae05 clarification of ascii vs. symbolic entities;
wenzelm
parents: 14632
diff changeset
   527
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   528
(*final declarations of this structure!*)
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   529
val length = sym_length;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   530
val explode = sym_explode;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   531
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   532
end;