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