Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python list initializers & equal initializer syntax #544

Closed
wants to merge 81 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
5d1be6e
Support list literals in LF syntax
oowekyala Sep 26, 2021
328285a
turn [] token into two separate tokens
oowekyala Sep 26, 2021
419cc0d
Update python generator
oowekyala Sep 26, 2021
32e3385
Fix python test
oowekyala Sep 26, 2021
c92da0f
Merge branch 'master' into list-init-syntax
oowekyala Oct 4, 2021
0fa8c20
Update some test code
oowekyala Oct 4, 2021
7784b3a
Rename TuplePrint -> ArrayAsType
oowekyala Oct 4, 2021
f08bd72
Forbid writing [,] as list literal
oowekyala Oct 4, 2021
abfb605
Merge branch 'master' into list-init-syntax
oowekyala Oct 14, 2021
adc20a5
Refactor to use a new Initializer production
oowekyala Oct 14, 2021
599627c
Add tests
oowekyala Oct 14, 2021
03fcc57
Fix grammar
oowekyala Oct 14, 2021
ec1d7d8
Add tests
oowekyala Oct 14, 2021
9a8b6e7
Remove peelParens
oowekyala Oct 14, 2021
a8cf467
Fix checking of types
oowekyala Oct 14, 2021
48040d3
Fix C++ param initializer
oowekyala Oct 14, 2021
451e928
Fix C tests
oowekyala Oct 14, 2021
43fe613
Fix TS tests
oowekyala Oct 14, 2021
4bb0a66
Enrich c++ initializer test
oowekyala Oct 14, 2021
f500a34
Fix python empty lists
oowekyala Oct 14, 2021
57585df
Fix typo
oowekyala Oct 15, 2021
9c0697a
Merge branch 'master' into list-init-syntax
oowekyala Oct 15, 2021
30bd55a
1 class support for arithmetic exprs in more contexts
oowekyala Oct 15, 2021
1f78382
Test type inference with eq assignment
oowekyala Oct 15, 2021
6cb60fe
Remove offset checks
oowekyala Oct 15, 2021
0434933
Add common test for expressions
oowekyala Oct 15, 2021
6c0f65f
Move test
oowekyala Oct 15, 2021
8a3e49a
Apply suggestions from code review
lhstrh Oct 19, 2021
f4f3d4d
Merge branch 'master' into list-init-syntax
oowekyala Oct 20, 2021
f1924ad
Fix TargetTypes
oowekyala Oct 20, 2021
1605bec
Fix TargetTypes w/ arithmetic
oowekyala Oct 20, 2021
84d92a9
Simplify C++ generator
oowekyala Oct 20, 2021
0b5f021
Fix cpp state initializers
oowekyala Oct 21, 2021
c2f7490
Really fix C++
oowekyala Oct 22, 2021
b5bcd3d
Cleanups
oowekyala Oct 22, 2021
2cff9ea
Merge branch 'master' into list-init-syntax
oowekyala Oct 25, 2021
b257d1c
Merge branch 'master' into list-init-syntax
oowekyala Oct 27, 2021
b701c42
Fix merge
oowekyala Oct 27, 2021
b2dde5c
Merge branch 'master' into list-init-syntax
oowekyala Nov 7, 2021
6252667
Add support for array initializer literals in C
oowekyala Nov 7, 2021
640a048
Refactor GeneratorBase
oowekyala Nov 7, 2021
08ed643
More cleanups for TS
oowekyala Nov 7, 2021
8ebc6e6
Cleanup TS further
oowekyala Nov 7, 2021
ff2e42e
Make all generators use TargetTypes default methods
oowekyala Nov 7, 2021
bc08e83
Fix C++ parameters
oowekyala Nov 7, 2021
b19695d
Fix validation of state vars
oowekyala Nov 7, 2021
e2b009a
Fix validation tests
oowekyala Nov 7, 2021
30a0028
Fix TS state initializers
oowekyala Nov 7, 2021
39cbe2d
Fix TS ctor generation
oowekyala Nov 7, 2021
8348a1b
Remove dead code
oowekyala Nov 7, 2021
27b94cb
Fix C++ initializers
oowekyala Nov 7, 2021
2924bf8
Fix Python tuples
oowekyala Nov 7, 2021
f2207db
Support missing initializer in C
oowekyala Nov 7, 2021
ed3fde9
Convert some stuff from AstUtils to java
oowekyala Nov 8, 2021
006ec71
Fix C++ tests
oowekyala Nov 8, 2021
7093364
Missing ctor in typescript test
oowekyala Nov 8, 2021
4213044
Fix TS tests
oowekyala Nov 8, 2021
b1cd66a
Guard against null time values
oowekyala Nov 8, 2021
e268913
Hopefully fix C++ for good
oowekyala Nov 8, 2021
0ad105b
Merge branch 'allow-after-0' into list-init-syntax
oowekyala Dec 7, 2021
ce4a7ad
Merge branch 'master' into list-init-syntax
oowekyala Jan 23, 2022
35729b8
Fix some conflicts
oowekyala Jan 23, 2022
5d32616
Merge branch 'master' into list-init-syntax
oowekyala Jan 29, 2022
f02fa39
Fix conflicts...
oowekyala Jan 29, 2022
f073ec6
Fix conflicts...
oowekyala Jan 29, 2022
bfb524b
Fix conflicts...
oowekyala Jan 29, 2022
8043724
Fix conflicts...
oowekyala Jan 29, 2022
c618d57
Fix conflicts...
oowekyala Jan 29, 2022
01d3453
Resolved conflicts with master. Special attention paid to conflict wi…
lhstrh Feb 4, 2022
5dd07de
Update kotlin version
oowekyala Feb 5, 2022
554ec24
Merge branch 'update-kotlin-version' into list-init-syntax
oowekyala Feb 5, 2022
a675fbd
Fix validator tests
oowekyala Feb 5, 2022
30b7157
Fix some other tests
oowekyala Feb 5, 2022
5b93e15
Try to fix bugs in CGenerator
oowekyala Feb 5, 2022
3c195d5
Update reactor-c to master version
oowekyala Feb 6, 2022
14fe4c0
Fix TS delay tests
oowekyala Feb 6, 2022
8aa7ab9
Fix python errors
oowekyala Feb 6, 2022
7e7f215
Revert "Update kotlin version"
oowekyala Feb 8, 2022
fc26561
Merge remote-tracking branch 'origin/master' into list-init-syntax
oowekyala Feb 21, 2022
a4b7fe8
fix merge...
oowekyala Feb 22, 2022
19de993
Fix more conflicts - compiles
oowekyala Feb 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins {
id "com.github.johnrengelman.shadow" version "${shadowJarVersion}"
id 'java'
id 'jacoco'
id 'org.jetbrains.kotlin.jvm' version "${kotlinVersion}"
}

