| author | berghofe | 
| Fri, 10 Mar 2000 15:03:05 +0100 | |
| changeset 8411 | d30df828a974 | 
| parent 7784 | 228283fa5de4 | 
| child 9289 | be6e79d1aae0 | 
| permissions | -rw-r--r-- | 
| 18 | 1 | (* Title: Pure/Syntax/lexicon.ML | 
| 0 | 2 | ID: $Id$ | 
| 18 | 3 | Author: Tobias Nipkow and Markus Wenzel, TU Muenchen | 
| 0 | 4 | |
| 4703 | 5 | Lexer for the inner Isabelle syntax (terms and types). | 
| 18 | 6 | *) | 
| 0 | 7 | |
| 8 | signature LEXICON0 = | |
| 4247 
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
 wenzelm parents: 
3828diff
changeset | 9 | sig | 
| 0 | 10 | val is_identifier: string -> bool | 
| 4247 
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
 wenzelm parents: 
3828diff
changeset | 11 | val implode_xstr: string list -> string | 
| 
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
 wenzelm parents: 
3828diff
changeset | 12 | val explode_xstr: string -> string list | 
| 4703 | 13 | val scan_id: string list -> string * string list | 
| 14 | val scan_longid: string list -> string * string list | |
| 15 | val scan_var: string list -> string * string list | |
| 16 | val scan_tid: string list -> string * string list | |
| 4902 | 17 | val scan_tvar: string list -> string * string list | 
| 4703 | 18 | val scan_nat: string list -> string * string list | 
| 19 | val scan_int: string list -> string * string list | |
| 18 | 20 | val string_of_vname: indexname -> string | 
| 2583 
690835a06cf2
added string_of_vname' (treats neg. index as free);
 wenzelm parents: 
2363diff
changeset | 21 | val string_of_vname': indexname -> string | 
| 4703 | 22 | val indexname: string list -> indexname | 
| 23 | val read_var: string -> term | |
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 24 | val const: string -> term | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 25 | val free: string -> term | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 26 | val var: indexname -> term | 
| 5260 | 27 | val skolem: string -> string | 
| 5286 | 28 | val dest_skolem: string -> string | 
| 5860 | 29 | val read_nat: string -> int option | 
| 7784 | 30 | val read_idents: string -> string list | 
| 4247 
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
 wenzelm parents: 
3828diff
changeset | 31 | end; | 
| 0 | 32 | |
| 33 | signature LEXICON = | |
| 4247 
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
 wenzelm parents: 
