Skip to content

Commit

Permalink
Moved ANTLR specific classes to axiom.lang.antlr
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
  • Loading branch information
tonydamage committed May 19, 2020
1 parent 4459d85 commit ac85a7e
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 85 deletions.
@@ -0,0 +1,71 @@
/*
* 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.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Set;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;

import com.evolveum.axiom.api.AxiomIdentifier;
import com.evolveum.axiom.lang.antlr.AxiomLexer;
import com.evolveum.axiom.lang.antlr.AxiomParser;
import com.evolveum.axiom.lang.antlr.AxiomParser.StatementContext;
import com.evolveum.axiom.lang.spi.AxiomIdentifierResolver;
import com.evolveum.axiom.lang.spi.AxiomStatementStreamListener;
import com.evolveum.axiom.lang.spi.AxiomSyntaxException;

public class AxiomAntlrStatementSource {

private final StatementContext 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) {
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();
errorListener.validate();
return statement;
}

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

protected AxiomAntlrStatementSource(String sourceName, StatementContext statement) {
this.sourceName = sourceName;
this.root = statement;
}

public String sourceName() {
return sourceName;
}

protected final StatementContext root() {
return root;
}

public final void stream(AxiomIdentifierResolver statements, AxiomIdentifierResolver arguments, AxiomStatementStreamListener listener,
Optional<Set<AxiomIdentifier>> emitOnly) {
AxiomAntlrVisitor<?> visitor = new AxiomAntlrVisitor<>(sourceName, statements, arguments, listener, emitOnly.orElse(null));
visitor.visit(root);
}

}
Expand Up @@ -4,7 +4,7 @@
* 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.impl;
package com.evolveum.axiom.lang.antlr;

import java.util.Optional;
import java.util.Set;
Expand Down
Expand Up @@ -4,20 +4,14 @@
* 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.impl;
package com.evolveum.axiom.lang.antlr;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;

import com.evolveum.axiom.lang.spi.AxiomSyntaxException;

public class AxiomErrorListener extends BaseErrorListener {
Expand All @@ -33,7 +27,6 @@ public AxiomErrorListener(String source) {
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line,
final int charPositionInLine, final String msg, final RecognitionException e) {
exceptions.add(new AxiomSyntaxException(source, line, charPositionInLine, msg));

}

public void validate() throws AxiomSyntaxException {
Expand Down
Expand Up @@ -4,7 +4,7 @@
* 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.impl;
package com.evolveum.axiom.lang.antlr;

import java.beans.Statement;
import java.io.IOException;
Expand All @@ -30,66 +30,52 @@
import com.evolveum.axiom.lang.spi.AxiomStatementStreamListener;
import com.evolveum.axiom.lang.spi.AxiomSyntaxException;

public class AxiomStatementSource implements AxiomModelInfo, AxiomIdentifierResolver {
public class AxiomModelStatementSource extends AxiomAntlrStatementSource implements AxiomIdentifierResolver {

private static final String IMPORT = "import";
private static final String NAMESPACE = "namespace";
private final StatementContext root;
private String sourceName;

private String name;
private Map<String,String> imports;
private String namespace;

public static AxiomStatementSource from(InputStream stream) throws IOException, AxiomSyntaxException {
public static AxiomModelStatementSource from(InputStream stream) throws IOException, AxiomSyntaxException {
return from(null, CharStreams.fromStream(stream));
}

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

public static AxiomStatementSource from(String sourceName, CharStream stream) throws AxiomSyntaxException {

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();
errorListener.validate();
return new AxiomStatementSource(sourceName, statement, imports(statement));
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));
}

private AxiomStatementSource(String sourceName, StatementContext statement, Map<String, String> imports) {
this.sourceName = sourceName;
this.root = statement;
private AxiomModelStatementSource(String sourceName, StatementContext statement, String namespace, String name, Map<String, String> imports) {
super(sourceName, statement);
this.name = name;
this.imports = imports;
this.namespace = namespace;
}

@Override
public String getModelName() {
return root.argument().identifier().localIdentifier().getText();
}

@Override
public String getNamespace() {
return null;
public String modelName() {
return name;
}

@Override
public String getDescription() {
// TODO Auto-generated method stub
return null;
public String namespace() {
return namespace;
}

public void stream(AxiomIdentifierResolver resolver, AxiomStatementStreamListener listener) {
stream(resolver, listener, Optional.empty());
}

private void stream(AxiomIdentifierResolver resolver, AxiomStatementStreamListener listener,
public void stream(AxiomIdentifierResolver resolver, AxiomStatementStreamListener listener,
Optional<Set<AxiomIdentifier>> emitOnly) {
AxiomAntlrVisitor<?> visitor = new AxiomAntlrVisitor<>(sourceName, resolver, BUILTIN_TYPES.or(this).or(resolver), listener, emitOnly.orElse(null));
visitor.visit(root);
stream(resolver, BUILTIN_TYPES.or(this).or(resolver), listener, emitOnly);
}

public static Map<String,String> imports(AxiomParser.StatementContext root) {
Expand Down

This file was deleted.

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import com.evolveum.axiom.api.AxiomIdentifier;
import com.evolveum.axiom.lang.api.AxiomItemDefinition;
import com.evolveum.axiom.lang.spi.AxiomStatement;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import com.google.common.collect.ImmutableMap.Builder;

public class AxiomStatementImpl<V> implements AxiomStatement<V> {
Expand Down

This file was deleted.

Expand Up @@ -14,6 +14,7 @@
import com.evolveum.axiom.concepts.Lazy;
import com.evolveum.axiom.lang.api.AxiomBuiltIn.Type;
import com.evolveum.axiom.lang.api.AxiomIdentifierDefinition.Scope;
import com.evolveum.axiom.lang.antlr.AxiomModelStatementSource;
import com.evolveum.axiom.lang.api.AxiomBuiltIn;
import com.evolveum.axiom.lang.api.AxiomItemDefinition;
import com.evolveum.axiom.lang.api.AxiomSchemaContext;
Expand All @@ -34,19 +35,19 @@ public class ModelReactorContext implements AxiomIdentifierResolver {
private static final String AXIOM_LANG_RESOURCE = "/axiom-lang.axiom";
private static final String AXIOM_BUILTIN_RESOURCE = "/axiom-base-types.axiom";

private static final Lazy<AxiomStatementSource> BASE_LANGUAGE_SOURCE = Lazy.from(() -> {
private static final Lazy<AxiomModelStatementSource> BASE_LANGUAGE_SOURCE = Lazy.from(() -> {
InputStream stream = AxiomBuiltIn.class.getResourceAsStream(AXIOM_LANG_RESOURCE);
try {
return AxiomStatementSource.from(AXIOM_LANG_RESOURCE, stream);
return AxiomModelStatementSource.from(AXIOM_LANG_RESOURCE, stream);
} catch (Exception e) {
throw new RuntimeException(e);
}
});

private static final Lazy<AxiomStatementSource> BASE_TYPES_SOURCE = Lazy.from(() -> {
private static final Lazy<AxiomModelStatementSource> BASE_TYPES_SOURCE = Lazy.from(() -> {
InputStream stream = AxiomBuiltIn.class.getResourceAsStream(AXIOM_BUILTIN_RESOURCE);
try {
return AxiomStatementSource.from(AXIOM_BUILTIN_RESOURCE, stream);
return AxiomModelStatementSource.from(AXIOM_BUILTIN_RESOURCE, stream);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -184,7 +185,7 @@ public void addRules(StatementRule<?>... rules) {
}
}

public void loadModelFromSource(AxiomStatementSource statementSource) {
public void loadModelFromSource(AxiomModelStatementSource statementSource) {
statementSource.stream(this, new AxiomStatementStreamBuilder(this, new Root()));
}

Expand Down
Expand Up @@ -175,7 +175,6 @@ public RuleErrorMessage errorMessage() {
if(maybeDelegate instanceof Supplier && notFound != null) {
return notFound.get();
}
// TODO Auto-generated method stub
return super.errorMessage();
}

Expand Down
Expand Up @@ -6,10 +6,10 @@
import java.io.InputStream;

import com.evolveum.axiom.concepts.Lazy;
import com.evolveum.axiom.lang.antlr.AxiomModelStatementSource;
import com.evolveum.axiom.lang.api.AxiomBuiltIn;
import com.evolveum.axiom.lang.api.AxiomItemDefinition;
import com.evolveum.axiom.lang.api.AxiomSchemaContext;
import com.evolveum.axiom.lang.impl.AxiomStatementSource;
import com.evolveum.axiom.lang.impl.ModelReactorContext;
import com.evolveum.axiom.lang.spi.AxiomSyntaxException;
import com.evolveum.midpoint.tools.testng.AbstractUnitTest;
Expand All @@ -28,13 +28,13 @@ protected static AxiomSchemaContext parseInputStream(String name, InputStream st

protected static AxiomSchemaContext parseInputStream(String name, InputStream stream, AxiomItemDefinition rootItemDefinition) throws AxiomSyntaxException, FileNotFoundException, IOException {
ModelReactorContext reactorContext =ModelReactorContext.defaultReactor();
AxiomStatementSource statementSource = AxiomStatementSource.from(name, stream);
AxiomModelStatementSource statementSource = AxiomModelStatementSource.from(name, stream);
reactorContext.loadModelFromSource(statementSource);
return reactorContext.computeSchemaContext();
}

protected static AxiomStatementSource source(String name) throws AxiomSyntaxException, IOException {
protected static AxiomModelStatementSource source(String name) throws AxiomSyntaxException, IOException {
InputStream stream = new FileInputStream(COMMON_DIR_PATH + name);
return AxiomStatementSource.from(name, stream);
return AxiomModelStatementSource.from(name, stream);
}
}
Expand Up @@ -21,14 +21,13 @@

import com.evolveum.axiom.api.AxiomIdentifier;
import com.evolveum.axiom.concepts.Lazy;
import com.evolveum.axiom.lang.antlr.AxiomModelStatementSource;
import com.evolveum.axiom.lang.api.AxiomBuiltIn;
import com.evolveum.axiom.lang.api.AxiomItemDefinition;
import com.evolveum.axiom.lang.api.AxiomSchemaContext;
import com.evolveum.axiom.lang.api.AxiomTypeDefinition;
import com.evolveum.axiom.lang.api.AxiomBuiltIn.Item;
import com.evolveum.axiom.lang.api.AxiomBuiltIn.Type;

import com.evolveum.axiom.lang.impl.AxiomStatementSource;
import com.evolveum.axiom.lang.impl.ModelReactorContext;
import com.evolveum.axiom.lang.spi.AxiomSyntaxException;
import com.evolveum.midpoint.tools.testng.AbstractUnitTest;
Expand Down
Expand Up @@ -21,14 +21,13 @@

import com.evolveum.axiom.api.AxiomIdentifier;
import com.evolveum.axiom.concepts.Lazy;
import com.evolveum.axiom.lang.antlr.AxiomModelStatementSource;
import com.evolveum.axiom.lang.api.AxiomBuiltIn;
import com.evolveum.axiom.lang.api.AxiomItemDefinition;
import com.evolveum.axiom.lang.api.AxiomSchemaContext;
import com.evolveum.axiom.lang.api.AxiomTypeDefinition;
import com.evolveum.axiom.lang.api.AxiomBuiltIn.Item;
import com.evolveum.axiom.lang.api.AxiomBuiltIn.Type;

import com.evolveum.axiom.lang.impl.AxiomStatementSource;
import com.evolveum.axiom.lang.impl.ModelReactorContext;
import com.evolveum.axiom.lang.spi.AxiomSyntaxException;
import com.evolveum.midpoint.tools.testng.AbstractUnitTest;
Expand Down

0 comments on commit ac85a7e

Please sign in to comment.