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