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