author | wenzelm |
Tue, 11 Jul 2006 12:17:01 +0200 | |
changeset 20076 | def4ad161528 |
parent 19482 | 9f11af8f7ef9 |
child 20675 | cb19d18aef01 |
permissions | -rw-r--r-- |
240 | 1 |
(* Title: Pure/Syntax/syn_ext.ML |
2 |
ID: $Id$ |
|
911
55754d6d399c
new in mixfix annotations: "' " (quote space) separates delimiters without
wenzelm
parents:
865
diff
changeset
|
3 |
Author: Markus Wenzel and Carsten Clasohm, TU Muenchen |
240 | 4 |
|
5 |
Syntax extension (internal interface). |
|
6 |
*) |
|
7 |
||
8 |
signature SYN_EXT0 = |
|
4050 | 9 |
sig |
6760 | 10 |
val dddot_indexname: indexname |
11 |
val constrainC: string |
|
240 | 12 |
val typeT: typ |
5690 | 13 |
val max_pri: int |
15754 | 14 |
val stamp_trfun: stamp -> string * 'a -> string * ('a * stamp) |
15 |
val mk_trfun: string * 'a -> string * ('a * stamp) |
|
16 |
val eq_trfun: ('a * stamp) * ('a * stamp) -> bool |
|
15835
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
17 |
val tokentrans_mode: string -> (string * (string -> string * real)) list -> |
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
18 |
(string * string * (string -> string * real)) list |
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
19 |
val standard_token_classes: string list |
4050 | 20 |
end; |
240 | 21 |
|
22 |
signature SYN_EXT = |
|
4050 | 23 |
sig |
240 | 24 |
include SYN_EXT0 |
1510 | 25 |
val logic: string |
26 |
val args: string |
|
27 |
val cargs: string |
|
28 |
val any: string |
|
29 |
val sprop: string |
|
30 |
val typ_to_nonterm: typ -> string |
|
31 |
datatype xsymb = |
|
32 |
Delim of string | |
|
33 |
Argument of string * int | |
|
34 |
Space of string | |
|
35 |
Bg of int | Brk of int | En |
|
36 |
datatype xprod = XProd of string * xsymb list * string * int |
|
37 |
val chain_pri: int |
|
4701 | 38 |
val delims_of: xprod list -> string list list |
1510 | 39 |
datatype mfix = Mfix of string * typ * string * int list * int |
40 |
datatype syn_ext = |
|
41 |
SynExt of { |
|
42 |
xprods: xprod list, |
|
43 |
consts: string list, |
|
2913 | 44 |
prmodes: string list, |
18857 | 45 |
parse_ast_translation: |
46 |
(string * ((Context.generic -> Ast.ast list -> Ast.ast) * stamp)) list, |
|
1510 | 47 |
parse_rules: (Ast.ast * Ast.ast) list, |
18857 | 48 |
parse_translation: |
49 |
(string * ((Context.generic -> term list -> term) * stamp)) list, |
|
50 |
print_translation: |
|
51 |
(string * ((Context.generic -> bool -> typ -> term list -> term) * stamp)) list, |
|
1510 | 52 |
print_rules: (Ast.ast * Ast.ast) list, |
18857 | 53 |
print_ast_translation: |
54 |
(string * ((Context.generic -> Ast.ast list -> Ast.ast) * stamp)) list, |
|
6322 | 55 |
token_translation: (string * string * (string -> string * real)) list} |
19004 | 56 |
val mfix_delims: string -> string list |
4054 | 57 |
val mfix_args: string -> int |
12865 | 58 |
val escape_mfix: string -> string |
18566 | 59 |
val unlocalize_mfix: string -> string |
14903 | 60 |
val syn_ext': bool -> (string -> bool) -> mfix list -> |
18857 | 61 |
string list -> (string * ((Context.generic -> Ast.ast list -> Ast.ast) * stamp)) list * |
62 |
(string * ((Context.generic -> term list -> term) * stamp)) list * |
|
63 |
(string * ((Context.generic -> bool -> typ -> term list -> term) * stamp)) list * |
|
64 |
(string * ((Context.generic -> Ast.ast list -> Ast.ast) * stamp)) list |
|
6322 | 65 |
-> (string * string * (string -> string * real)) list |
1510 | 66 |
-> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext |
14903 | 67 |
val syn_ext: mfix list -> string list -> |
18857 | 68 |
(string * ((Context.generic -> Ast.ast list -> Ast.ast) * stamp)) list * |
69 |
(string * ((Context.generic -> term list -> term) * stamp)) list * |
|
70 |
(string * ((Context.generic -> bool -> typ -> term list -> term) * stamp)) list * |
|
71 |
(string * ((Context.generic -> Ast.ast list -> Ast.ast) * stamp)) list |
|
6322 | 72 |
-> (string * string * (string -> string * real)) list |
1510 | 73 |
-> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext |
14903 | 74 |
val syn_ext_const_names: string list -> syn_ext |
75 |
val syn_ext_rules: (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext |
|
76 |
val syn_ext_trfuns: |
|
15754 | 77 |
(string * ((Ast.ast list -> Ast.ast) * stamp)) list * |
78 |
(string * ((term list -> term) * stamp)) list * |
|
79 |
(string * ((bool -> typ -> term list -> term) * stamp)) list * |
|
80 |
(string * ((Ast.ast list -> Ast.ast) * stamp)) list -> syn_ext |
|
16610 | 81 |
val syn_ext_advanced_trfuns: |
18857 | 82 |
(string * ((Context.generic -> Ast.ast list -> Ast.ast) * stamp)) list * |
83 |
(string * ((Context.generic -> term list -> term) * stamp)) list * |
|
84 |
(string * ((Context.generic -> bool -> typ -> term list -> term) * stamp)) list * |
|
85 |
(string * ((Context.generic -> Ast.ast list -> Ast.ast) * stamp)) list -> syn_ext |
|
14903 | 86 |
val syn_ext_tokentrfuns: (string * string * (string -> string * real)) list -> syn_ext |
15835
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
87 |
val standard_token_markers: string list |
1510 | 88 |
val pure_ext: syn_ext |
4050 | 89 |
end; |
240 | 90 |
|
15754 | 91 |
structure SynExt: SYN_EXT = |
240 | 92 |
struct |
93 |
||
2694 | 94 |
|
240 | 95 |
(** misc definitions **) |
96 |
||
7472 | 97 |
val dddot_indexname = ("dddot", 0); |
6760 | 98 |
val constrainC = "_constrain"; |
99 |
||
100 |
||
240 | 101 |
(* syntactic categories *) |
102 |
||
103 |
val logic = "logic"; |
|
104 |
val logicT = Type (logic, []); |
|
105 |
||
106 |
val args = "args"; |
|
1178 | 107 |
val cargs = "cargs"; |
240 | 108 |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
240
diff
changeset
|
109 |
val typeT = Type ("type", []); |
240 | 110 |
|
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
111 |
val sprop = "#prop"; |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
112 |
val spropT = Type (sprop, []); |
240 | 113 |
|
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
114 |
val any = "any"; |
624 | 115 |
val anyT = Type (any, []); |
116 |
||
780 | 117 |
|
240 | 118 |
|
119 |
(** datatype xprod **) |
|
120 |
||
121 |
(*Delim s: delimiter s |
|
122 |
Argument (s, p): nonterminal s requiring priority >= p, or valued token |
|
123 |
Space s: some white space for printing |
|
124 |
Bg, Brk, En: blocks and breaks for pretty printing*) |
|
125 |
||
126 |
datatype xsymb = |
|
127 |
Delim of string | |
|
128 |
Argument of string * int | |
|
129 |
Space of string | |
|
130 |
Bg of int | Brk of int | En; |
|
131 |
||
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
132 |
fun is_delim (Delim _) = true |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
133 |
| is_delim _ = false; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
134 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
135 |
fun is_terminal (Delim _) = true |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
136 |
| is_terminal (Argument (s, _)) = Lexicon.is_terminal s |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
137 |
| is_terminal _ = false; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
138 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
139 |
fun is_argument (Argument _) = true |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
140 |
| is_argument _ = false; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
141 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
142 |
fun is_index (Argument ("index", _)) = true |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
143 |
| is_index _ = false; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
144 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
145 |
val index = Argument ("index", 1000); |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
146 |
|
240 | 147 |
|
148 |
(*XProd (lhs, syms, c, p): |
|
149 |
lhs: name of nonterminal on the lhs of the production |
|
150 |
syms: list of symbols on the rhs of the production |
|
151 |
c: head of parse tree |
|
152 |
p: priority of this production*) |
|
153 |
||
154 |
datatype xprod = XProd of string * xsymb list * string * int; |
|
155 |
||
156 |
val max_pri = 1000; (*maximum legal priority*) |
|
157 |
val chain_pri = ~1; (*dummy for chain productions*) |
|
158 |
||
159 |
fun delims_of xprods = |
|
19004 | 160 |
fold (fn XProd (_, xsymbs, _, _) => |
161 |
fold (fn Delim s => insert (op =) s | _ => I) xsymbs) xprods [] |
|
162 |
|> map Symbol.explode; |
|
240 | 163 |
|
164 |
||
165 |
||
166 |
(** datatype mfix **) |
|
167 |
||
168 |
(*Mfix (sy, ty, c, ps, p): |
|
169 |
sy: rhs of production as symbolic string |
|
170 |
ty: type description of production |
|
171 |
c: head of parse tree |
|
172 |
ps: priorities of arguments in sy |
|
173 |
p: priority of production*) |
|
174 |
||
175 |
datatype mfix = Mfix of string * typ * string * int list * int; |
|
176 |
||
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
177 |
fun err_in_mfix msg (Mfix (sy, _, const, _, _)) = |
18678 | 178 |
cat_error msg ("in mixfix annotation " ^ quote sy ^ " for " ^ quote const); |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
179 |
|
240 | 180 |
|
181 |
(* typ_to_nonterm *) |
|
182 |
||
865 | 183 |
fun typ_to_nt _ (Type (c, _)) = c |
184 |
| typ_to_nt default _ = default; |
|
185 |
||
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
186 |
(*get nonterminal for rhs*) |
865 | 187 |
val typ_to_nonterm = typ_to_nt any; |
240 | 188 |
|
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
189 |
(*get nonterminal for lhs*) |
865 | 190 |
val typ_to_nonterm1 = typ_to_nt logic; |
240 | 191 |
|
192 |
||
19004 | 193 |
(* read mixfix annotations *) |
4050 | 194 |
|
195 |
local |
|
19004 | 196 |
|
197 |
fun is_meta c = c mem ["(", ")", "/", "_", "\\<index>"]; |
|
198 |
||
199 |
val scan_delim_char = |
|
200 |
$$ "'" |-- Scan.one ((not o Symbol.is_blank) andf Symbol.not_eof) || |
|
201 |
Scan.one ((not o is_meta) andf (not o Symbol.is_blank) andf Symbol.not_eof); |
|
202 |
||
203 |
fun read_int ["0", "0"] = ~1 |
|
204 |
| read_int cs = #1 (Library.read_int cs); |
|
4050 | 205 |
|
19004 | 206 |
val scan_sym = |
207 |
$$ "_" >> K (Argument ("", 0)) || |
|
208 |
$$ "\\<index>" >> K index || |
|
209 |
$$ "(" |-- Scan.any Symbol.is_digit >> (Bg o read_int) || |
|
210 |
$$ ")" >> K En || |
|
211 |
$$ "/" -- $$ "/" >> K (Brk ~1) || |
|
212 |
$$ "/" |-- Scan.any Symbol.is_blank >> (Brk o length) || |
|
213 |
Scan.any1 Symbol.is_blank >> (Space o implode) || |
|
214 |
Scan.repeat1 scan_delim_char >> (Delim o implode); |
|
14819 | 215 |
|
19004 | 216 |
val scan_symb = |
217 |
scan_sym >> SOME || |
|
218 |
$$ "'" -- Scan.one Symbol.is_blank >> K NONE; |
|
219 |
||
19305 | 220 |
val scan_symbs = Scan.repeat scan_symb --| Scan.ahead (~$$ "'"); |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19305
diff
changeset
|
221 |
val read_symbs = map_filter I o the o Scan.read Symbol.stopper scan_symbs; |
4050 | 222 |
|
19004 | 223 |
fun unique_index xsymbs = |
224 |
if length (List.filter is_index xsymbs) <= 1 then xsymbs |
|
225 |
else error "Duplicate index arguments (\\<index>)"; |
|
4050 | 226 |
|
19004 | 227 |
in |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
228 |
|
19004 | 229 |
val read_mfix = unique_index o read_symbs o Symbol.explode; |
230 |
||
231 |
fun mfix_delims sy = fold_rev (fn Delim s => cons s | _ => I) (read_mfix sy) []; |
|
232 |
val mfix_args = length o List.filter is_argument o read_mfix; |
|
233 |
||
234 |
val escape_mfix = implode o map (fn s => if is_meta s then "'" ^ s else s) o Symbol.explode; |
|
235 |
||
4050 | 236 |
end; |
237 |
||
18566 | 238 |
val unlocalize_mfix = |
239 |
let |
|
240 |
fun unloc ("'" :: "\\<^loc>" :: ss) = unloc ss |
|
241 |
| unloc ("\\<^loc>" :: ss) = unloc ss |
|
242 |
| unloc (s :: ss) = s :: unloc ss |
|
243 |
| unloc [] = []; |
|
244 |
in Symbol.explode #> unloc #> implode end; |
|
245 |
||
4050 | 246 |
|
240 | 247 |
(* mfix_to_xprod *) |
248 |
||
14903 | 249 |
fun mfix_to_xprod convert is_logtype (mfix as Mfix (sy, typ, const, pris, pri)) = |
240 | 250 |
let |
251 |
fun check_pri p = |
|
252 |
if p >= 0 andalso p <= max_pri then () |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
253 |
else err_in_mfix ("Precedence out of range: " ^ string_of_int p) mfix; |
240 | 254 |
|
255 |
fun blocks_ok [] 0 = true |
|
256 |
| blocks_ok [] _ = false |
|
257 |
| blocks_ok (Bg _ :: syms) n = blocks_ok syms (n + 1) |
|
258 |
| blocks_ok (En :: _) 0 = false |
|
259 |
| blocks_ok (En :: syms) n = blocks_ok syms (n - 1) |
|
260 |
| blocks_ok (_ :: syms) n = blocks_ok syms n; |
|
261 |
||
262 |
fun check_blocks syms = |
|
263 |
if blocks_ok syms 0 then () |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
264 |
else err_in_mfix "Unbalanced block parentheses" mfix; |
240 | 265 |
|
266 |
||
267 |
val cons_fst = apfst o cons; |
|
268 |
||
269 |
fun add_args [] ty [] = ([], typ_to_nonterm1 ty) |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
270 |
| add_args [] _ _ = err_in_mfix "Too many precedences" mfix |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
271 |
| add_args ((arg as Argument ("index", _)) :: syms) ty ps = |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
272 |
cons_fst arg (add_args syms ty ps) |
240 | 273 |
| add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) [] = |
274 |
cons_fst (Argument (typ_to_nonterm ty, 0)) (add_args syms tys []) |
|
275 |
| add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) (p :: ps) = |
|
276 |
cons_fst (Argument (typ_to_nonterm ty, p)) (add_args syms tys ps) |
|
277 |
| add_args (Argument _ :: _) _ _ = |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
278 |
err_in_mfix "More arguments than in corresponding type" mfix |
240 | 279 |
| add_args (sym :: syms) ty ps = cons_fst sym (add_args syms ty ps); |
280 |
||
281 |
fun rem_pri (Argument (s, _)) = Argument (s, chain_pri) |
|
282 |
| rem_pri sym = sym; |
|
283 |
||
4054 | 284 |
fun logify_types copy_prod (a as (Argument (s, p))) = |
14903 | 285 |
if s <> "prop" andalso is_logtype s then Argument (logic, p) else a |
4054 | 286 |
| logify_types _ a = a; |
240 | 287 |
|
2364 | 288 |
|
19004 | 289 |
val raw_symbs = read_mfix sy handle ERROR msg => err_in_mfix msg mfix; |
15570 | 290 |
val args = List.filter (fn Argument _ => true | _ => false) raw_symbs; |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
291 |
val (const', typ', parse_rules) = |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
292 |
if not (exists is_index args) then (const, typ, []) |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
293 |
else |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
294 |
let |
14697 | 295 |
val indexed_const = if const <> "" then "_indexed_" ^ const |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
296 |
else err_in_mfix "Missing constant name for indexed syntax" mfix; |
14697 | 297 |
val rangeT = Term.range_type typ handle Match => |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
298 |
err_in_mfix "Missing structure argument for indexed syntax" mfix; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
299 |
|
20076 | 300 |
val xs = map Ast.Variable (Name.invent_list [] "xa" (length args - 1)); |
19012 | 301 |
val (xs1, xs2) = chop (find_index is_index args) xs; |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
302 |
val i = Ast.Variable "i"; |
14697 | 303 |
val lhs = Ast.mk_appl (Ast.Constant indexed_const) |
304 |
(xs1 @ [Ast.mk_appl (Ast.Constant "_index") [i]] @ xs2); |
|
305 |
val rhs = Ast.mk_appl (Ast.Constant const) (i :: xs); |
|
306 |
in (indexed_const, rangeT, [(lhs, rhs)]) end; |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
307 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
308 |
val (symbs, lhs) = add_args raw_symbs typ' pris; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
309 |
|
2364 | 310 |
val copy_prod = |
311 |
lhs mem ["prop", "logic"] |
|
312 |
andalso const <> "" |
|
313 |
andalso not (null symbs) |
|
314 |
andalso not (exists is_delim symbs); |
|
315 |
val lhs' = |
|
316 |
if convert andalso not copy_prod then |
|
14903 | 317 |
(if lhs = "prop" then sprop else if is_logtype lhs then logic else lhs) |
2364 | 318 |
else lhs; |
4054 | 319 |
val symbs' = map (logify_types copy_prod) symbs; |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
320 |
val xprod = XProd (lhs', symbs', const', pri); |
240 | 321 |
|
15570 | 322 |
val _ = (List.app check_pri pris; check_pri pri; check_blocks symbs'); |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
323 |
val xprod' = |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
324 |
if Lexicon.is_terminal lhs' then err_in_mfix ("Illegal lhs: " ^ lhs') mfix |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
325 |
else if const <> "" then xprod |
15570 | 326 |
else if length (List.filter is_argument symbs') <> 1 then |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
327 |
err_in_mfix "Copy production must have exactly one argument" mfix |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
328 |
else if exists is_terminal symbs' then xprod |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
329 |
else XProd (lhs', map rem_pri symbs', "", chain_pri); |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
330 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
331 |
in (xprod', parse_rules) end; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
332 |
|
240 | 333 |
|
334 |
||
335 |
(** datatype syn_ext **) |
|
336 |
||
337 |
datatype syn_ext = |
|
338 |
SynExt of { |
|
339 |
xprods: xprod list, |
|
340 |
consts: string list, |
|
2913 | 341 |
prmodes: string list, |
18857 | 342 |
parse_ast_translation: |
343 |
(string * ((Context.generic -> Ast.ast list -> Ast.ast) * stamp)) list, |
|
1510 | 344 |
parse_rules: (Ast.ast * Ast.ast) list, |
18857 | 345 |
parse_translation: |
346 |
(string * ((Context.generic -> term list -> term) * stamp)) list, |
|
347 |
print_translation: |
|
348 |
(string * ((Context.generic -> bool -> typ -> term list -> term) * stamp)) list, |
|
1510 | 349 |
print_rules: (Ast.ast * Ast.ast) list, |
18857 | 350 |
print_ast_translation: |
351 |
(string * ((Context.generic -> Ast.ast list -> Ast.ast) * stamp)) list, |
|
15754 | 352 |
token_translation: (string * string * (string -> string * real)) list}; |
240 | 353 |
|
354 |
||
355 |
(* syn_ext *) |
|
356 |
||
14903 | 357 |
fun syn_ext' convert is_logtype mfixes consts trfuns tokentrfuns (parse_rules, print_rules) = |
240 | 358 |
let |
359 |
val (parse_ast_translation, parse_translation, print_translation, |
|
360 |
print_ast_translation) = trfuns; |
|
361 |
||
14903 | 362 |
val (xprods, parse_rules') = map (mfix_to_xprod convert is_logtype) mfixes |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19305
diff
changeset
|
363 |
|> split_list |> apsnd (rev o flat); |
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
19012
diff
changeset
|
364 |
val mfix_consts = |
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
19012
diff
changeset
|
365 |
distinct (op =) (map (fn Mfix x => #3 x) mfixes @ map (fn XProd x => #3 x) xprods); |
240 | 366 |
in |
367 |
SynExt { |
|
624 | 368 |
xprods = xprods, |
11546 | 369 |
consts = consts union_string mfix_consts, |
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
19012
diff
changeset
|
370 |
prmodes = distinct (op =) (map (fn (m, _, _) => m) tokentrfuns), |
240 | 371 |
parse_ast_translation = parse_ast_translation, |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
372 |
parse_rules = parse_rules' @ parse_rules, |
240 | 373 |
parse_translation = parse_translation, |
374 |
print_translation = print_translation, |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
375 |
print_rules = map swap parse_rules' @ print_rules, |
2694 | 376 |
print_ast_translation = print_ast_translation, |
377 |
token_translation = tokentrfuns} |
|
240 | 378 |
end; |
379 |
||
2382 | 380 |
|
14903 | 381 |
val syn_ext = syn_ext' true (K false); |
555
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
wenzelm
parents:
441
diff
changeset
|
382 |
|
14903 | 383 |
fun syn_ext_const_names cs = syn_ext [] cs ([], [], [], []) [] ([], []); |
384 |
fun syn_ext_rules rules = syn_ext [] [] ([], [], [], []) [] rules; |
|
16610 | 385 |
fun syn_ext_advanced_trfuns trfuns = syn_ext [] [] trfuns [] ([], []); |
14903 | 386 |
fun syn_ext_tokentrfuns trfuns = syn_ext [] [] ([], [], [], []) trfuns ([], []); |
2382 | 387 |
|
16610 | 388 |
fun syn_ext_trfuns (atrs, trs, tr's, atr's) = |
389 |
let fun simple (name, (f, s)) = (name, (K f, s)) in |
|
390 |
syn_ext_advanced_trfuns (map simple atrs, map simple trs, map simple tr's, map simple atr's) |
|
391 |
end; |
|
392 |
||
15754 | 393 |
fun stamp_trfun s (c, f) = (c, (f, s)); |
394 |
fun mk_trfun tr = stamp_trfun (stamp ()) tr; |
|
395 |
fun eq_trfun ((_, s1:stamp), (_, s2)) = s1 = s2; |
|
396 |
||
240 | 397 |
|
15835
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
398 |
(* token translations *) |
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
399 |
|
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
400 |
fun tokentrans_mode m trs = map (fn (s, f) => (m, s, f)) trs; |
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
401 |
|
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
402 |
val standard_token_classes = |
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
403 |
["class", "tfree", "tvar", "free", "bound", "var", "num", "xnum", "xstr"]; |
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
404 |
|
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
405 |
val standard_token_markers = map (fn s => "_" ^ s) standard_token_classes; |
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
406 |
|
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
wenzelm
parents:
15754
diff
changeset
|
407 |
|
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
408 |
(* pure_ext *) |
240 | 409 |
|
14903 | 410 |
val pure_ext = syn_ext' false (K false) |
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
411 |
[Mfix ("_", spropT --> propT, "", [0], 0), |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
412 |
Mfix ("_", logicT --> anyT, "", [0], 0), |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
413 |
Mfix ("_", spropT --> anyT, "", [0], 0), |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
414 |
Mfix ("'(_')", logicT --> logicT, "", [0], max_pri), |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
415 |
Mfix ("'(_')", spropT --> spropT, "", [0], max_pri), |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
416 |
Mfix ("_::_", [logicT, typeT] ---> logicT, "_constrain", [4, 0], 3), |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
417 |
Mfix ("_::_", [spropT, typeT] ---> spropT, "_constrain", [4, 0], 3)] |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
418 |
[] |
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
419 |
([], [], [], []) |
2694 | 420 |
[] |
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
421 |
([], []); |
240 | 422 |
|
423 |
end; |