forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDefinitionParsing.java
More file actions
443 lines (398 loc) · 20.4 KB
/
DefinitionParsing.java
File metadata and controls
443 lines (398 loc) · 20.4 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright (c) 2015-2019 K Team. All Rights Reserved.
package org.kframework.kompile;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections15.ListUtils;
import org.kframework.Collections;
import org.kframework.attributes.Location;
import org.kframework.attributes.Source;
import org.kframework.builtin.BooleanUtils;
import org.kframework.definition.Bubble;
import org.kframework.definition.Context;
import org.kframework.definition.ContextAlias;
import org.kframework.definition.Definition;
import org.kframework.definition.DefinitionTransformer;
import org.kframework.definition.Module;
import org.kframework.definition.Rule;
import org.kframework.definition.Sentence;
import org.kframework.kore.K;
import org.kframework.kore.KApply;
import org.kframework.kore.Sort;
import org.kframework.parser.TreeNodesToKORE;
import org.kframework.parser.concrete2kore.ParseCache;
import org.kframework.parser.concrete2kore.ParseCache.ParsedSentence;
import org.kframework.parser.concrete2kore.ParseInModule;
import org.kframework.parser.concrete2kore.ParserUtils;
import org.kframework.parser.concrete2kore.generator.RuleGrammarGenerator;
import org.kframework.parser.concrete2kore.kernel.Scanner;
import org.kframework.parser.outer.Outer;
import org.kframework.utils.BinaryLoader;
import org.kframework.utils.StringUtil;
import org.kframework.utils.errorsystem.KEMException;
import org.kframework.utils.errorsystem.KExceptionManager;
import org.kframework.utils.errorsystem.ParseFailedException;
import org.kframework.utils.errorsystem.KException.ExceptionType;
import org.kframework.utils.file.FileUtil;
import scala.Option;
import scala.Tuple2;
import scala.collection.Set;
import scala.util.Either;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.kframework.Collections.*;
import static org.kframework.definition.Constructors.*;
import static org.kframework.kore.KORE.*;
/**
* A bundle of code doing various aspects of definition parsing.
* TODO: In major need of refactoring.
* @cos refactored this code out of Kompile but none (or close to none) of it was originally written by him.
*/
public class DefinitionParsing {
public static final Sort START_SYMBOL = Sort("#RuleContent");
private final File cacheFile;
private boolean autoImportDomains;
private boolean kore;
private final KExceptionManager kem;
private final FileUtil files;
private final ParserUtils parser;
private final boolean cacheParses;
private final BinaryLoader loader;
public final AtomicInteger parsedBubbles = new AtomicInteger(0);
public final AtomicInteger cachedBubbles = new AtomicInteger(0);
private final boolean isStrict;
private final boolean profileRules;
private final List<File> lookupDirectories;
public DefinitionParsing(
List<File> lookupDirectories,
boolean isStrict,
boolean profileRules,
KExceptionManager kem,
FileUtil files,
ParserUtils parser,
boolean cacheParses,
File cacheFile,
boolean autoImportDomains,
boolean kore) {
this.lookupDirectories = lookupDirectories;
this.kem = kem;
this.files = files;
this.parser = parser;
this.cacheParses = cacheParses;
this.cacheFile = cacheFile;
this.autoImportDomains = autoImportDomains;
this.kore = kore;
this.loader = new BinaryLoader(this.kem);
this.isStrict = isStrict;
this.profileRules = profileRules;
}
public java.util.Set<Module> parseModules(CompiledDefinition definition, String mainModule, File definitionFile) {
Definition def = parser.loadDefinition(
mainModule,
mutable(definition.getParsedDefinition().modules()),
"require " + StringUtil.enquoteCString(definitionFile.getPath()),
Source.apply(definitionFile.getAbsolutePath()),
definitionFile.getParentFile(),
ListUtils.union(lookupDirectories,
Lists.newArrayList(Kompile.BUILTIN_DIRECTORY)),
kore);
errors = java.util.Collections.synchronizedSet(Sets.newHashSet());
caches = new HashMap<>();
if (cacheParses) {
try {
caches = loader.load(Map.class, cacheFile);
} catch (FileNotFoundException e) {
} catch (IOException | ClassNotFoundException e) {
kem.registerInternalHiddenWarning("Invalidating serialized cache due to corruption.", e);
}
}
Module modWithConfig;
ResolveConfig resolveConfig = new ResolveConfig(definition.getParsedDefinition(), isStrict, kore, this::parseBubble, this::getParser);
gen = new RuleGrammarGenerator(definition.getParsedDefinition());
try {
def = DefinitionTransformer.from(resolveConfig::apply, "parse config bubbles").apply(def);
} catch (KEMException e) {
errors.add(e);
throwExceptionIfThereAreErrors();
throw new AssertionError("should not reach this statement");
}
def = resolveNonConfigBubbles(def, gen);
saveCachesAndReportParsingErrors();
return mutable(def.entryModules());
}
private void saveCachesAndReportParsingErrors() {
saveCaches();
throwExceptionIfThereAreErrors();
}
private void saveCaches() {
if (cacheParses) {
loader.saveOrDie(cacheFile, caches);
}
}
public Definition parseDefinitionAndResolveBubbles(File definitionFile, String mainModuleName, String mainProgramsModule, java.util.Set<String> excludedModuleTags) {
Definition parsedDefinition = parseDefinition(definitionFile, mainModuleName, mainProgramsModule);
Stream<Module> modules = Stream.of(parsedDefinition.mainModule());
modules = Stream.concat(modules, stream(parsedDefinition.mainModule().importedModules()));
Option<Module> syntaxModule = parsedDefinition.getModule(mainProgramsModule);
if (syntaxModule.isDefined()) {
modules = Stream.concat(modules, Stream.of(syntaxModule.get()));
modules = Stream.concat(modules, stream(syntaxModule.get().importedModules()));
}
modules = Stream.concat(modules, Stream.of(parsedDefinition.getModule("K-REFLECTION").get()));
modules = Stream.concat(modules, Stream.of(parsedDefinition.getModule("STDIN-STREAM").get()));
modules = Stream.concat(modules, Stream.of(parsedDefinition.getModule("STDOUT-STREAM").get()));
modules = Stream.concat(modules,
stream(parsedDefinition.entryModules()).filter(m -> !stream(m.sentences()).anyMatch(s -> s instanceof Bubble)));
Definition trimmed = Definition(parsedDefinition.mainModule(), modules.collect(Collections.toSet()),
parsedDefinition.att());
trimmed = Kompile.excludeModulesByTag(excludedModuleTags).apply(trimmed);
Definition afterResolvingConfigBubbles = resolveConfigBubbles(trimmed, parsedDefinition.getModule("DEFAULT-CONFIGURATION").get());
RuleGrammarGenerator gen = new RuleGrammarGenerator(afterResolvingConfigBubbles);
Definition afterResolvingAllOtherBubbles = resolveNonConfigBubbles(afterResolvingConfigBubbles, gen);
saveCachesAndReportParsingErrors();
return afterResolvingAllOtherBubbles;
}
private void throwExceptionIfThereAreErrors() {
if (!errors.isEmpty()) {
kem.addAllKException(errors.stream().map(e -> e.exception).collect(Collectors.toList()));
throw KEMException.compilerError("Had " + errors.size() + " parsing errors.");
}
}
public Definition parseDefinition(File definitionFile, String mainModuleName, String mainProgramsModule) {
Definition definition = parser.loadDefinition(
mainModuleName,
mainProgramsModule, FileUtil.load(definitionFile),
definitionFile,
definitionFile.getParentFile(),
ListUtils.union(lookupDirectories,
Lists.newArrayList(Kompile.BUILTIN_DIRECTORY)),
autoImportDomains,
kore);
return definition;
}
protected Definition resolveConfigBubbles(Definition definition, Module defaultConfiguration) {
boolean hasConfigDecl = stream(definition.mainModule().sentences())
.filter(s -> s instanceof Bubble)
.map(b -> (Bubble) b)
.filter(b -> b.sentenceType().equals("config"))
.findFirst().isPresent();
Definition definitionWithConfigBubble;
if (!hasConfigDecl) {
definitionWithConfigBubble = DefinitionTransformer.from(mod -> {
if (mod == definition.mainModule()) {
java.util.Set<Module> imports = mutable(mod.imports());
imports.add(defaultConfiguration);
return Module(mod.name(), (Set<Module>) immutable(imports), mod.localSentences(), mod.att());
}
return mod;
}, "adding default configuration").apply(definition);
} else {
definitionWithConfigBubble = definition;
}
errors = java.util.Collections.synchronizedSet(Sets.newHashSet());
caches = new HashMap<>();
if (cacheParses) {
try {
caches = loader.load(Map.class, cacheFile);
} catch (FileNotFoundException e) {
} catch (IOException | ClassNotFoundException e) {
kem.registerInternalHiddenWarning("Invalidating serialized cache due to corruption.", e);
}
}
ResolveConfig resolveConfig = new ResolveConfig(definitionWithConfigBubble, isStrict, kore, this::parseBubble, this::getParser);
gen = new RuleGrammarGenerator(definitionWithConfigBubble);
try {
Definition defWithConfig = DefinitionTransformer.from(resolveConfig::apply, "parsing configurations").apply(definitionWithConfigBubble);
return defWithConfig;
} catch (KEMException e) {
errors.add(e);
throwExceptionIfThereAreErrors();
throw new AssertionError("should not reach this statement");
}
}
Map<String, ParseCache> caches;
private java.util.Set<KEMException> errors;
RuleGrammarGenerator gen;
public java.util.Set<KEMException> errors() {
return errors;
}
public Definition resolveNonConfigBubbles(Definition defWithConfig, RuleGrammarGenerator gen) {
Module ruleParserModule = gen.getRuleGrammar(defWithConfig.mainModule());
ParseCache cache = loadCache(ruleParserModule);
try (ParseInModule parser = RuleGrammarGenerator.getCombinedGrammar(cache.getModule(), isStrict, profileRules, files)) {
return DefinitionTransformer.from(m -> this.resolveNonConfigBubbles(m, parser.getScanner(), gen), "parsing rules").apply(defWithConfig);
}
}
private Module resolveNonConfigBubbles(Module module, Scanner scanner, RuleGrammarGenerator gen) {
if (stream(module.localSentences())
.filter(s -> s instanceof Bubble)
.map(b -> (Bubble) b)
.filter(b -> !b.sentenceType().equals("config")).count() == 0)
return module;
Module ruleParserModule = gen.getRuleGrammar(module);
ParseCache cache = loadCache(ruleParserModule);
try (ParseInModule parser = RuleGrammarGenerator.getCombinedGrammar(cache.getModule(), isStrict, profileRules, files)) {
if (stream(module.localSentences()).filter(s -> s instanceof Bubble).findAny().isPresent()) {
parser.initialize();
}
// this scanner is not good for this module, so we must generate a new scanner.
boolean needNewScanner = !scanner.getModule().importedModuleNames().contains(module.name());
final Scanner realScanner = needNewScanner ? parser.getScanner() : scanner;
Set<Sentence> ruleSet = stream(module.localSentences())
.parallel()
.filter(s -> s instanceof Bubble)
.map(b -> (Bubble) b)
.filter(b -> b.sentenceType().equals("rule"))
.flatMap(b -> performParse(cache.getCache(), parser, realScanner, b))
.map(this::upRule)
.collect(Collections.toSet());
Set<Sentence> contextSet = stream(module.localSentences())
.parallel()
.filter(s -> s instanceof Bubble)
.map(b -> (Bubble) b)
.filter(b -> b.sentenceType().equals("context"))
.flatMap(b -> performParse(cache.getCache(), parser, realScanner, b))
.map(this::upContext)
.collect(Collections.toSet());
Set<Sentence> aliasSet = stream(module.localSentences())
.parallel()
.filter(s -> s instanceof Bubble)
.map(b -> (Bubble) b)
.filter(b -> b.sentenceType().equals("alias"))
.flatMap(b -> performParse(cache.getCache(), parser, realScanner, b))
.map(this::upAlias)
.collect(Collections.toSet());
if (needNewScanner) {
realScanner.close();//required for Windows.
}
return Module(module.name(), module.imports(),
stream((Set<Sentence>) module.localSentences().$bar(ruleSet).$bar(contextSet).$bar(aliasSet)).filter(b -> !(b instanceof Bubble)).collect(Collections.toSet()), module.att());
}
}
public Rule parseRule(CompiledDefinition compiledDef, String contents, Source source) {
errors = java.util.Collections.synchronizedSet(Sets.newHashSet());
gen = new RuleGrammarGenerator(compiledDef.kompiledDefinition);
try (ParseInModule parser = RuleGrammarGenerator
.getCombinedGrammar(gen.getRuleGrammar(compiledDef.executionModule()), isStrict, profileRules, files)) {
java.util.Set<K> res = performParse(new HashMap<>(), parser, parser.getScanner(),
new Bubble("rule", contents, Att().add("contentStartLine", Integer.class, 1)
.add("contentStartColumn", Integer.class, 1).add(Source.class, source)))
.collect(Collectors.toSet());
if (!errors.isEmpty()) {
throw errors.iterator().next();
}
return upRule(res.iterator().next());
}
}
private Rule upRule(K contents) {
KApply ruleContents = (KApply) contents;
List<org.kframework.kore.K> items = ruleContents.klist().items();
switch (ruleContents.klabel().name()) {
case "#ruleNoConditions":
return Rule(items.get(0), BooleanUtils.TRUE, BooleanUtils.TRUE, ruleContents.att());
case "#ruleRequires":
return Rule(items.get(0), items.get(1), BooleanUtils.TRUE, ruleContents.att());
case "#ruleEnsures":
return Rule(items.get(0), BooleanUtils.TRUE, items.get(1), ruleContents.att());
case "#ruleRequiresEnsures":
return Rule(items.get(0), items.get(1), items.get(2), ruleContents.att());
default:
throw new AssertionError("Wrong KLabel for rule content");
}
}
private Context upContext(K contents) {
KApply ruleContents = (KApply) contents;
List<K> items = ruleContents.klist().items();
switch (ruleContents.klabel().name()) {
case "#ruleNoConditions":
return Context(items.get(0), BooleanUtils.TRUE, ruleContents.att());
case "#ruleRequires":
return Context(items.get(0), items.get(1), ruleContents.att());
default:
throw KEMException.criticalError("Illegal context with ensures clause detected.", contents);
}
}
private ContextAlias upAlias(K contents) {
KApply ruleContents = (KApply) contents;
List<K> items = ruleContents.klist().items();
switch (ruleContents.klabel().name()) {
case "#ruleNoConditions":
return ContextAlias(items.get(0), BooleanUtils.TRUE, ruleContents.att());
case "#ruleRequires":
return ContextAlias(items.get(0), items.get(1), ruleContents.att());
default:
throw KEMException.criticalError("Illegal context alias with ensures clause detected.", contents);
}
}
private ParseCache loadCache(Module parser) {
ParseCache cachedParser = caches.get(parser.name());
if (cachedParser == null || !equalsSyntax(cachedParser.getModule(), parser) || cachedParser.isStrict() != isStrict) {
cachedParser = new ParseCache(parser, isStrict, java.util.Collections.synchronizedMap(new HashMap<>()));
caches.put(parser.name(), cachedParser);
}
return cachedParser;
}
private boolean equalsSyntax(Module _this, Module that) {
if (!_this.productions().equals(that.productions())) return false;
if (!_this.priorities().equals(that.priorities())) return false;
if (!_this.leftAssoc().equals(that.leftAssoc())) return false;
if (!_this.rightAssoc().equals(that.rightAssoc())) return false;
return _this.sortDeclarations().equals(that.sortDeclarations());
}
private Stream<? extends K> parseBubble(Module module, Bubble b) {
ParseCache cache = loadCache(gen.getConfigGrammar(module));
try (ParseInModule parser = RuleGrammarGenerator.getCombinedGrammar(cache.getModule(), isStrict, profileRules, files)) {
return performParse(cache.getCache(), parser, parser.getScanner(), b);
}
}
private ParseInModule getParser(Module module) {
ParseCache cache = loadCache(gen.getConfigGrammar(module));
return RuleGrammarGenerator.getCombinedGrammar(cache.getModule(), isStrict);
}
private Stream<? extends K> performParse(Map<String, ParsedSentence> cache, ParseInModule parser, Scanner scanner, Bubble b) {
int startLine = b.att().get("contentStartLine", Integer.class);
int startColumn = b.att().get("contentStartColumn", Integer.class);
Source source = b.att().get(Source.class);
Tuple2<Either<java.util.Set<ParseFailedException>, K>, java.util.Set<ParseFailedException>> result;
if (cache.containsKey(b.contents())) {
ParsedSentence parse = cache.get(b.contents());
Optional<Source> cacheSource = parse.getParse().source();
//Cache might contain content from an identical file but another source path.
//The content will have wrong Source attribute and must be invalidated.
if (cacheSource.isPresent() && cacheSource.get().equals(source)) {
cachedBubbles.getAndIncrement();
kem.addAllKException(parse.getWarnings().stream().map(e -> e.getKException()).collect(Collectors.toList()));
return Stream.of(parse.getParse());
}
}
result = parser.parseString(b.contents(), START_SYMBOL, scanner, source, startLine, startColumn, true, b.att().contains("anywhere"));
parsedBubbles.getAndIncrement();
if (kem.options.warnings2errors && !result._2().isEmpty()) {
for (KEMException err : result._2()) {
if (kem.options.warnings.includesExceptionType(err.exception.getType())) {
errors.add(KEMException.asError(err));
}
}
} else {
kem.addAllKException(result._2().stream().map(e -> e.getKException()).collect(Collectors.toList()));
}
if (result._1().isRight()) {
KApply k = (KApply) new TreeNodesToKORE(Outer::parseSort, isStrict).down(result._1().right().get());
k = KApply(k.klabel(), k.klist(), k.att().addAll(b.att().remove("contentStartLine").remove("contentStartColumn").remove(Source.class).remove(Location.class)));
cache.put(b.contents(), new ParsedSentence(k, new HashSet<>(result._2())));
return Stream.of(k);
} else {
errors.addAll(result._1().left().get());
return Stream.empty();
}
}
}