-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathProcessGroupAttributes.java
More file actions
76 lines (68 loc) · 3.15 KB
/
ProcessGroupAttributes.java
File metadata and controls
76 lines (68 loc) · 3.15 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
// Copyright (c) K Team. All Rights Reserved.
package org.kframework.compile;
import org.kframework.Collections;
import org.kframework.attributes.Att;
import org.kframework.attributes.HasLocation;
import org.kframework.kil.Definition;
import org.kframework.kil.Module;
import org.kframework.kil.Syntax;
import org.kframework.utils.errorsystem.KEMException;
import scala.util.Either;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* A pass which handles all "user group" attributes. Specifically,
*
* - Replace every attribute [group(att1,...,attN)] with the underlying attributes [att1,...,attN].
* - If --pedantic-attributes is disabled, then additionally convert every unrecognized attribute key to a user group.
*
*/
public class ProcessGroupAttributes {
private static Att convertRawKeysToUserGroups(Att att, HasLocation node) {
// During parsing, an attribute my-att is inserted as either
// - Key("my-att", KeyType.BuiltIn) if a recognized built-in
// - Key("my-att", KeyType.RawKey) otherwise
//
// Thus, if --pedantic-attributes is disabled, we should replace every Key(..., KeyType.Raw) with
// Key(..., KeyType.UserGroup).
List<Att.Key> newGroups = Collections.stream(att.rawKeys())
.map((k) -> {
Optional<Att.Key> groupKey = Att.getUserGroupOptional(k.key());
if (groupKey.isEmpty()) {
throw new AssertionError("Found Att.Key(" + k.key() + ", KeyType.RawKey), " +
"but outer parsing should have produced Att.Key(" + k.key() + ", KeyType.BuiltIn) " +
"instead");
}
return groupKey.get();
}).collect(Collectors.toList());
for (Att.Key group : newGroups) {
att = att.remove(Att.unsafeRawKey(group.key())).add(group);
}
return att;
}
public static Att getProcessedAtt(Att att, HasLocation node, boolean pedanticAttributes) {
Either<String, Att> newAttOrError = att.withGroupAttAsUserGroups();
if (newAttOrError.isLeft()) {
throw KEMException.compilerError(newAttOrError.left().get(), node);
}
Att newAtt = newAttOrError.right().get();
if (!pedanticAttributes) {
newAtt = convertRawKeysToUserGroups(newAtt, node);
}
return newAtt;
}
public static void apply(Module m, boolean pedanticAttributes) {
m.setAttributes(getProcessedAtt(m.getAttributes(), m, pedanticAttributes));
m.getItems().stream()
.filter((modItem) -> modItem instanceof Syntax)
.flatMap((s) -> ((Syntax) s).getPriorityBlocks().stream())
.flatMap((pb) -> pb.getProductions().stream())
.forEach((p) -> p.setAttributes(getProcessedAtt(p.getAttributes(), p, pedanticAttributes)));
}
public static void apply(Definition d, boolean pedanticAttributes) {
d.getItems().stream()
.filter((item) -> item instanceof Module)
.forEach((m) -> apply((Module) m, pedanticAttributes));
}
}