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