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