subprojects {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jupiterVersion=5.8.2
jUnitPlatformVersion=1.8.2
jUnitVersion=4.13.2
kotlinJvmTarget=11
kotlinVersion=1.4.30
kotlinVersion=1.5.31
lsp4jVersion=0.12.0
mwe2LaunchVersion=2.12.1
openTest4jVersion=1.2.0
Expand Down
57 changes: 56 additions & 1 deletion org.lflang.tests/src/org/lflang/tests/LFParsingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class LFParsingTest {


@Test
public void testLexingEmptyTargetProperties() throws Exception {
public void testParsingEmptyTargetProperties() throws Exception {
assertNoParsingErrorsIn("target C { }; \nreactor Foo {}");
assertNoParsingErrorsIn("target C {a:b,}; \nreactor Foo {}");
expectParsingErrorIn("target C {,}; \nreactor Foo {}");
Expand All @@ -38,6 +38,61 @@ public void testLexingEmptyTargetProperties() throws Exception {
// assertNoParsingErrorsIn("target C {x:[,]}; \nreactor Foo {}");
}

@Test
public void testParsingListLiterals() throws Exception {
assertNoParsingErrorsIn("target Python; \nreactor Foo(p([1,])) {}");
// [,] is not an ok list literal
expectParsingErrorIn("target Python; \nreactor Foo(p([,])) {}");
}

@Test
public void testParsingInitializers() throws Exception {
assertNoParsingErrorsIn("target Python; \nreactor Foo(p = [1,]) {}");
assertNoParsingErrorsIn("target Python; \nreactor Foo(p = (1,)) {}");
assertNoParsingErrorsIn("target Python; \nreactor Foo(p = ()) {}");
assertNoParsingErrorsIn("target Python; \nreactor Foo(p = []) {}");
assertNoParsingErrorsIn("target Python; \nreactor Foo(p = 1) {}");

assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p = [1,] }");
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p = (1,) }");
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p = () }");
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p = [] }");
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p = 1 }");


// [,] is not an ok list literal
expectParsingErrorIn("target Python; \nreactor Foo(p = [,]) {}");
}

@Test
public void testParsingParenInitializers() throws Exception {
assertNoParsingErrorsIn("target Python; \nreactor Foo(p ([1,])) {}");
assertNoParsingErrorsIn("target Python; \nreactor Foo(p ((1,))) {}");
assertNoParsingErrorsIn("target Python; \nreactor Foo(p (())) {}");
assertNoParsingErrorsIn("target Python; \nreactor Foo(p ([])) {}");
assertNoParsingErrorsIn("target Python; \nreactor Foo(p (1)) {}");

assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p([1,]) }");
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p((1,)) }");
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p(()) }");
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p([]) }");
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p(1) }");


