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