src/Pure/Isar/outer_parse.ML
author wenzelm
Thu Nov 19 11:49:57 1998 +0100 (1998-11-19)
changeset 5940 33bdc03bba7e
parent 5917 dcb669fda86b
child 5960 2bebd0d0a961
permissions -rw-r--r--
fixed method syntax;
wenzelm@5826
     1
(*  Title:      Pure/Isar/outer_parse.ML
wenzelm@5826
     2
    ID:         $Id$
wenzelm@5826
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5826
     4
wenzelm@5826
     5
Generic parsers for Isabelle/Isar outer syntax.
wenzelm@5826
     6
*)
wenzelm@5826
     7
wenzelm@5826
     8
signature OUTER_PARSE =
wenzelm@5826
     9
sig
wenzelm@5826
    10
  type token
wenzelm@5826
    11
  val group: string -> (token list -> 'a) -> token list -> 'a
wenzelm@5826
    12
  val !!! : (token list -> 'a) -> token list -> 'a
wenzelm@5826
    13
  val $$$ : string -> token list -> string * token list
wenzelm@5826
    14
  val position: (token list -> 'a * 'b) -> token list -> ('a * Position.T) * 'b
wenzelm@5826
    15
  val keyword: token list -> string * token list
wenzelm@5826
    16
  val short_ident: token list -> string * token list
wenzelm@5826
    17
  val long_ident: token list -> string * token list
wenzelm@5826
    18
  val sym_ident: token list -> string * token list
wenzelm@5826
    19
  val term_var: token list -> string * token list
wenzelm@5826
    20
  val text_var: token list -> string * token list
wenzelm@5826
    21
  val type_ident: token list -> string * token list
wenzelm@5826
    22
  val type_var: token list -> string * token list
wenzelm@5826
    23
  val number: token list -> string * token list
wenzelm@5826
    24
  val string: token list -> string * token list
wenzelm@5826
    25
  val verbatim: token list -> string * token list
wenzelm@5826
    26
  val eof: token list -> string * token list
wenzelm@5826
    27
  val not_eof: token list -> token * token list
wenzelm@5826
    28
  val nat: token list -> int * token list
wenzelm@5826
    29
  val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    30
  val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    31
  val list: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    32
  val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    33
  val name: token list -> bstring * token list
wenzelm@5826
    34
  val xname: token list -> xstring * token list
wenzelm@5826
    35
  val text: token list -> string * token list
wenzelm@5826
    36
  val sort: token list -> xsort * token list
wenzelm@5826
    37
  val arity: token list -> (xsort list * xsort) * token list
wenzelm@5826
    38
  val type_args: token list -> string list * token list
wenzelm@5826
    39
  val typ: token list -> string * token list
wenzelm@5826
    40
  val opt_infix: token list -> Syntax.mixfix * token list
wenzelm@5826
    41
  val opt_mixfix: token list -> Syntax.mixfix * token list
wenzelm@5826
    42
  val const: token list -> (bstring * string * Syntax.mixfix) * token list
wenzelm@5826
    43
  val term: token list -> string * token list
wenzelm@5826
    44
  val prop: token list -> string * token list
wenzelm@5826
    45
  val attribs: token list -> Args.src list * token list
wenzelm@5826
    46
  val opt_attribs: token list -> Args.src list * token list
wenzelm@5917
    47
  val thm_name: string -> token list -> (bstring * Args.src list) * token list
wenzelm@5917
    48
  val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list
wenzelm@5917
    49
  val xthm: token list -> (xstring * Args.src list) * token list
wenzelm@5917
    50
  val xthms1: token list -> (xstring * Args.src list) list * token list
wenzelm@5826
    51
  val method: token list -> Method.text * token list
wenzelm@5826
    52
  val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
wenzelm@5826
    53
  val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
wenzelm@5826
    54
end;
wenzelm@5826
    55
wenzelm@5826
    56
structure OuterParse: OUTER_PARSE =
wenzelm@5826
    57
struct
wenzelm@5826
    58
wenzelm@5826
    59
type token = OuterLex.token;
wenzelm@5826
    60
wenzelm@5826
    61
wenzelm@5826
    62
(** error handling **)
wenzelm@5826
    63
wenzelm@5826
    64
(* group atomic parsers (no cuts!) *)
wenzelm@5826
    65
wenzelm@5826
    66
fun fail_with s = Scan.fail_with
wenzelm@5826
    67
  (fn [] => s ^ " expected (past end-of-file!)"
wenzelm@5826
    68
    | (tok :: _) => s ^ " expected,\nbut " ^ OuterLex.name_of tok ^ " " ^
wenzelm@5826
    69
      quote (OuterLex.val_of tok) ^ OuterLex.pos_of tok ^ " was found");
wenzelm@5826
    70
wenzelm@5826
    71
fun group s scan = scan || fail_with s;
wenzelm@5826
    72
wenzelm@5826
    73
wenzelm@5877
    74
(* cut *)
wenzelm@5826
    75
wenzelm@5826
    76
fun !!! scan =
wenzelm@5826
    77
  let
wenzelm@5826
    78
    fun get_pos [] = " (past end-of-file!)"
wenzelm@5826
    79
      | get_pos (tok :: _) = OuterLex.pos_of tok;
wenzelm@5826
    80
wenzelm@5826
    81
    fun err (toks, None) = "Outer syntax error" ^ get_pos toks
wenzelm@5826
    82
      | err (toks, Some msg) = "Outer syntax error" ^ get_pos toks ^ ": " ^ msg;
wenzelm@5826
    83
  in Scan.!! err scan end;
wenzelm@5826
    84
wenzelm@5826
    85
wenzelm@5826
    86
wenzelm@5826
    87
(** basic parsers **)
wenzelm@5826
    88
wenzelm@5826
    89
(* utils *)
wenzelm@5826
    90
wenzelm@5826
    91
fun triple1 ((x, y), z) = (x, y, z);
wenzelm@5826
    92
fun triple2 (x, (y, z)) = (x, y, z);
wenzelm@5826
    93
wenzelm@5826
    94
wenzelm@5826
    95
(* tokens *)
wenzelm@5826
    96
wenzelm@5826
    97
fun position scan =
wenzelm@5826
    98
  (Scan.ahead (Scan.one OuterLex.not_eof) >> OuterLex.position_of) -- scan >> Library.swap;
wenzelm@5826
    99
wenzelm@5826
   100
fun kind k =
wenzelm@5826
   101
  group (OuterLex.str_of_kind k)
wenzelm@5826
   102
    (Scan.one (OuterLex.is_kind k) >> OuterLex.val_of);
wenzelm@5826
   103
wenzelm@5826
   104
val keyword = kind OuterLex.Keyword;
wenzelm@5826
   105
val short_ident = kind OuterLex.Ident;
wenzelm@5826
   106
val long_ident = kind OuterLex.LongIdent;
wenzelm@5826
   107
val sym_ident = kind OuterLex.SymIdent;
wenzelm@5826
   108
val term_var = kind OuterLex.Var;
wenzelm@5826
   109
val text_var = kind OuterLex.TextVar;
wenzelm@5826
   110
val type_ident = kind OuterLex.TypeIdent;
wenzelm@5826
   111
val type_var = kind OuterLex.TypeVar;
wenzelm@5826
   112
val number = kind OuterLex.Nat;
wenzelm@5826
   113
val string = kind OuterLex.String;
wenzelm@5826
   114
val verbatim = kind OuterLex.Verbatim;
wenzelm@5826
   115
val eof = kind OuterLex.EOF;
wenzelm@5826
   116
wenzelm@5826
   117
fun $$$ x =
wenzelm@5826
   118
  group (OuterLex.str_of_kind OuterLex.Keyword ^ " " ^ quote x)
wenzelm@5826
   119
    (Scan.one (OuterLex.is_kind OuterLex.Keyword andf (equal x o OuterLex.val_of))
wenzelm@5826
   120
      >> OuterLex.val_of);
wenzelm@5826
   121
wenzelm@5877
   122
val nat = number >> (fst o Term.read_int o Symbol.explode);
wenzelm@5826
   123
wenzelm@5826
   124
val not_eof = Scan.one OuterLex.not_eof;
wenzelm@5826
   125
wenzelm@5826
   126
wenzelm@5826
   127
(* enumerations *)
wenzelm@5826
   128
wenzelm@5826
   129
fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- scan) >> op ::;
wenzelm@5826
   130
fun enum sep scan = enum1 sep scan || Scan.succeed [];
wenzelm@5826
   131
wenzelm@5826
   132
fun list1 scan = enum1 "," scan;
wenzelm@5826
   133
fun list scan = enum "," scan;
wenzelm@5826
   134
wenzelm@5826
   135
wenzelm@5826
   136
(* names *)
wenzelm@5826
   137
wenzelm@5877
   138
val name = group "name declaration" (short_ident || sym_ident || string);
wenzelm@5877
   139
val xname = group "name reference" (short_ident || long_ident || sym_ident || string);
wenzelm@5826
   140
val text = group "text" (short_ident || long_ident || string || verbatim);
wenzelm@5826
   141
wenzelm@5826
   142
wenzelm@5826
   143
(* sorts *)
wenzelm@5826
   144
wenzelm@5826
   145
val sort =
wenzelm@5826
   146
  xname >> single || $$$ "{" |-- !!! (list xname --| $$$ "}");
wenzelm@5826
   147
wenzelm@5826
   148
val arity =
wenzelm@5826
   149
  Scan.optional ($$$ "(" |-- !!! (Scan.repeat1 sort --| $$$ ")")) [] -- sort;
wenzelm@5826
   150
wenzelm@5826
   151
wenzelm@5826
   152
(* types *)
wenzelm@5826
   153
wenzelm@5826
   154
val typ =
wenzelm@5826
   155
  group "type" (short_ident || long_ident || type_ident || type_var || string);
wenzelm@5826
   156
wenzelm@5826
   157
val type_args =
wenzelm@5826
   158
  type_ident >> single ||
wenzelm@5826
   159
  $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
wenzelm@5826
   160
  Scan.succeed [];
wenzelm@5826
   161
wenzelm@5826
   162
wenzelm@5826
   163
(* mixfix annotations *)
wenzelm@5826
   164
wenzelm@5826
   165
val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName);
wenzelm@5826
   166
val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName);
wenzelm@5826
   167
