src/Pure/Isar/args.ML
author wenzelm
Fri Apr 07 17:36:25 2000 +0200 (2000-04-07 ago)
changeset 8681 957a5fe9b212
parent 8549 851d39c10d9f
child 8687 24bff69370f0
permissions -rw-r--r--
apply etc.: comments;
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@5878
     5
Concrete argument syntax (of attributes and methods).
wenzelm@5822
     6
*)
wenzelm@5822
     7
wenzelm@5822
     8
signature ARGS =
wenzelm@5822
     9
sig
wenzelm@5822
    10
  type T
wenzelm@5822
    11
  val val_of: T -> string
wenzelm@5878
    12
  val pos_of: T -> Position.T
wenzelm@5878
    13
  val str_of: T -> string
wenzelm@5878
    14
  val ident: string * Position.T -> T
wenzelm@5878
    15
  val string: string * Position.T -> T
wenzelm@5878
    16
  val keyword: string * Position.T -> T
wenzelm@5822
    17
  val stopper: T * (T -> bool)
wenzelm@5822
    18
  val not_eof: T -> bool
wenzelm@5878
    19
  val position: (T list -> 'a * 'b) -> T list -> ('a * Position.T) * 'b
wenzelm@5878
    20
  val !!! : (T list -> 'a) -> T list -> 'a
wenzelm@5822
    21
  val $$$ : string -> T list -> string * T list
wenzelm@5822
    22
  val name: T list -> string * T list
wenzelm@5878
    23
  val nat: T list -> int * T list
wenzelm@5878
    24
  val var: T list -> indexname * T list
wenzelm@6447
    25
  val enum: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    26
  val enum1: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    27
  val and_list: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    28
  val and_list1: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@5878
    29
  val global_typ: theory * T list -> typ * (theory * T list)
wenzelm@5878
    30
  val global_term: theory * T list -> term * (theory * T list)
wenzelm@5878
    31
  val global_prop: theory * T list -> term * (theory * T list)
wenzelm@5878
    32
  val local_typ: Proof.context * T list -> typ * (Proof.context * T list)
wenzelm@5878
    33
  val local_term: Proof.context * T list -> term * (Proof.context * T list)
wenzelm@5878
    34
  val local_prop: Proof.context * T list -> term * (Proof.context * T list)
wenzelm@7553
    35
  val bang_facts: Proof.context * T list -> thm list * (Proof.context * T list)
wenzelm@8536
    36
  val goal_spec: ((int -> tactic) -> tactic) -> ('a * T list)
wenzelm@8536
    37
    -> ((int -> tactic) -> tactic) * ('a * T list)
wenzelm@5822
    38
  type src
wenzelm@5878
    39
  val src: (string * T list) * Position.T -> src
wenzelm@5878
    40
  val dest_src: src -> (string * T list) * Position.T
wenzelm@5878
    41
  val attribs: T list -> src list * T list
wenzelm@5878
    42
  val opt_attribs: T list -> src list * T list
wenzelm@8282
    43
  val syntax: string -> ('a * T list -> 'b * ('a * T list)) -> src -> 'a -> 'a * 'b
wenzelm@5822
    44
end;
wenzelm@5822
    45
wenzelm@5822
    46
structure Args: ARGS =
wenzelm@5822
    47
struct
wenzelm@5822
    48
wenzelm@5822
    49
wenzelm@5822
    50
(** datatype T **)
wenzelm@5822
    51
wenzelm@5822
    52
datatype kind = Ident | String | Keyword | EOF;
wenzelm@5878
    53
datatype T = Arg of kind * (string * Position.T);
wenzelm@5822
    54
wenzelm@5878
    55
fun val_of (Arg (_, (x, _))) = x;
wenzelm@5878
    56
fun pos_of (Arg (_, (_, pos))) = pos;
wenzelm@5822
    57
wenzelm@5878
    58
fun str_of (Arg (Ident, (x, _))) = enclose "'" "'" x
wenzelm@5878
    59
  | str_of (Arg (String, (x, _))) = quote x
wenzelm@5878
    60
  | str_of (Arg (Keyword, (x, _))) = x
wenzelm@5878
    61
  | str_of (Arg (EOF, _)) = "end-of-text";
wenzelm@5822
    62
wenzelm@5878
    63
fun arg kind x_pos = Arg (kind, x_pos);
wenzelm@5822
    64
val ident = arg Ident;
wenzelm@5822
    65
val string = arg String;
wenzelm@5822
    66
val keyword = arg Keyword;
wenzelm@5822
    67
wenzelm@5822
    68
wenzelm@5822
    69
(* eof *)
wenzelm@5822
    70
wenzelm@5878
    71
val eof = arg EOF ("", Position.none);
wenzelm@5822
    72
wenzelm@5822
    73
fun is_eof (Arg (EOF, _)) = true
wenzelm@5822
    74
  | is_eof _ = false;
wenzelm@5822
    75
wenzelm@5822
    76
val stopper = (eof, is_eof);
wenzelm@5822
    77
val not_eof = not o is_eof;
wenzelm@5822
    78
wenzelm@5822
    79
wenzelm@5822
    80
wenzelm@5822
    81
(** scanners **)
wenzelm@5822
    82
wenzelm@5878
    83
(* position *)
wenzelm@5878
    84
wenzelm@5878
    85
fun position scan = (Scan.ahead (Scan.one not_eof) >> pos_of) -- scan >> Library.swap;
wenzelm@5878
    86
wenzelm@5878
    87
wenzelm@5878
    88
(* cut *)
wenzelm@5878
    89
wenzelm@5878
    90
fun !!! scan =
wenzelm@5878
    91
  let
wenzelm@5878
    92
    fun get_pos [] = " (past end-of-text!)"
wenzelm@5878
    93
      | get_pos (Arg (_, (_, pos)) :: _) = Position.str_of pos;
wenzelm@5878
    94
wenzelm@5878
    95
    fun err (args, None) = "Argument syntax error" ^ get_pos args
wenzelm@5878
    96
      | err (args, Some msg) = "Argument syntax error" ^ get_pos args ^ ": " ^ msg;
wenzelm@5878
    97
  in Scan.!! err scan end;
wenzelm@5878
    98
wenzelm@5878
    99
wenzelm@5822
   100
(* basic *)
wenzelm@5822
   101
wenzelm@5878
   102
fun $$$$ x = Scan.one (fn Arg (k, (y, _)) => (k = Ident orelse k = Keyword) andalso x = y);
wenzelm@5878
   103
fun $$$ x = $$$$ x >> val_of;
wenzelm@5878
   104
wenzelm@5878
   105
val name = Scan.one (fn Arg (k, (x, _)) => k = Ident orelse k = String) >> val_of;
wenzelm@5878
   106
wenzelm@8233
   107
val keyword_symid =
wenzelm@8233
   108
  Scan.one (fn Arg (k, (x, _)) => k = Keyword andalso OuterLex.is_sid x) >> val_of;
wenzelm@8233
   109
wenzelm@5878
   110
fun kind f = Scan.one (K true) :--
wenzelm@5878
   111
  (fn Arg (Ident, (x, _)) =>
wenzelm@5878
   112
    (case f x of Some y => Scan.succeed y | _ => Scan.fail)
wenzelm@5878
   113
  | _ => Scan.fail) >> #2;
wenzelm@5878
   114
wenzelm@5878
   115
val nat = kind Syntax.read_nat;
wenzelm@5878
   116
val var = kind (apsome #1 o try Term.dest_Var o Syntax.read_var);
wenzelm@5822
   117
wenzelm@5822
   118
wenzelm@5878
   119
(* enumerations *)
wenzelm@5878
   120
wenzelm@6447
   121
fun enum1 sep scan = scan -- Scan.repeat (Scan.lift ($$$ sep) |-- scan) >> op ::;
wenzelm@5878
   122
fun enum sep scan = enum1 sep scan || Scan.succeed [];
wenzelm@5878
   123
wenzelm@6447
   124
fun and_list1 scan = enum1 "and" scan;
wenzelm@6447
   125
fun and_list scan = enum "and" scan;
wenzelm@5878
   126
wenzelm@5822
   127
wenzelm@5878
   128
(* terms and types *)
wenzelm@5878
   129
wenzelm@5878
   130
fun gen_item read = Scan.depend (fn st => name >> (pair st o read st));
wenzelm@5878
   131
wenzelm@5878
   132
val global_typ = gen_item (ProofContext.read_typ o ProofContext.init);
wenzelm@5878
   133
val global_term = gen_item (ProofContext.read_term o ProofContext.init);
wenzelm@5878
   134
val global_prop = gen_item (ProofContext.read_prop o ProofContext.init);
wenzelm@5822
   135
wenzelm@5878
   136
val local_typ = gen_item ProofContext.read_typ;
wenzelm@5878
   137
val local_term = gen_item ProofContext.read_term;
wenzelm@5878
   138
val local_prop = gen_item ProofContext.read_prop;
wenzelm@5878
   139
wenzelm@5878
   140
wenzelm@7553
   141
(* bang facts *)
wenzelm@7553
   142
wenzelm@7553
   143
val bang_facts = Scan.depend (fn ctxt =>
wenzelm@7553
   144
  ($$$ "!" >> K (ProofContext.prems_of ctxt) || Scan.succeed []) >> pair ctxt);
wenzelm@7553
   145
wenzelm@7553
   146
wenzelm@8536
   147
(* goal specification *)
wenzelm@8536
   148
wenzelm@8536
   149
(* range *)
wenzelm@8233
   150
wenzelm@8536
   151
val from_to =
wenzelm@8536
   152
  nat -- ($$$$ "-" |-- nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||
wenzelm@8536
   153
  nat --| $$$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||
wenzelm@8549
   154
  nat >> (fn i => fn tac => tac i) ||
wenzelm@8549
   155
  $$$$ "!" >> K ALLGOALS;
wenzelm@8536
   156
wenzelm@8536
   157
val goal = $$$$ "[" |-- !!! (from_to --| $$$$ "]");
wenzelm@8536
   158
fun goal_spec def = Scan.lift (Scan.optional goal def);
wenzelm@8233
   159
wenzelm@8233
   160
wenzelm@5878
   161
(* args *)
wenzelm@5878
   162
wenzelm@5878
   163
val exclude = explode "(){}[],";
wenzelm@5822
   164
wenzelm@5878
   165
fun atom_arg blk = Scan.one (fn Arg (k, (x, _)) =>
wenzelm@5878
   166
  k <> Keyword orelse not (x mem exclude) orelse blk andalso x = ",");
wenzelm@5878
   167
wenzelm@5878
   168
fun paren_args l r scan = $$$$ l -- !!! (scan true -- $$$$ r)
wenzelm@5878
   169
  >> (fn (x, (ys, z)) => x :: ys @ [z]);
wenzelm@5822
   170
wenzelm@5878
   171
fun args blk x = Scan.optional (args1 blk) [] x
wenzelm@5878
   172
and args1 blk x =
wenzelm@5878
   173
  ((Scan.repeat1
wenzelm@5878
   174
    (Scan.repeat1 (atom_arg blk) ||
wenzelm@5878
   175
      paren_args "(" ")" args ||
wenzelm@5878
   176
      paren_args "{" "}" args ||
wenzelm@5878
   177
      paren_args "[" "]" args)) >> flat) x;
wenzelm@5822
   178
wenzelm@5822
   179
wenzelm@5822
   180
wenzelm@5822
   181
(** type src **)
wenzelm@5822
   182
wenzelm@5878
   183
datatype src = Src of (string * T list) * Position.T;
wenzelm@5822
   184
wenzelm@5878
   185
val src = Src;
wenzelm@5878
   186
fun dest_src (Src src) = src;
wenzelm@5878
   187
wenzelm@5878
   188
fun err_in_src kind msg (Src ((s, args), pos)) =
wenzelm@5878
   189
  error (kind ^ " " ^ s ^ Position.str_of pos ^ ": " ^ msg ^ "\n  " ^
wenzelm@5878
   190
    space_implode " " (map str_of args));
wenzelm@5822
   191
wenzelm@5822
   192
wenzelm@5822
   193
(* argument syntax *)
wenzelm@5822
   194
wenzelm@8282
   195
fun syntax kind scan (src as Src ((s, args), pos)) st =
wenzelm@5878
   196
  (case handle_error (Scan.error (Scan.finite' stopper (Scan.option scan))) (st, args) of
wenzelm@5878
   197
    OK (Some x, (st', [])) => (st', x)
wenzelm@5878
   198
  | OK (_, (_, args')) => err_in_src kind "bad arguments" (Src ((s, args'), pos))
wenzelm@5911
   199
  | Error msg => err_in_src kind ("\n" ^ msg) src);
wenzelm@5878
   200
wenzelm@5878
   201
wenzelm@5878
   202
(* attribs *)
wenzelm@5878
   203
wenzelm@6447
   204
fun list1 scan = scan -- Scan.repeat ($$$ "," |-- scan) >> op ::;
wenzelm@6447
   205
fun list scan = list1 scan || Scan.succeed [];
wenzelm@6447
   206
wenzelm@8233
   207
val attrib = position ((keyword_symid || name) -- !!! (args false)) >> src;
wenzelm@5878
   208
val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
wenzelm@5878
   209
val opt_attribs = Scan.optional attribs [];
wenzelm@5822
   210
wenzelm@5822
   211
wenzelm@5822
   212
end;