3828diff
changeset | 34 | sig | 
| 18 | 35 | include LEXICON0 | 
| 36 | val is_xid: string -> bool | |
| 330 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 clasohm parents: 
237diff
changeset | 37 | val is_tid: string -> bool | 
| 18 | 38 | datatype token = | 
| 39 | Token of string | | |
| 40 | IdentSy of string | | |
| 3828 | 41 | LongIdentSy of string | | 
| 18 | 42 | VarSy of string | | 
| 43 | TFreeSy of string | | |
| 44 | TVarSy of string | | |
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 45 | NumSy of string | | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 46 | StrSy of string | | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
164diff
changeset | 47 | EndToken | 
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 48 | val idT: typ | 
| 3828 | 49 | val longidT: typ | 
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 50 | val varT: typ | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 51 | val tidT: typ | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 52 | val tvarT: typ | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
164diff
changeset | 53 | val terminals: string list | 
| 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
164diff
changeset | 54 | val is_terminal: string -> bool | 
| 18 | 55 | val str_of_token: token -> string | 
| 56 | val display_token: token -> string | |
| 57 | val matching_tokens: token * token -> bool | |
| 330 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 clasohm parents: 
237diff
changeset | 58 | val token_assoc: (token option * 'a list) list * token -> 'a list | 
| 18 | 59 | val valued_token: token -> bool | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
164diff
changeset | 60 | val predef_term: string -> token option | 
| 4703 | 61 | val tokenize: Scan.lexicon -> bool -> string list -> token list | 
| 4247 
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
 wenzelm parents: 
3828diff
changeset | 62 | end; | 
| 0 | 63 | |
| 1507 | 64 | structure Lexicon : LEXICON = | 
| 0 | 65 | struct | 
| 66 | ||
| 4247 
9bba9251bb4d
added implode_xstr: string list -> string, explode_xstr: string -> string list;
 wenzelm parents: 
3828diff
changeset | 67 | |
| 18 | 68 | (** is_identifier etc. **) | 
| 69 | ||
| 70 | fun is_ident [] = false | |
| 4703 | 71 | | is_ident (c :: cs) = Symbol.is_letter c andalso forall Symbol.is_letdig cs; | 
| 18 | 72 | |
| 4703 | 73 | val is_identifier = is_ident o Symbol.explode; | 
| 18 | 74 | |
| 75 | fun is_xid s = | |
| 4703 | 76 | (case Symbol.explode s of | 
| 18 | 77 | "_" :: cs => is_ident cs | 
| 78 | | cs => is_ident cs); | |
| 79 | ||
| 330 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 clasohm parents: 
237diff
changeset | 80 | fun is_tid s = | 
| 4703 | 81 | (case Symbol.explode s of | 
| 18 | 82 | "'" :: cs => is_ident cs | 
| 83 | | _ => false); | |
| 84 | ||
| 0 | 85 | |
| 86 | ||
| 4703 | 87 | (** basic scanners **) | 
| 88 | ||
| 89 | val scan_letter_letdigs = Scan.one Symbol.is_letter -- Scan.any Symbol.is_letdig >> op ::; | |
| 90 | val scan_digits1 = Scan.any1 Symbol.is_digit; | |
| 91 | ||
| 92 | val scan_id = scan_letter_letdigs >> implode; | |
| 93 | val scan_longid = scan_id ^^ (Scan.repeat1 ($$ "." ^^ scan_id) >> implode); | |
| 94 | val scan_tid = $$ "'" ^^ scan_id; | |
| 95 | ||
| 96 | val scan_nat = scan_digits1 >> implode; | |
| 5513 | 97 | val scan_int = $$ "-" ^^ scan_nat || scan_nat; | 
| 4703 | 98 | |
| 99 | val scan_id_nat = scan_id ^^ Scan.optional ($$ "." ^^ scan_nat) ""; | |
| 100 | val scan_var = $$ "?" ^^ scan_id_nat; | |
| 4902 | 101 | val scan_tvar = $$ "?" ^^ $$ "'" ^^ scan_id_nat; | 
| 4703 | 102 | |
| 103 | ||
| 104 | ||
| 18 | 105 | (** string_of_vname **) | 
| 0 | 106 | |
| 18 | 107 | fun string_of_vname (x, i) = | 
| 108 | let | |
| 109 | val si = string_of_int i; | |
| 6962 | 110 | val dot = if_none (try (Symbol.is_digit o last_elem o Symbol.explode) x) true; | 
| 18 | 111 | in | 
| 4703 | 112 | if dot then "?" ^ x ^ "." ^ si | 
| 113 | else if i = 0 then "?" ^ x | |
| 114 | else "?" ^ x ^ si | |
| 18 | 115 | end; | 
| 0 | 116 | |
| 4703 | 117 | fun string_of_vname' (x, ~1) = x | 
| 118 | | string_of_vname' xi = string_of_vname xi; | |
| 2583 
690835a06cf2
added string_of_vname' (treats neg. index as free);
 wenzelm parents: 
2363diff
changeset | 119 | |
| 18 | 120 | |
| 0 | 121 | |
| 18 | 122 | (** datatype token **) | 
| 0 | 123 | |
| 18 | 124 | datatype token = | 
| 125 | Token of string | | |
| 126 | IdentSy of string | | |
| 3828 | 127 | LongIdentSy of string | | 
| 18 | 128 | VarSy of string | | 
| 129 | TFreeSy of string | | |
| 130 | TVarSy of string | | |
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 131 | NumSy of string | | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 132 | StrSy of string | | 
| 18 | 133 | EndToken; | 
| 0 | 134 | |
| 135 | ||
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
164diff
changeset | 136 | (* terminal arguments *) | 
| 0 | 137 | |
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 138 | val idT = Type ("id", []);
 | 
| 3828 | 139 | val longidT = Type ("longid", []);
 | 
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 140 | val varT = Type ("var", []);
 | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 141 | val tidT = Type ("tid", []);
 | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 142 | val tvarT = Type ("tvar", []);
 | 
| 0 | 143 | |
| 3828 | 144 | val terminals = ["id", "longid", "var", "tid", "tvar", "xnum", "xstr"]; | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
164diff
changeset | 145 | |
| 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
164diff
changeset | 146 | fun is_terminal s = s mem terminals; | 
| 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
164diff
changeset | 147 | |
| 0 | 148 | |
| 18 | 149 | (* str_of_token *) | 
| 0 | 150 | |
| 18 | 151 | fun str_of_token (Token s) = s | 
| 152 | | str_of_token (IdentSy s) = s | |
| 3828 | 153 | | str_of_token (LongIdentSy s) = s | 
| 18 | 154 | | str_of_token (VarSy s) = s | 
| 155 | | str_of_token (TFreeSy s) = s | |
| 156 | | str_of_token (TVarSy s) = s | |
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 157 | | str_of_token (NumSy s) = s | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 158 | | str_of_token (StrSy s) = s | 
| 376 
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
 wenzelm parents: 
330diff
changeset | 159 | | str_of_token EndToken = "EOF"; | 
| 0 | 160 | |
| 18 | 161 | |
| 162 | (* display_token *) | |
| 0 | 163 | |
| 18 | 164 | fun display_token (Token s) = quote s | 
| 165 |   | display_token (IdentSy s) = "id(" ^ s ^ ")"
 | |
| 3828 | 166 |   | display_token (LongIdentSy s) = "longid(" ^ s ^ ")"
 | 
| 18 | 167 |   | display_token (VarSy s) = "var(" ^ s ^ ")"
 | 
| 330 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 clasohm parents: 
237diff
changeset | 168 |   | display_token (TFreeSy s) = "tid(" ^ s ^ ")"
 | 
| 18 | 169 |   | display_token (TVarSy s) = "tvar(" ^ s ^ ")"
 | 
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 170 |   | display_token (NumSy s) = "xnum(" ^ s ^ ")"
 | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 171 |   | display_token (StrSy s) = "xstr(" ^ s ^ ")"
 | 
| 18 | 172 | | display_token EndToken = ""; | 
| 0 | 173 | |
| 18 | 174 | |
| 175 | (* matching_tokens *) | |
| 0 | 176 | |
| 18 | 177 | fun matching_tokens (Token x, Token y) = (x = y) | 
| 178 | | matching_tokens (IdentSy _, IdentSy _) = true | |
| 3828 | 179 | | matching_tokens (LongIdentSy _, LongIdentSy _) = true | 
| 18 | 180 | | matching_tokens (VarSy _, VarSy _) = true | 
| 181 | | matching_tokens (TFreeSy _, TFreeSy _) = true | |
| 182 | | matching_tokens (TVarSy _, TVarSy _) = true | |
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 183 | | matching_tokens (NumSy _, NumSy _) = true | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 184 | | matching_tokens (StrSy _, StrSy _) = true | 
| 18 | 185 | | matching_tokens (EndToken, EndToken) = true | 
| 186 | | matching_tokens _ = false; | |
| 0 | 187 | |
| 188 | ||
| 376 
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
 wenzelm parents: 
330diff
changeset | 189 | (* token_assoc *) | 
| 
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
 wenzelm parents: 
330diff
changeset | 190 | |
| 330 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 clasohm parents: 
237diff
changeset | 191 | fun token_assoc (list, key) = | 
| 376 
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
 wenzelm parents: 
330diff
changeset | 192 | let | 
| 
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
 wenzelm parents: 
330diff
changeset | 193 | fun assoc [] = [] | 
| 
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
 wenzelm parents: 
330diff
changeset | 194 | | assoc ((keyi, xi) :: pairs) = | 
| 
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
 wenzelm parents: 
330diff
changeset | 195 | if is_none keyi orelse matching_tokens (the keyi, key) then | 
| 
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
 wenzelm parents: 
330diff
changeset | 196 | assoc pairs @ xi | 
| 
d3d01131470f
extended signature SCANNER by some basic scanners and type lexicon;
 wenzelm parents: 
330diff
changeset | 197 | else assoc pairs; | 
| 330 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 clasohm parents: 
237diff
changeset | 198 | in assoc list end; | 
| 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 clasohm parents: 
237diff
changeset | 199 | |
| 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 clasohm parents: 
237diff
changeset | 200 | |
| 18 | 201 | (* valued_token *) | 
| 0 | 202 | |
| 18 | 203 | fun valued_token (Token _) = false | 
| 204 | | valued_token (IdentSy _) = true | |
| 3828 | 205 | | valued_token (LongIdentSy _) = true | 
| 18 | 206 | | valued_token (VarSy _) = true | 
| 207 | | valued_token (TFreeSy _) = true | |
| 208 | | valued_token (TVarSy _) = true | |
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 209 | | valued_token (NumSy _) = true | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 210 | | valued_token (StrSy _) = true | 
| 18 | 211 | | valued_token EndToken = false; | 
| 0 | 212 | |
| 213 | ||
| 18 | 214 | (* predef_term *) | 
| 0 | 215 | |
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 216 | fun predef_term "id" = Some (IdentSy "id") | 
| 3828 | 217 | | predef_term "longid" = Some (LongIdentSy "longid") | 
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 218 | | predef_term "var" = Some (VarSy "var") | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 219 | | predef_term "tid" = Some (TFreeSy "tid") | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 220 | | predef_term "tvar" = Some (TVarSy "tvar") | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 221 | | predef_term "xnum" = Some (NumSy "xnum") | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 222 | | predef_term "xstr" = Some (StrSy "xstr") | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 223 | | predef_term _ = None; | 
| 0 | 224 | |
| 225 | ||
| 4703 | 226 | (* xstr tokens *) | 
| 18 | 227 | |
| 4703 | 228 | val scan_chr = | 
| 229 | $$ "\\" |-- Scan.one Symbol.not_eof || | |
| 230 | Scan.one (not_equal "'" andf Symbol.not_eof) || | |
| 231 | $$ "'" --| Scan.ahead (Scan.one (not_equal "'")); | |
| 18 | 232 | |
| 4703 | 233 | val scan_str = | 
| 234 | $$ "'" |-- $$ "'" |-- | |
| 4921 | 235 | !! (fn (cs, _) => "Inner lexical error: malformed literal string at " ^ | 
| 5112 | 236 |       quote ("''" ^ Symbol.beginning cs))
 | 
| 4921 | 237 | (Scan.repeat scan_chr --| $$ "'" --| $$ "'"); | 
| 18 | 238 | |
| 0 | 239 | |
| 4703 | 240 | fun implode_xstr cs = enclose "''" "''" (implode (map (fn "'" => "\\'" | c => c) cs)); | 
| 18 | 241 | |
| 4703 | 242 | fun explode_xstr str = | 
| 5868 | 243 | (case Scan.read Symbol.stopper scan_str (Symbol.explode str) of | 
| 244 | Some cs => cs | |
| 245 |   | _ => error ("Inner lexical error: literal string expected at " ^ quote str));
 | |
| 18 | 246 | |
| 247 | ||
| 248 | ||
| 249 | (** tokenize **) | |
| 250 | ||
| 2363 | 251 | fun tokenize lex xids chs = | 
| 18 | 252 | let | 
| 253 | val scan_xid = | |
| 254 | if xids then $$ "_" ^^ scan_id || scan_id | |
| 255 | else scan_id; | |
| 256 | ||
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 257 | val scan_val = | 
| 4902 | 258 | scan_tvar >> pair TVarSy || | 
| 4703 | 259 | scan_var >> pair VarSy || | 
| 260 | scan_tid >> pair TFreeSy || | |
| 5868 | 261 | $$ "#" ^^ scan_int >> pair NumSy || | 
| 3828 | 262 | scan_longid >> pair LongIdentSy || | 
| 18 | 263 | scan_xid >> pair IdentSy; | 
| 264 | ||
| 4703 | 265 | val scan_lit = Scan.literal lex >> (pair Token o implode); | 
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 266 | |
| 4703 | 267 | val scan_token = | 
| 268 | Scan.max (op <= o pairself snd) scan_lit scan_val >> (fn (tk, s) => Some (tk s)) || | |
| 269 | scan_str >> (Some o StrSy o implode_xstr) || | |
| 270 | Scan.one Symbol.is_blank >> K None; | |
| 18 | 271 | in | 
| 4938 | 272 | (case Scan.error (Scan.finite Symbol.stopper (Scan.repeat scan_token)) chs of | 
| 4703 | 273 | (toks, []) => mapfilter I toks @ [EndToken] | 
| 274 |     | (_, cs) => error ("Inner lexical error at: " ^ quote (implode cs)))
 | |
| 18 | 275 | end; | 
| 276 | ||
| 277 | ||
| 278 | ||
| 279 | (** scan variables **) | |
| 280 | ||
| 281 | (* scan_vname *) | |
| 282 | ||
| 283 | fun scan_vname chrs = | |
| 284 | let | |
| 285 | fun nat_of_chs n [] = n | |
| 286 | | nat_of_chs n (c :: cs) = nat_of_chs (n * 10 + (ord c - ord "0")) cs; | |
| 287 | ||
| 4703 | 288 | val nat = nat_of_chs 0; | 
| 18 | 289 | |
| 290 | fun split_vname chs = | |
| 4703 | 291 | let val (cs, ds) = take_suffix Symbol.is_digit chs | 
| 292 | in (implode cs, nat ds) end | |
| 18 | 293 | |
| 294 | val scan = | |
| 4703 | 295 | scan_letter_letdigs -- Scan.optional ($$ "." |-- scan_digits1 >> nat) ~1; | 
| 18 | 296 | in | 
| 297 | (case scan chrs of | |
| 298 | ((cs, ~1), cs') => (split_vname cs, cs') | |
| 299 | | ((cs, i), cs') => ((implode cs, i), cs')) | |
| 300 | end; | |
| 301 | ||
| 302 | ||
| 4703 | 303 | (* indexname *) | 
| 18 | 304 | |
| 4703 | 305 | fun indexname cs = | 
| 5868 | 306 | (case Scan.read Symbol.stopper scan_vname cs of | 
| 307 | Some xi => xi | |
| 4703 | 308 |   | _ => error ("Lexical error in variable name: " ^ quote (implode cs)));
 | 
| 18 | 309 | |
| 310 | ||
| 4703 | 311 | (* read_var *) | 
| 18 | 312 | |
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 313 | fun const c = Const (c, dummyT); | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 314 | fun free x = Free (x, dummyT); | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 315 | fun var xi = Var (xi, dummyT); | 
| 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 316 | |
| 4703 | 317 | fun read_var str = | 
| 18 | 318 | let | 
| 550 
353eea6ec232
replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
 wenzelm parents: 
376diff
changeset | 319 |     fun tvar (x, i) = var ("'" ^ x, i);
 | 
| 18 | 320 | |
| 321 | val scan = | |
| 4703 | 322 | $$ "?" |-- $$ "'" |-- scan_vname >> tvar || | 
| 323 | $$ "?" |-- scan_vname >> var || | |
| 324 | Scan.any Symbol.not_eof >> (free o implode); | |
| 5868 | 325 | in the (Scan.read Symbol.stopper scan (Symbol.explode str)) end; | 
| 4587 | 326 | |
| 327 | ||
| 5260 | 328 | (* variable kinds *) | 
| 329 | ||
| 5286 | 330 | val skolem = suffix "__"; | 
| 331 | val dest_skolem = unsuffix "__"; | |
| 5260 | 332 | |
| 333 | ||
| 5860 | 334 | (* read_nat *) | 
| 335 | ||
| 336 | fun read_nat str = | |
| 5868 | 337 | apsome (#1 o Term.read_int) (Scan.read Symbol.stopper scan_digits1 (Symbol.explode str)); | 
| 5860 | 338 | |
| 339 | ||
| 7784 | 340 | (* read_ident(s) *) | 
| 341 | ||
| 342 | fun read_idents str = | |
| 343 | let | |
| 344 | val blanks = Scan.any Symbol.is_blank; | |
| 345 | val junk = Scan.any Symbol.not_eof; | |
| 346 | val idents = Scan.repeat1 (blanks |-- scan_id --| blanks) -- junk; | |
| 347 | in | |
| 348 | (case Scan.read Symbol.stopper idents (Symbol.explode str) of | |
| 349 | Some (ids, []) => ids | |
| 350 |     | Some (_, bad) => error ("Bad identifier: " ^ quote (implode bad))
 | |
| 351 |     | None => error ("No identifier found in: " ^ quote str))
 | |
| 352 | end; | |
| 353 | ||
| 354 | ||
| 0 | 355 | end; |