src/Pure/General/symbol.ML
author skalberg
Thu, 28 Aug 2003 01:56:40 +0200
changeset 14171 0cab06e3bbd0
parent 13730 09aeb7346d3f
child 14173 a3690aeb79d4
permissions -rw-r--r--
Extended the notion of letter and digit, such that now one may use greek, gothic, euler, or calligraphic letters as normal letters.
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
8806
wenzelm
parents: 8663
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     5
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 11010
diff changeset
     6
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
     7
*)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     8
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     9
signature SYMBOL =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    10
sig
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    11
  type symbol
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    12
  val space: symbol
10953
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
    13
  val spaces: int -> symbol
6857
6e6eb8d92377 added sync marker;
wenzelm
parents: 6692
diff changeset
    14
  val sync: symbol
6e6eb8d92377 added sync marker;
wenzelm
parents: 6692
diff changeset
    15
  val is_sync: symbol -> bool
6e6eb8d92377 added sync marker;
wenzelm
parents: 6692
diff changeset
    16
  val not_sync: symbol -> bool
10747
794cd4d768b5 scan: malformed result;
wenzelm
parents: 10738
diff changeset
    17
  val malformed: symbol
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    18
  val eof: symbol
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    19
  val is_eof: symbol -> bool
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    20
  val not_eof: symbol -> bool
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    21
  val stopper: symbol * (symbol -> bool)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    22
  val is_ascii: symbol -> bool
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    23
  val is_letter: symbol -> bool
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    24
  val is_digit: symbol -> bool
12904
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
    25
  val is_quasi: symbol -> bool
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    26
  val is_quasi_letter: symbol -> bool
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    27
  val is_letdig: symbol -> bool
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    28
  val is_blank: symbol -> bool
13559
51c3ac47d127 added checking so that (rename_tac "x y") is rejected, since
paulson
parents: 12904
diff changeset
    29
  val is_identifier: symbol -> bool
8230
6f8aa407bcf9 is_symbolic;
wenzelm
parents: 6857
diff changeset
    30
  val is_symbolic: symbol -> bool
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    31
  val is_printable: symbol -> bool
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    32
  val length: symbol list -> int
11010
2c6559297be3 added strip_blanks;
wenzelm
parents: 10953
diff changeset
    33
  val strip_blanks: string -> string
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    34
  val beginning: symbol list -> string
