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