Skip to content

Commit

Permalink
Axiom: Added representation of prefix+localName and use it
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 19, 2020
1 parent 2288093 commit e57826c
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 143 deletions.
Expand Up @@ -7,20 +7,13 @@
package com.evolveum.axiom.api.stream;

import com.evolveum.axiom.api.AxiomName;
import com.evolveum.axiom.api.AxiomPrefixedName;
import com.evolveum.axiom.concepts.SourceLocation;
import com.evolveum.axiom.lang.spi.AxiomNameResolver;

public interface AxiomItemStream {

interface Target {
void startItem(AxiomName item, SourceLocation loc);
void endItem(SourceLocation loc);

void startValue(Object value, SourceLocation loc);
void endValue(SourceLocation loc);

default void startInfra(AxiomName item, SourceLocation loc) {};
default void endInfra(SourceLocation loc) {};
interface Target extends AxiomStreamTarget<AxiomName> {

}

Expand All @@ -29,6 +22,9 @@ interface TargetWithResolver extends Target {
AxiomNameResolver itemResolver();
AxiomNameResolver valueResolver();

default AxiomStreamTarget<AxiomPrefixedName> asPrefixed(AxiomNameResolver sourceLocal) {
return new PrefixedToQualifiedNameAdapter(this, () -> itemResolver().or(sourceLocal), () -> valueResolver().or(sourceLocal));
}
}

}
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Evolveum and contributors
e * 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.
Expand Down
Expand Up @@ -24,7 +24,7 @@ public static SourceLocation from(String source, int line, int pos) {

@Override
public String toString() {
return sourceName + "["+ line + ":" + character + "]";
return sourceName + (line >= 0 ? "["+ line + ":" + character + "]" : "");
}

public String getSource() {
Expand Down
Expand Up @@ -12,7 +12,9 @@
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;
Expand All @@ -24,34 +26,33 @@

public abstract class AbstractAxiomAntlrVisitor<T> extends AxiomBaseVisitor<T> {

private final Optional<Set<AxiomName>> limit;
private final String sourceName;

private interface StartDelegate {
void start(AxiomName identifier, SourceLocation location);
void start(AxiomPrefixedName identifier, SourceLocation location);
}
private interface EndDelegate {
void end(SourceLocation location);
}


public AbstractAxiomAntlrVisitor(String name, Set<AxiomName> limit) {
public AbstractAxiomAntlrVisitor(String name) {
this.sourceName = name;
this.limit = Optional.ofNullable(limit);
}

private AxiomName statementIdentifier(IdentifierContext identifier) {
private AxiomPrefixedName statementIdentifier(IdentifierContext identifier) {
String prefix = nullableText(identifier.prefix());
String localName = identifier.localIdentifier().getText();
AxiomName ret = resolveItemName(prefix, localName);
AxiomSemanticException.check(ret != null, sourceLocation(identifier.start), "Item %s not allowed at this place.", identifier.getText());
return ret;
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 AxiomItemStream.Target delegate();
protected abstract AxiomName resolveItemName(String prefix, String localName);
protected abstract AxiomName resolveArgument(String prefix, String localName);
protected abstract AxiomStreamTarget<AxiomPrefixedName> delegate();

private String nullableText(ParserRuleContext prefix) {
return prefix != null ? prefix.getText() : "";
Expand All @@ -60,41 +61,37 @@ private String nullableText(ParserRuleContext prefix) {

@Override
public T visitItem(ItemContext ctx) {
AxiomName identifier = statementIdentifier(ctx.itemBody().identifier());
AxiomPrefixedName identifier = statementIdentifier(ctx.itemBody().identifier());
return processItemBody(identifier, ctx.itemBody(), delegate()::startItem, delegate()::endItem);
}

public T processItemBody(AxiomName identifier, ItemBodyContext ctx, StartDelegate start, EndDelegate end) {
if(canEmit(identifier)) {

SourceLocation startLoc = sourceLocation(ctx.identifier().start);
start.start(identifier, startLoc);
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;
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;
if(argument != null) {
value = convert(argument);
valueStart = sourceLocation(argument.start);
} else {
value = null;
valueStart = startLoc;
}
return defaultResult();

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) {
AxiomName identifier = statementIdentifier(ctx.itemBody().identifier());
AxiomPrefixedName identifier = statementIdentifier(ctx.itemBody().identifier());
return processItemBody(identifier, ctx.itemBody(), delegate()::startInfra, delegate()::endInfra);
}

Expand All @@ -106,34 +103,34 @@ private Object convert(ArgumentContext ctx) {
}
}

private boolean canEmit(AxiomName identifier) {
if (limit.isPresent()) {
return limit.get().contains(identifier);
}
return true;
}

@Override
public final T visitArgument(ArgumentContext ctx) {

return defaultResult();
}

private AxiomName convert(IdentifierContext argument) {
private AxiomPrefixedName convert(IdentifierContext argument) {
return argumentIdentifier(argument);
}

private AxiomName argumentIdentifier(IdentifierContext identifier) {
private AxiomPrefixedName argumentIdentifier(IdentifierContext identifier) {
String prefix = nullableText(identifier.prefix());
String localName = identifier.localIdentifier().getText();
return resolveArgument(prefix, localName);
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());
Expand Down
Expand Up @@ -16,8 +16,10 @@
import org.antlr.v4.runtime.CommonTokenStream;

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.AxiomItemStream.TargetWithResolver;
import com.evolveum.axiom.api.stream.AxiomStreamTarget;
import com.evolveum.axiom.lang.antlr.AxiomParser.ItemContext;
import com.evolveum.axiom.lang.spi.AxiomNameResolver;
import com.evolveum.axiom.lang.spi.AxiomSyntaxException;
Expand Down Expand Up @@ -71,13 +73,8 @@ public void stream(AxiomItemStream.TargetWithResolver target, Optional<Set<Axiom

public final void stream(TargetWithResolver target, Optional<Set<AxiomName>> emitOnly,
AxiomNameResolver resolver) {
AxiomAntlrVisitor2<?> visitor = new AxiomAntlrVisitor2<>(sourceName, target, emitOnly.orElse(null), resolver);
visitor.visit(root);
}

public final void stream(AxiomNameResolver statements, AxiomNameResolver arguments, AxiomItemStream.Target listener,
Optional<Set<AxiomName>> emitOnly) {
AxiomAntlrVisitor<?> visitor = new AxiomAntlrVisitor<>(sourceName, statements, arguments, listener, emitOnly.orElse(null));
AxiomStreamTarget<AxiomPrefixedName> prefixedTarget = target.asPrefixed(resolver);
AxiomAntlrVisitor2<?> visitor = new AxiomAntlrVisitor2<>(sourceName, prefixedTarget);
visitor.visit(root);
}

Expand Down

This file was deleted.

Expand Up @@ -6,37 +6,20 @@
*/
package com.evolveum.axiom.lang.antlr;

import java.util.Set;

import com.evolveum.axiom.api.AxiomName;
import com.evolveum.axiom.api.stream.AxiomItemStream;
import com.evolveum.axiom.api.stream.AxiomItemStream.Target;
import com.evolveum.axiom.lang.spi.AxiomNameResolver;
import com.evolveum.axiom.api.AxiomPrefixedName;
import com.evolveum.axiom.api.stream.AxiomStreamTarget;

public class AxiomAntlrVisitor2<T> extends AbstractAxiomAntlrVisitor<T> {

private final AxiomItemStream.TargetWithResolver delegate;
private final AxiomNameResolver sourceLocal;
private final AxiomStreamTarget<AxiomPrefixedName> delegate;

public AxiomAntlrVisitor2(String name, AxiomItemStream.TargetWithResolver delegate,
Set<AxiomName> limit, AxiomNameResolver resolver) {
super(name, limit);
public AxiomAntlrVisitor2(String name, AxiomStreamTarget<AxiomPrefixedName> delegate) {
super(name);
this.delegate = delegate;
this.sourceLocal = resolver;
}

@Override
protected AxiomName resolveArgument(String prefix, String localName) {
return delegate.valueResolver().or(sourceLocal).resolveIdentifier(prefix, localName);
}

@Override
protected AxiomName resolveItemName(String prefix, String localName) {
return delegate.itemResolver().or(sourceLocal).resolveIdentifier(prefix, localName);
}

@Override
protected Target delegate() {
protected AxiomStreamTarget<AxiomPrefixedName> delegate() {
return delegate;
}
}
Expand Up @@ -84,7 +84,7 @@ public Map<String, String> imports() {
public static Map<String,String> imports(AxiomParser.ValueContext root) {
Map<String,String> prefixMap = new HashMap<>();
root.item().stream().filter(s -> IMPORT.equals(s.itemBody().identifier().getText())).forEach(c -> {
String namespace = AxiomAntlrVisitor.convert(c.itemBody().value().argument().string());
String namespace = AxiomAntlrVisitor2.convert(c.itemBody().value().argument().string());
String prefix = prefix(c.itemBody().value());
prefixMap.put(prefix, namespace);
});
Expand All @@ -94,13 +94,13 @@ public static Map<String,String> imports(AxiomParser.ValueContext root) {


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

private static String prefix(AxiomParser.ValueContext c) {
return AxiomAntlrVisitor.convertToString(c.item()
return AbstractAxiomAntlrVisitor.convertToString(c.item()
.stream().filter(s -> PREFIX.equals(s.itemBody().identifier().getText()))
.findFirst().get().itemBody().value().argument());
}
Expand Down
Expand Up @@ -41,6 +41,15 @@ public void apply(Context<AxiomIdentifier> rule) throws AxiomSemanticException {
}
}
},*/
/*
SET_TYPE(all(),all()) {
@Override
public void apply(Lookup<AxiomName> context, ActionBuilder<AxiomName> action) throws AxiomSemanticException {
}
},*/

MOVE_ARGUMENT_VALUE(all(),all()) {
@Override
Expand Down
Expand Up @@ -12,6 +12,7 @@
import org.jetbrains.annotations.NotNull;

import com.evolveum.axiom.api.AxiomName;
import com.evolveum.axiom.api.AxiomPrefixedName;
import com.evolveum.axiom.api.meta.Inheritance;
import com.evolveum.axiom.api.schema.AxiomItemDefinition;
import com.evolveum.axiom.api.schema.AxiomTypeDefinition;
Expand Down Expand Up @@ -66,4 +67,8 @@ static AxiomNameResolver defaultNamespaceFromType(AxiomTypeDefinition type) {
};
}

default AxiomName resolve(AxiomPrefixedName prefixedName) {
return resolveIdentifier(prefixedName.getPrefix(), prefixedName.getLocalName());
}

}

0 comments on commit e57826c

Please sign in to comment.