forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKPrint.java
More file actions
341 lines (301 loc) · 14 KB
/
KPrint.java
File metadata and controls
341 lines (301 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (c) 2018-2019 K Team. All Rights Reserved.
package org.kframework.unparser;
import com.davekoelle.AlphanumComparator;
import com.google.inject.Inject;
import org.kframework.attributes.Att;
import org.kframework.backend.kore.ModuleToKORE;
import org.kframework.builtin.Sorts;
import org.kframework.compile.AddSortInjections;
import org.kframework.compile.ExpandMacros;
import org.kframework.definition.Definition;
import org.kframework.definition.Module;
import org.kframework.kompile.CompiledDefinition;
import org.kframework.kompile.KompileOptions;
import org.kframework.kore.Assoc;
import org.kframework.kore.K;
import org.kframework.kore.KApply;
import org.kframework.kore.KVariable;
import org.kframework.kore.Sort;
import org.kframework.kore.TransformK;
import org.kframework.main.GlobalOptions;
import org.kframework.parser.concrete2kore.generator.RuleGrammarGenerator;
import org.kframework.parser.concrete2kore.ParseInModule;
import org.kframework.parser.ProductionReference;
import org.kframework.utils.errorsystem.KEMException;
import org.kframework.utils.errorsystem.KExceptionManager;
import org.kframework.utils.file.FileUtil;
import org.kframework.utils.file.TTYInfo;
import scala.Tuple2;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import scala.Option;
import static org.kframework.kore.KORE.*;
/**
* Class for printing information and outputting to files using various serializers.
*/
public class KPrint {
private final KExceptionManager kem;
private final FileUtil files;
private final TTYInfo tty;
private final KompileOptions kompileOptions;
public final PrintOptions options;
private final Optional<CompiledDefinition> compiledDefinition;
public KPrint() {
this(new KExceptionManager(new GlobalOptions()), FileUtil.testFileUtil(), new TTYInfo(false, false, false), new PrintOptions(), Optional.empty(), new KompileOptions());
}
public KPrint(CompiledDefinition compiledDefinition) {
this(new KExceptionManager(compiledDefinition.kompileOptions.global), FileUtil.testFileUtil(), new TTYInfo(false, false, false), new PrintOptions(), compiledDefinition);
}
@Inject
public KPrint(KExceptionManager kem, FileUtil files, TTYInfo tty, PrintOptions options, CompiledDefinition compiledDefinition) {
this(kem, files, tty, options, Optional.of(compiledDefinition), compiledDefinition.kompileOptions);
}
public KPrint(KExceptionManager kem, FileUtil files, TTYInfo tty, PrintOptions options, Optional<CompiledDefinition> compiledDefinition, KompileOptions kompileOptions) {
this.kem = kem;
this.files = files;
this.tty = tty;
this.options = options;
this.compiledDefinition = compiledDefinition;
this.kompileOptions = kompileOptions;
}
//TODO(dwightguth): use Writer
public void outputFile(String output) {
outputFile(output.getBytes());
}
public void outputFile(byte[] output) {
outputFile(output, System.out);
}
public void outputFile(byte[] output, OutputStream out) {
if (options.outputFile == null) {
try {
out.write(output);
out.flush();
} catch (IOException e) {
throw KEMException.internalError(e.getMessage(), e);
}
} else {
files.saveToWorkingDirectory(options.outputFile, output);
}
}
public void prettyPrint(Definition def, Module module, Consumer<byte[]> print, K result, ColorSetting colorize) {
print.accept(prettyPrint(def, module, result, colorize));
}
public void prettyPrint(Definition def, Module module, Consumer<byte[]> print, K result) {
print.accept(prettyPrint(def, module, result, options.color(tty.stdout, files.getEnv())));
}
public byte[] prettyPrint(Definition def, Module module, K result) {
return prettyPrint(def, module, result, options.color(tty.stdout, files.getEnv()));
}
public byte[] prettyPrint(Definition def, Module module, K orig, ColorSetting colorize) {
return prettyPrint(def, module, orig, colorize, options.output);
}
public byte[] prettyPrint(CompiledDefinition def, Module module, K orig) {
return prettyPrint(def, module, orig, options.color(tty.stdout, files.getEnv()), options.output);
}
public byte[] prettyPrint(CompiledDefinition def, Module module, K orig, ColorSetting colorize, OutputModes outputMode) {
return prettyPrint(def.kompiledDefinition, module, orig, colorize, outputMode);
}
public byte[] prettyPrint(Definition def, Module module, K orig, ColorSetting colorize, OutputModes outputMode) {
switch (outputMode) {
case KAST:
case NONE:
case BINARY:
case JSON:
case PRETTY:
return prettyPrint(module, orig, colorize, outputMode);
case PROGRAM: {
K result = abstractTerm(module, orig);
RuleGrammarGenerator gen = new RuleGrammarGenerator(def);
Module unparsingModule = RuleGrammarGenerator.getCombinedGrammar(gen.getProgramsGrammar(module), false).getParsingModule();
return (unparseTerm(result, unparsingModule, colorize) + "\n").getBytes();
}
case KORE:
if (!compiledDefinition.isPresent()) {
throw KEMException.criticalError("KORE output requires a compiled definition.");
}
CompiledDefinition cdef = compiledDefinition.get();
ModuleToKORE converter = new ModuleToKORE(module, files, cdef.topCellInitializer, kompileOptions);
K result = ExpandMacros.forNonSentences(module, files, kompileOptions, false).expand(orig);
result = new AddSortInjections(module).addInjections(result);
converter.convert(result);
return converter.toString().getBytes();
default:
throw KEMException.criticalError("Unsupported output mode without a CompiledDefinition: " + outputMode);
}
}
public byte[] prettyPrint(Module module, K orig, ColorSetting colorize, OutputModes outputMode) {
K result = abstractTerm(module, orig);
switch (outputMode) {
case KAST:
case NONE:
case BINARY:
case JSON:
case LATEX:
return serialize(result, outputMode);
case PRETTY: {
Module unparsingModule = RuleGrammarGenerator.getCombinedGrammar(module, false).getExtensionModule();
return (unparseTerm(result, unparsingModule, colorize) + "\n").getBytes();
} default:
throw KEMException.criticalError("Unsupported output mode without a Definition: " + outputMode);
}
}
public byte[] serialize(K term) {
return KPrint.serialize(term, options.output);
}
public static byte[] serialize(K term, OutputModes outputMode) {
switch (outputMode) {
case KAST:
return (ToKast.apply(term) + "\n").getBytes();
case NONE:
return "".getBytes();
case BINARY:
return ToBinary.apply(term);
case JSON:
return ToJson.apply(term);
case LATEX:
return ToLatex.apply(term);
default:
throw KEMException.criticalError("Unsupported serialization mode: " + outputMode);
}
}
public String unparseTerm(K input, Module test) {
return unparseTerm(input, test, options.color(tty.stdout, files.getEnv()));
}
public String unparseTerm(K input, Module test, ColorSetting colorize) {
return unparseInternal(test, input, colorize);
}
private String unparseInternal(Module mod, K input, ColorSetting colorize) {
ExpandMacros expandMacros = ExpandMacros.forNonSentences(mod, files, kompileOptions, true);
return Formatter.format(
new AddBrackets(mod).addBrackets((ProductionReference) ParseInModule.disambiguateForUnparse(mod, KOREToTreeNodes.apply(KOREToTreeNodes.up(mod, expandMacros.expand(input)), mod))), options.color(tty.stdout, files.getEnv()));
}
public K abstractTerm(Module mod, K term) {
K origNames = options.restoreOriginalNames ? restoreOriginalNameIfPresent(term) : term;
K collectionsSorted = options.noSortCollections ? origNames : sortCollections(mod, origNames);
//non-determinism is still possible if associative/commutative collection terms start with anonymous vars
K alphaRenamed = options.noAlphaRenaming ? collectionsSorted : alphaRename(collectionsSorted);
K squashedTerm = squashTerms(mod, alphaRenamed);
K flattenedTerm = flattenTerms(mod, squashedTerm);
return flattenedTerm;
}
private K sortCollections(Module mod, K input) {
Module unparsingModule = RuleGrammarGenerator.getCombinedGrammar(mod, false).getExtensionModule();
return new TransformK() {
@Override
public K apply(KApply k) {
if (k.klabel() instanceof KVariable) {
return super.apply(k);
}
Att att = unparsingModule.attributesFor().apply(KLabel(k.klabel().name()));
if (att.contains("comm") && att.contains("assoc") && att.contains("unit")) {
List<K> items = new ArrayList<>(Assoc.flatten(k.klabel(), k.klist().items(), KLabel(att.get("unit"))));
List<Tuple2<String, K>> printed = new ArrayList<>();
for (K item : items) {
String s = unparseInternal(unparsingModule, apply(item), ColorSetting.OFF);
printed.add(Tuple2.apply(s, item));
}
printed.sort(Comparator.comparing(Tuple2::_1, new AlphanumComparator()));
items = printed.stream().map(Tuple2::_2).map(this::apply).collect(Collectors.toList());
return items.stream().reduce((k1, k2) -> KApply(k.klabel(), k1, k2)).orElse(KApply(KLabel(att.get("unit"))));
}
return super.apply(k);
}
}.apply(input);
}
private K alphaRename(K input) {
return new TransformK() {
Map<KVariable, KVariable> renames = new HashMap<>();
int newCount = 0;
@Override
public K apply(KVariable k) {
if (k.att().contains("anonymous")) {
return renames.computeIfAbsent(k, k2 -> KVariable("V" + newCount++, k.att()));
}
return k;
}
}.apply(input);
}
private K restoreOriginalNameIfPresent(K input) {
return new TransformK() {
@Override
public K apply(KVariable k) {
if (k.att().contains("originalName")) {
return KVariable(k.att().get("originalName"), k.att());
}
return k;
}
}.apply(input);
}
private K squashTerms(Module mod, K input) {
return new TransformK() {
@Override
public K apply(KApply orig) {
String name = orig.klabel().name();
return options.omittedKLabels.contains(name) ? omitTerm(mod, orig)
: options.tokenizedKLabels.contains(name) ? tokenizeTerm(mod, orig)
: options.tokastKLabels.contains(name) ? toKASTTerm(mod, orig)
: super.apply(orig) ;
}
}.apply(input);
}
private static K omitTerm(Module mod, KApply kapp) {
Sort finalSort = Sorts.K();
Option<Sort> termSort = mod.sortFor().get(kapp.klabel());
if (! termSort.isEmpty()) {
finalSort = termSort.get();
}
return KApply(kapp.klabel(), KToken(kapp.klabel().name(), finalSort));
}
private K tokenizeTerm(Module mod, KApply kapp) {
Module unparsingModule = RuleGrammarGenerator.getCombinedGrammar(mod, false).getExtensionModule();
String tokenizedTerm = unparseTerm(kapp, unparsingModule, ColorSetting.OFF);
Sort finalSort = Sorts.K();
Option<Sort> termSort = mod.sortFor().get(kapp.klabel());
if (! termSort.isEmpty()) {
finalSort = termSort.get();
}
return KApply(kapp.klabel(), KToken(tokenizedTerm, finalSort));
}
private static K toKASTTerm(Module mod, KApply kapp) {
String kastTerm = ToKast.apply(kapp);
Sort finalSort = Sorts.K();
Option<Sort> termSort = mod.sortFor().get(kapp.klabel());
if (! termSort.isEmpty()) {
finalSort = termSort.get();
}
return KToken(kastTerm, finalSort);
}
private K flattenTerms(Module mod, K input) {
return new TransformK() {
@Override
public K apply(KApply orig) {
K newK = super.apply(orig);
if (! (newK instanceof KApply)) {
return newK;
}
KApply kapp = (KApply) newK;
String name = orig.klabel().name();
return options.flattenedKLabels.contains(name) ? flattenTerm(mod, kapp)
: kapp ;
}
}.apply(input);
}
private static K flattenTerm(Module mod, KApply kapp) {
List<K> items = new ArrayList<>();
Att att = mod.attributesFor().apply(KLabel(kapp.klabel().name()));
if (att.contains("assoc") && att.contains("unit")) {
items = Assoc.flatten(kapp.klabel(), kapp.klist().items(), KLabel(att.get("unit")));
} else {
items = kapp.klist().items();
}
return KApply(kapp.klabel(), KList(items), kapp.att());
}
}