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