13730
09aeb7346d3f default_output now escapes \'s more carefully.
berghofe
parents: 13559
diff changeset
    35
  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
    36
  val scan: string list -> symbol * string list
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
    37
  val scanner: string -> (symbol list -> 'a * symbol list) -> symbol list -> 'a
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    38
  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
    39
    (symbol, (string, 'a) Source.source) Source.source
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    40
  val explode: string -> symbol list
12904
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
    41
  val bump_string: string -> string
10953
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
    42
  val default_indent: string * int -> string
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
    43
  val add_mode: string -> (string -> string * real) * (string * int -> string) -> unit
6692
05c56f41e661 avoid string constants;
wenzelm
parents: 6640
diff changeset
    44
  val symbolsN: string
05c56f41e661 avoid string constants;
wenzelm
parents: 6640
diff changeset
    45
  val xsymbolsN: string
10923
e34948f18541 export plain_output;
wenzelm
parents: 10747
diff changeset
    46
  val plain_output: string -> string
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    47
  val output: string -> string
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    48
  val output_width: string -> string * real
10953
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
    49
  val indent: string * int -> string
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    50
end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    51
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    52
structure Symbol: SYMBOL =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    53
struct
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    54
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    55
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    56
(** generalized characters **)
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    57
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    58
(*symbols, which are considered the smallest entities of any Isabelle
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    59
  string, may be of the following form:
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    60
    (a) ASCII symbols: a
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    61
    (b) printable symbols: \<ident>
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    62
    (c) control symbols: \<^ident>
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    63
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 11010
diff changeset
    64
  output is subject to the print_mode variable (default: verbatim),
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 11010
diff changeset
    65
  actual interpretation in display is up to front-end tools;
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    66
*)
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    67
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    68
type symbol = string;
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    69
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    70
val space = " ";
10953
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
    71
fun spaces k = Library.replicate_string k space;
6857
6e6eb8d92377 added sync marker;
wenzelm
parents: 6692
diff changeset
    72
val sync = "\\<^sync>";
10747
794cd4d768b5 scan: malformed result;
wenzelm
parents: 10738
diff changeset
    73
val malformed = "\\<^malformed>";
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    74
val eof = "";
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    75
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    76
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    77
(* kinds *)
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    78
6857
6e6eb8d92377 added sync marker;
wenzelm
parents: 6692
diff changeset
    79
fun is_sync s = s = sync;
6e6eb8d92377 added sync marker;
wenzelm
parents: 6692
diff changeset
    80
fun not_sync s = s <> sync;
6e6eb8d92377 added sync marker;
wenzelm
parents: 6692
diff changeset
    81
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    82
fun is_eof s = s = eof;
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    83
fun not_eof s = s <> eof;
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    84
val stopper = (eof, is_eof);
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    85
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    86
fun is_ascii s = size s = 1 andalso ord s < 128;
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
    87
14171
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
    88
local
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
    89
    fun wrap s = "\\<" ^ s ^ ">"
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
    90
    val pre_digits =
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
    91
	["zero","one","two","three","four",
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
    92
	 "five","six","seven","eight","nine"]
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
    93
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
    94
    val cal_letters =
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
    95
	["A","B","C","D","E","F","G","H","I","J","K","L","M",
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
    96
	 "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
    97
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
    98
    val small_letters =
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
    99
	["a","b","c","d","e","f","g","h","i","j","k","l","m",
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   100
	 "n","o","p","q","r","s","t","u","v","w","x","y","z"]
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   101
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   102
    val goth_letters =
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   103
	["AA","BB","CC","DD","EE","FF","GG","HH","II","JJ","KK","LL","MM",
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   104
	 "NN","OO","PP","QQ","RR","SS","TT","UU","VV","WW","XX","YY","ZZ",
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   105
	 "aa","bb","cc","dd","ee","ff","gg","hh","ii","jj","kk","ll","mm",
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   106
	 "nn","oo","pp","qq","rr","ss","tt","uu","vv","ww","xx","yy","zz"]
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   107
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   108
    val greek_letters =
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   109
	["alpha","beta","gamma","delta","epsilon","zeta","eta","theta",
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   110
	 "iota","kappa",(*"lambda",*)"mu","nu","xi","pi","rho","sigma","tau",
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   111
	 "upsilon","phi","psi","omega","Gamma","Delta","Theta","Lambda",
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   112
	 "Xi","Pi","Sigma","Upsilon","Phi","Psi","Omega"]
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   113
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   114
    val bbb_letters = ["bool","complex","nat","rat","real","int"]
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   115
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   116
    val pre_letters =
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   117
	cal_letters   @
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   118
	small_letters @
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   119
	goth_letters  @
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   120
	greek_letters
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   121
in
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   122
val ext_digits  = map wrap pre_digits
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   123
val ext_letters = map wrap pre_letters
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   124
val ext_letdigs = ext_digits @ ext_letters
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   125
fun is_ext_digit  s = String.isPrefix "\\<" s andalso s mem ext_digits
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   126
fun is_ext_letter s = String.isPrefix "\\<" s andalso s mem ext_letters
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   127
fun is_ext_letdig s = String.isPrefix "\\<" s andalso s mem ext_letdigs
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   128
end
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   129
     
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   130
fun is_letter s =
14171
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   131
    (size s = 1 andalso
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   132
     (ord "A" <= ord s andalso ord s <= ord "Z" orelse
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   133
      ord "a" <= ord s andalso ord s <= ord "z"))
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   134
    orelse is_ext_letter s
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   135
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   136
fun is_digit s =
14171
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   137
    (size s = 1 andalso ord "0" <= ord s andalso ord s <= ord "9")
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   138
    orelse is_ext_digit s
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   139
12904
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   140
fun is_quasi "_" = true
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   141
  | is_quasi "'" = true
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   142
  | is_quasi _ = false;
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   143
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   144
fun is_quasi_letter s = is_quasi s orelse is_letter s;
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   145
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   146
val is_blank =
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   147
  fn " " => true | "\t" => true | "\n" => true | "\^L" => true
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   148
    | "\160" => true | "\\<spacespace>" => true
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   149
    | _ => false;
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   150
12904
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   151
fun is_letdig s = is_quasi_letter s orelse is_digit s;
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   152
8230
6f8aa407bcf9 is_symbolic;
wenzelm
parents: 6857
diff changeset
   153
fun is_symbolic s =
14171
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   154
  size s > 2 andalso nth_elem_string (2, s) <> "^"
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   155
  andalso not (is_ext_letdig s);
8230
6f8aa407bcf9 is_symbolic;
wenzelm
parents: 6857
diff changeset
   156
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   157
fun is_printable s =
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   158
  size s = 1 andalso ord space <= ord s andalso ord s <= ord "~" orelse
14171
0cab06e3bbd0 Extended the notion of letter and digit, such that now one may use greek,
skalberg
parents: 13730
diff changeset
   159
  is_ext_letdig s orelse
8230
6f8aa407bcf9 is_symbolic;
wenzelm
parents: 6857
diff changeset
   160
  is_symbolic s;
6f8aa407bcf9 is_symbolic;
wenzelm
parents: 6857
diff changeset
   161
13559
51c3ac47d127 added checking so that (rename_tac "x y") is rejected, since
paulson
parents: 12904
diff changeset
   162
fun is_identifier s =
51c3ac47d127 added checking so that (rename_tac "x y") is rejected, since
paulson
parents: 12904
diff changeset
   163
  case (explode s) of
51c3ac47d127 added checking so that (rename_tac "x y") is rejected, since
paulson
parents: 12904
diff changeset
   164
      [] => false
51c3ac47d127 added checking so that (rename_tac "x y") is rejected, since
paulson
parents: 12904
diff changeset
   165
    | c::cs => is_letter c andalso forall is_letdig cs;
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   166
10738
3a610089c43b tuned length;
wenzelm
parents: 9961
diff changeset
   167
fun sym_length ss = foldl (fn (n, s) =>
3a610089c43b tuned length;
wenzelm
parents: 9961
diff changeset
   168
  (if not (is_printable s) then 0 else
3a610089c43b tuned length;
wenzelm
parents: 9961
diff changeset
   169
    (case Library.try String.substring (s, 2, 4) of
3a610089c43b tuned length;
wenzelm
parents: 9961
diff changeset
   170
      Some s' => if s' = "long" orelse s' = "Long" then 2 else 1
3a610089c43b tuned length;
wenzelm
parents: 9961
diff changeset
   171
    | None => 1)) + n) (0, ss);
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   172
11010
2c6559297be3 added strip_blanks;
wenzelm
parents: 10953
diff changeset
   173
fun strip_blanks s =
2c6559297be3 added strip_blanks;
wenzelm
parents: 10953
diff changeset
   174
  implode (#1 (Library.take_suffix is_blank (#2 (Library.take_prefix is_blank (explode s)))));
2c6559297be3 added strip_blanks;
wenzelm
parents: 10953
diff changeset
   175
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   176
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   177
(* beginning *)
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   178
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   179
val smash_blanks = map (fn s => if is_blank s then space else s);
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   180
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   181
fun beginning raw_ss =
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   182
  let
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   183
    val (all_ss, _) = take_suffix is_blank raw_ss;
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   184
    val dots = if length all_ss > 10 then " ..." else "";
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   185
    val (ss, _) = take_suffix is_blank (take (10, all_ss));
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   186
  in implode (smash_blanks ss) ^ dots end;
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   187
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   188
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   189
8998
56c44eee46ad fixed comment;
wenzelm
parents: 8806
diff changeset
   190
(** scanning through symbols **)
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   191
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   192
fun scanner msg scan chs =
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   193
  let
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   194
    fun err_msg cs = msg ^ ": " ^ beginning cs;
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   195
    val fin_scan = Scan.error (Scan.finite stopper (!! (fn (cs, _) => err_msg cs) scan));
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   196
  in
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   197
    (case fin_scan chs of
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   198
      (result, []) => result
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   199
    | (_, rest) => error (err_msg rest))
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   200
  end;
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   201
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   202
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6320
diff changeset
   203
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   204
(** symbol input **)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   205
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   206
(* scan *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   207
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   208
val scan_id = Scan.one is_letter ^^ (Scan.any is_letdig >> implode);
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   209
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   210
val scan =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   211
  ($$ "\\" --| Scan.optional ($$ "\\") "") ^^ $$ "<" ^^
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   212
    !! (fn (cs, _) => "Malformed symbolic character specification: \\" ^ "<" ^ beginning cs)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   213
      (Scan.optional ($$ "^") "" ^^ scan_id ^^ $$ ">") ||
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   214
  Scan.one not_eof;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   215
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   216
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   217
(* source *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   218
10747
794cd4d768b5 scan: malformed result;
wenzelm
parents: 10738
diff changeset
   219
val recover = Scan.any ((not o is_blank) andf not_eof) >> K [malformed];
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   220
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   221
fun source do_recover src =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   222
  Source.source stopper (Scan.bulk scan) (if do_recover then Some recover else None) src;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   223
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   224
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   225
(* explode *)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   226
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   227
fun no_syms [] = true
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   228
  | no_syms ("\\" :: "<" :: _) = false
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 11010
diff changeset
   229
  | no_syms (_ :: cs) = no_syms cs;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   230
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   231
fun sym_explode str =
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   232
  let val chs = explode str in
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   233
    if no_syms chs then chs     (*tune trivial case*)
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 11010
diff changeset
   234
    else the (Scan.read stopper (Scan.repeat scan) chs)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   235
  end;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   236
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   237
12904
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   238
(* bump_string -- increment suffix of lowercase letters like a base 26 number *)
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   239
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   240
fun bump_string str =
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   241
  let
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   242
    fun bump [] = ["a"]
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   243
      | bump ("z" :: ss) = "a" :: bump ss
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   244
      | bump (s :: ss) =
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   245
          if size s = 1 andalso ord "a" <= ord s andalso ord s < ord "z"
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   246
          then chr (ord s + 1) :: ss
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   247
          else "a" :: s :: ss;
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   248
    val (cs, qs) = Library.take_suffix is_quasi (sym_explode str);
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   249
  in implode (rev (bump (rev cs)) @ qs) end;
c208d71702d1 added is_quasi;
wenzelm
parents: 12116
diff changeset
   250
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   251
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   252
(** symbol output **)
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   253
10953
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
   254
(* default *)
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   255
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   256
fun string_size s = (s, real (size s));
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   257
13730
09aeb7346d3f default_output now escapes \'s more carefully.
berghofe
parents: 13559
diff changeset
   258
val escape = Scan.repeat
09aeb7346d3f default_output now escapes \'s more carefully.
berghofe
parents: 13559
diff changeset
   259
  ((($$ "\\" >> K "\\\\") ^^ Scan.optional ($$ "\\" >> K "\\\\") "" ^^ $$ "<" ^^
09aeb7346d3f default_output now escapes \'s more carefully.
berghofe
parents: 13559
diff changeset
   260
      Scan.optional ($$ "^") "" ^^ scan_id ^^ $$ ">") ||
09aeb7346d3f default_output now escapes \'s more carefully.
berghofe
parents: 13559
diff changeset
   261
   Scan.one not_eof) >> implode;
09aeb7346d3f default_output now escapes \'s more carefully.
berghofe
parents: 13559
diff changeset
   262
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   263
fun default_output s =
6320
4df282137880 tuned using nth_elem_string, exists_string;
wenzelm
parents: 6272
diff changeset
   264
  if not (exists_string (equal "\\") s) then string_size s
13730
09aeb7346d3f default_output now escapes \'s more carefully.
berghofe
parents: 13559
diff changeset
   265
  else string_size (fst (Scan.finite stopper escape (explode s)));
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   266
10953
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
   267
fun default_indent (_: string, k) = spaces k;
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   268
10953
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
   269
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   270
(* maintain modes *)
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   271
6692
05c56f41e661 avoid string constants;
wenzelm
parents: 6640
diff changeset
   272
val symbolsN = "symbols";
05c56f41e661 avoid string constants;
wenzelm
parents: 6640
diff changeset
   273
val xsymbolsN = "xsymbols";
05c56f41e661 avoid string constants;
wenzelm
parents: 6640
diff changeset
   274
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 11010
diff changeset
   275
val modes =
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 11010
diff changeset
   276
  ref (Symtab.empty: ((string -> string * real) * (string * int -> string)) Symtab.table);
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   277
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   278
fun lookup_mode name = Symtab.lookup (! modes, name);
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   279
10953
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
   280
fun add_mode name m =
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   281
 (if is_none (lookup_mode name) then ()
6320
4df282137880 tuned using nth_elem_string, exists_string;
wenzelm
parents: 6272
diff changeset
   282
  else warning ("Redeclaration of symbol print mode " ^ quote name);
10953
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
   283
  modes := Symtab.update ((name, m), ! modes));
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
   284
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
   285
fun get_mode () =
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
   286
  if_none (get_first lookup_mode (! print_mode)) (default_output, default_indent);
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   287
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   288
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   289
(* mode output *)
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   290
10953
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
   291
fun output_width x = #1 (get_mode ()) x;
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   292
val output = #1 o output_width;
10923
e34948f18541 export plain_output;
wenzelm
parents: 10747
diff changeset
   293
val plain_output = #1 o default_output;
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   294
10953
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
   295
fun indent x = #2 (get_mode ()) x;
ea024d025463 added spaces;
wenzelm
parents: 10923
diff changeset
   296
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   297
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   298
(*final declarations of this structure!*)
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   299
val length = sym_length;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   300
val explode = sym_explode;
6272
52d99d68d3be added output_width;
wenzelm
parents: 6225
diff changeset
   301
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   302
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   303
end;