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