wenzelm@5826
   168
val binder =
wenzelm@5826
   169
  $$$ "binder" |--
wenzelm@5826
   170
    !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
wenzelm@5826
   171
  >> (Syntax.Binder o triple2);
wenzelm@5826
   172
wenzelm@5826
   173
wenzelm@5826
   174
val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [];
wenzelm@5826
   175
wenzelm@5826
   176
val mixfix =
wenzelm@5826
   177
  string -- opt_pris -- Scan.optional nat Syntax.max_pri
wenzelm@5826
   178
  >> (Syntax.Mixfix o triple1);
wenzelm@5826
   179
wenzelm@5826
   180
fun opt_fix fix =
wenzelm@5826
   181
  Scan.optional ($$$ "(" |-- !!! (fix --| $$$ ")")) Syntax.NoSyn;
wenzelm@5826
   182
wenzelm@5826
   183
val opt_infix = opt_fix (infxl || infxr);
wenzelm@5826
   184
val opt_mixfix = opt_fix (mixfix || infxl || infxr || binder);
wenzelm@5826
   185
wenzelm@5826
   186
wenzelm@5826
   187
(* consts *)
wenzelm@5826
   188
wenzelm@5826
   189
val const =
wenzelm@5826
   190
  name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2;
wenzelm@5826
   191
