src/Pure/Syntax/lexicon.ML
author wenzelm
Thu, 27 Mar 2008 17:35:56 +0100
changeset 26439 e38f7e1c07ce
parent 26007 3760d3ff4cce
child 27121 38367dbccae5
permissions -rw-r--r--
tuned 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
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
     5
Lexer for the inner Isabelle syntax (terms and types).
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     6
*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
signature LEXICON0 =
4247
9bba9251bb4d added implode_xstr: string list -> string, explode_xstr: string -> string list;
wenzelm
parents: 3828
diff changeset
     9
sig
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
  val is_identifier: string -> bool
14679
6ed90bd68eda added is_ascii_identifier;
wenzelm
parents: 13808
diff changeset
    11
  val is_ascii_identifier: string -> bool
20165
4de20306a88a export is_tid;
wenzelm
parents: 20096
diff changeset
    12
  val is_tid: 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
26007
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    15
  val scan_id: Symbol.symbol list -> Symbol.symbol list * Symbol.symbol list
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    16
  val scan_longid: Symbol.symbol list -> Symbol.symbol list * Symbol.symbol list
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    17
  val scan_tid: Symbol.symbol list -> Symbol.symbol list * Symbol.symbol list
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    18
  val scan_nat: Symbol.symbol list -> Symbol.symbol list * Symbol.symbol list
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    19
  val scan_int: Symbol.symbol list -> Symbol.symbol list * Symbol.symbol list
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    20
  val scan_hex: Symbol.symbol list -> Symbol.symbol list * Symbol.symbol list
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    21
  val scan_bin: Symbol.symbol list -> Symbol.symbol list * Symbol.symbol list
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    22
  val scan_var: Symbol.symbol list -> Symbol.symbol list * Symbol.symbol list
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    23
  val scan_tvar: Symbol.symbol list -> Symbol.symbol list * Symbol.symbol list
20313
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
    24
  val read_indexname: string -> indexname
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
    25
  val read_var: string -> term
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
    26
  val read_variable: string -> indexname option
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    27
  val const: string -> term
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    28
  val free: string -> term
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    29
  val var: indexname -> term
5860
ed11c9890852 added read_nat;
wenzelm
parents: 5513
diff changeset
    30
  val read_nat: string -> int option
20313
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
    31
  val read_int: string -> int option
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24583
diff changeset
    32
  val read_xnum: string -> {radix: int, leading_zeros: int, value: int}
7784
228283fa5de4 read_idents;
wenzelm
parents: 7472
diff changeset
    33
  val read_idents: string -> string list
19002
2fbb3d809026 added fixedN, constN;
wenzelm
parents: 18375
diff changeset
    34
  val fixedN: string
2fbb3d809026 added fixedN, constN;
wenzelm
parents: 18375
diff changeset
    35
  val constN: string
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
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    42
  datatype token =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    43
    Token of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    44
    IdentSy of string |
3828
f6a7ca242dc2 added longid;
wenzelm
parents: 2583
diff changeset
    45
    LongIdentSy of string |
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    46
    VarSy of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    47
    TFreeSy of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    48
    TVarSy of string |
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    49
    NumSy of string |
11697
8dd899efbd35 added "num" token;
wenzelm
parents: 9326
diff changeset
    50
    XNumSy of string |
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    51
    StrSy of string |
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
    52
    EndToken
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    53
  val idT: typ
3828
f6a7ca242dc2 added longid;
wenzelm
parents: 2583
diff changeset
    54
  val longidT: typ
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    55
  val varT: typ
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    56
  val tidT: typ
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    57
  val tvarT: typ
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
    58
  val terminals: string list
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
    59
  val is_terminal: string -> bool
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    60
  val str_of_token: token -> string
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    61
  val display_token: token -> string
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    62
  val matching_tokens: token * token -> bool
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    63
  val valued_token: token -> bool
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
    64
  val predef_term: string -> token option
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
    65
  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
    66
