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