Skip to content

Commit

Permalink
More source information fixes (finos#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-m-knight-gs authored Nov 13, 2024
1 parent 8314587 commit 685ac8c
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.eclipse.collections.api.list.ListIterable;
import org.finos.legend.pure.m3.navigation.Instance;
import org.finos.legend.pure.m3.navigation.M3Properties;
import org.finos.legend.pure.m3.tests.AbstractPureTestWithCoreCompiledPlatform;
import org.finos.legend.pure.m3.tests.AbstractPureTestWithCoreCompiled;
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.PureParserException;
Expand All @@ -26,7 +26,7 @@

import java.util.UUID;

public abstract class AbstractPrimitiveParsingTest extends AbstractPureTestWithCoreCompiledPlatform
public abstract class AbstractPrimitiveParsingTest extends AbstractPureTestWithCoreCompiled
{
@BeforeClass
public static void setUp()
Expand All @@ -51,26 +51,58 @@ protected void assertFailsToParse(String string)
}

protected void assertFailsToParse(String expectedInfo, String string)
{
assertFailsToParse(expectedInfo, null, null, string);
}

protected void assertFailsToParse(Integer expectedStart, Integer expectedEnd, String string)
{
assertFailsToParse(null, expectedStart, expectedEnd, string);
}

protected void assertFailsToParse(String expectedInfo, Integer expectedStart, Integer expectedEnd, String string)
{
PureParserException e = Assert.assertThrows(PureParserException.class, () -> parsePrimitiveValue(string));

assertPureException(PureParserException.class, expectedInfo, null, 3, null, 3, null, 3, null, e);
SourceInformation sourceInfo = e.getSourceInformation();
int start = 5;
if (sourceInfo.getStartColumn() < start)
if (expectedStart == null)
{
if (sourceInfo.getStartColumn() <= 0)
{
Assert.fail("Expected start column to be at least 1, got: " + sourceInfo.getStartColumn() + ", string: \"" + string + "\", source info: " + sourceInfo.getMessage());
}
if (sourceInfo.getColumn() <= 0)
{
Assert.fail("Expected column to be at least 1, got: " + sourceInfo.getStartColumn() + ", string: \"" + string + "\", source info: " + sourceInfo.getMessage());
}
}
else
{
if (sourceInfo.getStartColumn() != expectedStart)
{
Assert.fail("Expected start column to be " + expectedStart + ", got: " + sourceInfo.getStartColumn() + ", string: \"" + string + "\", source info: " + sourceInfo.getMessage());
}
if (sourceInfo.getColumn() != expectedStart)
{
Assert.fail("Expected start column to be " + expectedStart + ", got: " + sourceInfo.getStartColumn() + ", string: \"" + string + "\", source info: " + sourceInfo.getMessage());
}
}

if (expectedEnd == null)
{
Assert.fail("Expected start column to be at least " + start + ", got: " + sourceInfo.getStartColumn() + ", string: \"" + string + "\"");
if (sourceInfo.getEndColumn() > string.length())
{
Assert.fail("Expected end column to be at most " + string.length() + ", got: " + sourceInfo.getEndColumn() + ", string: \"" + string + "\", source info: " + sourceInfo.getMessage());
}
}
if (sourceInfo.getColumn() < start)
else
{
Assert.fail("Expected column to be at least " + start + ", got: " + sourceInfo.getStartColumn() + ", string: \"" + string + "\"");
if (sourceInfo.getEndColumn() != expectedEnd)
{
Assert.fail("Expected end column to be " + expectedEnd + ", got: " + sourceInfo.getEndColumn() + ", string: \"" + string + "\", source info: " + sourceInfo.getMessage());
}
}
// TODO re-enable
// int end = string.length() + 5;
// if (sourceInfo.getEndColumn() > end)
// {
// Assert.fail("Expected end column to be at most " + end + ", got: " + sourceInfo.getEndColumn() + ", string: \"" + string + "\"");
// }
}

private CoreInstance parsePrimitiveValue(String string)
Expand All @@ -80,7 +112,7 @@ private CoreInstance parsePrimitiveValue(String string)
String functionSignature = functionName + "():" + typeString + "[1]";
String testFunctionString = "function " + functionSignature + "\n" +
"{\n" +
" " + string + "\n" +
string + "\n" +
"}\n";
compileTestSource(testFunctionString);
CoreInstance function = runtime.getFunction(functionSignature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static void setUp()
public void clearRuntime()
{
runtime.delete("fromString.pure");
runtime.compile();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public void testMonth()
assertParsesTo("2014-02", "%2014-02");
assertParsesTo("2014-02", "%2014-2");

assertFailsToParse("%2014-1b");
assertFailsToParse("Invalid Pure Date: '%2014-34'", "%2014-34");
assertFailsToParse(8, 8, "%2014-1b");
assertFailsToParse("Invalid Pure Date: '%2014-34'", 1, 8, "%2014-34");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void testHour()
assertParsesTo("2014-02-07T07", "%2014-2-7T000007");

assertFailsToParse("%2014-02-07Tb");
assertFailsToParse("Invalid Pure Date: '%2014-02-07T55'", "%2014-02-07T55");
assertFailsToParse("Invalid Pure Date: '%2014-02-07T55'", 1, 14, "%2014-02-07T55");
}

@Test
Expand All @@ -38,7 +38,7 @@ public void testMinute()
assertParsesTo("2014-02-07T07:03+0000", "%2014-2-7T7:0000003");

assertFailsToParse("%2014-02-07T07:b");
assertFailsToParse("%2014-02-07T07:95");
assertFailsToParse(1, 17, "%2014-02-07T07:95");
}

@Test
Expand All @@ -49,7 +49,7 @@ public void testSecond()
assertParsesTo("2014-02-07T07:03:01+0000", "%2014-2-7T7:3:000000001");

assertFailsToParse("%2014-02-07T07:03:b");
assertFailsToParse("Invalid Pure Date: '%2014-02-07T07:03:95'", "%2014-02-07T07:03:95");
assertFailsToParse("Invalid Pure Date: '%2014-02-07T07:03:95'", 1, 20, "%2014-02-07T07:03:95");
}