// [,] is not an ok list literal
expectParsingErrorIn("target Python; \nreactor Foo(p([,])) {}");
}

@Test
public void testParsingParentheses() throws Exception {
// unnecessary parentheses are allowed
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p = ((1)) }");
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p = (1) }");
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p = (1,) }");
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p = (1,2) }");
assertNoParsingErrorsIn("target Python; \nreactor Foo{ state p = (1,2,) }");
}

@Test
public void testLexingLifetimeAnnots() throws Exception {
assertNoParsingErrorsIn(makeLfTargetCode("Rust",
Expand Down
6 changes: 5 additions & 1 deletion org.lflang.tests/src/org/lflang/tests/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,12 @@ public static void runSingleTestAndPrintResults(LFTest test, Class<? extends Tes
Injector injector = new LFStandaloneSetup(new LFRuntimeModule()).createInjectorAndDoEMFRegistration();
TestBase runner;
try {
Constructor<?>[] ctors = testClass.getConstructors();
if (ctors.length == 0) {
throw new IllegalStateException("No public constructor in " + testClass);
}
@SuppressWarnings("unchecked")
Constructor<? extends TestBase> constructor = (Constructor<? extends TestBase>) testClass.getConstructors()[0];
Constructor<? extends TestBase> constructor = (Constructor<? extends TestBase>) ctors[0];
runner = constructor.newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.lflang.ASTUtils;
import org.lflang.lf.Instantiation;
import org.lflang.lf.Literal;
import org.lflang.lf.Model;
import org.lflang.lf.Parameter;
import org.lflang.lf.StateVar;
import org.lflang.lf.Value;
import org.lflang.tests.LFInjectorProvider;

/**
Expand Down Expand Up @@ -222,34 +224,34 @@ public void initialValue() throws Exception {
Parameter parameter = (Parameter)obj;
if (parameter.getName() == "x") {
var values = ASTUtils.initialValue(parameter, null);
Assertions.assertEquals(values.get(0).getLiteral(), "1");
Assertions.assertEquals(asLiteral(values), "1");

values = ASTUtils.initialValue(parameter,
List.of(map.get("a1")));
Assertions.assertEquals(values.get(0).getLiteral(), "2");
Assertions.assertEquals(asLiteral(values), "2");

values = ASTUtils.initialValue(parameter,
List.of(map.get("a2")));
Assertions.assertEquals(values.get(0).getLiteral(), "-1");
Assertions.assertEquals(asLiteral(values), "-1");

values = ASTUtils.initialValue(parameter,
List.of(map.get("a1"), map.get("b1")));
Assertions.assertEquals(values.get(0).getLiteral(), "3");
Assertions.assertEquals(asLiteral(values), "3");

values = ASTUtils.initialValue(parameter,
List.of(map.get("a2"), map.get("b1")));
Assertions.assertEquals(values.get(0).getLiteral(), "-1");
Assertions.assertEquals(asLiteral(values), "-1");

values = ASTUtils.initialValue(parameter,
List.of(map.get("a1"), map.get("b2")));
Assertions.assertEquals(values.get(0).getLiteral(), "-2");
Assertions.assertEquals(asLiteral(values), "-2");

values = ASTUtils.initialValue(parameter,
List.of(map.get("a2"), map.get("b2")));
Assertions.assertEquals(values.get(0).getLiteral(), "-1");
Assertions.assertEquals(asLiteral(values), "-1");
} else if (parameter.getName() == "y") {
var values = ASTUtils.initialValue(parameter, null);
Assertions.assertEquals(values.get(0).getLiteral(), "2");
Assertions.assertEquals(asLiteral(values), "2");

try {
values = ASTUtils.initialValue(parameter,
Expand All @@ -261,13 +263,17 @@ public void initialValue() throws Exception {

values = ASTUtils.initialValue(parameter,
List.of(map.get("b1")));
Assertions.assertEquals(values.get(0).getLiteral(), "3");
Assertions.assertEquals(asLiteral(values), "3");

values = ASTUtils.initialValue(parameter,
List.of(map.get("b2")));
Assertions.assertEquals(values.get(0).getLiteral(), "-2");
Assertions.assertEquals(asLiteral(values), "-2");
}
}
});
}

private Object asLiteral(List<Value> values) {
return ((Literal) values.get(0)).getLiteral();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ public void nonzeroAfterMustHaveUnits() throws Exception {
" b = new X()",
" a.y -> b.x after 1",
"}");
validator.assertError(parseWithoutError(testCase), LfPackage.eINSTANCE.getTime(),
validator.assertError(parseWithoutError(testCase), LfPackage.eINSTANCE.getConnection(),
null, "Missing time unit.");
}

Expand Down Expand Up @@ -936,7 +936,7 @@ public void nonZeroTimeValueWithoutUnits() throws Exception {
" printf(\"Hello World.\\n\");",
" =}",
"}");
validator.assertError(parseWithoutError(testCase), LfPackage.eINSTANCE.getValue(), null, "Missing time unit.");
validator.assertError(parseWithoutError(testCase), LfPackage.eINSTANCE.getTimer(), null, "Missing time unit.");
}

/**
Expand All @@ -963,8 +963,8 @@ public void parameterTypeMismatch() throws Exception {
" printf(\"Hello World.\\n\");",
" =}",
"}");
validator.assertError(parseWithoutError(testCase), LfPackage.eINSTANCE.getValue(),
null, "Parameter is not of time type");
validator.assertError(parseWithoutError(testCase), LfPackage.eINSTANCE.getTimer(),
null, "Referenced parameter does not have time type.");
}

/**
Expand All @@ -991,7 +991,7 @@ public void targetCodeInTimeArgument() throws Exception {
" printf(\"Hello World.\\n\");",
" =}",
"}");
validator.assertError(parseWithoutError(testCase), LfPackage.eINSTANCE.getValue(),
validator.assertError(parseWithoutError(testCase), LfPackage.eINSTANCE.getTimer(),
null, "Invalid time literal");
}

Expand Down Expand Up @@ -1095,7 +1095,7 @@ public void overflowingAssignmentC() throws Exception {
validator.assertError(parseWithoutError(testCase), LfPackage.eINSTANCE.getAssignment(), null,
"Time value used to specify a deadline exceeds the maximum of " +
TimeValue.MAX_LONG_DEADLINE + " nanoseconds.");
}
}

/**
* Report missing trigger.
Expand Down Expand Up @@ -1242,20 +1242,19 @@ public void stateAndParameterDeclarationsInC() throws Exception {
validator.assertError(model, LfPackage.eINSTANCE.getParameter(), null,
"Type declaration missing.");
validator.assertError(model, LfPackage.eINSTANCE.getParameter(), null,
"Missing time unit.");
"Uninitialized parameter.");
validator.assertError(model, LfPackage.eINSTANCE.getParameter(), null,
"Invalid time literal.");
"Missing time unit.");
validator.assertError(model, LfPackage.eINSTANCE.getParameter(), null,
"Time parameter cannot be initialized using a list.");
"Expected exactly one time value.");
validator.assertError(model, LfPackage.eINSTANCE.getStateVar(), null,
"Missing time unit.");
validator.assertError(model, LfPackage.eINSTANCE.getParameter(), null,
"Parameter cannot be initialized using parameter.");

validator.assertError(model, LfPackage.eINSTANCE.getStateVar(), null,
"Referenced parameter does not denote a time.");
validator.assertError(model, LfPackage.eINSTANCE.getStateVar(), null,
"Invalid time literal.");
validator.assertError(model, LfPackage.eINSTANCE.getParameter(), null,
"Uninitialized parameter.");
validator.assertError(model, LfPackage.eINSTANCE.getValue(), null,
"Referenced parameter does not have time type.");
validator.assertError(model, LfPackage.eINSTANCE.getTimer(), null,
"Missing time unit.");
}

Expand Down
26 changes: 26 additions & 0 deletions org.lflang.tests/src/org/lflang/tests/lsp/ErrorInserter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.lflang.tests.lsp;

import static java.lang.Math.max;
import static java.lang.Math.min;

import java.io.Closeable;
import java.io.IOException;
import java.io.PrintWriter;
Expand All @@ -14,6 +17,7 @@
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -164,8 +168,30 @@ public void insert(String line, Random random) {
});
}

/**
* Get the text of the test around the given line,
* for error reporting.
*/
public String getLinesAround(int line) {
final int LINES_AROUND = 2;
int minLine = max(0, line - LINES_AROUND);
List<String> linesAround =
lines.stream().skip(minLine).limit(2 * LINES_AROUND + 1).collect(Collectors.toList());
// prefix each line with its line number
ListIterator<String> iter = linesAround.listIterator();
int i = 0;
while (iter.hasNext()) {
String next = iter.next();
int lineNo = minLine + i;
iter.set(lineNo + "| " + next);
i++;
}
return String.join("\n", linesAround);
}

/**
* Alter the content of this test.
*
* @param alterer A function whose first argument is an iterator over the lines of {@code this}, whose second
* argument is the line most recently returned by that iterator, and whose return value is
* whether an alteration was successfully performed. This function is only applied within
Expand Down
3 changes: 3 additions & 0 deletions org.lflang.tests/src/org/lflang/tests/lsp/LspTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ private void targetLanguageValidationTest(Target target, ErrorInserter errorInse
diagnostic -> diagnostic.getRange().getStart().getLine() == badLine
);
System.out.println(result ? " Success." : " but the expected error could not be found.");
if (!result) {
System.err.println(alteredTest.getLinesAround(badLine));
}
return result;
}
)),
Expand Down
Loading