Skip to content

Commit

Permalink
Fix source info for TDS and RelationStoreAccessor DSLs (finos#895)
Browse files Browse the repository at this point in the history
* Fix source info for TDS inline DSL

* Fix source info for RelationStoreAccessor inline DSL
  • Loading branch information
kevin-m-knight-gs authored Nov 16, 2024
1 parent 7c859ba commit b88d5f0
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface InlineDSL extends CoreInstanceFactoriesRegistry

boolean match(String code);

CoreInstance parse(String code, ImportGroup importId, String fileName, int offsetX, int offsetY, ModelRepository modelRepository, Context context);
CoreInstance parse(String code, ImportGroup importId, String fileName, int columnOffset, int lineOffset, ModelRepository modelRepository, Context context);

RichIterable<MatchRunner> getProcessors();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ public void testPackagedElementsHaveNonOverlappingSourceInfo()
}

@Test
@Ignore
public void testPackagedElementsContainAllOthers()
{
CoreInstance packageClass = runtime.getCoreInstance(M3Paths.Package);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public class GraphDSL implements InlineDSL
{
private static RootGraphFetchTreeVisibilityValidator ROOTGRAPHFETCHTREE_VISIBILITYVALIDATOR = new RootGraphFetchTreeVisibilityValidator();
private static final RootGraphFetchTreeVisibilityValidator ROOTGRAPHFETCHTREE_VISIBILITYVALIDATOR = new RootGraphFetchTreeVisibilityValidator();

@Override
public String getName()
Expand All @@ -48,9 +48,9 @@ public boolean match(String code)
}

@Override
public CoreInstance parse(String code, ImportGroup importId, String fileName, int offsetX, int offsetY, ModelRepository modelRepository, Context context)
public CoreInstance parse(String code, ImportGroup importId, String fileName, int columnOffset, int lineOffset, ModelRepository modelRepository, Context context)
{
return new GraphAntlrParser().parse(code, importId, fileName, offsetX, offsetY, modelRepository, context);
return new GraphAntlrParser().parse(code, importId, fileName, columnOffset, lineOffset, modelRepository, context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@

public class NavigationPath implements InlineDSL
{
private static VisibilityValidator VISIBILITY_VALIDATOR = new PathVisibilityValidator();
private static MilestoningDatesVarNamesExtractor MILESTONING_EXTRACTOR = new PathMilestoningDatesVarNamesExtractor();
private static final VisibilityValidator VISIBILITY_VALIDATOR = new PathVisibilityValidator();
private static final MilestoningDatesVarNamesExtractor MILESTONING_EXTRACTOR = new PathMilestoningDatesVarNamesExtractor();

@Override
public String getName()
Expand All @@ -50,9 +50,9 @@ public boolean match(String code)
}

@Override
public CoreInstance parse(String code, ImportGroup importId, String fileName, int offsetX, int offsetY, ModelRepository modelRepository, Context context)
public CoreInstance parse(String code, ImportGroup importId, String fileName, int columnOffset, int lineOffset, ModelRepository modelRepository, Context context)
{
return new NavigationParser().parse(code, importId, fileName, offsetX, offsetY, modelRepository, context);
return new NavigationParser().parse(code, importId, fileName, columnOffset, lineOffset, modelRepository, context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
import org.finos.legend.pure.m4.ModelRepository;
import org.finos.legend.pure.m4.coreinstance.CoreInstance;
import org.finos.legend.pure.m4.coreinstance.SourceInformation;
import org.finos.legend.pure.m4.serialization.grammar.antlr.AntlrSourceInformation;
import org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RelationStoreAccessor implements InlineDSL
{
private static VisibilityValidator VISIBILITY_VALIDATOR = new RelationStoreAccessorValidation();
private static final VisibilityValidator VISIBILITY_VALIDATOR = new RelationStoreAccessorValidation();

@Override
public String getName()
Expand All @@ -54,26 +56,22 @@ public boolean match(String code)
}

@Override
public CoreInstance parse(String code, ImportGroup importId, String fileName, int offsetX, int offsetY, ModelRepository modelRepository, Context context)
public CoreInstance parse(String code, ImportGroup importId, String fileName, int columnOffset, int lineOffset, ModelRepository modelRepository, Context context)
{
AntlrSourceInformation sourceInformation = new AntlrSourceInformation(offsetX, offsetY, fileName, true);

ProcessorSupport processorSupport = new M3ProcessorSupport(context, modelRepository);
SourceInformation src = new SourceInformation(fileName, offsetX, offsetY, offsetX, offsetY + code.length());
SourceInformation src = getSourceInfo(code, fileName, columnOffset, lineOffset);
String info = code.trim().substring(1).trim();
String first = info.substring(0, 1);
if (!"{".equals(first) && !"}".equals(info.substring(info.length() - 2, info.length() - 1)))
{
throw new PureParserException(sourceInformation.getPureSourceInformation(0, 0, 0, code.length()), "RelationStoreAccessor must be of the form #>{a::Store.table}#");
throw new PureParserException(src, "RelationStoreAccessor must be of the form #>{a::Store.table}#");
}
info = info.substring(1, info.length() - 1);

String[] path = info.split("\\.");

org.finos.legend.pure.m3.coreinstance.meta.pure.store.RelationStoreAccessor<?> rel = ((org.finos.legend.pure.m3.coreinstance.meta.pure.store.RelationStoreAccessor<?>) processorSupport.newAnonymousCoreInstance(src, M2StorePaths.RelationStoreAccessor));
rel._path(Lists.mutable.with(path));

return rel;
return ((org.finos.legend.pure.m3.coreinstance.meta.pure.store.RelationStoreAccessor<?>) processorSupport.newAnonymousCoreInstance(src, M2StorePaths.RelationStoreAccessor))
._path(Lists.mutable.with(path));
}

@Override
Expand Down Expand Up @@ -117,4 +115,19 @@ public MilestoningDatesVarNamesExtractor getMilestoningDatesVarNamesExtractor()
{
return null;
}

private static SourceInformation getSourceInfo(String text, String fileName, int columnOffset, int lineOffset)
{
int endLine = lineOffset;
int endLineIndex = 0;
Matcher matcher = Pattern.compile("\\R").matcher(text);
while (matcher.find())
{
endLine++;
endLineIndex = matcher.end();
}

int endColumn = (endLine == lineOffset) ? (text.length() + columnOffset - 1) : (text.length() - endLineIndex);
return new SourceInformation(fileName, lineOffset, columnOffset, endLine, endColumn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.deephaven.csv.parsers.DataType;
import io.deephaven.csv.reading.CsvReader;
import io.deephaven.csv.sinks.SinkFactory;
import io.deephaven.csv.util.CsvReaderException;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.impl.utility.ArrayIterate;
Expand Down Expand Up @@ -46,10 +47,13 @@
import org.finos.legend.pure.m4.coreinstance.SourceInformation;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TDSExtension implements InlineDSL
{
private static VisibilityValidator VISIBILITY_VALIDATOR = new TDSVisibilityValidator();
private static final VisibilityValidator VISIBILITY_VALIDATOR = new TDSVisibilityValidator();

@Override
public String getName()
Expand All @@ -64,86 +68,12 @@ public boolean match(String code)
}

@Override
public CoreInstance parse(String code, ImportGroup importId, String fileName, int offsetX, int offsetY, ModelRepository modelRepository, Context context)
public CoreInstance parse(String code, ImportGroup importId, String fileName, int columnOffset, int lineOffset, ModelRepository modelRepository, Context context)
{
String val = code.substring("TDS".length()).trim();
String text = code.substring("TDS".length()).trim();
SourceInformation sourceInfo = getSourceInfo(code, fileName, columnOffset, lineOffset);
ProcessorSupport processorSupport = new M3ProcessorSupport(context, modelRepository);
return parse(val, fileName, processorSupport);
}

private static SinkFactory makeMySinkFactory()
{
return SinkFactory.arrays(
null,
null,
null,
null,
null,
null,
null,
null,
null,
Long.MIN_VALUE,
Long.MIN_VALUE);
}

public static TDS<?> parse(String val, String fileName, ProcessorSupport processorSupport)
{
final CsvReader.Result result;

try
{
result = CsvReader.read(CsvSpecs.csv(), new ByteArrayInputStream(val.getBytes()), makeMySinkFactory());
}
catch (Exception e)
{
throw new RuntimeException(e);
}

SourceInformation src = new SourceInformation(fileName, 0, 0, 0, 0);
Class<?> tdsType = (Class<?>) processorSupport.package_getByUserPath(M2TDSPaths.TDS);
TDS<?> tds = ((TDS<?>) processorSupport.newEphemeralAnonymousCoreInstance(M2TDSPaths.TDS));
GenericType tdsGenericType = (GenericType) processorSupport.newAnonymousCoreInstance(src, M3Paths.GenericType);
tdsGenericType._rawType(tdsType);
GenericType typeParam = (GenericType) processorSupport.newAnonymousCoreInstance(src, M3Paths.GenericType);
typeParam._rawType(_RelationType.build(ArrayIterate.collect(result.columns(), c -> _Column.getColumnInstance(c.name(), false, convertType(c.dataType()), (Multiplicity) org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.newMultiplicity(0, 1, processorSupport), src, processorSupport)), src, processorSupport));
tdsGenericType._typeArgumentsAdd(typeParam);
tds._classifierGenericType(tdsGenericType);
tds._csv(val);

return tds;
}

private static String convertType(DataType dataType)
{
String value = "";
switch (dataType)
{
case BOOLEAN_AS_BYTE:
value = "Boolean";
break;
case BYTE:
case SHORT:
case INT:
case LONG:
value = "Integer";
break;
case DATETIME_AS_LONG:
value = "Date";
break;
case FLOAT:
case DOUBLE:
value = "Float";
break;
case STRING:
case CHAR:
value = "String";
break;
case TIMESTAMP_AS_LONG:
case CUSTOM:
throw new RuntimeException("Not possible");
}
return value;
return parse(text, sourceInfo, processorSupport);
}

@Override
Expand Down Expand Up @@ -187,4 +117,111 @@ public MilestoningDatesVarNamesExtractor getMilestoningDatesVarNamesExtractor()
{
return null;
}

public static TDS<?> parse(String text, ProcessorSupport processorSupport)
{
return parse(text, (SourceInformation) null, processorSupport);
}

public static TDS<?> parse(String text, String fileName, ProcessorSupport processorSupport)
{
return parse(text, (fileName == null) ? null : getSourceInfo(text, fileName, 0, 0), processorSupport);
}

public static TDS<?> parse(String text, SourceInformation sourceInfo, ProcessorSupport processorSupport)
{
CsvReader.Result result;
try
{
result = CsvReader.read(CsvSpecs.csv(), new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)), makeMySinkFactory());
}
catch (CsvReaderException e)
{
throw new RuntimeException(e);
}

Class<?> tdsType = (Class<?>) processorSupport.package_getByUserPath(M2TDSPaths.TDS);
GenericType typeParam = ((GenericType) processorSupport.newAnonymousCoreInstance(sourceInfo, M3Paths.GenericType))
._rawType(_RelationType.build(ArrayIterate.collect(result.columns(), c -> _Column.getColumnInstance(c.name(), false, convertType(c.dataType()), (Multiplicity) org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.newMultiplicity(0, 1, processorSupport), sourceInfo, processorSupport)), sourceInfo, processorSupport));
GenericType tdsGenericType = ((GenericType) processorSupport.newAnonymousCoreInstance(sourceInfo, M3Paths.GenericType))
._rawType(tdsType)
._typeArgumentsAdd(typeParam);

return ((TDS<?>) processorSupport.newAnonymousCoreInstance(sourceInfo, M2TDSPaths.TDS))
._classifierGenericType(tdsGenericType)
._csv(text);
}

private static SourceInformation getSourceInfo(String text, String fileName, int columnOffset, int lineOffset)
{
int endLine = lineOffset;
int endLineIndex = 0;
Matcher matcher = Pattern.compile("\\R").matcher(text);
while (matcher.find())
{
endLine++;
endLineIndex = matcher.end();
}

int endColumn = (endLine == lineOffset) ? (text.length() + columnOffset - 1) : (text.length() - endLineIndex);
return new SourceInformation(fileName, lineOffset, columnOffset, endLine, endColumn);
}

private static String convertType(DataType dataType)
{
switch (dataType)
{
case BOOLEAN_AS_BYTE:
{
return M3Paths.Boolean;
}
case BYTE:
case SHORT:
case INT:
case LONG:
{
return M3Paths.Integer;
}
case DATETIME_AS_LONG:
{
return M3Paths.Date;
}
case FLOAT:
case DOUBLE:
{
return M3Paths.Float;
}
case STRING:
case CHAR:
{
return M3Paths.String;
}
case TIMESTAMP_AS_LONG:
case CUSTOM:
{
throw new RuntimeException("Not possible");
}
default:
{
// TODO is this correct?
return "";
}
}
}

private static SinkFactory makeMySinkFactory()
{
return SinkFactory.arrays(
null,
null,
null,
null,
null,
null,
null,
null,
null,
Long.MIN_VALUE,
Long.MIN_VALUE);
}
}
Loading

0 comments on commit b88d5f0

Please sign in to comment.