@Test
Expand All @@ -58,7 +58,7 @@ public void testSubSecond()
assertParsesTo("2014-02-07T07:03:01.0003742635+0000", "%2014-02-07T07:03:01.0003742635");
assertParsesTo("2014-02-07T07:03:01.000374263500000000+0000", "%2014-2-7T7:3:1.000374263500000000");

assertFailsToParse("%2014-02-07T07:03:01.95b6");
assertFailsToParse(24, 25, "%2014-02-07T07:03:01.95b6");
}

@Test
Expand All @@ -69,7 +69,7 @@ public void testTimeZone()
assertParsesTo("2014-03-01T01:48:01.0003742635+0000", "%2014-02-28T20:03:01.0003742635-0545");
assertParsesTo("2012-02-29T01:48:01.0003742635+0000", "%2012-02-28T20:03:01.0003742635-0545");

assertFailsToParse("%2014-02-07T07:03:01.0132-43252342");
assertFailsToParse(31, 34, "%2014-02-07T07:03:01.0132-43252342");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class TestLatestDateParsing extends AbstractPrimitiveParsingTest
@Test
public void testLatestDateIsNotGenerallyUsable()
{
assertFailsToParse("%latest");
assertFailsToParse(1, 7, "%latest");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public void testDay()
assertParsesTo("2014-02-07", "%2014-2-7");

assertFailsToParse("%2014-02-1b");
assertFailsToParse("Invalid Pure Date: '%2014-02-53'", "%2014-02-53");
assertFailsToParse("Invalid Pure Date: '%2014-02-29'", "%2014-02-29");
assertFailsToParse("Invalid Pure Date: '%2016-06-31'", "%2016-06-31");
assertFailsToParse("Invalid Pure Date: '%2014-02-53'", 1, 11, "%2014-02-53");
assertFailsToParse("Invalid Pure Date: '%2014-02-29'", 1, 11, "%2014-02-29");
assertFailsToParse("Invalid Pure Date: '%2016-06-31'", 1, 11, "%2016-06-31");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import org.finos.legend.pure.m4.ModelRepository;
import org.junit.Test;



public class TestStrictTimeParsing extends AbstractPrimitiveParsingTest
{
@Test
Expand All @@ -29,7 +27,7 @@ public void testHour()
assertParsesTo("17:04", "%17:04");

assertFailsToParse("expected: a valid identifier text; found: '%'","%b:09");
assertFailsToParse("Invalid Pure StrictTime: '%55:09'", "%55:09");
assertFailsToParse("Invalid Pure StrictTime: '%55:09'", 1, 6, "%55:09");
}

@Test
Expand All @@ -40,8 +38,8 @@ public void testMinute()
assertParsesTo("07:03", "%7:0000003");

assertFailsToParse("expected: '}' found: ':'","%07:b");
assertFailsToParse("Invalid Pure StrictTime: '%19:99'", "%19:99");
assertFailsToParse("Invalid Pure StrictTime: '%20:95'","%20:95");
assertFailsToParse("Invalid Pure StrictTime: '%19:99'", 1, 6, "%19:99");
assertFailsToParse("Invalid Pure StrictTime: '%20:95'",1, 6, "%20:95");
}