wenzelm@5826
   192
wenzelm@5826
   193
(* terms *)
wenzelm@5826
   194
wenzelm@5826
   195
val trm = short_ident || long_ident || term_var || text_var || string;
wenzelm@5826
   196
wenzelm@5826
   197
val term = group "term" trm;
wenzelm@5826
   198
val prop = group "proposition" trm;
wenzelm@5826
   199
wenzelm@5826
   200
wenzelm@5826
   201
(* arguments *)
wenzelm@5826
   202
wenzelm@5877
   203
val keyword_symid = Scan.one (OuterLex.keyword_pred OuterLex.is_sid) >> OuterLex.val_of;
wenzelm@5826
   204
wenzelm@5877
   205
fun atom_arg blk =
wenzelm@5826
   206
  group "argument"
wenzelm@5877
   207
    (position (short_ident || long_ident || sym_ident || term_var || text_var ||
wenzelm@5877
   208
        type_ident || type_var || number) >> Args.ident ||
wenzelm@5877
   209
      position keyword_symid >> Args.keyword ||
wenzelm@5877
   210
      position string >> Args.string ||
wenzelm@5877
   211
      position (if blk then $$$ "," else Scan.fail) >> Args.keyword);
wenzelm@5826
   212
wenzelm@5877
   213
fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
wenzelm@5826
   214
  >> (fn (x, (ys, z)) => Args.keyword x :: ys @ [Args.keyword z]);
