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