@Test
Expand All @@ -52,7 +50,7 @@ public void testSecond()
assertParsesTo("07:03:01", "%7:3:000000001");

assertFailsToParse("expected: '}' found: ':'","%07:03:b");
assertFailsToParse("Invalid Pure StrictTime: '%07:03:95'", "%07:03:95");
assertFailsToParse("Invalid Pure StrictTime: '%07:03:95'", 1, 9, "%07:03:95");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testEnumGeneralizationToEnum()
{
compileTestSource("fromString.pure", "Enum test::TestEnum {A, B, C}");
PureParserException e = Assert.assertThrows(PureParserException.class, () -> compileTestSource("testSource.pure", "Enum test::TestEnum2 extends test::TestEnum {}"));
assertPureException(PureParserException.class, "expected: '{' found: 'extends'", "testSource.pure", 1, 22, 1, 22, 1, 29, e);
assertPureException(PureParserException.class, "expected: '{' found: 'extends'", "testSource.pure", 1, 22, 1, 22, 1, 28, e);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public int getOffsetColumn()
return this.offsetColumn;
}

public SourceInformation getPureSourceInformation(int line, int column)
{
return getPureSourceInformation(line, column, line, column, line, column);
}

public SourceInformation getPureSourceInformation(int beginLine, int beginColumn, int endLine, int endColumn)
{
return getPureSourceInformation(beginLine, beginColumn, beginLine, beginColumn, endLine, endColumn);
Expand Down Expand Up @@ -101,14 +106,12 @@ public SourceInformation getPureSourceInformation(Token firstToken, Token middle

public SourceInformation getSourceInformationForUnknownErrorPosition(int line, int charPositionInLine)
{
int adjustedLine = adjustLine(line);
int adjustedCol = adjustColumn(line, charPositionInLine + 1);
return new SourceInformation(this.sourceName, adjustedLine, adjustedCol, adjustedLine, adjustedCol);
return getPureSourceInformation(line, charPositionInLine + 1);
}

public SourceInformation getSourceInformationForOffendingToken(int line, int charPositionInLine, Token offendingToken)
{
return new SourceInformation(this.sourceName, adjustLine(line), adjustColumn(line, charPositionInLine + 1), offendingToken.getLine() + this.offsetLine, offendingToken.getStopIndex() + 1 + this.offsetColumn);
return getPureSourceInformation(line, charPositionInLine + 1, offendingToken.getLine(), offendingToken.getCharPositionInLine() + offendingToken.getText().length());
}

private int adjustLine(int line)
Expand Down

0 comments on commit 685ac8c

Please sign in to comment.