src/Pure/Isar/outer_parse.ML
author wenzelm
Wed Sep 01 21:17:37 1999 +0200 (1999-09-01)
changeset 7418 87c12d352bab
parent 7352 d98001b492b3
child 7477 c7caea1ce78c
permissions -rw-r--r--
any_props: improved error;
removed "*" method combinator;
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@7026
    15
  val command: token list -> string * token list
wenzelm@5826
    16
  val keyword: token list -> string * token list
wenzelm@5826
    17
  val short_ident: token list -> string * token list
wenzelm@5826
    18
  val long_ident: token list -> string * token list
wenzelm@5826
    19
  val sym_ident: token list -> string * token list
wenzelm@5826
    20
  val term_var: token list -> string * token list
wenzelm@5826
    21
  val text_var: token list -> string * token list
wenzelm@5826
    22
  val type_ident: token list -> string * token list
wenzelm@5826
    23
  val type_var: token list -> string * token list
wenzelm@5826
    24
  val number: token list -> string * token list
wenzelm@5826
    25
  val string: token list -> string * token list
wenzelm@5826
    26
  val verbatim: token list -> string * token list
wenzelm@6860
    27
  val sync: token list -> string * token list
wenzelm@5826
    28
  val eof: token list -> string * token list
wenzelm@5826
    29
  val not_eof: token list -> token * token list
wenzelm@5826
    30
  val nat: token list -> int * token list
