author | lcp |
Mon, 31 Oct 1994 18:01:02 +0100 | |
changeset 670 | ff4c6691de9d |
parent 624 | 33b9b5da3e6f |
child 764 | b60e77395d1a |
permissions | -rw-r--r-- |
240 | 1 |
(* Title: Pure/Syntax/syn_ext.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
5 |
Syntax extension (internal interface). |
|
6 |
*) |
|
7 |
||
8 |
signature SYN_EXT0 = |
|
9 |
sig |
|
10 |
val typeT: typ |
|
11 |
val constrainC: string |
|
12 |
end; |
|
13 |
||
14 |
signature SYN_EXT = |
|
15 |
sig |
|
16 |
include SYN_EXT0 |
|
17 |
structure Ast: AST |
|
18 |
local open Ast in |
|
19 |
val logic: string |
|
20 |
val args: string |
|
21 |
val applC: string |
|
22 |
val typ_to_nonterm: typ -> string |
|
23 |
datatype xsymb = |
|
24 |
Delim of string | |
|
25 |
Argument of string * int | |
|
26 |
Space of string | |
|
27 |
Bg of int | Brk of int | En |
|
28 |
datatype xprod = XProd of string * xsymb list * string * int |
|
29 |
val max_pri: int |
|
30 |
val chain_pri: int |
|
31 |
val delims_of: xprod list -> string list |
|
32 |
datatype mfix = Mfix of string * typ * string * int list * int |
|
33 |
datatype syn_ext = |
|
34 |
SynExt of { |
|
35 |
roots: string list, |
|
36 |
xprods: xprod list, |
|
37 |
consts: string list, |
|
38 |
parse_ast_translation: (string * (ast list -> ast)) list, |
|
39 |
parse_rules: (ast * ast) list, |
|
40 |
parse_translation: (string * (term list -> term)) list, |
|
41 |
print_translation: (string * (term list -> term)) list, |
|
42 |
print_rules: (ast * ast) list, |
|
43 |
print_ast_translation: (string * (ast list -> ast)) list} |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
240
diff
changeset
|
44 |
val syn_ext: string list -> string list -> mfix list -> string list -> |
240 | 45 |
(string * (ast list -> ast)) list * (string * (term list -> term)) list * |
46 |
(string * (term list -> term)) list * (string * (ast list -> ast)) list |
|
47 |
-> (ast * ast) list * (ast * ast) list -> syn_ext |
|
555
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
48 |
val syn_ext_roots: string list -> string list -> syn_ext |
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
49 |
val syn_ext_const_names: string list -> string list -> syn_ext |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
240
diff
changeset
|
50 |
val syn_ext_rules: string list -> (ast * ast) list * (ast * ast) list -> syn_ext |
555
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
51 |
val syn_ext_trfuns: string list -> |
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
52 |
(string * (ast list -> ast)) list * (string * (term list -> term)) list * |
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
53 |
(string * (term list -> term)) list * (string * (ast list -> ast)) list |
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
54 |
-> syn_ext |
240 | 55 |
end |
56 |
end; |
|
57 |
||
58 |
functor SynExtFun(structure Lexicon: LEXICON and Ast: AST): SYN_EXT = |
|
59 |
struct |
|
60 |
||
61 |
structure Ast = Ast; |
|
62 |
open Lexicon Ast; |
|
63 |
||
64 |
||
65 |
(** misc definitions **) |
|
66 |
||
67 |
(* syntactic categories *) |
|
68 |
||
69 |
val logic = "logic"; |
|
70 |
val logicT = Type (logic, []); |
|
71 |
||
72 |
val logic1 = "logic1"; |
|
73 |
val logic1T = Type (logic1, []); |
|
74 |
||
75 |
val args = "args"; |
|
76 |
val argsT = Type (args, []); |
|
77 |
||
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
240
diff
changeset
|
78 |
val typeT = Type ("type", []); |
240 | 79 |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
240
diff
changeset
|
80 |
val funT = Type ("fun", []); |
240 | 81 |
|
624 | 82 |
val any = "any" |
83 |
val anyT = Type (any, []); |
|
84 |
||
240 | 85 |
|
86 |
(* constants *) |
|
87 |
||
88 |
val applC = "_appl"; |
|
89 |
val constrainC = "_constrain"; |
|
90 |
||
91 |
||
92 |
||
93 |
(** datatype xprod **) |
|
94 |
||
95 |
(*Delim s: delimiter s |
|
96 |
Argument (s, p): nonterminal s requiring priority >= p, or valued token |
|
97 |
Space s: some white space for printing |
|
98 |
Bg, Brk, En: blocks and breaks for pretty printing*) |
|
99 |
||
100 |
datatype xsymb = |
|
101 |
Delim of string | |
|
102 |
Argument of string * int | |
|
103 |
Space of string | |
|
104 |
Bg of int | Brk of int | En; |
|
105 |
||
106 |
||
107 |
(*XProd (lhs, syms, c, p): |
|
108 |
lhs: name of nonterminal on the lhs of the production |
|
109 |
syms: list of symbols on the rhs of the production |
|
110 |
c: head of parse tree |
|
111 |
p: priority of this production*) |
|
112 |
||
113 |
datatype xprod = XProd of string * xsymb list * string * int; |
|
114 |
||
115 |
val max_pri = 1000; (*maximum legal priority*) |
|
116 |
val chain_pri = ~1; (*dummy for chain productions*) |
|
117 |
||
118 |
||
119 |
(* delims_of *) |
|
120 |
||
121 |
fun delims_of xprods = |
|
122 |
let |
|
123 |
fun del_of (Delim s) = Some s |
|
124 |
| del_of _ = None; |
|
125 |
||
126 |
fun dels_of (XProd (_, xsymbs, _, _)) = |
|
127 |
mapfilter del_of xsymbs; |
|
128 |
in |
|
129 |
distinct (flat (map dels_of xprods)) |
|
130 |
end; |
|
131 |
||
132 |
||
133 |
||
134 |
(** datatype mfix **) |
|
135 |
||
136 |
(*Mfix (sy, ty, c, ps, p): |
|
137 |
sy: rhs of production as symbolic string |
|
138 |
ty: type description of production |
|
139 |
c: head of parse tree |
|
140 |
ps: priorities of arguments in sy |
|
141 |
p: priority of production*) |
|
142 |
||
143 |
datatype mfix = Mfix of string * typ * string * int list * int; |
|
144 |
||
145 |
||
146 |
(* typ_to_nonterm *) |
|
147 |
||
148 |
fun typ_to_nonterm (Type (c, _)) = c |
|
624 | 149 |
| typ_to_nonterm _ = any; |
240 | 150 |
|
151 |
fun typ_to_nonterm1 (Type (c, _)) = c |
|
152 |
| typ_to_nonterm1 _ = logic1; |
|
153 |
||
154 |
||
155 |
(* mfix_to_xprod *) |
|
156 |
||
157 |
fun mfix_to_xprod (Mfix (sy, typ, const, pris, pri)) = |
|
158 |
let |
|
159 |
fun err msg = |
|
160 |
(writeln ("Error in mixfix annotation " ^ quote sy ^ " for " ^ quote const); |
|
161 |
error msg); |
|
162 |
||
163 |
fun check_pri p = |
|
164 |
if p >= 0 andalso p <= max_pri then () |
|
165 |
else err ("precedence out of range: " ^ string_of_int p); |
|
166 |
||
167 |
fun blocks_ok [] 0 = true |
|
168 |
| blocks_ok [] _ = false |
|
169 |
| blocks_ok (Bg _ :: syms) n = blocks_ok syms (n + 1) |
|
170 |
| blocks_ok (En :: _) 0 = false |
|
171 |
| blocks_ok (En :: syms) n = blocks_ok syms (n - 1) |
|
172 |
| blocks_ok (_ :: syms) n = blocks_ok syms n; |
|
173 |
||
174 |
fun check_blocks syms = |
|
175 |
if blocks_ok syms 0 then () |
|
176 |
else err "unbalanced block parentheses"; |
|
177 |
||
178 |
||
179 |
fun is_meta c = c mem ["(", ")", "/", "_"]; |
|
180 |
||
181 |
fun scan_delim_char ("'" :: c :: cs) = |
|
182 |
if is_blank c then err "illegal spaces in delimiter" else (c, cs) |
|
183 |
| scan_delim_char ["'"] = err "trailing escape character" |
|
184 |
| scan_delim_char (chs as c :: cs) = |
|
185 |
if is_blank c orelse is_meta c then raise LEXICAL_ERROR else (c, cs) |
|
186 |
| scan_delim_char [] = raise LEXICAL_ERROR; |
|
187 |
||
188 |
val scan_symb = |
|
189 |
$$ "_" >> K (Argument ("", 0)) || |
|
190 |
$$ "(" -- scan_int >> (Bg o #2) || |
|
191 |
$$ ")" >> K En || |
|
192 |
$$ "/" -- $$ "/" >> K (Brk ~1) || |
|
193 |
$$ "/" -- scan_any is_blank >> (Brk o length o #2) || |
|
194 |
scan_any1 is_blank >> (Space o implode) || |
|
195 |
repeat1 scan_delim_char >> (Delim o implode); |
|
196 |
||
197 |
||
198 |
val cons_fst = apfst o cons; |
|
199 |
||
200 |
fun add_args [] ty [] = ([], typ_to_nonterm1 ty) |
|
201 |
| add_args [] _ _ = err "too many precedences" |
|
202 |
| add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) [] = |
|
203 |
cons_fst (Argument (typ_to_nonterm ty, 0)) (add_args syms tys []) |
|
204 |
| add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) (p :: ps) = |
|
205 |
cons_fst (Argument (typ_to_nonterm ty, p)) (add_args syms tys ps) |
|
206 |
| add_args (Argument _ :: _) _ _ = |
|
207 |
err "more arguments than in corresponding type" |
|
208 |
| add_args (sym :: syms) ty ps = cons_fst sym (add_args syms ty ps); |
|
209 |
||
210 |
||
211 |
fun is_arg (Argument _) = true |
|
212 |
| is_arg _ = false; |
|
213 |
||
214 |
fun is_term (Delim _) = true |
|
215 |
| is_term (Argument (s, _)) = is_terminal s |
|
216 |
| is_term _ = false; |
|
217 |
||
218 |
fun rem_pri (Argument (s, _)) = Argument (s, chain_pri) |
|
219 |
| rem_pri sym = sym; |
|
220 |
||
221 |
||
222 |
val (raw_symbs, _) = repeat scan_symb (explode sy); |
|
223 |
val (symbs, lhs) = add_args raw_symbs typ pris; |
|
224 |
val xprod = XProd (lhs, symbs, const, pri); |
|
225 |
in |
|
226 |
seq check_pri pris; |
|
227 |
check_pri pri; |
|
228 |
check_blocks symbs; |
|
229 |
||
230 |
if is_terminal lhs then err ("illegal lhs: " ^ lhs) |
|
231 |
else if const <> "" then xprod |
|
232 |
else if length (filter is_arg symbs) <> 1 then |
|
233 |
err "copy production must have exactly one argument" |
|
234 |
else if exists is_term symbs then xprod |
|
235 |
else XProd (lhs, map rem_pri symbs, "", chain_pri) |
|
236 |
end; |
|
237 |
||
238 |
||
239 |
(** datatype syn_ext **) |
|
240 |
||
241 |
datatype syn_ext = |
|
242 |
SynExt of { |
|
243 |
roots: string list, |
|
244 |
xprods: xprod list, |
|
245 |
consts: string list, |
|
246 |
parse_ast_translation: (string * (ast list -> ast)) list, |
|
247 |
parse_rules: (ast * ast) list, |
|
248 |
parse_translation: (string * (term list -> term)) list, |
|
249 |
print_translation: (string * (term list -> term)) list, |
|
250 |
print_rules: (ast * ast) list, |
|
251 |
print_ast_translation: (string * (ast list -> ast)) list}; |
|
252 |
||
253 |
||
254 |
(* syn_ext *) |
|
255 |
||
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
240
diff
changeset
|
256 |
fun syn_ext all_roots new_roots mfixes consts trfuns rules = |
240 | 257 |
let |
258 |
val (parse_ast_translation, parse_translation, print_translation, |
|
259 |
print_ast_translation) = trfuns; |
|
260 |
val (parse_rules, print_rules) = rules; |
|
261 |
||
624 | 262 |
val mfix_consts = distinct (map (fn (Mfix (_, _, c, _, _)) => c) mfixes); |
263 |
val mfixes' = (if "prop" mem new_roots then |
|
264 |
[Mfix ("'(_')", Type ("@prop_H", []) |
|
265 |
--> Type ("@prop_H", []), "", [0], max_pri), |
|
266 |
Mfix ("'(_')", Type ("@logic_H", []) |
|
267 |
--> Type ("@logic_H", []), "", [0], max_pri), |
|
268 |
Mfix ("'(_')", anyT --> anyT, "", [0], max_pri), |
|
269 |
Mfix ("_", propT --> Type ("@prop_H", []), "", [0], 0), |
|
270 |
Mfix ("_", propT --> anyT, "", [0], 0)] |
|
271 |
else []) @ |
|
272 |
(if all_roots = new_roots then |
|
273 |
[Mfix ("_", logicT --> Type ("@logic_H", []), "", [0], 0), |
|
274 |
Mfix ("_", logicT --> anyT, "", [0], 0)] |
|
275 |
else []) |
|
276 |
val xprods = map mfix_to_xprod mfixes |
|
277 |
@ map mfix_to_xprod mfixes'; |
|
240 | 278 |
in |
279 |
SynExt { |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
240
diff
changeset
|
280 |
roots = new_roots, |
624 | 281 |
xprods = xprods, |
368 | 282 |
consts = filter is_xid (consts union mfix_consts), |
240 | 283 |
parse_ast_translation = parse_ast_translation, |
284 |
parse_rules = parse_rules, |
|
285 |
parse_translation = parse_translation, |
|
286 |
print_translation = print_translation, |
|
287 |
print_rules = print_rules, |
|
288 |
print_ast_translation = print_ast_translation} |
|
289 |
end; |
|
290 |
||
291 |
||
555
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
292 |
fun syn_ext_roots roots new_roots = |
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
293 |
syn_ext roots new_roots [] [] ([], [], [], []) ([], []); |
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
294 |
|
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
295 |
fun syn_ext_const_names roots cs = |
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
296 |
syn_ext roots [] [] cs ([], [], [], []) ([], []); |
240 | 297 |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
240
diff
changeset
|
298 |
fun syn_ext_rules roots rules = |
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
240
diff
changeset
|
299 |
syn_ext roots [] [] [] ([], [], [], []) rules; |
240 | 300 |
|
555
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
301 |
fun syn_ext_trfuns roots trfuns = |
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
302 |
syn_ext roots [] [] [] trfuns ([], []); |
240 | 303 |
|
304 |
||
305 |
end; |