1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package io.earcam.instrumental.module.jpms.parser;
20
21 import static java.util.Locale.ROOT;
22 import static java.util.stream.Collectors.toSet;
23
24 import java.util.Set;
25 import java.util.TreeSet;
26 import java.util.stream.Stream;
27
28 import io.earcam.instrumental.module.jpms.ModuleInfo;
29 import io.earcam.instrumental.module.jpms.ModuleInfoBuilder;
30 import io.earcam.instrumental.module.jpms.RequireModifier;
31 import io.earcam.instrumental.module.jpms.parser.Java9BaseListener;
32 import io.earcam.instrumental.module.jpms.parser.Java9Parser.ExportsDirectiveContext;
33 import io.earcam.instrumental.module.jpms.parser.Java9Parser.ModuleDeclarationContext;
34 import io.earcam.instrumental.module.jpms.parser.Java9Parser.ModuleNameContext;
35 import io.earcam.instrumental.module.jpms.parser.Java9Parser.OpensDirectiveContext;
36 import io.earcam.instrumental.module.jpms.parser.Java9Parser.ProvidesDirectiveContext;
37 import io.earcam.instrumental.module.jpms.parser.Java9Parser.RequiresDirectiveContext;
38 import io.earcam.instrumental.module.jpms.parser.Java9Parser.RequiresModifierContext;
39 import io.earcam.instrumental.module.jpms.parser.Java9Parser.UsesDirectiveContext;
40
41
42
43
44
45
46
47
48 class AntlrParser extends Java9BaseListener {
49
50 private final ModuleInfoBuilder builder = ModuleInfo.moduleInfo();
51
52
53
54
55
56
57
58
59
60 public ModuleInfoBuilder builder()
61 {
62 return builder;
63 }
64
65
66 @Override
67 public void enterModuleDeclaration(ModuleDeclarationContext ctx)
68 {
69 super.enterModuleDeclaration(ctx);
70 builder.named(ctx.moduleName().getText());
71 }
72
73
74 @Override
75 public void enterRequiresDirective(RequiresDirectiveContext ctx)
76 {
77 super.enterRequiresDirective(ctx);
78 Set<RequireModifier> modifiers = ctx.requiresModifier().stream()
79 .map(RequiresModifierContext::getText)
80 .map(m -> m.toUpperCase(ROOT))
81 .map(RequireModifier::valueOf)
82 .collect(toSet());
83
84 builder.requiring(ctx.moduleName().getText(), modifiers, null);
85 }
86
87
88 @Override
89 public void enterExportsDirective(ExportsDirectiveContext ctx)
90 {
91 super.enterExportsDirective(ctx);
92 String[] to = toModuleNames(ctx.moduleName().stream());
93 builder.exporting(ctx.packageName().getText(), to);
94 }
95
96
97 private String[] toModuleNames(Stream<ModuleNameContext> stream)
98 {
99 return stream
100 .map(ModuleNameContext::getText)
101 .toArray(s -> new String[s]);
102 }
103
104
105 @Override
106 public void enterOpensDirective(OpensDirectiveContext ctx)
107 {
108 super.enterOpensDirective(ctx);
109 String[] to = toModuleNames(ctx.moduleName().stream());
110 builder.opening(ctx.packageName().getText(), to);
111 }
112
113
114 @Override
115 public void enterUsesDirective(UsesDirectiveContext ctx)
116 {
117 super.enterUsesDirective(ctx);
118 builder.using(ctx.getChild(1).getText());
119 }
120
121
122 @Override
123 public void enterProvidesDirective(ProvidesDirectiveContext ctx)
124 {
125 super.enterProvidesDirective(ctx);
126
127 TreeSet<String> concretes = new TreeSet<>();
128 for(int i = 3; i < ctx.getChildCount(); i += 2) {
129 concretes.add(ctx.getChild(i).getText());
130 }
131
132 builder.providing(ctx.getChild(1).getText(), concretes);
133 }
134 }