wenzelm@5826
    31
  val enum: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    32
  val enum1: string -> (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    33
  val list: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    34
  val list1: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@6013
    35
  val and_list: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@6013
    36
  val and_list1: (token list -> 'a * token list) -> token list -> 'a list * token list
wenzelm@5826
    37
  val name: token list -> bstring * token list
wenzelm@5826
    38
  val xname: token list -> xstring * token list
wenzelm@5826
    39
  val text: token list -> string * token list
wenzelm@6553
    40
  val comment: token list -> Comment.text * token list
wenzelm@6553
    41
  val marg_comment: token list -> Comment.text * token list
wenzelm@6553
    42
  val interest: token list -> Comment.interest * token list
wenzelm@5826
    43
  val sort: token list -> xsort * token list
wenzelm@5826
    44
  val arity: token list -> (xsort list * xsort) * token list
wenzelm@6372
    45
  val simple_arity: token list -> (xsort list * xclass) * token list
wenzelm@5826
    46
  val type_args: token list -> string list * token list
wenzelm@5826
    47
  val typ: token list -> string * token list
wenzelm@5826
    48
  val opt_infix: token list -> Syntax.mixfix * token list
wenzelm@5826
    49
  val opt_mixfix: token list -> Syntax.mixfix * token list
wenzelm@5826
    50
  val const: token list -> (bstring * string * Syntax.mixfix) * token list
wenzelm@5826
    51
  val term: token list -> string * token list
wenzelm@5826
    52
  val prop: token list -> string * token list
wenzelm@6935
    53
  val propp: token list -> (string * (string list * string list)) * token list
wenzelm@6949
    54
  val termp: token list -> (string * string list) * token list
wenzelm@5826
    55
  val attribs: token list -> Args.src list * token list
wenzelm@5826
    56
  val opt_attribs: token list -> Args.src list * token list
wenzelm@5917
    57
  val thm_name: string -> token list -> (bstring * Args.src list) * token list
wenzelm@5917
    58
  val opt_thm_name: string -> token list -> (bstring * Args.src list) * token list
wenzelm@6372
    59
  val spec_name: token list -> ((bstring * string) * Args.src list) * token list
wenzelm@6372
    60
  val spec_opt_name: token list -> ((bstring * string) * Args.src list) * token list
wenzelm@5917
    61
  val xthm: token list -> (xstring * Args.src list) * token list
wenzelm@5917
    62
  val xthms1: token list -> (xstring * Args.src list) list * token list
wenzelm@5826
    63
  val method: token list -> Method.text * token list
wenzelm@5826
    64
  val triple1: ('a * 'b) * 'c -> 'a * 'b * 'c
wenzelm@5826
    65
  val triple2: 'a * ('b * 'c) -> 'a * 'b * 'c
wenzelm@6430
    66
  val triple_swap: ('a * 'b) * 'c -> ('a * 'c) * 'b
wenzelm@5826
    67
end;
wenzelm@5826
    68
wenzelm@5826
    69
structure OuterParse: OUTER_PARSE =
wenzelm@5826
    70
struct
wenzelm@5826
    71
wenzelm@5826
    72
type token = OuterLex.token;
wenzelm@5826
    73
wenzelm@5826
    74
wenzelm@5826
    75
(** error handling **)
wenzelm@5826
    76
wenzelm@5826
    77
(* group atomic parsers (no cuts!) *)
wenzelm@5826
    78
wenzelm@5826
    79
fun fail_with s = Scan.fail_with
wenzelm@5826
    80
  (fn [] => s ^ " expected (past end-of-file!)"
wenzelm@5826
    81
    | (tok :: _) => s ^ " expected,\nbut " ^ OuterLex.name_of tok ^ " " ^
wenzelm@5826
    82
      quote (OuterLex.val_of tok) ^ OuterLex.pos_of tok ^ " was found");
wenzelm@5826
    83
wenzelm@5826
    84
fun group s scan = scan || fail_with s;
wenzelm@5826
    85
wenzelm@5826
    86
wenzelm@5877
    87
(* cut *)
wenzelm@5826
    88
wenzelm@5826
    89
fun !!! scan =
wenzelm@5826
    90
  let
wenzelm@5826
    91
    fun get_pos [] = " (past end-of-file!)"
wenzelm@5826
    92
      | get_pos (tok :: _) = OuterLex.pos_of tok;
wenzelm@5826
    93
wenzelm@5826
    94
    fun err (toks, None) = "Outer syntax error" ^ get_pos toks
wenzelm@5826
    95
      | err (toks, Some msg) = "Outer syntax error" ^ get_pos toks ^ ": " ^ msg;
wenzelm@5826
    96
  in Scan.!! err scan end;
wenzelm@5826
    97
wenzelm@5826
    98
wenzelm@5826
    99
wenzelm@5826
   100
(** basic parsers **)
wenzelm@5826
   101
wenzelm@5826
   102
(* utils *)
wenzelm@5826
   103
wenzelm@5826
   104
fun triple1 ((x, y), z) = (x, y, z);
wenzelm@5826
   105
fun triple2 (x, (y, z)) = (x, y, z);
wenzelm@6430
   106
fun triple_swap ((x, y), z) = ((x, z), y);
wenzelm@5826
   107
wenzelm@5826
   108
wenzelm@5826
   109
(* tokens *)
wenzelm@5826
   110
wenzelm@5826
   111
fun position scan =
wenzelm@5826
   112
  (Scan.ahead (Scan.one OuterLex.not_eof) >> OuterLex.position_of) -- scan >> Library.swap;
wenzelm@5826
   113
wenzelm@5826
   114
fun kind k =
wenzelm@5826
   115
  group (OuterLex.str_of_kind k)
wenzelm@5826
   116
    (Scan.one (OuterLex.is_kind k) >> OuterLex.val_of);
wenzelm@5826
   117
wenzelm@7026
   118
val command = kind OuterLex.Command;
wenzelm@5826
   119
val keyword = kind OuterLex.Keyword;
wenzelm@5826
   120
val short_ident = kind OuterLex.Ident;
wenzelm@5826
   121
val long_ident = kind OuterLex.LongIdent;
wenzelm@5826
   122
val sym_ident = kind OuterLex.SymIdent;
wenzelm@5826
   123
val term_var = kind OuterLex.Var;
wenzelm@5826
   124
val text_var = kind OuterLex.TextVar;
wenzelm@5826
   125
val type_ident = kind OuterLex.TypeIdent;
wenzelm@5826
   126
val type_var = kind OuterLex.TypeVar;
wenzelm@5826
   127
val number = kind OuterLex.Nat;
wenzelm@5826
   128
val string = kind OuterLex.String;
wenzelm@5826
   129
val verbatim = kind OuterLex.Verbatim;
wenzelm@6860
   130
val sync = kind OuterLex.Sync;
wenzelm@5826
   131
val eof = kind OuterLex.EOF;
wenzelm@5826
   132
wenzelm@5826
   133
fun $$$ x =
wenzelm@5826
   134
  group (OuterLex.str_of_kind OuterLex.Keyword ^ " " ^ quote x)
wenzelm@7026
   135
    (Scan.one (OuterLex.keyword_with (equal x)) >> OuterLex.val_of);
wenzelm@5826
   136
wenzelm@5877
   137
val nat = number >> (fst o Term.read_int o Symbol.explode);
wenzelm@5826
   138
wenzelm@5826
   139
val not_eof = Scan.one OuterLex.not_eof;
wenzelm@5826
   140
wenzelm@5826
   141
wenzelm@5826
   142
(* enumerations *)
wenzelm@5826
   143
wenzelm@6003
   144
fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- !!! scan) >> op ::;
wenzelm@5826
   145
fun enum sep scan = enum1 sep scan || Scan.succeed [];
wenzelm@5826
   146
wenzelm@5826
   147
fun list1 scan = enum1 "," scan;
wenzelm@5826
   148
fun list scan = enum "," scan;
wenzelm@5826
   149
wenzelm@6013
   150
fun and_list1 scan = enum1 "and" scan;
wenzelm@6013
   151
fun and_list scan = enum "and" scan;
wenzelm@6013
   152
wenzelm@5826
   153
wenzelm@5960
   154
(* names and text *)
wenzelm@5826
   155
wenzelm@5877
   156
val name = group "name declaration" (short_ident || sym_ident || string);
wenzelm@5877
   157
val xname = group "name reference" (short_ident || long_ident || sym_ident || string);
wenzelm@6553
   158
val text = group "text" (short_ident || long_ident || sym_ident || string || verbatim);
wenzelm@6553
   159
wenzelm@6553
   160
wenzelm@6553
   161
(* formal comments *)
wenzelm@6553
   162
wenzelm@6553
   163
val comment = text >> Comment.plain;
wenzelm@6726
   164
val marg_comment = Scan.optional ($$$ "--" |-- comment) Comment.none;
wenzelm@7171
   165
wenzelm@7171
   166
val interest = Scan.optional ($$$ "%%" >> K Comment.no_interest ||
wenzelm@7171
   167
  $$$ "%" |-- Scan.optional nat 1 >> Comment.interest) Comment.default_interest;
wenzelm@5826
   168
wenzelm@5826
   169
wenzelm@6372
   170
(* sorts and arities *)
wenzelm@5826
   171
wenzelm@5826
   172
val sort =
wenzelm@5826
   173
  xname >> single || $$$ "{" |-- !!! (list xname --| $$$ "}");
wenzelm@5826
   174
wenzelm@6372
   175
fun gen_arity cod =
wenzelm@7352
   176
  Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) [] -- cod;
