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