src/Pure/Isar/args.ML
author skalberg
Sun Feb 13 17:15:14 2005 +0100 (2005-02-13)
changeset 15531 08c8dad8e399
parent 14981 e73f8140af78
child 15570 8d8c70b41bab
permissions -rw-r--r--
Deleted Library.option type.
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@9748
    14
  val string_of: T -> string
wenzelm@5878
    15
  val ident: string * Position.T -> T
wenzelm@5878
    16
  val string: string * Position.T -> T
wenzelm@5878
    17
  val keyword: string * Position.T -> T
wenzelm@5822
    18
  val stopper: T * (T -> bool)
wenzelm@5822
    19
  val not_eof: T -> bool
wenzelm@5878
    20
  val position: (T list -> 'a * 'b) -> T list -> ('a * Position.T) * 'b
wenzelm@5878
    21
  val !!! : (T list -> 'a) -> T list -> 'a
wenzelm@5822
    22
  val $$$ : string -> T list -> string * T list
wenzelm@10035
    23
  val add: T list -> string * T list
wenzelm@10035
    24
  val del: T list -> string * T list
wenzelm@8803
    25
  val colon: T list -> string * T list
wenzelm@10035
    26
  val query: T list -> string * T list
wenzelm@10035
    27
  val bang: T list -> string * T list
wenzelm@10035
    28
  val query_colon: T list -> string * T list
wenzelm@10035
    29
  val bang_colon: T list -> string * T list
wenzelm@8803
    30
  val parens: (T list -> 'a * T list) -> T list -> 'a * T list
wenzelm@10150
    31
  val bracks: (T list -> 'a * T list) -> T list -> 'a * T list
wenzelm@9809
    32
  val mode: string -> 'a * T list -> bool * ('a * T list)
wenzelm@5822
    33
  val name: T list -> string * T list
wenzelm@8687
    34
  val name_dummy: T list -> string option * T list
wenzelm@5878
    35
  val nat: T list -> int * T list
wenzelm@9538
    36
  val int: T list -> int * T list
wenzelm@5878
    37
  val var: T list -> indexname * T list
wenzelm@6447
    38
  val enum: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    39
  val enum1: string -> ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    40
  val and_list: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@6447
    41
  val and_list1: ('a * T list -> 'b * ('a * T list)) -> 'a * T list -> 'b list * ('a * T list)
wenzelm@14780
    42
  val global_typ_raw: theory * T list -> typ * (theory * T list)
wenzelm@5878
    43
  val global_typ: theory * T list -> typ * (theory * T list)
wenzelm@5878
    44
  val global_term: theory * T list -> term * (theory * T list)
wenzelm@5878
    45
  val global_prop: theory * T list -> term * (theory * T list)
wenzelm@14780
    46
  val local_typ_raw: Proof.context * T list -> typ * (Proof.context * T list)
wenzelm@5878
    47
  val local_typ: Proof.context * T list -> typ * (Proof.context * T list)
wenzelm@5878
    48
  val local_term: Proof.context * T list -> term * (Proof.context * T list)
wenzelm@5878
    49
  val local_prop: Proof.context * T list -> term * (Proof.context * T list)
wenzelm@7553
    50
  val bang_facts: Proof.context * T list -> thm list * (Proof.context * T list)
wenzelm@8536
    51
  val goal_spec: ((int -> tactic) -> tactic) -> ('a * T list)
wenzelm@8536
    52
    -> ((int -> tactic) -> tactic) * ('a * T list)
wenzelm@5822
    53
  type src
wenzelm@5878
    54
  val src: (string * T list) * Position.T -> src
wenzelm@5878
    55
  val dest_src: src -> (string * T list) * Position.T
wenzelm@5878
    56
  val attribs: T list -> src list * T list
wenzelm@5878
    57
  val opt_attribs: T list -> src list * T list
wenzelm@8282
    58
  val syntax: string -> ('a * T list -> 'b * ('a * T list)) -> src -> 'a -> 'a * 'b
wenzelm@5822
    59
end;
wenzelm@5822
    60
wenzelm@5822
    61
structure Args: ARGS =
wenzelm@5822
    62
struct
wenzelm@5822
    63
wenzelm@5822
    64
wenzelm@5822
    65
(** datatype T **)
wenzelm@5822
    66
wenzelm@5822
    67
datatype kind = Ident | String | Keyword | EOF;
wenzelm@5878
    68
datatype T = Arg of kind * (string * Position.T);
wenzelm@5822
    69
wenzelm@5878
    70
fun val_of (Arg (_, (x, _))) = x;
wenzelm@5878
    71
fun pos_of (Arg (_, (_, pos))) = pos;
wenzelm@5822
    72
wenzelm@9901
    73
fun str_of (Arg (Ident, (x, _))) = x
kleing@14625
    74
  | str_of (Arg (String, (x, _))) = Library.quote x
wenzelm@5878
    75
  | str_of (Arg (Keyword, (x, _))) = x
wenzelm@5878
    76
  | str_of (Arg (EOF, _)) = "end-of-text";
wenzelm@5822
    77
wenzelm@9748
    78
fun string_of (Arg (Ident, (x, _))) = x
kleing@14625
    79
  | string_of (Arg (String, (x, _))) = Library.quote x
wenzelm@9748
    80
  | string_of (Arg (Keyword, (x, _))) = x
wenzelm@9748
    81
  | string_of (Arg (EOF, _)) = "";
wenzelm@9748
    82
wenzelm@5878
    83
fun arg kind x_pos = Arg (kind, x_pos);
wenzelm@5822
    84
val ident = arg Ident;
wenzelm@5822
    85
val string = arg String;
wenzelm@5822
    86
val keyword = arg Keyword;
wenzelm@5822
    87
wenzelm@5822
    88
wenzelm@5822
    89
(* eof *)
wenzelm@5822
    90
wenzelm@5878
    91
val eof = arg EOF ("", Position.none);
wenzelm@5822
    92
wenzelm@5822
    93
fun is_eof (Arg (EOF, _)) = true
wenzelm@5822
    94
  | is_eof _ = false;
wenzelm@5822
    95
wenzelm@5822
    96
val stopper = (eof, is_eof);
wenzelm@5822
    97
val not_eof = not o is_eof;
wenzelm@5822
    98
wenzelm@5822
    99
wenzelm@5822
   100
wenzelm@5822
   101
(** scanners **)
wenzelm@5822
   102
wenzelm@5878
   103
(* position *)
wenzelm@5878
   104
wenzelm@5878
   105
fun position scan = (Scan.ahead (Scan.one not_eof) >> pos_of) -- scan >> Library.swap;
wenzelm@5878
   106
wenzelm@5878
   107
wenzelm@5878
   108
(* cut *)
wenzelm@5878
   109
wenzelm@5878
   110
fun !!! scan =
wenzelm@5878
   111
  let
wenzelm@5878
   112
    fun get_pos [] = " (past end-of-text!)"
wenzelm@5878
   113
      | get_pos (Arg (_, (_, pos)) :: _) = Position.str_of pos;
wenzelm@5878
   114
skalberg@15531
   115
    fun err (args, NONE) = "Argument syntax error" ^ get_pos args
skalberg@15531
   116
      | err (args, SOME msg) = "Argument syntax error" ^ get_pos args ^ ": " ^ msg;
wenzelm@5878
   117
  in Scan.!! err scan end;
wenzelm@5878
   118
wenzelm@5878
   119
wenzelm@5822
   120
(* basic *)
wenzelm@5822
   121
wenzelm@5878
   122
fun $$$$ x = Scan.one (fn Arg (k, (y, _)) => (k = Ident orelse k = Keyword) andalso x = y);
wenzelm@5878
   123
fun $$$ x = $$$$ x >> val_of;
wenzelm@5878
   124
wenzelm@10035
   125
val add = $$$ "add";
wenzelm@10035
   126
val del = $$$ "del";
wenzelm@8803
   127
val colon = $$$ ":";
wenzelm@10035
   128
val query = $$$ "?";
wenzelm@10035
   129
val bang = $$$ "!";
wenzelm@10035
   130
val query_colon = $$$ "?:";
wenzelm@10035
   131
val bang_colon = $$$ "!:";
wenzelm@10035
   132
wenzelm@8803
   133
fun parens scan = $$$ "(" |-- scan --| $$$ ")";
wenzelm@10150
   134
fun bracks scan = $$$ "[" |-- scan --| $$$ "]";
wenzelm@9809
   135
fun mode s = Scan.lift (Scan.optional (parens ($$$$ s) >> K true) false);
wenzelm@8803
   136
wenzelm@5878
   137
val name = Scan.one (fn Arg (k, (x, _)) => k = Ident orelse k = String) >> val_of;
skalberg@15531
   138
val name_dummy = $$$ "_" >> K NONE || name >> SOME;
wenzelm@5878
   139
wenzelm@8233
   140
val keyword_symid =
wenzelm@8233
   141
  Scan.one (fn Arg (k, (x, _)) => k = Keyword andalso OuterLex.is_sid x) >> val_of;
wenzelm@8233
   142
wenzelm@5878
   143
fun kind f = Scan.one (K true) :--
wenzelm@14563
   144
  (fn Arg (Ident, (x, _)) =>
skalberg@15531
   145
    (case f x of SOME y => Scan.succeed y | _ => Scan.fail)
wenzelm@14563
   146
  | _ => Scan.fail) >> #2;
wenzelm@5878
   147
wenzelm@5878
   148
val nat = kind Syntax.read_nat;
wenzelm@9538
   149
val int = Scan.optional ($$$ "-" >> K ~1) 1 -- nat >> op *;
wenzelm@5822
   150
wenzelm@14563
   151
(*read variable name; leading '?' may be omitted if name contains no dot*)
wenzelm@14563
   152
val var = kind (apsome #1 o (fn s =>
skalberg@15531
   153
  SOME (Term.dest_Var (Syntax.read_var s))
skalberg@15531
   154
    handle _ => SOME (Term.dest_Var (Syntax.read_var ("?" ^ s)))
skalberg@15531
   155
    handle _ => NONE));
wenzelm@14563
   156
wenzelm@5822
   157
wenzelm@5878
   158
(* enumerations *)
wenzelm@5878
   159
wenzelm@6447
   160
fun enum1 sep scan = scan -- Scan.repeat (Scan.lift ($$$ sep) |-- scan) >> op ::;
wenzelm@5878
   161
fun enum sep scan = enum1 sep scan || Scan.succeed [];
wenzelm@5878
   162
wenzelm@6447
   163
fun and_list1 scan = enum1 "and" scan;
wenzelm@6447
   164
fun and_list scan = enum "and" scan;
wenzelm@5878
   165
wenzelm@5822
   166
wenzelm@5878
   167
(* terms and types *)
wenzelm@5878
   168
wenzelm@5878
   169
fun gen_item read = Scan.depend (fn st => name >> (pair st o read st));
wenzelm@5878
   170
wenzelm@14780
   171
val global_typ_raw = gen_item (ProofContext.read_typ_raw o ProofContext.init);
wenzelm@5878
   172
val global_typ = gen_item (ProofContext.read_typ o ProofContext.init);
wenzelm@5878
   173
val global_term = gen_item (ProofContext.read_term o ProofContext.init);
wenzelm@5878
   174
val global_prop = gen_item (ProofContext.read_prop o ProofContext.init);
wenzelm@5822
   175
wenzelm@14780
   176
val local_typ_raw = gen_item ProofContext.read_typ_raw;
wenzelm@5878
   177
val local_typ = gen_item ProofContext.read_typ;
wenzelm@5878
   178
val local_term = gen_item ProofContext.read_term;
wenzelm@5878
   179
val local_prop = gen_item ProofContext.read_prop;
wenzelm@5878
   180
wenzelm@5878
   181
wenzelm@7553
   182
(* bang facts *)
wenzelm@7553
   183
wenzelm@7553
   184
val bang_facts = Scan.depend (fn ctxt =>
wenzelm@7553
   185
  ($$$ "!" >> K (ProofContext.prems_of ctxt) || Scan.succeed []) >> pair ctxt);
wenzelm@7553
   186
wenzelm@7553
   187
wenzelm@8536
   188
(* goal specification *)
wenzelm@8536
   189
wenzelm@8536
   190
(* range *)
wenzelm@8233
   191
wenzelm@8536
   192
val from_to =
wenzelm@8536
   193
  nat -- ($$$$ "-" |-- nat) >> (fn (i, j) => fn tac => Seq.INTERVAL tac i j) ||
wenzelm@8536
   194
  nat --| $$$$ "-" >> (fn i => fn tac => fn st => Seq.INTERVAL tac i (Thm.nprems_of st) st) ||
wenzelm@8549
   195
  nat >> (fn i => fn tac => tac i) ||
wenzelm@8549
   196
  $$$$ "!" >> K ALLGOALS;
wenzelm@8536
   197
wenzelm@8536
   198
val goal = $$$$ "[" |-- !!! (from_to --| $$$$ "]");
wenzelm@8536
   199
fun goal_spec def = Scan.lift (Scan.optional goal def);
wenzelm@8233
   200
wenzelm@8233
   201
wenzelm@5878
   202
(* args *)
wenzelm@5878
   203
wenzelm@9126
   204
val exclude = explode "()[],";
wenzelm@5822
   205
wenzelm@5878
   206
fun atom_arg blk = Scan.one (fn Arg (k, (x, _)) =>
wenzelm@5878
   207
  k <> Keyword orelse not (x mem exclude) orelse blk andalso x = ",");
wenzelm@5878
   208
wenzelm@5878
   209
fun paren_args l r scan = $$$$ l -- !!! (scan true -- $$$$ r)
wenzelm@5878
   210
  >> (fn (x, (ys, z)) => x :: ys @ [z]);
wenzelm@5822
   211
wenzelm@5878
   212
fun args blk x = Scan.optional (args1 blk) [] x
wenzelm@5878
   213
and args1 blk x =
wenzelm@5878
   214
  ((Scan.repeat1
wenzelm@5878
   215
    (Scan.repeat1 (atom_arg blk) ||
wenzelm@5878
   216
      paren_args "(" ")" args ||
wenzelm@5878
   217
      paren_args "[" "]" args)) >> flat) x;
wenzelm@5822
   218
wenzelm@5822
   219
wenzelm@5822
   220
wenzelm@5822
   221
(** type src **)
wenzelm@5822
   222
wenzelm@5878
   223
datatype src = Src of (string * T list) * Position.T;
wenzelm@5822
   224
wenzelm@5878
   225
val src = Src;
wenzelm@5878
   226
fun dest_src (Src src) = src;
wenzelm@5878
   227
wenzelm@5878
   228
fun err_in_src kind msg (Src ((s, args), pos)) =
wenzelm@11503
   229
  error (kind ^ " " ^ quote s ^ Position.str_of pos ^ ": " ^ msg ^ "\n  " ^
wenzelm@5878
   230
    space_implode " " (map str_of args));
kleing@14625
   231
    
wenzelm@5822
   232
wenzelm@5822
   233
(* argument syntax *)
wenzelm@5822
   234
wenzelm@8282
   235
fun syntax kind scan (src as Src ((s, args), pos)) st =
wenzelm@9901
   236
  (case Scan.error (Scan.finite' stopper (Scan.option scan)) (st, args) of
skalberg@15531
   237
    (SOME x, (st', [])) => (st', x)
wenzelm@9901
   238
  | (_, (_, args')) => err_in_src kind "bad arguments" (Src ((s, args'), pos)));
wenzelm@5878
   239
wenzelm@5878
   240
wenzelm@5878
   241
(* attribs *)
wenzelm@5878
   242
wenzelm@6447
   243
fun list1 scan = scan -- Scan.repeat ($$$ "," |-- scan) >> op ::;
wenzelm@6447
   244
fun list scan = list1 scan || Scan.succeed [];
wenzelm@6447
   245
wenzelm@8233
   246
val attrib = position ((keyword_symid || name) -- !!! (args false)) >> src;
wenzelm@5878
   247
val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
wenzelm@5878
   248
val opt_attribs = Scan.optional attribs [];
wenzelm@5822
   249
wenzelm@5822
   250
wenzelm@5822
   251
end;