wenzelm@6372
   177
wenzelm@6372
   178
val arity = gen_arity sort;
wenzelm@6372
   179
val simple_arity = gen_arity name;
wenzelm@5826
   180
wenzelm@5826
   181
wenzelm@5826
   182
(* types *)
wenzelm@5826
   183
wenzelm@5826
   184
val typ =
wenzelm@6553
   185
  group "type" (short_ident || long_ident || sym_ident || type_ident || type_var || string);
wenzelm@5826
   186
wenzelm@5826
   187
val type_args =
wenzelm@5826
   188
  type_ident >> single ||
wenzelm@5826
   189
  $$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
wenzelm@5826
   190
  Scan.succeed [];
wenzelm@5826
   191
wenzelm@5826
   192
wenzelm@5826
   193
(* mixfix annotations *)
wenzelm@5826
   194
wenzelm@5826
   195
val infxl = $$$ "infixl" |-- !!! (nat >> Syntax.Infixl || string -- nat >> Syntax.InfixlName);
wenzelm@5826
   196
val infxr = $$$ "infixr" |-- !!! (nat >> Syntax.Infixr || string -- nat >> Syntax.InfixrName);
wenzelm@5826
   197
wenzelm@5826
   198
val binder =
wenzelm@5826
   199
  $$$ "binder" |--