wenzelm@5826
   215
wenzelm@5877
   216
fun args blk x = Scan.optional (args1 blk) [] x
wenzelm@5877
   217
and args1 blk x =
wenzelm@5826
   218
  ((Scan.repeat1
wenzelm@5877
   219
    (Scan.repeat1 (atom_arg blk) ||
wenzelm@5826
   220
      paren_args "(" ")" args ||
wenzelm@5826
   221
      paren_args "{" "}" args ||
wenzelm@5826
   222
      paren_args "[" "]" args)) >> flat) x;
wenzelm@5826
   223
wenzelm@5826
   224
wenzelm@5826
   225
(* theorem names *)
wenzelm@5826
   226
wenzelm@5877
   227
val attrib = position (xname -- !!! (args false)) >> Args.src;
wenzelm@5826
   228
val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
wenzelm@5826
   229
val opt_attribs = Scan.optional attribs [];
wenzelm@5826
   230
wenzelm@5917
   231
fun thm_name s = name -- opt_attribs --| $$$ s;
wenzelm@5917
   232
fun opt_thm_name s = Scan.optional (thm_name s) ("", []);
wenzelm@5917
   233
wenzelm@5917
   234
val xthm = xname -- opt_attribs;
wenzelm@5917
   235
val xthms1 = Scan.repeat1 xthm;
wenzelm@5826
   236
wenzelm@5826
   237
wenzelm@5826
   238
(* proof methods *)
wenzelm@5826
   239
wenzelm@5826
   240
fun meth4 x =
wenzelm@5877
   241
 (position (xname >> rpair []) >> (Method.Source o Args.src) ||
wenzelm@5826
   242
  $$$ "(" |-- meth0 --| $$$ ")") x
wenzelm@5826
   243
and meth3 x =
wenzelm@5826
   244
 (meth4 --| $$$ "?" >> Method.Try ||
wenzelm@5826
   245
  meth4 --| $$$ "*" >> Method.Repeat ||
wenzelm@5826
   246
  meth4 --| $$$ "+" >> Method.Repeat1 ||
wenzelm@5940
   247
  meth4) x
wenzelm@5940
   248
and meth2 x =
wenzelm@5940
   249
 (position (xname -- args1 false) >> (Method.Source o Args.src) ||
wenzelm@5826
   250
  meth3) x
wenzelm@5826
   251
and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
wenzelm@5826
   252
and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
wenzelm@5826
   253
wenzelm@5940
   254
val method = meth3;
wenzelm@5826
   255
wenzelm@5826
   256
wenzelm@5826
   257
end;