author | haftmann |
Mon, 15 Jun 2009 08:16:08 +0200 | |
changeset 31636 | 138625ae4067 |
parent 31056 | 01ac77eb660b |
child 31665 | a1f4d3b3f6c8 |
permissions | -rw-r--r-- |
28060 | 1 |
(* Title: Tools/code/code_printer.ML |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
||
4 |
Generic operations for pretty printing of target language code. |
|
5 |
*) |
|
6 |
||
7 |
signature CODE_PRINTER = |
|
8 |
sig |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28064
diff
changeset
|
9 |
val nerror: thm -> string -> 'a |
28060 | 10 |
|
11 |
val @@ : 'a * 'a -> 'a list |
|
12 |
val @| : 'a list * 'a -> 'a list |
|
13 |
val str: string -> Pretty.T |
|
14 |
val concat: Pretty.T list -> Pretty.T |
|
15 |
val brackets: Pretty.T list -> Pretty.T |
|
16 |
val semicolon: Pretty.T list -> Pretty.T |
|
17 |
val enum_default: string -> string -> string -> string -> Pretty.T list -> Pretty.T |
|
18 |
||
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
19 |
val first_upper: string -> string |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
20 |
val first_lower: string -> string |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
21 |
type var_ctxt |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
22 |
val make_vars: string list -> var_ctxt |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
23 |
val intro_vars: string list -> var_ctxt -> var_ctxt |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
24 |
val lookup_var: var_ctxt -> string -> string |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
25 |
|
31056
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
26 |
type literals |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
27 |
val Literals: { literal_char: string -> string, literal_string: string -> string, |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
28 |
literal_numeral: bool -> int -> string, |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
29 |
literal_list: Pretty.T list -> Pretty.T, infix_cons: int * string } |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
30 |
-> literals |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
31 |
val literal_char: literals -> string -> string |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
32 |
val literal_string: literals -> string -> string |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
33 |
val literal_numeral: literals -> bool -> int -> string |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
34 |
val literal_list: literals -> Pretty.T list -> Pretty.T |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
35 |
val infix_cons: literals -> int * string |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
36 |
|
28060 | 37 |
type lrx |
38 |
val L: lrx |
|
39 |
val R: lrx |
|
40 |
val X: lrx |
|
41 |
type fixity |
|
42 |
val BR: fixity |
|
43 |
val NOBR: fixity |
|
44 |
val INFX: int * lrx -> fixity |
|
45 |
val APP: fixity |
|
46 |
val brackify: fixity -> Pretty.T list -> Pretty.T |
|
47 |
val brackify_infix: int * lrx -> fixity -> Pretty.T list -> Pretty.T |
|
48 |
||
49 |
type itype = Code_Thingol.itype |
|
50 |
type iterm = Code_Thingol.iterm |
|
51 |
type const = Code_Thingol.const |
|
52 |
type dict = Code_Thingol.dict |
|
53 |
type tyco_syntax |
|
54 |
type const_syntax |
|
31056
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
55 |
type proto_const_syntax |
28060 | 56 |
val parse_infix: ('a -> 'b) -> lrx * int -> string |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28064
diff
changeset
|
57 |
-> int * ((fixity -> 'b -> Pretty.T) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28064
diff
changeset
|
58 |
-> fixity -> 'a list -> Pretty.T) |
28060 | 59 |
val parse_syntax: ('a -> 'b) -> OuterParse.token list |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28064
diff
changeset
|
60 |
-> (int * ((fixity -> 'b -> Pretty.T) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28064
diff
changeset
|
61 |
-> fixity -> 'a list -> Pretty.T)) option * OuterParse.token list |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28064
diff
changeset
|
62 |
val simple_const_syntax: (int * ((fixity -> iterm -> Pretty.T) |
31056
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
63 |
-> fixity -> (iterm * itype) list -> Pretty.T)) option -> proto_const_syntax option |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
64 |
val activate_const_syntax: theory -> literals |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
65 |
-> proto_const_syntax -> Code_Thingol.naming -> const_syntax * Code_Thingol.naming |
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
66 |
val gen_pr_app: (thm -> var_ctxt -> const * iterm list -> Pretty.T list) |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
67 |
-> (thm -> var_ctxt -> fixity -> iterm -> Pretty.T) |
31056
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
68 |
-> (string -> const_syntax option) |
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
69 |
-> thm -> var_ctxt -> fixity -> const * iterm list -> Pretty.T |
28060 | 70 |
val gen_pr_bind: ((string option * Pretty.T option) * itype -> Pretty.T) |
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
71 |
-> (thm -> var_ctxt -> fixity -> iterm -> Pretty.T) |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28064
diff
changeset
|
72 |
-> thm -> fixity |
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
73 |
-> (string option * iterm option) * itype -> var_ctxt -> Pretty.T * var_ctxt |
28060 | 74 |
|
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
75 |
val mk_name_module: Name.context -> string option -> (string -> string option) |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
76 |
-> 'a Graph.T -> string -> string |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
77 |
val dest_name: string -> string * string |
28060 | 78 |
end; |
79 |
||
80 |
structure Code_Printer : CODE_PRINTER = |
|
81 |
struct |
|
82 |
||
83 |
open Code_Thingol; |
|
84 |
||
85 |
fun nerror thm s = error (s ^ ",\nin equation " ^ Display.string_of_thm thm); |
|
86 |
||
87 |
(** assembling text pieces **) |
|
88 |
||
89 |
infixr 5 @@; |
|
90 |
infixr 5 @|; |
|
91 |
fun x @@ y = [x, y]; |
|
92 |
fun xs @| y = xs @ [y]; |
|
93 |
val str = PrintMode.setmp [] Pretty.str; |
|
94 |
val concat = Pretty.block o Pretty.breaks; |
|
95 |
val brackets = Pretty.enclose "(" ")" o Pretty.breaks; |
|
96 |
fun semicolon ps = Pretty.block [concat ps, str ";"]; |
|
97 |
fun enum_default default sep opn cls [] = str default |
|
98 |
| enum_default default sep opn cls xs = Pretty.enum sep opn cls xs; |
|
99 |
||
100 |
||
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
101 |
(** names and variable name contexts **) |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
102 |
|
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
103 |
type var_ctxt = string Symtab.table * Name.context; |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
104 |
|
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
105 |
fun make_vars names = (fold (fn name => Symtab.update_new (name, name)) names Symtab.empty, |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
106 |
Name.make_context names); |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
107 |
|
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
108 |
fun intro_vars names (namemap, namectxt) = |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
109 |
let |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
110 |
val (names', namectxt') = Name.variants names namectxt; |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
111 |
val namemap' = fold2 (curry Symtab.update) names names' namemap; |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
112 |
in (namemap', namectxt') end; |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
113 |
|
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
114 |
fun lookup_var (namemap, _) name = case Symtab.lookup namemap name |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
115 |
of SOME name' => name' |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
116 |
| NONE => error ("Invalid name in context: " ^ quote name); |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
117 |
|
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
118 |
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode; |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
119 |
val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode; |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
120 |
|
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
121 |
|
31056
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
122 |
(** pretty literals **) |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
123 |
|
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
124 |
datatype literals = Literals of { |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
125 |
literal_char: string -> string, |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
126 |
literal_string: string -> string, |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
127 |
literal_numeral: bool -> int -> string, |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
128 |
literal_list: Pretty.T list -> Pretty.T, |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
129 |
infix_cons: int * string |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
130 |
}; |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
131 |
|
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
132 |
fun dest_Literals (Literals lits) = lits; |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
133 |
|
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
134 |
val literal_char = #literal_char o dest_Literals; |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
135 |
val literal_string = #literal_string o dest_Literals; |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
136 |
val literal_numeral = #literal_numeral o dest_Literals; |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
137 |
val literal_list = #literal_list o dest_Literals; |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
138 |
val infix_cons = #infix_cons o dest_Literals; |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
139 |
|
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
140 |
|
28060 | 141 |
(** syntax printer **) |
142 |
||
143 |
(* binding priorities *) |
|
144 |
||
145 |
datatype lrx = L | R | X; |
|
146 |
||
147 |
datatype fixity = |
|
148 |
BR |
|
149 |
| NOBR |
|
150 |
| INFX of (int * lrx); |
|
151 |
||
152 |
val APP = INFX (~1, L); |
|
153 |
||
154 |
fun fixity_lrx L L = false |
|
155 |
| fixity_lrx R R = false |
|
156 |
| fixity_lrx _ _ = true; |
|
157 |
||
158 |
fun fixity NOBR _ = false |
|
159 |
| fixity _ NOBR = false |
|
160 |
| fixity (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) = |
|
161 |
pr < pr_ctxt |
|
162 |
orelse pr = pr_ctxt |
|
163 |
andalso fixity_lrx lr lr_ctxt |
|
164 |
orelse pr_ctxt = ~1 |
|
165 |
| fixity BR (INFX _) = false |
|
166 |
| fixity _ _ = true; |
|
167 |
||
168 |
fun gen_brackify _ [p] = p |
|
169 |
| gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps |
|
170 |
| gen_brackify false (ps as _::_) = Pretty.block ps; |
|
171 |
||
172 |
fun brackify fxy_ctxt = |
|
173 |
gen_brackify (fixity BR fxy_ctxt) o Pretty.breaks; |
|
174 |
||
175 |
fun brackify_infix infx fxy_ctxt = |
|
176 |
gen_brackify (fixity (INFX infx) fxy_ctxt) o Pretty.breaks; |
|
177 |
||
178 |
||
179 |
(* generic syntax *) |
|
180 |
||
181 |
type tyco_syntax = int * ((fixity -> itype -> Pretty.T) |
|
182 |
-> fixity -> itype list -> Pretty.T); |
|
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
183 |
type const_syntax = int * ((var_ctxt -> fixity -> iterm -> Pretty.T) |
31056
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
184 |
-> thm -> var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T); |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
185 |
type proto_const_syntax = int * (string list * (literals -> string list |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
186 |
-> (var_ctxt -> fixity -> iterm -> Pretty.T) |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
187 |
-> thm -> var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T)); |
28060 | 188 |
|
31056
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
189 |
fun simple_const_syntax (SOME (n, f)) = SOME (n, |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
190 |
([], (fn _ => fn _ => fn pr => fn thm => fn vars => f (pr vars)))) |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
191 |
| simple_const_syntax NONE = NONE; |
28060 | 192 |
|
31056
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
193 |
fun activate_const_syntax thy literals (n, (cs, f)) naming = |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
194 |
fold_map (Code_Thingol.ensure_declared_const thy) cs naming |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
195 |
|-> (fn cs' => pair (n, f literals cs')); |
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
196 |
|
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
197 |
fun gen_pr_app pr_app pr_term syntax_const thm vars fxy (app as ((c, (_, tys)), ts)) = |
28060 | 198 |
case syntax_const c |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28690
diff
changeset
|
199 |
of NONE => brackify fxy (pr_app thm vars app) |
28060 | 200 |
| SOME (k, pr) => |
201 |
let |
|
31056
01ac77eb660b
robustifed infrastructure for complex term syntax during code generation
haftmann
parents:
30648
diff
changeset
|
202 |
fun pr' fxy ts = pr (pr_term thm) thm vars fxy (ts ~~ curry Library.take k tys); |
28060 | 203 |
in if k = length ts |
204 |
then pr' fxy ts |
|
205 |
else if k < length ts |
|
206 |
then case chop k ts of (ts1, ts2) => |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28690
diff
changeset
|
207 |
brackify fxy (pr' APP ts1 :: map (pr_term thm vars BR) ts2) |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28690
diff
changeset
|
208 |
else pr_term thm vars fxy (Code_Thingol.eta_expand k app) |
28060 | 209 |
end; |
210 |
||
211 |
fun gen_pr_bind pr_bind pr_term thm (fxy : fixity) ((v, pat), ty : itype) vars = |
|
212 |
let |
|
213 |
val vs = case pat |
|
214 |
of SOME pat => Code_Thingol.fold_varnames (insert (op =)) pat [] |
|
215 |
| NONE => []; |
|
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
216 |
val vars' = intro_vars (the_list v) vars; |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
217 |
val vars'' = intro_vars vs vars'; |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
218 |
val v' = Option.map (lookup_var vars') v; |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28690
diff
changeset
|
219 |
val pat' = Option.map (pr_term thm vars'' fxy) pat; |
28060 | 220 |
in (pr_bind ((v', pat'), ty), vars'') end; |
221 |
||
222 |
||
223 |
(* mixfix syntax *) |
|
224 |
||
225 |
datatype 'a mixfix = |
|
226 |
Arg of fixity |
|
227 |
| Pretty of Pretty.T; |
|
228 |
||
229 |
fun mk_mixfix prep_arg (fixity_this, mfx) = |
|
230 |
let |
|
231 |
fun is_arg (Arg _) = true |
|
232 |
| is_arg _ = false; |
|
233 |
val i = (length o filter is_arg) mfx; |
|
234 |
fun fillin _ [] [] = |
|
235 |
[] |
|
236 |
| fillin pr (Arg fxy :: mfx) (a :: args) = |
|
237 |
(pr fxy o prep_arg) a :: fillin pr mfx args |
|
238 |
| fillin pr (Pretty p :: mfx) args = |
|
239 |
p :: fillin pr mfx args; |
|
240 |
in |
|
241 |
(i, fn pr => fn fixity_ctxt => fn args => |
|
242 |
gen_brackify (fixity fixity_this fixity_ctxt) (fillin pr mfx args)) |
|
243 |
end; |
|
244 |
||
245 |
fun parse_infix prep_arg (x, i) s = |
|
246 |
let |
|
247 |
val l = case x of L => INFX (i, L) | _ => INFX (i, X); |
|
248 |
val r = case x of R => INFX (i, R) | _ => INFX (i, X); |
|
249 |
in |
|
250 |
mk_mixfix prep_arg (INFX (i, x), |
|
251 |
[Arg l, (Pretty o Pretty.brk) 1, (Pretty o str) s, (Pretty o Pretty.brk) 1, Arg r]) |
|
252 |
end; |
|
253 |
||
254 |
fun parse_mixfix prep_arg s = |
|
255 |
let |
|
256 |
val sym_any = Scan.one Symbol.is_regular; |
|
257 |
val parse = Scan.optional ($$ "!" >> K true) false -- Scan.repeat ( |
|
258 |
($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR)) |
|
259 |
|| ($$ "_" >> K (Arg BR)) |
|
260 |
|| ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length)) |
|
261 |
|| (Scan.repeat1 |
|
262 |
( $$ "'" |-- sym_any |
|
263 |
|| Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")") |
|
264 |
sym_any) >> (Pretty o str o implode))); |
|
265 |
in case Scan.finite Symbol.stopper parse (Symbol.explode s) |
|
266 |
of ((_, p as [_]), []) => mk_mixfix prep_arg (NOBR, p) |
|
267 |
| ((b, p as _ :: _ :: _), []) => mk_mixfix prep_arg (if b then NOBR else BR, p) |
|
268 |
| _ => Scan.!! |
|
269 |
(the_default ("malformed mixfix annotation: " ^ quote s) o snd) Scan.fail () |
|
270 |
end; |
|
271 |
||
272 |
val (infixK, infixlK, infixrK) = ("infix", "infixl", "infixr"); |
|
273 |
||
274 |
fun parse_syntax prep_arg xs = |
|
275 |
Scan.option (( |
|
276 |
((OuterParse.$$$ infixK >> K X) |
|
277 |
|| (OuterParse.$$$ infixlK >> K L) |
|
278 |
|| (OuterParse.$$$ infixrK >> K R)) |
|
279 |
-- OuterParse.nat >> parse_infix prep_arg |
|
280 |
|| Scan.succeed (parse_mixfix prep_arg)) |
|
281 |
-- OuterParse.string |
|
282 |
>> (fn (parse, s) => parse s)) xs; |
|
283 |
||
284 |
val _ = List.app OuterKeyword.keyword [infixK, infixlK, infixrK]; |
|
285 |
||
28064 | 286 |
|
30648
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
287 |
(** module name spaces **) |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
288 |
|
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
289 |
val dest_name = |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
290 |
apfst Long_Name.implode o split_last o fst o split_last o Long_Name.explode; |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
291 |
|
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
292 |
fun mk_name_module reserved_names module_prefix module_alias program = |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
293 |
let |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
294 |
fun mk_alias name = case module_alias name |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
295 |
of SOME name' => name' |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
296 |
| NONE => name |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
297 |
|> Long_Name.explode |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
298 |
|> map (fn name => (the_single o fst) (Name.variants [name] reserved_names)) |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
299 |
|> Long_Name.implode; |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
300 |
fun mk_prefix name = case module_prefix |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
301 |
of SOME module_prefix => Long_Name.append module_prefix name |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
302 |
| NONE => name; |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
303 |
val tab = |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
304 |
Symtab.empty |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
305 |
|> Graph.fold ((fn name => Symtab.default (name, (mk_alias #> mk_prefix) name)) |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
306 |
o fst o dest_name o fst) |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
307 |
program |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
308 |
in the o Symtab.lookup tab end; |
17365ef082f3
clarified relationship of modules Code_Name and Code_Printer
haftmann
parents:
30161
diff
changeset
|
309 |
|
28060 | 310 |
end; (*struct*) |