Skip to content

Commit

Permalink
Axiom: Removed statement from grammar, replaced with item / value
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Tkacik <tony.tkacik@evolveum.com>
  • Loading branch information
tonydamage committed Jun 3, 2020
1 parent 9889f0d commit 286268e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
Expand Up @@ -17,7 +17,13 @@ fragment ESC : '\\';
STRING_SINGLEQUOTE: SQOUTE ((ESC SQOUTE) | ~[\n'])* SQOUTE;
STRING_DOUBLEQUOTE: DQOUTE ((ESC DQOUTE) | ~[\n"])* DQOUTE;
statement : SEP* identifier SEP* (argument)? SEP* (SEMICOLON | LEFT_BRACE SEP* (statement)* SEP* RIGHT_BRACE SEP*) SEP*;
//statement : SEP* identifier SEP* (argument)? SEP* (SEMICOLON | LEFT_BRACE SEP* (statement)* SEP* RIGHT_BRACE SEP*) SEP*;
item : SEP* identifier SEP* value;
value: (argument)? SEP* (SEMICOLON | LEFT_BRACE SEP* (item)* SEP* RIGHT_BRACE SEP*) SEP*;
identifier : (prefix COLON)? localIdentifier;
prefix : IDENTIFIER;
Expand Down
Expand Up @@ -32,7 +32,7 @@ static <V> AxiomValue<V> from(AxiomTypeDefinition typeDefinition, V value) {
default <T> Optional<AxiomValue<T>> onlyValue(Class<T> type, AxiomItemDefinition... components) {
Optional<AxiomValue<?>> current = Optional.of(this);
for(AxiomItemDefinition name : components) {
current = current.get().asComplex().flatMap(c -> c.item(name)).map(i -> i.onlyValue()); // map(v -> v.onlyValue().asComplex());
current = current.get().asComplex().flatMap(c -> c.item(name)).map(i -> i.onlyValue());
if(!current.isPresent()) {
return Optional.empty();
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
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.ItemContext;
import com.evolveum.axiom.lang.antlr.AxiomParser.StatementContext;
import com.evolveum.axiom.lang.antlr.AxiomParser.StringContext;

Expand Down Expand Up @@ -44,14 +45,14 @@ private String nullableText(ParserRuleContext prefix) {
return prefix != null ? prefix.getText() : "";
}


@Override
public final T visitStatement(StatementContext ctx) {
public T visitItem(ItemContext ctx) {
AxiomName identifier = statementIdentifier(ctx.identifier());
if(canEmit(identifier)) {
SourceLocation start = sourceLocation(ctx.identifier().start);
delegate().startItem(identifier, start);

ArgumentContext argument = ctx.argument();
ArgumentContext argument = ctx.value().argument();
final Object value;
final SourceLocation valueStart;
if(argument != null) {
Expand All @@ -62,7 +63,7 @@ public final T visitStatement(StatementContext ctx) {
valueStart = start;
}
delegate().startValue(value, valueStart);
T ret = super.visitStatement(ctx);
T ret = super.visitItem(ctx);
delegate().endValue(sourceLocation(ctx.stop));
delegate().endItem(sourceLocation(ctx.stop));
return ret;
Expand Down
Expand Up @@ -17,37 +17,37 @@

import com.evolveum.axiom.api.AxiomName;
import com.evolveum.axiom.api.stream.AxiomItemStream;
import com.evolveum.axiom.lang.antlr.AxiomParser.StatementContext;
import com.evolveum.axiom.lang.antlr.AxiomParser.ItemContext;
import com.evolveum.axiom.lang.spi.AxiomIdentifierResolver;
import com.evolveum.axiom.lang.spi.AxiomSyntaxException;

public class AxiomAntlrStatementSource {

private final StatementContext root;
private final ItemContext root;
private final String sourceName;

public static AxiomAntlrStatementSource from(String sourceName, InputStream stream) throws IOException, AxiomSyntaxException {
return from(sourceName, CharStreams.fromStream(stream));
}

public static StatementContext contextFrom(String sourceName, CharStream stream) {
public static ItemContext contextFrom(String sourceName, CharStream stream) {
AxiomLexer lexer = new AxiomLexer(stream);
AxiomParser parser = new AxiomParser(new CommonTokenStream(lexer));
lexer.removeErrorListeners();
parser.removeErrorListeners();
AxiomErrorListener errorListener = new AxiomErrorListener(sourceName);
parser.addErrorListener(errorListener);
StatementContext statement = parser.statement();
ItemContext statement = parser.item();
errorListener.validate();
return statement;
}

public static AxiomAntlrStatementSource from(String sourceName, CharStream stream) throws AxiomSyntaxException {
StatementContext statement = contextFrom(sourceName, stream);
ItemContext statement = contextFrom(sourceName, stream);
return new AxiomAntlrStatementSource(sourceName, statement);
}

protected AxiomAntlrStatementSource(String sourceName, StatementContext statement) {
protected AxiomAntlrStatementSource(String sourceName, ItemContext statement) {
this.sourceName = sourceName;
this.root = statement;
}
Expand All @@ -56,7 +56,7 @@ public String sourceName() {
return sourceName;
}

protected final StatementContext root() {
protected final ItemContext root() {
return root;
}

Expand Down
Expand Up @@ -20,7 +20,7 @@

import com.evolveum.axiom.api.AxiomName;
import com.evolveum.axiom.api.stream.AxiomItemStream;
import com.evolveum.axiom.lang.antlr.AxiomParser.StatementContext;
import com.evolveum.axiom.lang.antlr.AxiomParser.ItemContext;
import com.evolveum.axiom.lang.spi.AxiomIdentifierResolver;
import com.evolveum.axiom.lang.spi.AxiomSyntaxException;

Expand All @@ -42,12 +42,12 @@ public static AxiomModelStatementSource from(String sourceName, InputStream stre
}

public static AxiomModelStatementSource from(String sourceName, CharStream stream) throws AxiomSyntaxException {
StatementContext statement = AxiomAntlrStatementSource.contextFrom(sourceName, stream);
String name = statement.argument().identifier().localIdentifier().getText();
return new AxiomModelStatementSource(sourceName, statement, name, namespace(statement), imports(statement));
ItemContext root = AxiomAntlrStatementSource.contextFrom(sourceName, stream);
String name = root.value().argument().identifier().localIdentifier().getText();
return new AxiomModelStatementSource(sourceName, root, name, namespace(root.value()), imports(root.value()));
}

private AxiomModelStatementSource(String sourceName, StatementContext statement, String namespace, String name, Map<String, String> imports) {
private AxiomModelStatementSource(String sourceName, ItemContext statement, String namespace, String name, Map<String, String> imports) {
super(sourceName, statement);
this.name = name;
this.imports = imports;
Expand All @@ -73,21 +73,21 @@ public Map<String, String> imports() {
return imports;
}

public static Map<String,String> imports(AxiomParser.StatementContext root) {
public static Map<String,String> imports(AxiomParser.ValueContext root) {
Map<String,String> prefixMap = new HashMap<>();
root.statement().stream().filter(s -> IMPORT.equals(s.identifier().getText())).forEach(c -> {
String prefix = c.argument().identifier().localIdentifier().getText();
String namespace = namespace(c);
root.item().stream().filter(s -> IMPORT.equals(s.identifier().getText())).forEach(c -> {
String prefix = c.value().argument().identifier().localIdentifier().getText();
String namespace = namespace(c.value());
prefixMap.put(prefix, namespace);
});
prefixMap.put("",namespace(root));
return prefixMap;
}

private static String namespace(StatementContext c) {
return AxiomAntlrVisitor.convert(c.statement()
private static String namespace(AxiomParser.ValueContext c) {
return AxiomAntlrVisitor.convert(c.item()
.stream().filter(s -> NAMESPACE.equals(s.identifier().getText()))
.findFirst().get().argument().string());
.findFirst().get().value().argument().string());
}

@Override
Expand Down

0 comments on commit 286268e

Please sign in to comment.