src/Pure/Syntax/lexicon.ML
author wenzelm
Tue, 10 Dec 1996 12:55:37 +0100
changeset 2363 963285471dc5
parent 1507 f600215b6ea7
child 2583 690835a06cf2
permissions -rw-r--r--
tokenize: no gets exploded char list;
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
376
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
     5
Scanner combinators and Isabelle's main lexer (used for 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
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     8
infix 5 -- ^^;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     9
infix 3 >>;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    10
infix 0 ||;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
164
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    12
signature SCANNER =
1507
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1143
diff changeset
    13
  sig
164
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    14
  exception LEXICAL_ERROR
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    15
  val >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    16
  val || : ('a -> 'b) * ('a -> 'b) -> 'a -> 'b
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    17
  val -- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    18
  val ^^ : ('a -> string * 'b) * ('b -> string * 'c) -> 'a -> string * 'c
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    19
  val $$ : ''a -> ''a list -> ''a * ''a list
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    20
  val scan_empty: 'a list -> 'b list * 'a list
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    21
  val scan_one: ('a -> bool) -> 'a list -> 'a * 'a list
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    22
  val scan_any: ('a -> bool) -> 'a list -> 'a list * 'a list
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    23
  val scan_any1: ('a -> bool) -> 'a list -> 'a list * 'a list
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    24
  val scan_end: 'a list -> 'b list * 'a list
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    25
  val optional: ('a -> 'b * 'a) -> 'a -> 'b option * 'a
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    26
  val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    27
  val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
376
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
    28
  val max_of: ('a -> ('b * string) * 'a) -> ('a -> ('b * string) * 'a)
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
    29
    -> 'a -> ('b * string) option * 'a
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
    30
  val scan_id: string list -> string * string list
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
    31
  val scan_tid: string list -> string * string list
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
    32
  val scan_nat: string list -> string * string list
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
    33
  type lexicon
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
    34
  val dest_lexicon: lexicon -> string list
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
    35
  val empty_lexicon: lexicon
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
    36
  val extend_lexicon: lexicon -> string list -> lexicon
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
    37
  val make_lexicon: string list -> lexicon
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
    38
  val merge_lexicons: lexicon -> lexicon -> lexicon
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
    39
  val scan_literal: lexicon -> string list -> string * string list
1507
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1143
diff changeset
    40
  end;
164
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    41
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
signature LEXICON0 =
1507
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1143
diff changeset
    43
  sig
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
  val is_identifier: string -> bool
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    45
  val string_of_vname: indexname -> string
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
  val scan_varname: string list -> indexname * string list
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    47
  val scan_var: string -> term
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    48
  val const: string -> term
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    49
  val free: string -> term
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    50
  val var: indexname -> term
1507
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1143
diff changeset
    51
  end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
signature LEXICON =
1507
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1143
diff changeset
    54
  sig
164
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
    55
  include SCANNER
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    56
  include LEXICON0
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    57
  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
    58
  val is_tid: string -> bool
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    59
  datatype token =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    60
    Token of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    61
    IdentSy of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    62
    VarSy of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    63
    TFreeSy of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    64
    TVarSy of string |
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    65
    NumSy of string |
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    66
    StrSy of string |
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
    67
    EndToken
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    68
  val idT: typ
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    69
  val varT: typ
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    70
  val tidT: typ
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
    71
  val tvarT: typ
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
    72
  val terminals: string list
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
    73
  val is_terminal: string -> bool
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    74
  val str_of_token: token -> string
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    75
  val display_token: token -> string
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    76
  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
    77
  val token_assoc: (token option * 'a list) list * token -> 'a list
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    78
  val valued_token: token -> bool
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
    79
  val predef_term: string -> token option
2363
963285471dc5 tokenize: no gets exploded char list;
wenzelm
parents: 1507
diff changeset
    80
  val tokenize: lexicon -> bool -> string list -> token list
1507
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1143
diff changeset
    81
  end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
1507
f600215b6ea7 Elimination of fully-functorial style.
paulson
parents: 1143
diff changeset
    83
structure Lexicon : LEXICON =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    86
(** is_identifier etc. **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    87
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    88
fun is_ident [] = false
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    89
  | is_ident (c :: cs) = is_letter c andalso forall is_letdig cs;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    90
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    91
val is_identifier = is_ident o explode;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    92
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    93
fun is_xid s =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    94
  (case explode s of
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    95
    "_" :: cs => is_ident cs
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    96
  | cs => is_ident cs);
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    97
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 237
diff changeset
    98
fun is_tid s =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    99
  (case explode s of
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   100
    "'" :: cs => is_ident cs
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   101
  | _ => false);
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   102
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   103
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   105
(** string_of_vname **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   107
fun string_of_vname (x, i) =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   108
  let
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   109
    val si = string_of_int i;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   110
  in
1143
0dfb8b437f5d Now string_of_vname checks for the empty variable name,
lcp
parents: 550
diff changeset
   111
    (if is_digit (last_elem (explode x)) then "?" ^ x ^ "." ^ si
0dfb8b437f5d Now string_of_vname checks for the empty variable name,
lcp
parents: 550
diff changeset
   112
     else if i = 0 then "?" ^ x
0dfb8b437f5d Now string_of_vname checks for the empty variable name,
lcp
parents: 550
diff changeset
   113
     else "?" ^ x ^ si)
0dfb8b437f5d Now string_of_vname checks for the empty variable name,
lcp
parents: 550
diff changeset
   114
    handle LIST _ => "?NULL_VARIABLE." ^ si
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   115
  end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   116
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   117
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   118
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   119
(** datatype token **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   121
datatype token =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   122
  Token of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   123
  IdentSy of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   124
  VarSy of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   125
  TFreeSy of string |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   126
  TVarSy of string |
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   127
  NumSy of string |
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   128
  StrSy of string |
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   129
  EndToken;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   132
(* terminal arguments *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   134
val idT = Type ("id", []);
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   135
val varT = Type ("var", []);
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   136
val tidT = Type ("tid", []);
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   137
val tvarT = Type ("tvar", []);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   138
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   139
val terminals = ["id", "var", "tid", "tvar", "xnum", "xstr"];
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   140
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   141
fun is_terminal s = s mem terminals;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   142
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   143
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   144
(* str_of_token *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   146
fun str_of_token (Token s) = s
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   147
  | str_of_token (IdentSy s) = s
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   148
  | str_of_token (VarSy s) = s
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   149
  | str_of_token (TFreeSy s) = s
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   150
  | str_of_token (TVarSy s) = s
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   151
  | str_of_token (NumSy s) = s
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   152
  | str_of_token (StrSy s) = s
376
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   153
  | str_of_token EndToken = "EOF";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   155
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   156
(* display_token *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   157
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   158
fun display_token (Token s) = quote s
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   159
  | display_token (IdentSy s) = "id(" ^ s ^ ")"
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   160
  | 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
   161
  | display_token (TFreeSy s) = "tid(" ^ s ^ ")"
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   162
  | display_token (TVarSy s) = "tvar(" ^ s ^ ")"
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   163
  | display_token (NumSy s) = "xnum(" ^ s ^ ")"
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   164
  | display_token (StrSy s) = "xstr(" ^ s ^ ")"
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   165
  | display_token EndToken = "";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   166
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   167
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   168
(* matching_tokens *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   170
fun matching_tokens (Token x, Token y) = (x = y)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   171
  | matching_tokens (IdentSy _, IdentSy _) = true
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   172
  | matching_tokens (VarSy _, VarSy _) = true
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   173
  | matching_tokens (TFreeSy _, TFreeSy _) = true
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   174
  | matching_tokens (TVarSy _, TVarSy _) = true
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   175
  | matching_tokens (NumSy _, NumSy _) = true
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   176
  | matching_tokens (StrSy _, StrSy _) = true
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   177
  | matching_tokens (EndToken, EndToken) = true
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   178
  | matching_tokens _ = false;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   180
376
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   181
(* token_assoc *)
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   182
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 237
diff changeset
   183
fun token_assoc (list, key) =
376
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   184
  let
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   185
    fun assoc [] = []
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   186
      | assoc ((keyi, xi) :: pairs) =
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   187
          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
   188
            assoc pairs @ xi
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   189
          else assoc pairs;
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 237
diff changeset
   190
  in assoc list end;
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 237
diff changeset
   191
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 237
diff changeset
   192
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   193
(* valued_token *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   194
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   195
fun valued_token (Token _) = false
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   196
  | valued_token (IdentSy _) = true
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   197
  | valued_token (VarSy _) = true
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   198
  | valued_token (TFreeSy _) = true
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   199
  | valued_token (TVarSy _) = true
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   200
  | valued_token (NumSy _) = true
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   201
  | valued_token (StrSy _) = true
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   202
  | valued_token EndToken = false;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   203
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   204
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   205
(* predef_term *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   206
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   207
fun predef_term "id" = Some (IdentSy "id")
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   208
  | predef_term "var" = Some (VarSy "var")
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   209
  | predef_term "tid" = Some (TFreeSy "tid")
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   210
  | predef_term "tvar" = Some (TVarSy "tvar")
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   211
  | predef_term "xnum" = Some (NumSy "xnum")
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   212
  | predef_term "xstr" = Some (StrSy "xstr")
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   213
  | predef_term _ = None;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   214
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   215
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   217
(** datatype lexicon **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   218
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   219
datatype lexicon =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   220
  Empty |
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   221
  Branch of string * string * lexicon * lexicon * lexicon;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   222
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   223
val no_token = "";
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   224
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   225
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   226
(* dest_lexicon *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   227
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   228
fun dest_lexicon Empty = []
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   229
  | dest_lexicon (Branch (_, "", lt, eq, gt)) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   230
      dest_lexicon eq @ dest_lexicon lt @ dest_lexicon gt
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   231
  | dest_lexicon (Branch (_, str, lt, eq, gt)) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   232
      str :: (dest_lexicon eq @ dest_lexicon lt @ dest_lexicon gt);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   233
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   234
376
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   235
(* empty, extend, make, merge lexicons *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   236
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   237
val empty_lexicon = Empty;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   238
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   239
fun extend_lexicon lexicon strs =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   240
  let
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   241
    fun ext (lex, s) =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   242
      let
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   243
        fun add (Branch (d, a, lt, eq, gt)) (chs as c :: cs) =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   244
              if c < d then Branch (d, a, add lt chs, eq, gt)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   245
              else if c > d then Branch (d, a, lt, eq, add gt chs)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   246
              else Branch (d, if null cs then s else a, lt, add eq cs, gt)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   247
          | add Empty [c] =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   248
              Branch (c, s, Empty, Empty, Empty)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   249
          | add Empty (c :: cs) =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   250
              Branch (c, no_token, Empty, add Empty cs, Empty)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   251
          | add lex [] = lex;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   252
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   253
        val cs = explode s;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   254
      in
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   255
        if exists is_blank cs then
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   256
          sys_error ("extend_lexicon: blank in delimiter " ^ quote s)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   257
        else add lex cs
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   258
      end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   259
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   260
    foldl ext (lexicon, strs \\ dest_lexicon lexicon)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   261
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   262
376
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   263
val make_lexicon = extend_lexicon empty_lexicon;
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   264
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   265
fun merge_lexicons lex1 lex2 =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   266
  let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   267
    val strs1 = dest_lexicon lex1;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   268
    val strs2 = dest_lexicon lex2;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   269
  in
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   270
    if strs2 subset strs1 then lex1
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   271
    else if strs1 subset strs2 then lex2
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   272
    else extend_lexicon lex1 strs2
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 164
diff changeset
   273
  end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   274
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   275
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
(** scanners **)
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
exception LEXICAL_ERROR;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   280
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   281
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   282
(* scanner combinators *)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   283
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   284
fun (scan >> f) cs = apfst f (scan cs);
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   285
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   286
fun (scan1 || scan2) cs = scan1 cs handle LEXICAL_ERROR => scan2 cs;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   287
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   288
fun (scan1 -- scan2) cs =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   289
  let
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   290
    val (x, cs') = scan1 cs;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   291
    val (y, cs'') = scan2 cs';
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   292
  in
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   293
    ((x, y), cs'')
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   294
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   295
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   296
fun (scan1 ^^ scan2) = scan1 -- scan2 >> op ^;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   297
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   298
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   299
(* generic scanners *)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   300
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   301
fun $$ _ [] = raise LEXICAL_ERROR
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   302
  | $$ a (c :: cs) =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   303
      if a = c then (c, cs) else raise LEXICAL_ERROR;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   304
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   305
fun scan_empty cs = ([], cs);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   306
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   307
fun scan_one _ [] = raise LEXICAL_ERROR
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   308
  | scan_one pred (c :: cs) =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   309
      if pred c then (c, cs) else raise LEXICAL_ERROR;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   310
164
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
   311
fun scan_any _ [] = ([], [])
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
   312
  | scan_any pred (chs as c :: cs) =
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
   313
      if pred c then apfst (cons c) (scan_any pred cs)
43506f0a98ae added SCANNER;
wenzelm
parents: 18
diff changeset
   314
      else ([], chs);
18
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_any1 pred = scan_one pred -- scan_any pred >> op ::;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   317
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   318
fun scan_rest cs = (cs, []);
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   319
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   320
fun scan_end [] = ([], [])
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   321
  | scan_end _ = raise LEXICAL_ERROR;
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 optional scan = scan >> Some || scan_empty >> K None;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   324
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   325
fun repeat scan cs = (scan -- repeat scan >> op :: || scan_empty) cs;
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
fun repeat1 scan = scan -- repeat scan >> op ::;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   328
376
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   329
fun max_of scan1 scan2 cs =
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   330
  (case (optional scan1 cs, optional scan2 cs) of
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   331
    (tok1, (None, _)) => tok1
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   332
  | ((None, _), tok2) => tok2
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   333
  | (tok1 as (Some (_, s1), _), tok2 as (Some (_, s2), _)) =>
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   334
      if size s1 >= size s2 then tok1 else tok2);
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   335
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   336
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   337
(* other scanners *)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   338
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   339
val scan_letter_letdigs = scan_one is_letter -- scan_any is_letdig >> op ::;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   340
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   341
val scan_digits1 = scan_any1 is_digit;
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
val scan_id = scan_letter_letdigs >> implode;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   344
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   345
val scan_id_nat =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   346
  scan_id ^^ ($$ "." ^^ (scan_digits1 >> implode) || scan_empty >> K "");
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   347
376
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   348
val scan_tid = $$ "'" ^^ scan_id;
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   349
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   350
val scan_nat = scan_digits1 >> implode;
d3d01131470f extended signature SCANNER by some basic scanners and type lexicon;
wenzelm
parents: 330
diff changeset
   351
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   352
val scan_int = $$ "~" ^^ scan_nat || scan_nat;
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   353
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   354
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   355
(* scan_literal *)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   356
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   357
fun scan_literal lex chrs =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   358
  let
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   359
    fun scan_lit _ s_cs [] = s_cs
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   360
      | scan_lit Empty s_cs _ = s_cs
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   361
      | scan_lit (Branch (d, a, lt, eq, gt)) s_cs (chs as c :: cs) =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   362
          if c < d then scan_lit lt s_cs chs
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   363
          else if c > d then scan_lit gt s_cs chs
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   364
          else scan_lit eq (if a = no_token then s_cs else Some (a, cs)) cs;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   365
  in
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   366
    (case scan_lit lex None chrs of
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   367
      None => raise LEXICAL_ERROR
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   368
    | Some s_cs => s_cs)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   369
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   370
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   371
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   372
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   373
(** tokenize **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   374
2363
963285471dc5 tokenize: no gets exploded char list;
wenzelm
parents: 1507
diff changeset
   375
fun tokenize lex xids chs =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   376
  let
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   377
    val scan_xid =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   378
      if xids then $$ "_" ^^ scan_id || scan_id
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   379
      else scan_id;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   380
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   381
    val scan_lit = scan_literal lex >> pair Token;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   382
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   383
    val scan_val =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   384
      $$ "?" ^^ $$ "'" ^^ scan_id_nat >> pair TVarSy ||
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   385
      $$ "?" ^^ scan_id_nat >> pair VarSy ||
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   386
      $$ "'" ^^ scan_id >> pair TFreeSy ||
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   387
      $$ "#" ^^ scan_int >> pair NumSy ||
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   388
      scan_xid >> pair IdentSy;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   389
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   390
    fun scan_str ("'" :: "'" :: cs) = ([], cs)
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   391
      | scan_str ("\\" :: c :: cs) = apfst (cons c) (scan_str cs)
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   392
      | scan_str (c :: cs) = apfst (cons c) (scan_str cs)
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   393
      | scan_str [] = raise ERROR;
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   394
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   395
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   396
    fun scan (rev_toks, []) = rev (EndToken :: rev_toks)
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   397
      | scan (rev_toks, chs as "'" :: "'" :: cs) =
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   398
          let
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   399
            val (cs', cs'') = scan_str cs handle ERROR =>
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   400
              error ("Lexical error: missing quotes at end of string " ^
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   401
                quote (implode chs));
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   402
          in
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   403
            scan (StrSy (implode cs') :: rev_toks, cs'')
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   404
          end
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   405
      | scan (rev_toks, chs as c :: cs) =
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   406
          if is_blank c then scan (rev_toks, cs)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   407
          else
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   408
            (case max_of scan_lit scan_val chs of
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   409
              (None, _) => error ("Lexical error at: " ^ quote (implode chs))
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   410
            | (Some (tk, s), chs') => scan (tk s :: rev_toks, chs'));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   411
  in
2363
963285471dc5 tokenize: no gets exploded char list;
wenzelm
parents: 1507
diff changeset
   412
    scan ([], chs)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   413
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   414
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   415
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   416
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   417
(** scan variables **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   418
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   419
(* scan_vname *)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   420
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   421
fun scan_vname chrs =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   422
  let
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   423
    fun nat_of_chs n [] = n
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   424
      | 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
   425
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   426
    val nat_of = nat_of_chs 0;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   427
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   428
    fun split_vname chs =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   429
      let val (cs, ds) = take_suffix is_digit chs
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   430
      in (implode cs, nat_of ds) end
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   431
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   432
    val scan =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   433
      scan_letter_letdigs --
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   434
        ($$ "." -- scan_digits1 >> (nat_of o #2) || scan_empty >> K ~1);
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   435
  in
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   436
    (case scan chrs of
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   437
      ((cs, ~1), cs') => (split_vname cs, cs')
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   438
    | ((cs, i), cs') => ((implode cs, i), cs'))
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   439
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   440
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   441
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   442
(* scan_varname *)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   443
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   444
fun scan_varname chs =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   445
  scan_vname chs handle LEXICAL_ERROR
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   446
    => error ("scan_varname: bad varname " ^ quote (implode chs));
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   447
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   448
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   449
(* scan_var *)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   450
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   451
fun const c = Const (c, dummyT);
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   452
fun free x = Free (x, dummyT);
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   453
fun var xi = Var (xi, dummyT);
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   454
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   455
fun scan_var str =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   456
  let
550
353eea6ec232 replaced id, var, tid, tvar by idT, varT, tidT, tvarT;
wenzelm
parents: 376
diff changeset
   457
    fun tvar (x, i) = var ("'" ^ x, i);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   458
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   459
    val scan =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   460
      $$ "?" -- $$ "'" -- scan_vname -- scan_end >> (tvar o #2 o #1) ||
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   461
      $$ "?" -- scan_vname -- scan_end >> (var o #2 o #1) ||
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   462
      scan_rest >> (free o implode);
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   463
  in
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   464
    #1 (scan (explode str))
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   465
  end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   466
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   467
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   468