src/Pure/Isar/args.ML
author wenzelm
Thu Nov 19 11:45:26 1998 +0100 (1998-11-19)
changeset 5934 ecc224b81f7f
parent 5911 7da8033264fa
child 6447 83d8dabdae9a
permissions -rw-r--r--
term_pat vs. prop_pat;
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@5878
    25
  val enum1: string -> (T list -> 'a * T list) -> T list -> 'a list * T list
wenzelm@5878
    26
  val enum: string -> (T list -> 'a * T list) -> T list -> 'a list * T list
wenzelm@5878
    27
  val list1: (T list -> 'a * T list) -> T list -> 'a list * T list
wenzelm@5878
    28
  val list: (T list -> 'a * T list) -> T list -> 'a list * 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@5934
    32
  val global_term_pat: theory * T list -> term * (theory * T list)
wenzelm@5934
    33
  val global_prop_pat: theory * T list -> term * (theory * T list)
wenzelm@5878
    34
  val local_typ: Proof.context * T list -> typ * (Proof.context * T list)
wenzelm@5878
    35
  val local_term: Proof.context * T list -> term * (Proof.context * T list)
wenzelm@5878
    36
  val local_prop: Proof.context * T list -> term * (Proof.context * T list)
wenzelm@5934
    37
  val local_term_pat: Proof.context * T list -> term * (Proof.context * T list)
wenzelm@5934
    38
  val local_prop_pat: Proof.context * T list -> term * (Proof.context * T list)
wenzelm@5822
    39
  type src
wenzelm@5878
    40
  val src: (string * T list) * Position.T -> src
wenzelm@5878
    41
  val dest_src: src -> (string * T list) * Position.T
wenzelm@5878
    42
  val attribs: T list -> src list * T list
wenzelm@5878
    43
  val opt_attribs: T list -> src list * T list
wenzelm@5878
    44
  val syntax: string -> ('a * T list -> 'b * ('a * T list)) -> 'a -> src -> 'a * 'b
wenzelm@5822
    45
end;
wenzelm@5822
    46
wenzelm@5822
    47
structure Args: ARGS =
wenzelm@5822
    48
struct
wenzelm@5822
    49
wenzelm@5822
    50
wenzelm@5822
    51
(** datatype T **)
wenzelm@5822
    52
wenzelm@5822
    53
datatype kind = Ident | String | Keyword | EOF;
wenzelm@5878
    54
datatype T = Arg of kind * (string * Position.T);
wenzelm@5822
    55
wenzelm@5878
    56
fun val_of (Arg (_, (x, _))) = x;
wenzelm@5878
    57
fun pos_of (Arg (_, (_, pos))) = pos;
wenzelm@5822
    58
wenzelm@5878
    59
fun str_of (Arg (Ident, (x, _))) = enclose "'" "'" x
wenzelm@5878
    60
  | str_of (Arg (String, (x, _))) = quote x
wenzelm@5878
    61
  | str_of (Arg (Keyword, (x, _))) = x
wenzelm@5878
    62
  | str_of (Arg (EOF, _)) = "end-of-text";
wenzelm@5822
    63
wenzelm@5878
    64
fun arg kind x_pos = Arg (kind, x_pos);
wenzelm@5822
    65
val ident = arg Ident;
wenzelm@5822
    66
val string = arg String;
wenzelm@5822
    67
val keyword = arg Keyword;
wenzelm@5822
    68
wenzelm@5822
    69
wenzelm@5822
    70
(* eof *)
wenzelm@5822
    71
wenzelm@5878
    72
val eof = arg EOF ("", Position.none);
wenzelm@5822
    73
wenzelm@5822
    74
fun is_eof (Arg (EOF, _)) = true
wenzelm@5822
    75
  | is_eof _ = false;
wenzelm@5822
    76
wenzelm@5822
    77
val stopper = (eof, is_eof);
wenzelm@5822
    78
val not_eof = not o is_eof;
wenzelm@5822
    79
wenzelm@5822
    80
wenzelm@5822
    81
wenzelm@5822
    82
(** scanners **)
wenzelm@5822
    83
wenzelm@5878
    84
(* position *)
wenzelm@5878
    85
wenzelm@5878
    86
fun position scan = (Scan.ahead (Scan.one not_eof) >> pos_of) -- scan >> Library.swap;
wenzelm@5878
    87
wenzelm@5878
    88
wenzelm@5878
    89
(* cut *)
wenzelm@5878
    90
wenzelm@5878
    91
fun !!! scan =
wenzelm@5878
    92
  let
wenzelm@5878
    93
    fun get_pos [] = " (past end-of-text!)"
wenzelm@5878
    94
      | get_pos (Arg (_, (_, pos)) :: _) = Position.str_of pos;
wenzelm@5878
    95
wenzelm@5878
    96
    fun err (args, None) = "Argument syntax error" ^ get_pos args
wenzelm@5878
    97
      | err (args, Some msg) = "Argument syntax error" ^ get_pos args ^ ": " ^ msg;
wenzelm@5878
    98
  in Scan.!! err scan end;
wenzelm@5878
    99
wenzelm@5878
   100
wenzelm@5822
   101
(* basic *)
wenzelm@5822
   102
wenzelm@5878
   103
fun $$$$ x = Scan.one (fn Arg (k, (y, _)) => (k = Ident orelse k = Keyword) andalso x = y);
wenzelm@5878
   104
fun $$$ x = $$$$ x >> val_of;
wenzelm@5878
   105
wenzelm@5878
   106
val name = Scan.one (fn Arg (k, (x, _)) => k = Ident orelse k = String) >> val_of;
wenzelm@5878
   107
wenzelm@5878
   108
fun kind f = Scan.one (K true) :--
wenzelm@5878
   109
  (fn Arg (Ident, (x, _)) =>
wenzelm@5878
   110
    (case f x of Some y => Scan.succeed y | _ => Scan.fail)
wenzelm@5878
   111
  | _ => Scan.fail) >> #2;
wenzelm@5878
   112
wenzelm@5878
   113
val nat = kind Syntax.read_nat;
wenzelm@5878
   114
val var = kind (apsome #1 o try Term.dest_Var o Syntax.read_var);
wenzelm@5822
   115
wenzelm@5822
   116
wenzelm@5878
   117
(* enumerations *)
wenzelm@5878
   118
wenzelm@5878
   119
fun enum1 sep scan = scan -- Scan.repeat ($$$ sep |-- scan) >> op ::;
wenzelm@5878
   120
fun enum sep scan = enum1 sep scan || Scan.succeed [];
wenzelm@5878
   121
wenzelm@5878
   122
fun list1 scan = enum1 "," scan;
wenzelm@5878
   123
fun list scan = enum "," scan;
wenzelm@5878
   124
wenzelm@5822
   125
wenzelm@5878
   126
(* terms and types *)
wenzelm@5878
   127
wenzelm@5878
   128
fun gen_item read = Scan.depend (fn st => name >> (pair st o read st));
wenzelm@5878
   129
wenzelm@5878
   130
val global_typ = gen_item (ProofContext.read_typ o ProofContext.init);
wenzelm@5878
   131
val global_term = gen_item (ProofContext.read_term o ProofContext.init);
wenzelm@5878
   132
val global_prop = gen_item (ProofContext.read_prop o ProofContext.init);
wenzelm@5934
   133
val global_term_pat = gen_item (ProofContext.read_term_pat o ProofContext.init);
wenzelm@5934
   134
val global_prop_pat = gen_item (ProofContext.read_prop_pat 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@5934
   139
val local_term_pat = gen_item ProofContext.read_term_pat;
wenzelm@5934
   140
val local_prop_pat = gen_item ProofContext.read_prop_pat;
wenzelm@5878
   141
wenzelm@5878
   142
wenzelm@5878
   143
(* args *)
wenzelm@5878
   144
wenzelm@5878
   145
val exclude = explode "(){}[],";
wenzelm@5822
   146
wenzelm@5878
   147
fun atom_arg blk = Scan.one (fn Arg (k, (x, _)) =>
wenzelm@5878
   148
  k <> Keyword orelse not (x mem exclude) orelse blk andalso x = ",");
wenzelm@5878
   149
wenzelm@5878
   150
fun paren_args l r scan = $$$$ l -- !!! (scan true -- $$$$ r)
wenzelm@5878
   151
  >> (fn (x, (ys, z)) => x :: ys @ [z]);
wenzelm@5822
   152
wenzelm@5878
   153
fun args blk x = Scan.optional (args1 blk) [] x
wenzelm@5878
   154
and args1 blk x =
wenzelm@5878
   155
  ((Scan.repeat1
wenzelm@5878
   156
    (Scan.repeat1 (atom_arg blk) ||
wenzelm@5878
   157
      paren_args "(" ")" args ||
wenzelm@5878
   158
      paren_args "{" "}" args ||
wenzelm@5878
   159
      paren_args "[" "]" args)) >> flat) x;
wenzelm@5822
   160
wenzelm@5822
   161
wenzelm@5822
   162
wenzelm@5822
   163
(** type src **)
wenzelm@5822
   164
wenzelm@5878
   165
datatype src = Src of (string * T list) * Position.T;
wenzelm@5822
   166
wenzelm@5878
   167
val src = Src;
wenzelm@5878
   168
fun dest_src (Src src) = src;
wenzelm@5878
   169
wenzelm@5878
   170
fun err_in_src kind msg (Src ((s, args), pos)) =
wenzelm@5878
   171
  error (kind ^ " " ^ s ^ Position.str_of pos ^ ": " ^ msg ^ "\n  " ^
wenzelm@5878
   172
    space_implode " " (map str_of args));
wenzelm@5822
   173
wenzelm@5822
   174
wenzelm@5822
   175
(* argument syntax *)
wenzelm@5822
   176
wenzelm@5878
   177
fun syntax kind scan st (src as Src ((s, args), pos)) =
wenzelm@5878
   178
  (case handle_error (Scan.error (Scan.finite' stopper (Scan.option scan))) (st, args) of
wenzelm@5878
   179
    OK (Some x, (st', [])) => (st', x)
wenzelm@5878
   180
  | OK (_, (_, args')) => err_in_src kind "bad arguments" (Src ((s, args'), pos))
wenzelm@5911
   181
  | Error msg => err_in_src kind ("\n" ^ msg) src);
wenzelm@5878
   182
wenzelm@5878
   183
wenzelm@5878
   184
(* attribs *)
wenzelm@5878
   185
wenzelm@5878
   186
val attrib = position (name -- !!! (args false)) >> src;
wenzelm@5878
   187
val attribs = $$$ "[" |-- !!! (list attrib --| $$$ "]");
wenzelm@5878
   188
val opt_attribs = Scan.optional attribs [];
wenzelm@5822
   189
wenzelm@5822
   190
wenzelm@5822
   191
end;