wenzelm@5826
   200
    !!! (string -- ($$$ "[" |-- nat --| $$$ "]" -- nat || nat >> (fn n => (n, n))))
wenzelm@5826
   201
  >> (Syntax.Binder o triple2);
wenzelm@5826
   202
wenzelm@5826
   203
wenzelm@5826
   204
val opt_pris = Scan.optional ($$$ "[" |-- !!! (list nat --| $$$ "]")) [];
wenzelm@5826
   205
wenzelm@5826
   206
val mixfix =
wenzelm@5826
   207
  string -- opt_pris -- Scan.optional nat Syntax.max_pri
wenzelm@5826
   208
  >> (Syntax.Mixfix o triple1);
wenzelm@5826
   209
wenzelm@5826
   210
fun opt_fix fix =
wenzelm@5826
   211
  Scan.optional ($$$ "(" |-- !!! (fix --| $$$ ")")) Syntax.NoSyn;
wenzelm@5826
   212
wenzelm@5826
   213
val opt_infix = opt_fix (infxl || infxr);
wenzelm@6355
   214
val opt_mixfix = opt_fix (mixfix || binder || infxl || infxr);
wenzelm@5826
   215
wenzelm@5826
   216
wenzelm@5826
   217
(* consts *)
wenzelm@5826
   218
wenzelm@5826
   219
val const =
wenzelm@5826
   220
  name -- ($$$ "::" |-- !!! (typ -- opt_mixfix)) >> triple2;
wenzelm@5826
   221
wenzelm@5826
   222
wenzelm@5826
   223
(* terms *)
wenzelm@5826
   224
wenzelm@6983
   225
val trm = short_ident || long_ident || sym_ident || term_var || text_var || number || string;
wenzelm@5826
   226
wenzelm@5826
   227
val term = group "term" trm;
wenzelm@5826
   228
val prop = group "proposition" trm;
wenzelm@5826
   229
wenzelm@5826
   230
wenzelm@6949
   231
(* patterns *)
wenzelm@6935
   232
wenzelm@6949
   233
val is_terms = Scan.repeat1 ($$$ "is" |-- term);
wenzelm@6935
   234
val is_props = Scan.repeat1 ($$$ "is" |-- prop);
wenzelm@6935
   235
val concl_props = $$$ "concl" |-- !!! is_props;
wenzelm@7418
   236
val any_props = concl_props >> pair [] || is_props -- Scan.optional concl_props [];
wenzelm@6935
   237
wenzelm@6935
   238
val propp = prop -- Scan.optional ($$$ "(" |-- !!! (any_props --| $$$ ")")) ([], []);
wenzelm@6949
   239
val termp = term -- Scan.optional ($$$ "(" |-- !!! (is_terms --| $$$ ")")) [];
wenzelm@6935
   240
wenzelm@6935
   241
wenzelm@5826
   242
(* arguments *)
wenzelm@5826
   243
wenzelm@7026
   244
fun keyword_symid is_symid = Scan.one (OuterLex.keyword_with is_symid) >> OuterLex.val_of;
wenzelm@5826
   245
wenzelm@6983
   246
fun atom_arg is_symid blk =
wenzelm@5826
   247
  group "argument"
