Skip to content

Commit

Permalink
Avoid loading extensions from classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbey committed Oct 25, 2024
1 parent e7e709b commit 07321c4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.collections.impl.utility.ListIterate;
import org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation;
import org.finos.legend.engine.language.pure.grammar.from.PureGrammarParserContext;
import org.finos.legend.engine.language.pure.grammar.from.PureGrammarParserUtility;
import org.finos.legend.engine.language.pure.grammar.from.antlr4.connection.ConnectionParserGrammar;
import org.finos.legend.engine.language.pure.grammar.from.extension.ConnectionValueParser;
Expand Down Expand Up @@ -94,7 +95,7 @@ private Connection visitConnectionValue(ConnectionParserGrammar.ConnectionValueC
// only add current walker source information column offset if this is the first line
int columnOffset = (startLine == 1 ? walkerSourceInformation.getColumnOffset() : 0) + ctx.BRACE_OPEN().getSymbol().getCharPositionInLine() + ctx.BRACE_OPEN().getSymbol().getText().length();
ParseTreeWalkerSourceInformation connectionValueWalkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder(walkerSourceInformation.getSourceId(), lineOffset, columnOffset).withReturnSourceInfo(this.walkerSourceInformation.getReturnSourceInfo()).build();
ConnectionValueSourceCode connectionValueSourceCode = new ConnectionValueSourceCode(connectionValueCode, connectionType, sourceInformation, connectionValueWalkerSourceInformation, isProcessingEmbeddedConnection);
ConnectionValueSourceCode connectionValueSourceCode = new ConnectionValueSourceCode(connectionValueCode, connectionType, sourceInformation, connectionValueWalkerSourceInformation, isProcessingEmbeddedConnection, new PureGrammarParserContext(this.extensions));
ConnectionValueParser connectionValueParser = this.extensions.getConnectionValueParser(connectionType);
if (connectionValueParser == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.finos.legend.engine.language.pure.grammar.from.connection;

import org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation;
import org.finos.legend.engine.language.pure.grammar.from.PureGrammarParserContext;
import org.finos.legend.engine.protocol.pure.v1.model.SourceInformation;

public class ConnectionValueSourceCode
Expand All @@ -24,13 +25,15 @@ public class ConnectionValueSourceCode
public final SourceInformation sourceInformation;
public final ParseTreeWalkerSourceInformation walkerSourceInformation;
public final boolean isEmbedded;
public final PureGrammarParserContext context;

public ConnectionValueSourceCode(String code, String connectionType, SourceInformation sourceInformation, ParseTreeWalkerSourceInformation walkerSourceInformation, boolean isEmbedded)
public ConnectionValueSourceCode(String code, String connectionType, SourceInformation sourceInformation, ParseTreeWalkerSourceInformation walkerSourceInformation, boolean isEmbedded, PureGrammarParserContext context)
{
this.code = code;
this.connectionType = connectionType;
this.sourceInformation = sourceInformation;
this.walkerSourceInformation = walkerSourceInformation;
this.isEmbedded = isEmbedded;
this.context = context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public List<PureGrammarParserExtension> getExtensions()
return this.extensions.castToList();
}

public <T extends PureGrammarParserExtension> List<T> getExtensionsOfType(Class<T> ofType)
{
return this.extensions.selectInstancesOf(ofType).castToList();
}

public SectionParser getExtraSectionParser(String type)
{
return this.sectionParsers.get(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@

public interface IRelationalGrammarParserExtension extends PureGrammarParserExtension
{
static List<IRelationalGrammarParserExtension> getExtensions()
static List<IRelationalGrammarParserExtension> getExtensions(PureGrammarParserContext context)
{
return Lists.mutable.withAll(ServiceLoader.load(IRelationalGrammarParserExtension.class));
return context.getPureGrammarParserExtensions().getExtensionsOfType(IRelationalGrammarParserExtension.class);
}

static DatasourceSpecification process(DataSourceSpecificationSourceCode code, List<Function<DataSourceSpecificationSourceCode, DatasourceSpecification>> processors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
public class RelationalDatabaseConnectionParseTreeWalker
{
private final ParseTreeWalkerSourceInformation walkerSourceInformation;
private final PureGrammarParserContext context;

public RelationalDatabaseConnectionParseTreeWalker(ParseTreeWalkerSourceInformation walkerSourceInformation)
public RelationalDatabaseConnectionParseTreeWalker(PureGrammarParserContext context, ParseTreeWalkerSourceInformation walkerSourceInformation)
{
this.context = context;
this.walkerSourceInformation = walkerSourceInformation;
}

Expand Down Expand Up @@ -91,7 +93,7 @@ public void visitRelationalDatabaseConnectionValue(RelationalDatabaseConnectionP
private void handleLocalMode(RelationalDatabaseConnection connectionValue)
{
connectionValue.localMode = true;
List<IRelationalGrammarParserExtension> extensions = IRelationalGrammarParserExtension.getExtensions();
List<IRelationalGrammarParserExtension> extensions = IRelationalGrammarParserExtension.getExtensions(context);
try
{
connectionValue.datasourceSpecification = IRelationalGrammarParserExtension.process(
Expand All @@ -111,7 +113,7 @@ private void handleLocalMode(RelationalDatabaseConnection connectionValue)
private List<PostProcessor> visitRelationalPostProcessors(RelationalDatabaseConnectionParserGrammar.RelationalPostProcessorsContext postProcessorsContext)
{
List<RelationalDatabaseConnectionParserGrammar.SpecificationContext> specifications = postProcessorsContext.specification();
List<IRelationalGrammarParserExtension> extensions = IRelationalGrammarParserExtension.getExtensions();
List<IRelationalGrammarParserExtension> extensions = IRelationalGrammarParserExtension.getExtensions(context);
List<Function<PostProcessorSpecificationSourceCode, PostProcessor>> parsers = ListIterate.flatCollect(extensions, IRelationalGrammarParserExtension::getExtraPostProcessorParsers);
return ListIterate.collect(specifications, spec -> visitRelationalPostProcessor(spec, parsers));
}
Expand Down Expand Up @@ -149,7 +151,7 @@ private DatasourceSpecification visitRelationalDatabaseConnectionDatasourceSpeci
ParseTreeWalkerSourceInformation.offset(walkerSourceInformation, ctx.getStart())
);

List<IRelationalGrammarParserExtension> extensions = IRelationalGrammarParserExtension.getExtensions();
List<IRelationalGrammarParserExtension> extensions = IRelationalGrammarParserExtension.getExtensions(context);
DatasourceSpecification ds = IRelationalGrammarParserExtension.process(code, ListIterate.flatCollect(extensions, IRelationalGrammarParserExtension::getExtraDataSourceSpecificationParsers));

if (ds == null)
Expand All @@ -173,7 +175,7 @@ public AuthenticationStrategy visitRelationalDatabaseConnectionAuthenticationStr
ParseTreeWalkerSourceInformation.offset(walkerSourceInformation, ctx.getStart())
);

List<IRelationalGrammarParserExtension> extensions = IRelationalGrammarParserExtension.getExtensions();
List<IRelationalGrammarParserExtension> extensions = IRelationalGrammarParserExtension.getExtensions(context);
AuthenticationStrategy auth = IRelationalGrammarParserExtension.process(code, ListIterate.flatCollect(extensions, IRelationalGrammarParserExtension::getExtraAuthenticationStrategyParsers));

if (auth == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@
import org.finos.legend.engine.language.pure.grammar.from.antlr4.connection.authentication.AuthenticationStrategyParserGrammar;
import org.finos.legend.engine.language.pure.grammar.from.antlr4.connection.datasource.DataSourceSpecificationLexerGrammar;
import org.finos.legend.engine.language.pure.grammar.from.antlr4.connection.datasource.DataSourceSpecificationParserGrammar;
import org.finos.legend.engine.language.pure.grammar.from.antlr4.data.DataParserGrammar;
import org.finos.legend.engine.language.pure.grammar.from.antlr4.mapper.RelationalMapperLexerGrammar;
import org.finos.legend.engine.language.pure.grammar.from.antlr4.mapper.RelationalMapperParserGrammar;
import org.finos.legend.engine.language.pure.grammar.from.antlr4.mapping.MappingParserGrammar;
import org.finos.legend.engine.language.pure.grammar.from.authentication.AuthenticationStrategyParseTreeWalker;
import org.finos.legend.engine.language.pure.grammar.from.authentication.AuthenticationStrategySourceCode;
import org.finos.legend.engine.language.pure.grammar.from.connection.ConnectionValueSourceCode;
import org.finos.legend.engine.language.pure.grammar.from.data.DataParseTreeWalker;
import org.finos.legend.engine.language.pure.grammar.from.data.RelationalEmbeddedDataParser;
import org.finos.legend.engine.language.pure.grammar.from.datasource.DataSourceSpecificationParseTreeWalker;
import org.finos.legend.engine.language.pure.grammar.from.datasource.DataSourceSpecificationSourceCode;
import org.finos.legend.engine.language.pure.grammar.from.extension.ConnectionValueParser;
import org.finos.legend.engine.language.pure.grammar.from.extension.MappingElementParser;
import org.finos.legend.engine.language.pure.grammar.from.extension.MappingTestInputDataParser;
import org.finos.legend.engine.language.pure.grammar.from.extension.PureGrammarParserExtensionLoader;
import org.finos.legend.engine.language.pure.grammar.from.extension.PureGrammarParserExtensions;
import org.finos.legend.engine.language.pure.grammar.from.extension.SectionParser;
import org.finos.legend.engine.language.pure.grammar.from.extension.data.EmbeddedDataParser;
import org.finos.legend.engine.language.pure.grammar.from.mapper.RelationalMapperParseTreeWalker;
Expand Down Expand Up @@ -108,7 +108,7 @@ private static Section parseRelationalSection(SectionSourceCode sectionSourceCod
DefaultCodeSection section = new DefaultCodeSection();
section.parserName = sectionSourceCode.sectionType;
section.sourceInformation = parserInfo.sourceInformation;
RelationalParseTreeWalker walker = new RelationalParseTreeWalker(parserInfo.walkerSourceInformation, elementConsumer, section);
RelationalParseTreeWalker walker = new RelationalParseTreeWalker(parserInfo.walkerSourceInformation, elementConsumer, section, pureGrammarParserContext);
walker.visit((RelationalParserGrammar.DefinitionContext) parserInfo.rootContext);
return section;
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public Iterable<? extends MappingElementParser> getExtraMappingElementParsers()
{
MappingParserGrammar.MappingElementContext ctx = mappingElementSourceCode.mappingElementParserRuleContext;
SourceCodeParserInfo parserInfo = getRelationalMappingElementParserInfo(mappingElementSourceCode);
RelationalParseTreeWalker walker = new RelationalParseTreeWalker(parserInfo.walkerSourceInformation);
RelationalParseTreeWalker walker = new RelationalParseTreeWalker(parserInfo.walkerSourceInformation, parserContext);
if (parserInfo.rootContext instanceof RelationalParserGrammar.ClassMappingContext)
{
RootRelationalClassMapping classMapping = new RootRelationalClassMapping();
Expand Down Expand Up @@ -182,7 +182,7 @@ public Iterable<? extends ConnectionValueParser> getExtraConnectionParsers()
return Collections.singletonList(ConnectionValueParser.newParser(RELATIONAL_DATABASE_CONNECTION_TYPE, connectionValueSourceCode ->
{
SourceCodeParserInfo parserInfo = getRelationalDatabaseConnectionParserInfo(connectionValueSourceCode);
RelationalDatabaseConnectionParseTreeWalker walker = new RelationalDatabaseConnectionParseTreeWalker(parserInfo.walkerSourceInformation);
RelationalDatabaseConnectionParseTreeWalker walker = new RelationalDatabaseConnectionParseTreeWalker(connectionValueSourceCode.context, parserInfo.walkerSourceInformation);
RelationalDatabaseConnection connectionValue = new RelationalDatabaseConnection();
connectionValue.sourceInformation = connectionValueSourceCode.sourceInformation;
walker.visitRelationalDatabaseConnectionValue((RelationalDatabaseConnectionParserGrammar.DefinitionContext) parserInfo.rootContext, connectionValue, connectionValueSourceCode.isEmbedded);
Expand Down Expand Up @@ -411,21 +411,6 @@ private static SourceCodeParserInfo getRelationalDatabaseConnectionParserInfo(Co
return new SourceCodeParserInfo(connectionValueSourceCode.code, input, connectionValueSourceCode.sourceInformation, connectionValueSourceCode.walkerSourceInformation, lexer, parser, parser.definition());
}

public static RelationalOperationElement parseRelationalOperationElement(String code, String sourceId, int lineOffset, int columnOffset, boolean returnSourceInfo)
{
CharStream input = CharStreams.fromString(code);
ParseTreeWalkerSourceInformation parseTreeWalkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder(sourceId, lineOffset, columnOffset).withReturnSourceInfo(returnSourceInfo).build();
ParserErrorListener errorListener = new ParserErrorListener(parseTreeWalkerSourceInformation);
RelationalLexerGrammar lexer = new RelationalLexerGrammar(input);
lexer.removeErrorListeners();
lexer.addErrorListener(errorListener);
RelationalParserGrammar parser = new RelationalParserGrammar(new CommonTokenStream(lexer));
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
RelationalParseTreeWalker walker = new RelationalParseTreeWalker(parseTreeWalkerSourceInformation);
return walker.visitOperation(parser.operation(), null);
}

public static void propagateStorePath(RelationalAssociationMapping mapping)
{
RelationalPropertyMapping propertyMapping = (RelationalPropertyMapping) mapping.propertyMappings.stream()
Expand All @@ -443,4 +428,19 @@ public static void propagateStorePath(RelationalAssociationMapping mapping)
}
}
}

public static RelationalOperationElement parseRelationalOperationElement(String code, String sourceId, int lineOffset, int columnOffset, boolean returnSourceInfo)
{
CharStream input = CharStreams.fromString(code);
ParseTreeWalkerSourceInformation parseTreeWalkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder(sourceId, lineOffset, columnOffset).withReturnSourceInfo(returnSourceInfo).build();
ParserErrorListener errorListener = new ParserErrorListener(parseTreeWalkerSourceInformation);
RelationalLexerGrammar lexer = new RelationalLexerGrammar(input);
lexer.removeErrorListeners();
lexer.addErrorListener(errorListener);
RelationalParserGrammar parser = new RelationalParserGrammar(new CommonTokenStream(lexer));
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
RelationalParseTreeWalker walker = new RelationalParseTreeWalker(parseTreeWalkerSourceInformation, new PureGrammarParserContext(PureGrammarParserExtensions.fromExtensions(PureGrammarParserExtensionLoader.extensions())));
return walker.visitOperation(parser.operation(), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,19 @@ public class RelationalParseTreeWalker
private final DefaultCodeSection section;

private static final ImmutableSet<String> JOIN_TYPES = Sets.immutable.with("INNER", "OUTER");
private final PureGrammarParserContext context;

public RelationalParseTreeWalker(ParseTreeWalkerSourceInformation walkerSourceInformation)
public RelationalParseTreeWalker(ParseTreeWalkerSourceInformation walkerSourceInformation, PureGrammarParserContext context)
{
this(walkerSourceInformation, null, null);
this(walkerSourceInformation, null, null, context);
}

public RelationalParseTreeWalker(ParseTreeWalkerSourceInformation walkerSourceInformation, Consumer<PackageableElement> elementConsumer, DefaultCodeSection section)
public RelationalParseTreeWalker(ParseTreeWalkerSourceInformation walkerSourceInformation, Consumer<PackageableElement> elementConsumer, DefaultCodeSection section, PureGrammarParserContext context)
{
this.walkerSourceInformation = walkerSourceInformation;
this.elementConsumer = elementConsumer;
this.section = section;
this.context = context;
}

public void visit(RelationalParserGrammar.DefinitionContext ctx)
Expand Down Expand Up @@ -464,7 +466,7 @@ private Milestoning visitMilestoning(RelationalParserGrammar.MilestoningContext
ParseTreeWalkerSourceInformation.offset(walkerSourceInformation, ctx.getStart())
);

List<IRelationalGrammarParserExtension> extensions = IRelationalGrammarParserExtension.getExtensions();
List<IRelationalGrammarParserExtension> extensions = IRelationalGrammarParserExtension.getExtensions(context);
Milestoning milestoning = IRelationalGrammarParserExtension.process(code, ListIterate.flatCollect(extensions, IRelationalGrammarParserExtension::getExtraMilestoningParsers));

if (milestoning == null)
Expand Down

0 comments on commit 07321c4

Please sign in to comment.