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