src/Pure/Isar/args.ML
author wenzelm
Sat Jun 28 21:21:15 2008 +0200 (2008-06-28)
changeset 27382 b1285021424e
parent 27377 0b9295c598f6
child 27732 8dbf5761a24a
permissions -rw-r--r--
tuned nested args parser;
export generic_args1;
wenzelm@5822
     1
(*  Title:      Pure/Isar/args.ML
wenzelm@5822
     2
    ID:         $Id$
wenzelm@5822
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5822
     4
wenzelm@15703
     5
Concrete argument syntax of attributes, methods etc. -- with special
wenzelm@15703
     6
support for embedded values and static binding.
wenzelm@5822
     7
*)
wenzelm@5822
     8
wenzelm@5822
     9
signature ARGS =
wenzelm@5822
    10
sig
wenzelm@18728
    11
  datatype value =
wenzelm@21662
    12
    Text of string | Typ of typ | Term of term | Fact of thm list |
wenzelm@21662
    13
    Attribute of morphism -> attribute
wenzelm@5822
    14
  type T
wenzelm@9748
    15
  val string_of: T -> string
wenzelm@15703
    16
  val mk_ident: string * Position.T -> T
wenzelm@15703
    17
  val mk_string: string * Position.T -> T
wenzelm@18037
    18
  val mk_alt_string: string * Position.T -> T
wenzelm@15703
    19
  val mk_keyword: string * Position.T -> T
wenzelm@21496
    20
  val mk_text: string -> T
wenzelm@15703
    21
  val mk_typ: typ -> T
wenzelm@15703
    22
  val mk_term: term -> T
wenzelm@15703
    23
  val mk_fact: thm list -> T
wenzelm@21662
    24
  val mk_attribute: (morphism -> attribute) -> T
wenzelm@27234
    25
  val eof: T
wenzelm@5822
    26
  val stopper: T * (T -> bool)
wenzelm@5822
    27
  val not_eof: T -> bool
wenzelm@15703
    28
  type src
wenzelm@15703
    29
  val src: (string * T list) * Position.T -> src
wenzelm@15703
    30
  val dest_src: src -> (string * T list) * Position.T
wenzelm@21030
    31
  val pretty_src: Proof.context -> src -> Pretty.T
wenzelm@15703
    32
  val map_name: (string -> string) -> src -> src
wenzelm@21480
    33
  val morph_values: morphism -> src -> src
wenzelm@20263
    34
  val maxidx_values: src -> int -> int
wenzelm@15703
    35
  val assignable: src -> src
wenzelm@15703
    36
  val assign: value option -> T -> unit
wenzelm@15703
    37
  val closure: src -> src
wenzelm@27371
    38
  val context: Context.generic * T list -> Context.proof * (Context.generic * T list)
wenzelm@27371
    39
  val theory: Context.generic * T list -> Context.theory * (Context.generic * T list)