wenzelm@5877
   248
    (position (short_ident || long_ident || sym_ident || term_var || text_var ||
wenzelm@5877
   249
        type_ident || type_var || number) >> Args.ident ||
wenzelm@6983
   250
      position (keyword_symid is_symid) >> Args.keyword ||
wenzelm@5877
   251
      position string >> Args.string ||
wenzelm@5877
   252
      position (if blk then $$$ "," else Scan.fail) >> Args.keyword);
wenzelm@5826
   253
wenzelm@5877
   254
fun paren_args l r scan = position ($$$ l) -- !!! (scan true -- position ($$$ r))
wenzelm@5826
   255
  >> (fn (x, (ys, z)) => Args.keyword x :: ys @ [Args.keyword z]);
wenzelm@5826
   256
wenzelm@6983
   257
fun args is_symid blk x = Scan.optional (args1 is_symid blk) [] x
wenzelm@6983
   258
and args1 is_symid blk x =
wenzelm@5826
   259
  ((Scan.repeat1
wenzelm@6983
   260
    (Scan.repeat1 (atom_arg is_symid blk) ||
wenzelm@6983
   261
      paren_args "(" ")" (args is_symid) ||
wenzelm@6983
   262
      paren_args "{" "}" (args is_symid) ||
wenzelm@6983
   263
      paren_args "[" "]" (args is_symid))) >> flat) x;
wenzelm@5826
   264
wenzelm@5826
   265
wenzelm@6372
   266
(* theorem specifications *)
wenzelm@5826
   267
wenzelm@6983
   268
val attrib = position (xname -- !!! (args OuterLex.is_sid false)) >> Args.src;
wenzelm@5826
   269
val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
wenzelm@5826
   270
val opt_attribs = Scan.optional attribs [];
wenzelm@5826
   271
wenzelm@6398
   272
fun thm_name s = name -- opt_attribs --| $$$ s;
wenzelm@6511
   273
fun opt_thm_name s =
wenzelm@6511
   274
  Scan.optional ((name -- opt_attribs || (attribs >> pair "")) --| $$$ s) ("", []);;
wenzelm@5917
   275
wenzelm@6372
   276
val spec_name = thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
wenzelm@6372
   277
val spec_opt_name = opt_thm_name ":" -- prop >> (fn ((x, y), z) => ((x, z), y));
wenzelm@6372
   278
wenzelm@5917
   279
val xthm = xname -- opt_attribs;
wenzelm@6372
   280
val xthms1 = Scan.repeat1 xthm;
wenzelm@5826
   281
wenzelm@5826
   282
wenzelm@5826
   283
(* proof methods *)
wenzelm@5826
   284
wenzelm@6983
   285
fun is_symid_meth s =
wenzelm@7418
   286
  s <> "|" andalso s <> "?" andalso s <> "+" andalso OuterLex.is_sid s;
wenzelm@6983
   287
wenzelm@5826
   288
fun meth4 x =
wenzelm@5877
   289
 (position (xname >> rpair []) >> (Method.Source o Args.src) ||
wenzelm@6983
   290
  $$$ "(" |-- !!! (meth0 --| $$$ ")")) x
wenzelm@5826
   291
and meth3 x =
wenzelm@5826
   292
 (meth4 --| $$$ "?" >> Method.Try ||
wenzelm@5826
   293
  meth4 --| $$$ "+" >> Method.Repeat1 ||
wenzelm@5940
   294
  meth4) x
wenzelm@5940
   295
and meth2 x =
wenzelm@6983
   296
 (position (xname -- args1 is_symid_meth false) >> (Method.Source o Args.src) ||
wenzelm@5826
   297
  meth3) x
wenzelm@5826
   298
and meth1 x = (enum1 "," meth2 >> (fn [m] => m | ms => Method.Then ms)) x
wenzelm@5826
   299
and meth0 x = (enum1 "|" meth1 >> (fn [m] => m | ms => Method.Orelse ms)) x;
wenzelm@5826
   300
wenzelm@6558
   301
val method = meth3;
wenzelm@5826
   302
wenzelm@5826
   303
wenzelm@5826
   304
end;