-
Notifications
You must be signed in to change notification settings - Fork 188
/
AbstractAxiomAntlrVisitor.java
158 lines (124 loc) · 5.26 KB
/
AbstractAxiomAntlrVisitor.java
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
/*
* Copyright (c) 2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.axiom.lang.antlr;
import java.util.Optional;
import java.util.Set;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import com.evolveum.axiom.api.AxiomName;
import com.evolveum.axiom.api.AxiomPrefixedName;
import com.evolveum.axiom.api.stream.AxiomItemStream;
import com.evolveum.axiom.api.stream.AxiomStreamTarget;
import com.evolveum.axiom.concepts.SourceLocation;
import com.evolveum.axiom.lang.antlr.AxiomParser.ArgumentContext;
import com.evolveum.axiom.lang.antlr.AxiomParser.IdentifierContext;
import com.evolveum.axiom.lang.antlr.AxiomParser.ItemBodyContext;
import com.evolveum.axiom.lang.antlr.AxiomParser.ItemContext;
import com.evolveum.axiom.lang.antlr.AxiomParser.MetadataContext;
import com.evolveum.axiom.lang.antlr.AxiomParser.StringContext;
import com.evolveum.axiom.lang.spi.AxiomSemanticException;
public abstract class AbstractAxiomAntlrVisitor<T> extends AxiomBaseVisitor<T> {
private final String sourceName;
private interface StartDelegate {
void start(AxiomPrefixedName identifier, SourceLocation location);
}
private interface EndDelegate {
void end(SourceLocation location);
}
public AbstractAxiomAntlrVisitor(String name) {
this.sourceName = name;
}
private AxiomPrefixedName statementIdentifier(IdentifierContext identifier) {
String prefix = nullableText(identifier.prefix());
String localName = identifier.localIdentifier().getText();
return AxiomPrefixedName.from(prefix, localName);
//AxiomName ret = resolveItemName(prefix, localName);
//AxiomSemanticException.check(ret != null, sourceLocation(identifier.start), "Item %s not allowed at this place.", identifier.getText());
//return ret;
}
protected abstract AxiomStreamTarget<AxiomPrefixedName> delegate();
private String nullableText(ParserRuleContext prefix) {
return prefix != null ? prefix.getText() : "";
}
@Override
public T visitItem(ItemContext ctx) {
AxiomPrefixedName identifier = statementIdentifier(ctx.itemBody().identifier());
return processItemBody(identifier, ctx.itemBody(), delegate()::startItem, delegate()::endItem);
}
public T processItemBody(AxiomPrefixedName identifier, ItemBodyContext ctx, StartDelegate start, EndDelegate end) {
SourceLocation startLoc = sourceLocation(ctx.identifier().start);
start.start(identifier, startLoc);
ArgumentContext argument = ctx.value().argument();
final Object value;
final SourceLocation valueStart;
if(argument != null) {
value = convert(argument);
valueStart = sourceLocation(argument.start);
} else {
value = null;
valueStart = startLoc;
}
delegate().startValue(value, valueStart);
T ret = visitItemBody(ctx);
delegate().endValue(sourceLocation(ctx.stop));
end.end(sourceLocation(ctx.stop));
return ret;
}
@Override
public T visitMetadata(MetadataContext ctx) {
AxiomPrefixedName identifier = statementIdentifier(ctx.itemBody().identifier());
return processItemBody(identifier, ctx.itemBody(), delegate()::startInfra, delegate()::endInfra);
}
private Object convert(ArgumentContext ctx) {
if (ctx.identifier() != null) {
return (convert(ctx.identifier()));
} else {
return (convert(ctx.string()));
}
}
@Override
public final T visitArgument(ArgumentContext ctx) {
return defaultResult();
}
private AxiomPrefixedName convert(IdentifierContext argument) {
return argumentIdentifier(argument);
}
private AxiomPrefixedName argumentIdentifier(IdentifierContext identifier) {
String prefix = nullableText(identifier.prefix());
String localName = identifier.localIdentifier().getText();
return AxiomPrefixedName.from(prefix, localName);
}
private SourceLocation sourceLocation(Token start) {
return SourceLocation.from(sourceName, start.getLine(), start.getCharPositionInLine());
}
static String convertToString(ArgumentContext context) {
if(context.identifier() != null) {
return context.identifier().getText();
}
return convert(context.string());
}
static String convert(StringContext string) {
if(string.singleQuoteString() != null) {
return convertSingleQuote(string.singleQuoteString().getText());
}
if(string.doubleQuoteString() != null) {
return covertDoubleQuote(string.doubleQuoteString().getText());
}
return convertMultiline(string.multilineString().getText());
}
private static String convertSingleQuote(String text) {
int stop = text.length();
return text.substring(1, stop - 1);
}
private static String covertDoubleQuote(String text) {
int stop = text.length();
return text.substring(1, stop - 1);
}
private static String convertMultiline(String text) {
return text.replace("\"\"\"", "");
}
}