wenzelm@5878
    40
  val position: (T list -> 'a * 'b) -> T list -> ('a * Position.T) * 'b
wenzelm@5878
    41
  val !!! : (T list -> 'a) -> T list -> 'a
wenzelm@5822
    42
  val $$$ : string -> T list -> string * T list
wenzelm@10035
    43
  val add: T list -> string * T list
wenzelm@10035
    44
  val del: T list -> string * T list
wenzelm@8803
    45
  val colon: T list -> string * T list
wenzelm@10035
    46
  val query: T list -> string * T list
wenzelm@10035
    47
  val bang: T list -> string * T list
wenzelm@10035
    48
  val query_colon: T list -> string * T list
wenzelm@10035
    49
  val bang_colon: T list -> string * T list
wenzelm@8803
    50
  val parens: (T list -> 'a * T list) -> T list -> 'a * T list
wenzelm@10150
    51
  val bracks: (T list -> 'a * T list) -> T list -> 'a * T list
wenzelm@9809
    52
  val mode: string -> 'a * T list -> bool * ('a * T list)
wenzelm@15703
    53
  val maybe: (T list -> 'a * T list) -> T list -> 'a option * T list
wenzelm@24002
    54
  val terminator: T list -> T * T list
wenzelm@5822
    55
  val name: T list -> string * T list
wenzelm@18037
    56
  val alt_name: T list -> string * T list
wenzelm@16140
    57
  val symbol: T list -> string * T list
wenzelm@17064
    58
  val liberal_name: T list -> string * T list
wenzelm@5878
    59
  val nat: T list -> int * T list
wenzelm@9538
    60
  val int: T list -> int * T list
wenzelm@5878
    61
  val var: T list -> indexname * T list
wenzelm@15703
    62
  val list: (T list -> 'a * T list) -> T list -> 'a list * T list
wenzelm@15703
    63
  val list1: (T list -> 'a * T list) -> T list -> 'a list * T list
wenzelm@6447
    64
  val enum: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    65
  val enum1: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    66
  val and_list: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    67
  val and_list1: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@15703
    68
  val ahead: T list -> T * T list
wenzelm@21496
    69
  val internal_text: T list -> string * T list
wenzelm@15703
    70
  val internal_typ: T list -> typ * T list
wenzelm@15703
    71
  val internal_term: T list -> term * T list
wenzelm@15703
    72
  val internal_fact: T list -> thm list * T list
wenzelm@21662
    73
  val internal_attribute: T list -> (morphism -> attribute) * T list
wenzelm@21496
    74
  val named_text: (string -> string) -> T list -> string * T list
wenzelm@15703
    75
  val named_typ: (string -> typ) -> T list -> typ * T list
wenzelm@15703
    76
  val named_term: (string -> term) -> T list -> term * T list
wenzelm@15703
    77
  val named_fact: (string -> thm list) -> T list -> thm list * T list
wenzelm@24002
    78
  val named_attribute: (string -> morphism -> attribute) -> T list ->
wenzelm@24002
    79
    (morphism -> attribute) * T list
wenzelm@18635
    80
  val typ_abbrev: Context.generic * T list -> typ * (Context.generic * T list)
wenzelm@18635
    81
  val typ: Context.generic * T list -> typ * (Context.generic * T list)
wenzelm@18635
    82
  val term: Context.generic * T list -> term * (Context.generic * T list)
wenzelm@21724
    83
  val term_abbrev: Context.generic * T list -> term * (Context.generic * T list)
wenzelm@18635
    84
  val prop: Context.generic * T list -> term * (Context.generic * T list)
wenzelm@18635
    85
  val tyname: Context.generic * T list -> string * (Context.generic * T list)
wenzelm@18635
    86
  val const: Context.generic * T list -> string * (Context.generic * T list)
wenzelm@25343
    87
  val const_proper: Context.generic * T list -> string * (Context.generic * T list)
wenzelm@26336
    88
  val thm_sel: T list -> Facts.interval list * T list
wenzelm@18998
    89
  val bang_facts: Context.generic * T list -> thm list * (Context.generic * T list)
wenzelm@8536
    90
  val goal_spec: ((int -> tactic) -> tactic) -> ('a * T list)
wenzelm@8536
    91
    -> ((int -> tactic) -> tactic) * ('a * T list)
wenzelm@27382
    92
  val generic_args1: (string -> bool) -> T list -> T list * T list
wenzelm@15703
    93
  val attribs: (string -> string) -> T list -> src list * T list
wenzelm@15703
    94
  val opt_attribs: (string -> string) -> T list -> src list * T list
wenzelm@27377
    95
  val thm_name: (string -> string) -> string -> T list -> (string * src list) * T list
wenzelm@27377
    96
  val opt_thm_name: (string -> string) -> string -> T list -> (string * src list) * T list
haftmann@21879
    97
  val syntax: string -> ('b * T list -> 'a * ('b * T list)) -> src -> 'b -> 'a * 'b
wenzelm@18998
    98
  val context_syntax: string -> (Context.generic * T list -> 'a * (Context.generic * T list)) ->
haftmann@21879
    99
    src -> Proof.context -> 'a * Proof.context
wenzelm@5822
   100
end;
wenzelm@5822
   101
wenzelm@5822
   102
structure Args: ARGS =
wenzelm@5822
   103
struct
wenzelm@5822
   104
wenzelm@5822
   105
wenzelm@5822
   106
(** datatype T **)
wenzelm@5822
   107
wenzelm@15703
   108
(*An argument token is a symbol (with raw string value), together with
wenzelm@15703
   109
  an optional assigned internal value.  Note that an assignable ref
wenzelm@15703
   110
  designates an intermediate state of internalization -- it is NOT
wenzelm@15703
   111
  meant to persist.*)
wenzelm@15703
   112
wenzelm@18037
   113
datatype kind = Ident | String | AltString | Keyword | EOF;
wenzelm@15703
   114
wenzelm@15703
   115
type symbol = kind * string * Position.T;
wenzelm@5822
   116
wenzelm@15703
   117
datatype value =
wenzelm@21496
   118
  Text of string |
wenzelm@15703
   119
  Typ of typ |
wenzelm@15703
   120
  Term of term |
wenzelm@15703
   121
  Fact of thm list |
wenzelm@21662
   122
  Attribute of morphism -> attribute;
wenzelm@15703
   123
wenzelm@15703
   124
datatype slot =
wenzelm@15703
   125
  Empty |
wenzelm@15703
   126
  Value of value option |
wenzelm@15703
   127
  Assignable of value option ref;
wenzelm@15703
   128
wenzelm@15703
   129
datatype T = Arg of symbol * slot;
wenzelm@5822
   130
wenzelm@15703
   131
fun string_of (Arg ((Ident, x, _), _)) = x
wenzelm@18037
   132
  | string_of (Arg ((String, x, _), _)) = quote x
wenzelm@18037
   133
  | string_of (Arg ((AltString, x, _), _)) = enclose "`" "`" x
wenzelm@15703
   134
  | string_of (Arg ((Keyword, x, _), _)) = x
wenzelm@15703
   135
  | string_of (Arg ((EOF, _, _), _)) = "";
wenzelm@15703
   136
wenzelm@15703
   137
fun sym_of (Arg ((_, x, _), _)) = x;
wenzelm@15703
   138
fun pos_of (Arg ((_, _, pos), _)) = pos;
wenzelm@15703
   139
wenzelm@15703
   140
wenzelm@15703
   141
(* basic constructors *)
wenzelm@5822
   142
wenzelm@15703
   143
fun mk_symbol k (x, p) = Arg ((k, x, p), Empty);
wenzelm@15703
   144
fun mk_value k v = Arg ((Keyword, k, Position.none), Value (SOME v));
wenzelm@9748
   145
wenzelm@15703
   146
val mk_ident = mk_symbol Ident;
wenzelm@15703
   147
val mk_string = mk_symbol String;
wenzelm@18037
   148
val mk_alt_string = mk_symbol AltString;
wenzelm@15703
   149
val mk_keyword = mk_symbol Keyword;
wenzelm@21496
   150
val mk_text = mk_value "<text>" o Text;
wenzelm@15703
   151
val mk_typ = mk_value "<typ>" o Typ;
wenzelm@15703
   152
val mk_term = mk_value "<term>" o Term;
wenzelm@15703
   153
val mk_fact = mk_value "<fact>" o Fact;
wenzelm@15703
   154
val mk_attribute = mk_value "<attribute>" o Attribute;
wenzelm@5822
   155
wenzelm@5822
   156
wenzelm@5822
   157
(* eof *)
wenzelm@5822
   158
wenzelm@15703
   159
val eof = mk_symbol EOF ("", Position.none);
wenzelm@5822
   160
wenzelm@15703
   161
fun is_eof (Arg ((EOF, _, _), _)) = true
wenzelm@5822
   162
  | is_eof _ = false;
wenzelm@5822
   163
wenzelm@5822
   164
val stopper = (eof, is_eof);
wenzelm@5822
   165
val not_eof = not o is_eof;
wenzelm@5822
   166
wenzelm@5822
   167
wenzelm@5822
   168
wenzelm@15703
   169
(** datatype src **)
wenzelm@15703
   170
wenzelm@15703
   171
datatype src = Src of (string * T list) * Position.T;
wenzelm@15703
   172
wenzelm@15703
   173
val src = Src;
wenzelm@15703
   174
fun dest_src (Src src) = src;
wenzelm@15703
   175
wenzelm@21030
   176
fun pretty_src ctxt src =
wenzelm@21030
   177
  let
wenzelm@21697
   178
    val prt_thm = Pretty.backquote o ProofContext.pretty_thm ctxt;
wenzelm@21496
   179
    fun prt (Arg (_, Value (SOME (Text s)))) = Pretty.str (quote s)
wenzelm@24920
   180
      | prt (Arg (_, Value (SOME (Typ T)))) = Syntax.pretty_typ ctxt T
wenzelm@24920
   181
      | prt (Arg (_, Value (SOME (Term t)))) = Syntax.pretty_term ctxt t
wenzelm@21697
   182
      | prt (Arg (_, Value (SOME (Fact ths)))) =
wenzelm@21697
   183
          Pretty.enclose "(" ")" (Pretty.breaks (map prt_thm ths))
wenzelm@21030
   184
      | prt arg = Pretty.str (string_of arg);
wenzelm@21030
   185
    val (s, args) = #1 (dest_src src);
wenzelm@21030
   186
  in Pretty.block (Pretty.breaks (Pretty.str s :: map prt args)) end;
wenzelm@21030
   187
wenzelm@15703
   188
fun map_name f (Src ((s, args), pos)) = Src ((f s, args), pos);
wenzelm@15703
   189
fun map_args f (Src ((s, args), pos)) = Src ((s, map f args), pos);
wenzelm@15703
   190
wenzelm@15703
   191
wenzelm@15703
   192
(* values *)
wenzelm@15703
   193
wenzelm@15703
   194
fun map_value f (Arg (s, Value (SOME v))) = Arg (s, Value (SOME (f v)))
wenzelm@15703
   195
  | map_value _ arg = arg;
wenzelm@15703
   196
wenzelm@21480
   197
fun morph_values phi = map_args (map_value
wenzelm@21496
   198
  (fn Text s => Text s
wenzelm@21480
   199
    | Typ T => Typ (Morphism.typ phi T)
wenzelm@21480
   200
    | Term t => Term (Morphism.term phi t)
wenzelm@21976
   201
    | Fact ths => Fact (Morphism.fact phi ths)
wenzelm@22669
   202
    | Attribute att => Attribute (Morphism.transform phi att)));
wenzelm@15703
   203
wenzelm@20263
   204
fun maxidx_values (Src ((_, args), _)) = args |> fold
wenzelm@20263
   205
  (fn (Arg (_, Value (SOME (Typ T)))) => Term.maxidx_typ T
wenzelm@20263
   206
    | (Arg (_, Value (SOME (Term t)))) => Term.maxidx_term t
wenzelm@20263
   207
    | (Arg (_, Value (SOME (Fact ths)))) => fold Thm.maxidx_thm ths
wenzelm@20263
   208
    | _ => I);
wenzelm@20263
   209
wenzelm@15703
   210
wenzelm@15703
   211
(* static binding *)
wenzelm@15703
   212
wenzelm@15703
   213
(*1st stage: make empty slots assignable*)
wenzelm@15703
   214
val assignable =
wenzelm@15703
   215
  map_args (fn Arg (s, Empty) => Arg (s, Assignable (ref NONE)) | arg => arg);
wenzelm@15703
   216
wenzelm@15703
   217
(*2nd stage: assign values as side-effect of scanning*)
wenzelm@15703
   218
fun assign v (arg as Arg (_, Assignable r)) = r := v
wenzelm@15703
   219
  | assign _ _ = ();
wenzelm@15703
   220
wenzelm@15703
   221
val ahead = Scan.ahead (Scan.one not_eof);
wenzelm@15703
   222
fun touch scan = ahead -- scan >> (fn (arg, y) => (assign NONE arg; y));
wenzelm@15703
   223
wenzelm@15703
   224
(*3rd stage: static closure of final values*)
wenzelm@15703
   225
val closure =
wenzelm@15703
   226
  map_args (fn Arg (s, Assignable (ref v)) => Arg (s, Value v) | arg => arg);
wenzelm@15703
   227
wenzelm@15703
   228
wenzelm@15703
   229
wenzelm@5822
   230
(** scanners **)
wenzelm@5822
   231
wenzelm@27371
   232
(* context *)
wenzelm@27371
   233
wenzelm@27371
   234
fun context x = (Scan.state >> Context.proof_of) x;
wenzelm@27371
   235
fun theory x = (Scan.state >> Context.theory_of) x;
wenzelm@27371
   236
wenzelm@27371
   237
wenzelm@5878
   238
(* position *)
wenzelm@5878
   239
wenzelm@15703
   240
fun position scan =
wenzelm@15703
   241
  (Scan.ahead (Scan.one not_eof) >> pos_of) -- scan >> Library.swap;
wenzelm@5878
   242
wenzelm@5878
   243
wenzelm@5878
   244
(* cut *)
wenzelm@5878
   245
wenzelm@5878
   246
fun !!! scan =
wenzelm@5878
   247
  let
wenzelm@5878
   248
    fun get_pos [] = " (past end-of-text!)"
wenzelm@15703
   249
      | get_pos (arg :: _) = Position.str_of (pos_of arg);
wenzelm@5878
   250
skalberg@15531
   251
    fun err (args, NONE) = "Argument syntax error" ^ get_pos args
skalberg@15531
   252
      | err (args, SOME msg) = "Argument syntax error" ^ get_pos args ^ ": " ^ msg;
wenzelm@5878
   253
  in Scan.!! err scan end;
wenzelm@5878
   254
wenzelm@5878
   255
wenzelm@5822
   256
(* basic *)
wenzelm@5822
   257
wenzelm@15703
   258
fun some_ident f =
wenzelm@15703
   259
  touch (Scan.some (fn Arg ((Ident, x, _), _) => f x | _ => NONE));
wenzelm@15703
   260
wenzelm@15703
   261
val named =
wenzelm@15703
   262
  touch (Scan.one (fn Arg ((k, _, _), _) => k = Ident orelse k = String));
wenzelm@15703
   263
wenzelm@18037
   264
val alt_string =
wenzelm@18037
   265
  touch (Scan.one (fn Arg ((k, _, _), _) => k = AltString));
wenzelm@18037
   266
wenzelm@15703
   267
val symbolic =
wenzelm@15703
   268
  touch (Scan.one (fn Arg ((k, x, _), _) => k = Keyword andalso OuterLex.is_sid x));
wenzelm@15703
   269
wenzelm@15703
   270
fun &&& x =
wenzelm@15703
   271
  touch (Scan.one (fn Arg ((k, y, _), _) => k = Keyword andalso x = y));
wenzelm@15703
   272
wenzelm@15703
   273
fun $$$ x =
wenzelm@16140
   274
  touch (Scan.one (fn Arg ((k, y, _), _) => (k = Ident orelse k = Keyword) andalso x = y))
wenzelm@16140
   275
  >> sym_of;
wenzelm@5878
   276
wenzelm@10035
   277
val add = $$$ "add";
wenzelm@10035
   278
val del = $$$ "del";
wenzelm@8803
   279
val colon = $$$ ":";
wenzelm@10035
   280
val query = $$$ "?";
wenzelm@10035
   281
val bang = $$$ "!";
wenzelm@20111
   282
val query_colon = $$$ "?" ^^ $$$ ":";
wenzelm@20111
   283
val bang_colon = $$$ "!" ^^ $$$ ":";
wenzelm@10035
   284
wenzelm@8803
   285
fun parens scan = $$$ "(" |-- scan --| $$$ ")";
wenzelm@10150
   286
fun bracks scan = $$$ "[" |-- scan --| $$$ "]";
wenzelm@15703
   287
fun mode s = Scan.lift (Scan.optional (parens ($$$ s) >> K true) false);
wenzelm@15703
   288
fun maybe scan = $$$ "_" >> K NONE || scan >> SOME;
wenzelm@24002
   289
val terminator = Scan.ahead (Scan.one is_eof);
wenzelm@5878
   290
wenzelm@15703
   291
val name = named >> sym_of;
wenzelm@18037
   292
val alt_name = alt_string >> sym_of;
wenzelm@16140
   293
val symbol = symbolic >> sym_of;
wenzelm@17064
   294
val liberal_name = symbol || name;
wenzelm@8233
   295
wenzelm@24244
   296
val nat = some_ident Lexicon.read_nat;
wenzelm@9538
   297
val int = Scan.optional ($$$ "-" >> K ~1) 1 -- nat >> op *;
wenzelm@24244
   298
val var = some_ident Lexicon.read_variable;
wenzelm@14563
   299
wenzelm@5822
   300
wenzelm@5878
   301
(* enumerations *)
wenzelm@5878
   302
wenzelm@25999
   303
fun list1 scan = scan ::: Scan.repeat ($$$ "," |-- scan);
wenzelm@15703
   304
fun list scan = list1 scan || Scan.succeed [];
wenzelm@15703
   305
wenzelm@25999
   306
fun enum1 sep scan = scan ::: Scan.repeat (Scan.lift ($$$ sep) |-- scan);
wenzelm@5878
   307
fun enum sep scan = enum1 sep scan || Scan.succeed [];
wenzelm@5878
   308
wenzelm@6447
   309
fun and_list1 scan = enum1 "and" scan;
wenzelm@6447
   310
fun and_list scan = enum "and" scan;
wenzelm@5878
   311
wenzelm@5822
   312
wenzelm@15703
   313
(* values *)
wenzelm@15703
   314
wenzelm@15703
   315
fun value dest = Scan.some  (*no touch here*)
wenzelm@15703
   316
  (fn Arg (_, Value (SOME v)) => (SOME (dest v) handle Match => NONE)
wenzelm@15703
   317
    | Arg _ => NONE);
wenzelm@15703
   318
wenzelm@15703
   319
fun evaluate mk eval arg =
wenzelm@15703
   320
  let val x = eval (sym_of arg) in (assign (SOME (mk x)) arg; x) end;
wenzelm@15703
   321
wenzelm@21496
   322
val internal_text = value (fn Text s => s);
wenzelm@15703
   323
val internal_typ = value (fn Typ T => T);
wenzelm@15703
   324
val internal_term = value (fn Term t => t);
wenzelm@15703
   325
val internal_fact = value (fn Fact ths => ths);
wenzelm@15703
   326
val internal_attribute = value (fn Attribute att => att);
wenzelm@15703
   327
wenzelm@21496
   328
fun named_text intern = internal_text || named >> evaluate Text intern;
wenzelm@15703
   329
fun named_typ readT = internal_typ || named >> evaluate Typ readT;
wenzelm@15703
   330
fun named_term read = internal_term || named >> evaluate Term read;
wenzelm@18037
   331
fun named_fact get = internal_fact || (alt_string || named) >> evaluate Fact get;
wenzelm@24002
   332
fun named_attribute att = internal_attribute || named >> evaluate Attribute att;
wenzelm@15703
   333
wenzelm@15703
   334
wenzelm@5878
   335
(* terms and types *)
wenzelm@5878
   336
wenzelm@18635
   337
val typ_abbrev = Scan.peek (named_typ o ProofContext.read_typ_abbrev o Context.proof_of);
wenzelm@25331
   338
val typ = Scan.peek (named_typ o Syntax.read_typ o Context.proof_of);
wenzelm@24508
   339
val term = Scan.peek (named_term o Syntax.read_term o Context.proof_of);
wenzelm@21724
   340
val term_abbrev = Scan.peek (named_term o ProofContext.read_term_abbrev o Context.proof_of);
wenzelm@24508
   341
val prop = Scan.peek (named_term o Syntax.read_prop o Context.proof_of);
wenzelm@18635
   342
wenzelm@5878
   343
wenzelm@15703
   344
(* type and constant names *)
wenzelm@15703
   345
wenzelm@25323
   346
val tyname = Scan.peek (named_typ o ProofContext.read_tyname o Context.proof_of)
wenzelm@18998
   347
  >> (fn Type (c, _) => c | TFree (a, _) => a | _ => "");
wenzelm@15703
   348
wenzelm@25323
   349
val const = Scan.peek (named_term o ProofContext.read_const o Context.proof_of)
wenzelm@18998
   350
  >> (fn Const (c, _) => c | Free (x, _) => x | _ => "");
wenzelm@7553
   351
wenzelm@25343
   352
val const_proper = Scan.peek (named_term o ProofContext.read_const_proper o Context.proof_of)
wenzelm@25343
   353
  >> (fn Const (c, _) => c | _ => "");
wenzelm@25343
   354
wenzelm@15703
   355
wenzelm@15703
   356
(* misc *)
wenzelm@15703
   357
wenzelm@15703
   358
val thm_sel = parens (list1
wenzelm@26336
   359
 (nat --| $$$ "-" -- nat >> Facts.FromTo ||
wenzelm@26336
   360
  nat --| $$$ "-" >> Facts.From ||
wenzelm@26336
   361
  nat >> Facts.Single));
wenzelm@15703
   362
wenzelm@18998
   363
val bang_facts = Scan.peek (fn context =>
wenzelm@22932
   364
  $$$ "!" >> (fn _ => (warning "use of prems in proof method";
wenzelm@22932
   365
    Assumption.prems_of (Context.proof_of context))) || Scan.succeed []);
wenzelm@7553
   366
wenzelm@7553
   367
wenzelm@8536
   368
(* goal specification *)
wenzelm@8536
   369
wenzelm@8536
   370
val from_to =
wenzelm@15703
   371
  nat -- ($$$ "-" |-- nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||
wenzelm@15703
   372
  nat --| $$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||
wenzelm@8549
   373
  nat >> (fn i => fn tac => tac i) ||
wenzelm@15703
   374
  $$$ "!" >> K ALLGOALS;
wenzelm@8536
   375
wenzelm@15703
   376
val goal = $$$ "[" |-- !!! (from_to --| $$$ "]");
wenzelm@8536
   377
fun goal_spec def = Scan.lift (Scan.optional goal def);
wenzelm@8233
   378
wenzelm@8233
   379
wenzelm@27382
   380
(* nested args and attribs *)
wenzelm@15703
   381
wenzelm@15703
   382
local
wenzelm@5878
   383
wenzelm@27382
   384
fun parse_args is_symid =
wenzelm@27382
   385
  let
wenzelm@27382
   386
    fun atom blk = touch (Scan.one (fn Arg ((k, x, _), _) =>
wenzelm@27382
   387
      k <> Keyword orelse is_symid x orelse blk andalso x = ","));
wenzelm@5822
   388
wenzelm@27382
   389
    fun args blk x = Scan.optional (args1 blk) [] x
wenzelm@27382
   390
    and args1 blk x =
wenzelm@27382
   391
      ((Scan.repeat1
wenzelm@27382
   392
        (Scan.repeat1 (atom blk) ||
wenzelm@27382
   393
          argsp "(" ")" ||
wenzelm@27382
   394
          argsp "[" "]")) >> flat) x
wenzelm@27382
   395
    and argsp l r x =
wenzelm@27382
   396
      (Scan.trace (&&& l -- !!! (args true -- &&& r)) >> #2) x;
wenzelm@27382
   397
  in (args, args1) end;
wenzelm@15703
   398
wenzelm@15703
   399
in
wenzelm@5822
   400
wenzelm@27382
   401
fun generic_args1 is_symid = #2 (parse_args is_symid) false;
wenzelm@27382
   402
val arguments = #1 (parse_args OuterLex.is_sid) false;
wenzelm@27382
   403
wenzelm@15703
   404
fun attribs intern =
wenzelm@15703
   405
  let
wenzelm@21496
   406
    val attrib_name = internal_text || (symbolic || named) >> evaluate Text intern;
wenzelm@27382
   407
    val attrib = position (attrib_name -- !!! arguments) >> src;
wenzelm@15703
   408
  in $$$ "[" |-- !!! (list attrib --| $$$ "]") end;
wenzelm@15703
   409
wenzelm@15703
   410
fun opt_attribs intern = Scan.optional (attribs intern) [];
wenzelm@15703
   411
wenzelm@15703
   412
end;
wenzelm@5822
   413
wenzelm@5822
   414
wenzelm@27377
   415
(* theorem specifications *)
wenzelm@27377
   416
wenzelm@27377
   417
fun thm_name intern s = name -- opt_attribs intern --| $$$ s;
wenzelm@27377
   418
fun opt_thm_name intern s =
wenzelm@27377
   419
  Scan.optional ((name -- opt_attribs intern || attribs intern >> pair "") --| $$$ s) ("", []);
wenzelm@27377
   420
wenzelm@27377
   421
wenzelm@27382
   422
wenzelm@27382
   423
wenzelm@27382
   424
(** syntax wrapper **)
wenzelm@5822
   425
wenzelm@8282
   426
fun syntax kind scan (src as Src ((s, args), pos)) st =
wenzelm@9901
   427
  (case Scan.error (Scan.finite' stopper (Scan.option scan)) (st, args) of
haftmann@21879
   428
    (SOME x, (st', [])) => (x, st')
wenzelm@15703
   429
  | (_, (_, args')) =>
wenzelm@15703
   430
      error (kind ^ " " ^ quote s ^ Position.str_of pos ^ ": bad arguments\n  " ^
wenzelm@15703
   431
        space_implode " " (map string_of args')));
wenzelm@15703
   432
haftmann@21879
   433
fun context_syntax kind scan src = apsnd Context.the_proof o syntax kind scan src o Context.Proof;
wenzelm@18998
   434
wenzelm@5822
   435
end;