end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
14679
6ed90bd68eda added is_ascii_identifier;
wenzelm
parents: 13808
diff changeset
    68
structure Lexicon: LEXICON =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    71
(** is_identifier etc. **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    72
16150
c33fe18456fa moved is_ident to General/symbol.ML;
wenzelm
parents: 15991
diff changeset
    73
val is_identifier = Symbol.is_ident o Symbol.explode;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    74
14679
6ed90bd68eda added is_ascii_identifier;
wenzelm
parents: 13808
diff changeset
    75
fun is_ascii_identifier s =
6ed90bd68eda added is_ascii_identifier;
wenzelm
parents: 13808
diff changeset
    76
  let val cs = Symbol.explode s
16150
c33fe18456fa moved is_ident to General/symbol.ML;
wenzelm
parents: 15991
diff changeset
    77
  in forall Symbol.is_ascii cs andalso Symbol.is_ident cs end;
14679
6ed90bd68eda added is_ascii_identifier;
wenzelm
parents: 13808
diff changeset
    78
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    79
fun is_xid s =
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
    80
  (case Symbol.explode s of
16150
c33fe18456fa moved is_ident to General/symbol.ML;
wenzelm
parents: 15991
diff changeset
    81
    "_" :: cs => Symbol.is_ident cs
c33fe18456fa moved is_ident to General/symbol.ML;
wenzelm
parents: 15991
diff changeset
    82
  | cs => Symbol.is_ident cs);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    83
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 237
diff changeset
    84
fun is_tid s =
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
    85
  (case Symbol.explode s of
16150
c33fe18456fa moved is_ident to General/symbol.ML;
wenzelm
parents: 15991
diff changeset
    86
    "'" :: cs => Symbol.is_ident cs
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    87
  | _ => false);
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    88
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
    91
(** basic scanners **)
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
    92
26007
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    93
val scan_id = Scan.one Symbol.is_letter ::: Scan.many Symbol.is_letdig;
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    94
val scan_longid = scan_id @@@ (Scan.repeat1 ($$ "." ::: scan_id) >> flat);
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    95
val scan_tid = $$ "'" ::: scan_id;
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
    96
26007
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    97
val scan_nat = Scan.many1 Symbol.is_digit;
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    98
val scan_int = $$ "-" ::: scan_nat || scan_nat;
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
    99
val scan_hex = $$ "0" ::: $$ "x" ::: Scan.many1 Symbol.is_ascii_hex;
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   100
val scan_bin = $$ "0" ::: $$ "b" ::: Scan.many1 (fn s => s = "0" orelse s = "1");
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
   101
26007
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   102
val scan_id_nat = scan_id @@@ Scan.optional ($$ "." ::: scan_nat) [];
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   103
val scan_var = $$ "?" ::: scan_id_nat;
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   104
val scan_tvar = $$ "?" ::: $$ "'" ::: scan_id_nat;
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
   105
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
   106
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
   107
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   108
(** datatype token **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   110
datatype token =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   111
  Token of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   112
  IdentSy of string |
3828
f6a7ca242dc2 added longid;
wenzelm
parents: 2583
diff changeset
   113
  LongIdentSy of string |
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   114
  VarSy of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   115
  TFreeSy of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   116
  TVarSy of string |
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   117
  NumSy of string |
11697
8dd899efbd35 added "num" token;
wenzelm
parents: 9326
diff changeset
   118
  XNumSy of string |
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   119
  StrSy of string |
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   120
  EndToken;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   123
(* terminal arguments *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   124
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   125
val idT = Type ("id", []);
3828
f6a7ca242dc2 added longid;
wenzelm
parents: 2583
diff changeset
   126
val longidT = Type ("longid", []);
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   127
val varT = Type ("var", []);
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   128
val tidT = Type ("tid", []);
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   129
val tvarT = Type ("tvar", []);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
11697
8dd899efbd35 added "num" token;
wenzelm
parents: 9326
diff changeset
   131
val terminals = ["id", "longid", "var", "tid", "tvar", "num", "xnum", "xstr"];
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20313
diff changeset
   132
val is_terminal = member (op =) terminals;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   133
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   135
(* str_of_token *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   136
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   137
fun str_of_token (Token s) = s
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   138
  | str_of_token (IdentSy s) = s
3828
f6a7ca242dc2 added longid;
wenzelm
parents: 2583
diff changeset
   139
  | str_of_token (LongIdentSy s) = s
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   140
  | str_of_token (VarSy s) = s
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   141
  | str_of_token (TFreeSy s) = s
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   142
  | str_of_token (TVarSy s) = s
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   143
  | str_of_token (NumSy s) = s
11697
8dd899efbd35 added "num" token;
wenzelm
parents: 9326
diff changeset
   144
  | str_of_token (XNumSy s) = s
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   145
  | str_of_token (StrSy s) = s
376
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   146
  | str_of_token EndToken = "EOF";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   147
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   148
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   149
(* display_token *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   151
fun display_token (Token s) = quote s
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   152
  | display_token (IdentSy s) = "id(" ^ s ^ ")"
3828
f6a7ca242dc2 added longid;
wenzelm
parents: 2583
diff changeset
   153
  | display_token (LongIdentSy s) = "longid(" ^ s ^ ")"
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   154
  | 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
   155
  | display_token (TFreeSy s) = "tid(" ^ s ^ ")"
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   156
  | display_token (TVarSy s) = "tvar(" ^ s ^ ")"
11697
8dd899efbd35 added "num" token;
wenzelm
parents: 9326
diff changeset
   157
  | display_token (NumSy s) = "num(" ^ s ^ ")"
8dd899efbd35 added "num" token;
wenzelm
parents: 9326
diff changeset
   158
  | display_token (XNumSy s) = "xnum(" ^ s ^ ")"
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   159
  | display_token (StrSy s) = "xstr(" ^ s ^ ")"
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   160
  | display_token EndToken = "";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   161
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   162
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   163
(* matching_tokens *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   164
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   165
fun matching_tokens (Token x, Token y) = (x = y)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   166
  | matching_tokens (IdentSy _, IdentSy _) = true
3828
f6a7ca242dc2 added longid;
wenzelm
parents: 2583
diff changeset
   167
  | matching_tokens (LongIdentSy _, LongIdentSy _) = true
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   168
  | matching_tokens (VarSy _, VarSy _) = true
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   169
  | matching_tokens (TFreeSy _, TFreeSy _) = true
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   170
  | matching_tokens (TVarSy _, TVarSy _) = true
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   171
  | matching_tokens (NumSy _, NumSy _) = true
11697
8dd899efbd35 added "num" token;
wenzelm
parents: 9326
diff changeset
   172
  | matching_tokens (XNumSy _, XNumSy _) = true
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   173
  | matching_tokens (StrSy _, StrSy _) = true
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   174
  | matching_tokens (EndToken, EndToken) = true
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   175
  | matching_tokens _ = false;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   176
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   177
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   178
(* valued_token *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   180
fun valued_token (Token _) = false
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   181
  | valued_token (IdentSy _) = true
3828
f6a7ca242dc2 added longid;
wenzelm
parents: 2583
diff changeset
   182
  | valued_token (LongIdentSy _) = true
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   183
  | valued_token (VarSy _) = true
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   184
  | valued_token (TFreeSy _) = true
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   185
  | valued_token (TVarSy _) = true
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   186
  | valued_token (NumSy _) = true
11697
8dd899efbd35 added "num" token;
wenzelm
parents: 9326
diff changeset
   187
  | valued_token (XNumSy _) = true
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   188
  | valued_token (StrSy _) = true
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   189
  | valued_token EndToken = false;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   190
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   191
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   192
(* predef_term *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   193
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   194
fun predef_term "id" = SOME (IdentSy "id")
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   195
  | predef_term "longid" = SOME (LongIdentSy "longid")
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   196
  | predef_term "var" = SOME (VarSy "var")
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   197
  | predef_term "tid" = SOME (TFreeSy "tid")
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   198
  | predef_term "tvar" = SOME (TVarSy "tvar")
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   199
  | predef_term "num" = SOME (NumSy "num")
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   200
  | predef_term "xnum" = SOME (XNumSy "xnum")
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   201
  | predef_term "xstr" = SOME (StrSy "xstr")
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   202
  | predef_term _ = NONE;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   203
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   204
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
   205
(* xstr tokens *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   206
14730
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   207
fun lex_err msg prfx (cs, _) =
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   208
  "Inner lexical error: " ^ msg ^ " at " ^ quote (prfx ^ Symbol.beginning 10 cs);
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   209
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   210
val scan_chr =
21774
3f9324ff06e3 xstr: disallow backslashes;
wenzelm
parents: 20664
diff changeset
   211
  $$ "\\" |-- $$ "'" ||
23784
75e6b9dd5336 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 22686
diff changeset
   212
  Scan.one (fn s => s <> "\\" andalso s <> "'" andalso Symbol.is_regular s) ||
19305
5c16895d548b avoid polymorphic equality;
wenzelm
parents: 19002
diff changeset
   213
  $$ "'" --| Scan.ahead (~$$ "'");
14730
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   214
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   215
val scan_str =
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   216
  $$ "'" |-- $$ "'" |-- !! (lex_err "missing end of string" "''")
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   217
    (Scan.repeat scan_chr --| $$ "'" --| $$ "'");
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   218
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   219
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
   220
fun implode_xstr cs = enclose "''" "''" (implode (map (fn "'" => "\\'" | c => c) cs));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   221
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
   222
fun explode_xstr str =
5868
0022d0a913b5 tuned usage of read;
wenzelm
parents: 5860
diff changeset
   223
  (case Scan.read Symbol.stopper scan_str (Symbol.explode str) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   224
    SOME cs => cs
5868
0022d0a913b5 tuned usage of read;
wenzelm
parents: 5860
diff changeset
   225
  | _ => error ("Inner lexical error: literal string expected at " ^ quote str));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   226
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   227
14730
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   228
(* nested comments *)
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   229
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   230
val scan_cmt =
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   231
  Scan.depend (fn d => $$ "(" ^^ $$ "*" >> pair (d + 1)) ||
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   232
  Scan.depend (fn 0 => Scan.fail | d => $$ "*" ^^ $$ ")" >> pair (d - 1)) ||
19305
5c16895d548b avoid polymorphic equality;
wenzelm
parents: 19002
diff changeset
   233
  Scan.lift ($$ "*" --| Scan.ahead (~$$ ")")) ||
23784
75e6b9dd5336 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 22686
diff changeset
   234
  Scan.lift (Scan.one (fn s => s <> "*" andalso Symbol.is_regular s));
14730
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   235
14783
e7f7ed4c06f2 string_of_vname moved to term.ML;
wenzelm
parents: 14737
diff changeset
   236
val scan_comment =
e7f7ed4c06f2 string_of_vname moved to term.ML;
wenzelm
parents: 14737
diff changeset
   237
  $$ "(" -- $$ "*" -- !! (lex_err "missing end of comment" "(*")
14730
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   238
    (Scan.pass 0 (Scan.repeat scan_cmt) -- $$ "*" -- $$ ")")
14783
e7f7ed4c06f2 string_of_vname moved to term.ML;
wenzelm
parents: 14737
diff changeset
   239
  >> K ();
14730
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   240
59ab60c6fcc6 support nested comments;
wenzelm
parents: 14679
diff changeset
   241
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   242
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   243
(** tokenize **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   244
2363
963285471dc5 tokenize: no gets exploded char list;
wenzelm
parents: 1507
diff changeset
   245
fun tokenize lex xids chs =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   246
  let
26007
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   247
    fun token kind cs = (kind, implode cs);
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   248
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   249
    val scan_xid =
26007
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   250
      if xids then $$ "_" ::: scan_id || scan_id
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   251
      else scan_id;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   252
20096
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   253
    val scan_num = scan_hex || scan_bin || scan_int;
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   254
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   255
    val scan_val =
26007
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   256
      scan_tvar >> token TVarSy ||
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   257
      scan_var >> token VarSy ||
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   258
      scan_tid >> token TFreeSy ||
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   259
      scan_num >> token NumSy ||
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   260
      $$ "#" ::: scan_num >> token XNumSy ||
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   261
      scan_longid >> token LongIdentSy ||
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   262
      scan_xid >> token IdentSy;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   263
26007
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   264
    val scan_lit = Scan.literal lex >> token Token;
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   265
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
   266
    val scan_token =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   267
      scan_comment >> K NONE ||
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   268
      Scan.max (op <= o pairself snd) scan_lit scan_val >> (fn (tk, s) => SOME (tk s)) ||
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   269
      scan_str >> (SOME o StrSy o implode_xstr) ||
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   270
      Scan.one Symbol.is_blank >> K NONE;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   271
  in
4938
c8bbbf3c59fa Symbol.stopper;
wenzelm
parents: 4921
diff changeset
   272
    (case Scan.error (Scan.finite Symbol.stopper (Scan.repeat scan_token)) chs of
24245
4ffeb1dd048a Lexicon.tokenize: do not appen EndToken yet;
wenzelm
parents: 23802
diff changeset
   273
      (toks, []) => map_filter I toks
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
   274
    | (_, cs) => error ("Inner lexical error at: " ^ quote (implode cs)))
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   275
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   276
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   277
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   278
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   279
(** scan variables **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   280
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   281
(* scan_indexname *)
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   282
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   283
local
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   284
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   285
fun scan_vname chrs =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   286
  let
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   287
    fun nat n [] = n
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   288
      | nat n (c :: cs) = nat (n * 10 + (ord c - ord "0")) cs;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   289
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   290
    fun idxname cs ds = (implode (rev cs), nat 0 ds);
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   291
    fun chop_idx [] ds = idxname [] ds
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   292
      | chop_idx (cs as (_ :: "\\<^isub>" :: _)) ds = idxname cs ds
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   293
      | chop_idx (cs as (_ :: "\\<^isup>" :: _)) ds = idxname cs ds
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   294
      | chop_idx (c :: cs) ds =
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   295
          if Symbol.is_digit c then chop_idx cs (c :: ds)
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   296
          else idxname (c :: cs) ds;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   297
26007
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   298
    val scan = scan_id -- Scan.optional ($$ "." |-- scan_nat >> nat 0) ~1;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   299
  in
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   300
    (case scan chrs of
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   301
      ((cs, ~1), cs') => (chop_idx (rev cs) [], cs')
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   302
    | ((cs, i), cs') => ((implode cs, i), cs'))
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   303
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   304
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   305
in
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   306
15443
07f78cc82a73 indexname function now parses type variables as well; changed input
berghofe
parents: 14981
diff changeset
   307
val scan_indexname =
07f78cc82a73 indexname function now parses type variables as well; changed input
berghofe
parents: 14981
diff changeset
   308
     $$ "'" |-- scan_vname >> (fn (x, i) => ("'" ^ x, i))
07f78cc82a73 indexname function now parses type variables as well; changed input
berghofe
parents: 14981
diff changeset
   309
  || scan_vname;
07f78cc82a73 indexname function now parses type variables as well; changed input
berghofe
parents: 14981
diff changeset
   310
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   311
end;
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   312
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   313
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   314
(* indexname *)
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   315
20313
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   316
fun read_indexname s =
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   317
  (case Scan.read Symbol.stopper scan_indexname (Symbol.explode s) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   318
    SOME xi => xi
20313
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   319
  | _ => error ("Lexical error in variable name: " ^ quote s));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   320
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   321
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
   322
(* read_var *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   323
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   324
fun const c = Const (c, dummyT);
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   325
fun free x = Free (x, dummyT);
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   326
fun var xi = Var (xi, dummyT);
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   327
4703
a50ab39756db adapted to symbols, scan;
wenzelm
parents: 4587
diff changeset
   328
fun read_var str =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   329
  let
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   330
    val scan =
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   331
      $$ "?" |-- scan_indexname --| Scan.ahead (Scan.one Symbol.is_eof) >> var ||
23784
75e6b9dd5336 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 22686
diff changeset
   332
      Scan.many Symbol.is_regular >> (free o implode);
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   333
  in the (Scan.read Symbol.stopper scan (Symbol.explode str)) end;
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   334
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   335
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   336
(* read_variable *)
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   337
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   338
fun read_variable str =
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   339
  let val scan = $$ "?" |-- scan_indexname || scan_indexname
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   340
  in Scan.read Symbol.stopper scan (Symbol.explode str) end;
4587
6bce9ef27d7e added read_var;
wenzelm
parents: 4247
diff changeset
   341
6bce9ef27d7e added read_var;
wenzelm
parents: 4247
diff changeset
   342
19002
2fbb3d809026 added fixedN, constN;
wenzelm
parents: 18375
diff changeset
   343
(* specific identifiers *)
5260
1835a591d3a7 binding / skolem vars;
wenzelm
parents: 5112
diff changeset
   344
19002
2fbb3d809026 added fixedN, constN;
wenzelm
parents: 18375
diff changeset
   345
val constN = "\\<^const>";
2fbb3d809026 added fixedN, constN;
wenzelm
parents: 18375
diff changeset
   346
val fixedN = "\\<^fixed>";
2fbb3d809026 added fixedN, constN;
wenzelm
parents: 18375
diff changeset
   347
5260
1835a591d3a7 binding / skolem vars;
wenzelm
parents: 5112
diff changeset
   348
20313
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   349
(* read numbers *)
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   350
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   351
local
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   352
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   353
fun nat cs =
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   354
  Option.map (#1 o Library.read_int)
26007
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   355
    (Scan.read Symbol.stopper scan_nat cs);
5860
ed11c9890852 added read_nat;
wenzelm
parents: 5513
diff changeset
   356
20313
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   357
in
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   358
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   359
val read_nat = nat o Symbol.explode;
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   360
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   361
fun read_int s =
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   362
  (case Symbol.explode s of
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   363
    "-" :: cs => Option.map ~ (nat cs)
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   364
  | cs => nat cs);
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   365
bf9101cc4385 renamed Syntax.indexname to Syntax.read_indexname;
wenzelm
parents: 20165
diff changeset
   366
end;
5860
ed11c9890852 added read_nat;
wenzelm
parents: 5513
diff changeset
   367
ed11c9890852 added read_nat;
wenzelm
parents: 5513
diff changeset
   368
20096
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   369
(* read_xnum: hex/bin/decimal *)
9326
1625c1f172b3 added read_xnum;
wenzelm
parents: 9289
diff changeset
   370
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   371
local
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   372
20096
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   373
val ten = ord "0" + 10;
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   374
val a = ord "a";
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   375
val A = ord "A";
23802
cd09234405b6 sys_error;
wenzelm
parents: 23784
diff changeset
   376
val _ = a > A orelse sys_error "Bad ASCII";
20096
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   377
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   378
fun remap_hex c =
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   379
  let val x = ord c in
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   380
    if x >= a then chr (x - a + ten)
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   381
    else if x >= A then chr (x - A + ten)
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   382
    else c
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   383
  end;
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   384
21781
8314ebb5364d read_xnum: return leading_zeros, radix;
wenzelm
parents: 21774
diff changeset
   385
fun leading_zeros ["0"] = 0
8314ebb5364d read_xnum: return leading_zeros, radix;
wenzelm
parents: 21774
diff changeset
   386
  | leading_zeros ("0" :: cs) = 1 + leading_zeros cs
8314ebb5364d read_xnum: return leading_zeros, radix;
wenzelm
parents: 21774
diff changeset
   387
  | leading_zeros _ = 0;
8314ebb5364d read_xnum: return leading_zeros, radix;
wenzelm
parents: 21774
diff changeset
   388
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   389
in
15965
f422f8283491 Use of IntInf.int instead of int in most numeric simprocs; avoids
paulson
parents: 15570
diff changeset
   390
9326
1625c1f172b3 added read_xnum;
wenzelm
parents: 9289
diff changeset
   391
fun read_xnum str =
1625c1f172b3 added read_xnum;
wenzelm
parents: 9289
diff changeset
   392
  let
20096
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   393
    val (sign, radix, digs) =
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   394
      (case Symbol.explode (perhaps (try (unprefix "#")) str) of
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   395
        "0" :: "x" :: cs => (1, 16, map remap_hex cs)
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   396
      | "0" :: "b" :: cs => (1, 2, cs)
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   397
      | "-" :: cs => (~1, 10, cs)
7058714024b3 uniform treatment of num/xnum;
wenzelm
parents: 20091
diff changeset
   398
      | cs => (1, 10, cs));
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24583
diff changeset
   399
    val value = sign * #1 (Library.read_radix_int radix digs);
21781
8314ebb5364d read_xnum: return leading_zeros, radix;
wenzelm
parents: 21774
diff changeset
   400
  in {radix = radix, leading_zeros = leading_zeros digs, value = value} end;
9326
1625c1f172b3 added read_xnum;
wenzelm
parents: 9289
diff changeset
   401
15991
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   402
end;
670f8e4b5a98 added read_variable: optional question mark on input;
wenzelm
parents: 15965
diff changeset
   403
9326
1625c1f172b3 added read_xnum;
wenzelm
parents: 9289
diff changeset
   404
7784
228283fa5de4 read_idents;
wenzelm
parents: 7472
diff changeset
   405
(* read_ident(s) *)
228283fa5de4 read_idents;
wenzelm
parents: 7472
diff changeset
   406
228283fa5de4 read_idents;
wenzelm
parents: 7472
diff changeset
   407
fun read_idents str =
228283fa5de4 read_idents;
wenzelm
parents: 7472
diff changeset
   408
  let
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21781
diff changeset
   409
    val blanks = Scan.many Symbol.is_blank;
23784
75e6b9dd5336 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 22686
diff changeset
   410
    val junk = Scan.many Symbol.is_regular;
26007
3760d3ff4cce basic scanners: produce symbol list instead of imploded string;
wenzelm
parents: 24630
diff changeset
   411
    val idents = Scan.repeat1 (blanks |-- (scan_id >> implode) --| blanks) -- junk;
7784
228283fa5de4 read_idents;
wenzelm
parents: 7472
diff changeset
   412
  in
228283fa5de4 read_idents;
wenzelm
parents: 7472
diff changeset
   413
    (case Scan.read Symbol.stopper idents (Symbol.explode str) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   414
      SOME (ids, []) => ids
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   415
    | SOME (_, bad) => error ("Bad identifier: " ^ quote (implode bad))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15443
diff changeset
   416
    | NONE => error ("No identifier found in: " ^ quote str))
7784
228283fa5de4 read_idents;
wenzelm
parents: 7472
diff changeset
   417
  end;
228283fa5de4 read_idents;
wenzelm
parents: 7472
diff changeset
   418
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   419
end;