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

Replace Value with Expression in the grammar #1023

Merged
merged 66 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
14e8c9b
add basic expression syntax
cmnrd Mar 10, 2022
9a1427b
replace Value with Expressions in the grammar
cmnrd Mar 10, 2022
a4989a5
Use Expression instead of Value in ASTUtils
cmnrd Mar 10, 2022
50ded1d
fix various occurrences of Value in Kotlin code
cmnrd Mar 10, 2022
4fe7f28
fix all kotlin code
cmnrd Mar 10, 2022
12e8e04
update all occurences of Value in Java code
cmnrd Mar 10, 2022
1c7fde0
rename ValueGenerator -> ExpressionGenerator
cmnrd Mar 10, 2022
686303e
fix the CGenerator
cmnrd Mar 10, 2022
9143842
Merge branch 'master' into expressions
cmnrd Mar 11, 2022
23d79ae
fix compile errors after merge
cmnrd Mar 11, 2022
ae88382
fix compile errors in tests
cmnrd Mar 11, 2022
2b72c41
bugfixes
cmnrd Mar 11, 2022
745eedf
fix compile errors in diagram synthesis
cmnrd Mar 11, 2022
b563a55
fix validation of deadlines
cmnrd Mar 11, 2022
78e3dd2
Merge branch 'master' into expressions
cmnrd Mar 23, 2022
18e798c
fix use of eReferences in checkExpressionAsTime
cmnrd Mar 23, 2022
d213f43
bugfix
cmnrd Mar 23, 2022
872191d
fix unit tests
cmnrd Mar 23, 2022
2605217
fix bug in C++ generator
cmnrd Mar 23, 2022
647782c
use Expressions for after delays, add a validator rule
cmnrd Mar 23, 2022
a5a073b
remove all uses of Delay from the code base
cmnrd Mar 23, 2022
f9dd4ec
fix unit tests
cmnrd Mar 23, 2022
5ce567e
delete unused grammar rule
cmnrd Mar 23, 2022
beee9e5
simplify time check
cmnrd Mar 24, 2022
7c530cd
avoid code duplication and always use checkExpressionAsTime
cmnrd Mar 24, 2022
4d23097
Merge remote-tracking branch 'origin/master' into expressions
cmnrd Apr 21, 2022
f87774e
refactor: toText(Code) -> toTaggedText(Code)
cmnrd Apr 21, 2022
d3cd947
implement toText in dedicated switch class
cmnrd Apr 21, 2022
f32212f
fix compilation error in tests
cmnrd Apr 22, 2022
20f0ef8
bugfix
cmnrd Apr 22, 2022
750f8ae
bugfix
cmnrd Apr 22, 2022
1fb9003
fix a weird exception that suddenly occurred
cmnrd Apr 22, 2022
b98ea06
correctly deal with null
cmnrd Apr 22, 2022
a086884
fix logic error in validator
cmnrd Apr 22, 2022
70a2e4d
adjust unit tests
cmnrd Apr 22, 2022
a62b6c5
fix zero delays; no need to replicate expressions anymore
cmnrd Apr 22, 2022
a05bc20
add tests for zero delays
cmnrd Apr 22, 2022
038781c
bugfix in test
cmnrd Apr 29, 2022
59bd9e9
Merge remote-tracking branch 'origin/master' into expressions
cmnrd Apr 29, 2022
73e4495
remove Element case from toText as its specification differs from the…
cmnrd Apr 29, 2022
f35ce29
refactoring
cmnrd Apr 29, 2022
c2f7949
code should not be null when toText is called
cmnrd Apr 29, 2022
4e921dd
make ToText a singleton
cmnrd Apr 29, 2022
87842d8
use ToText also in diagrams
cmnrd Apr 29, 2022
97a1cc7
Merge remote-tracking branch 'origin/master' into expressions
cmnrd May 17, 2022
1ee3e3b
Simplify pr method.
petervdonovan May 18, 2022
82b4d4b
[tests] Ad hoc patch for LSP tests.
petervdonovan May 19, 2022
1b5dbd2
[tests] Further revise LSP tests.
petervdonovan May 19, 2022
8d4d047
[tests] Whitespace only.
petervdonovan May 19, 2022
3ef8ae5
[tests] Whitespace only.
petervdonovan May 19, 2022
ec064c1
[tests] Whitespace only.
petervdonovan May 19, 2022
065468a
[tests] Clean up after previous few commits.
petervdonovan May 19, 2022
1e8cbc5
Merge pull request #1183 from lf-lang/tests-fix-whitespace
petervdonovan May 19, 2022
911a3cb
Merge branch 'master' into expressions
petervdonovan May 19, 2022
d8675a5
Clean up after merge.
petervdonovan May 20, 2022
1e2ac1f
[tests] Another ad hoc patch in LSP tests.
petervdonovan May 20, 2022
0cafd5a
Merge branch 'master' into expressions
petervdonovan May 20, 2022
04f23bb
Address Python LSP test failure.
petervdonovan May 20, 2022
a121c89
[lsp] Rename toText etc. and refactor accordingly.
petervdonovan May 20, 2022
2d06ded
[lsp] Ad hoc patch over an NPE.
petervdonovan May 21, 2022
26df952
[refactoring] Bugfixes related to function renaming.
petervdonovan May 21, 2022
610d67e
[refactoring] Bugfix related to function renaming.
petervdonovan May 21, 2022
7c84b6f
[refactoring] Bugfix related to function renaming.
petervdonovan May 21, 2022
1b6cadd
[refactoring] Bugfix related to function renaming.
petervdonovan May 21, 2022
e933452
Address comments from code review.
petervdonovan May 23, 2022
a8e68ff
Address comments from code review.
petervdonovan May 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ private String createParameterLabel(ParameterInstance param, boolean bullet) {
}
if (!IterableExtensions.isNullOrEmpty(param.getInitialValue())) {
b.append("(");
b.append(IterableExtensions.join(param.getInitialValue(), ", ", _utilityExtensions::toText));
b.append(IterableExtensions.join(param.getInitialValue(), ", ", ASTUtils::toText));
b.append(")");
}
return b.toString();
Expand Down Expand Up @@ -1163,7 +1163,7 @@ private String createStateVariableLabel(StateVar variable, boolean bullet) {
}
if (!IterableExtensions.isNullOrEmpty(variable.getInit())) {
b.append("(");
b.append(IterableExtensions.join(variable.getInit(), ", ", _utilityExtensions::toText));
b.append(IterableExtensions.join(variable.getInit(), ", ", ASTUtils::toText));
b.append(")");
}
return b.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Pair;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.lflang.ASTUtils;
import org.lflang.diagram.synthesis.AbstractSynthesisExtensions;
import org.lflang.diagram.synthesis.LinguaFrancaSynthesis;
import org.lflang.diagram.synthesis.postprocessor.ReactionPortAdjustment;
Expand Down Expand Up @@ -175,7 +176,7 @@ public KRoundedRectangle addMainReactorFigure(KNode node, ReactorInstance reacto
if (reactorInstance.reactorDefinition.getHost() != null &&
getBooleanValue(LinguaFrancaSynthesis.SHOW_REACTOR_HOST)) {
KText hostNameText = _kContainerRenderingExtensions.addText(childContainer,
_utilityExtensions.toText(reactorInstance.reactorDefinition.getHost()));
ASTUtils.toText(reactorInstance.reactorDefinition.getHost()));
DiagramSyntheses.suppressSelectability(hostNameText);
_linguaFrancaStyleExtensions.underlineSelectionStyle(hostNameText);
setGridPlacementDataFromPointToPoint(hostNameText,
Expand Down Expand Up @@ -245,7 +246,7 @@ public ReactorFigureComponents addReactorFigure(KNode node, ReactorInstance reac

if (getBooleanValue(LinguaFrancaSynthesis.SHOW_REACTOR_HOST)) {
KText reactorHostText = _kContainerRenderingExtensions.addText(childContainer,
_utilityExtensions.toText(reactorInstance.getDefinition().getHost()));
ASTUtils.toText(reactorInstance.getDefinition().getHost()));
DiagramSyntheses.suppressSelectability(reactorHostText);
_linguaFrancaStyleExtensions.underlineSelectionStyle(reactorHostText);
setGridPlacementDataFromPointToPoint(reactorHostText,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@
import org.lflang.diagram.synthesis.AbstractSynthesisExtensions;
import org.lflang.generator.ReactorInstance;
import org.lflang.lf.Code;
import org.lflang.lf.Expression;
import org.lflang.lf.Host;
import org.lflang.lf.Literal;
import org.lflang.lf.ParameterReference;
import org.lflang.lf.Reactor;
import org.lflang.lf.Value;
import org.lflang.lf.Time;
import org.lflang.util.StringUtil;

import de.cau.cs.kieler.klighd.internal.util.KlighdInternalProperties;
Expand All @@ -61,44 +64,7 @@ public class UtilityExtensions extends AbstractSynthesisExtensions {

@Extension
private KGraphFactory _kGraphFactory = KGraphFactory.eINSTANCE;

/**
* Converts a timing value into readable text
*/
public String toText(Value value) {
if (value != null) {
if (value.getParameter() != null) {
return value.getParameter().getName();
} else if (value.getTime() != null) {
return value.getTime().getInterval() +
value.getTime().getUnit().toString();
} else if (value.getLiteral() != null) {
return value.getLiteral();
} else if (value.getCode() != null) {
return StringUtil.trimCodeBlock(value.getCode().getBody());
}
}
return "";
}

/**
* Converts a host value into readable text
*/
public String toText(Host host) {
StringBuilder sb = new StringBuilder();
if (host != null) {
if (!StringExtensions.isNullOrEmpty(host.getUser())) {
sb.append(host.getUser()).append("@");
}
if (!StringExtensions.isNullOrEmpty(host.getAddr())) {
sb.append(host.getAddr());
}
if (host.getPort() != 0) {
sb.append(":").append(host.getPort());
}
}
return sb.toString();
}


/**
* Returns true if the reactor is the primary reactor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.inject.Inject;

import org.eclipse.xtext.testing.InjectWith;
Expand All @@ -42,8 +41,10 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
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;
Expand Down Expand Up @@ -222,50 +223,47 @@ 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.assertInstanceOf(Literal.class, values.get(0));
Assertions.assertEquals(((Literal)values.get(0)).getLiteral(), "1");

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

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

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

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

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

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

try {
values = ASTUtils.initialValue(parameter,
List.of(map.get("a1")));
} catch (IllegalArgumentException ex) {
Assertions.assertTrue(ex.getMessage()
.startsWith("Parameter y is not"));
}
Assertions.assertThrows(IllegalArgumentException.class,
() -> ASTUtils.initialValue(parameter, List.of(map.get("a1"))));

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

values = ASTUtils.initialValue(parameter,
List.of(map.get("b2")));
Assertions.assertEquals(values.get(0).getLiteral(), "-2");
values = ASTUtils.initialValue(parameter, List.of(map.get("b2")));
Assertions.assertInstanceOf(Literal.class, values.get(0));
Assertions.assertEquals(((Literal)values.get(0)).getLiteral(), "-2");
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
***************/
package org.lflang.tests.compiler;

import static org.lflang.ASTUtils.withoutQuotes;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand All @@ -40,6 +38,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.lflang.Target;
import org.lflang.TargetProperty;
import org.lflang.TargetProperty.ArrayType;
Expand All @@ -53,6 +52,7 @@
import org.lflang.lf.Model;
import org.lflang.lf.Visibility;
import org.lflang.tests.LFInjectorProvider;
import org.lflang.util.StringUtil;

import com.google.inject.Inject;

Expand Down Expand Up @@ -906,8 +906,8 @@ public void nonzeroAfterMustHaveUnits() throws Exception {
" b = new X()",
" a.y -> b.x after 1",
"}");
validator.assertError(parseWithoutError(testCase), LfPackage.eINSTANCE.getTime(),
null, "Missing or invalid time unit.");
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, "Parameter is not of time type.");
}

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


Expand Down Expand Up @@ -1250,12 +1250,14 @@ public void stateAndParameterDeclarationsInC() throws Exception {
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.");
"Missing time unit.");
validator.assertError(model, LfPackage.eINSTANCE.getStateVar(), null,
"Invalid time literal.");
"Parameter is not of time type.");
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,
validator.assertError(model, LfPackage.eINSTANCE.getTimer(), null,
"Missing time unit.");
}

Expand Down Expand Up @@ -1696,7 +1698,7 @@ public void checkTargetProperties() throws Exception {
// Also make sure warnings are produced when files are not present.
if (prop.type == PrimitiveType.FILE) {
validator.assertWarning(model, LfPackage.eINSTANCE.getKeyValuePair(),
null, String.format("Could not find file: '%s'.", withoutQuotes(it)));
null, String.format("Could not find file: '%s'.", StringUtil.removeQuotes(it)));
}
}

Expand Down
24 changes: 13 additions & 11 deletions org.lflang.tests/src/org/lflang/tests/lsp/ErrorInserter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.ListIterator;
import java.util.Random;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
Expand All @@ -28,7 +29,8 @@ class ErrorInserter {

/** A basic error inserter builder on which more specific error inserters can be built. */
private static final Builder BASE_ERROR_INSERTER = new Builder()
.insertCondition(s -> Stream.of(";", "}", "{").anyMatch(s::endsWith))
.insertCondition((s0, s1) -> Stream.of(s0, s1).allMatch(it -> Stream.of(";", "}", "{").anyMatch(it::endsWith)))
.insertCondition((s0, s1) -> !s1.trim().startsWith("else"))
.insertable(" 0 = 1;").insertable("some_undeclared_var1524263 = 9;").insertable(" ++;");
public static final Builder C = BASE_ERROR_INSERTER
.replacer("lf_set(", "UNDEFINED_NAME2828376(")
Expand Down Expand Up @@ -79,7 +81,7 @@ private boolean get() {
* @param insertCondition Whether the error inserter is permitted to insert a line after a given line.
* @throws IOException if the content of {@code originalTest} cannot be read.
*/
private AlteredTest(Path originalTest, Predicate<String> insertCondition) throws IOException {
private AlteredTest(Path originalTest, BiPredicate<String, String> insertCondition) throws IOException {
this.badLines = new ArrayList<>();
this.path = originalTest;
this.lines = new LinkedList<>(); // Constant-time insertion during iteration is desired.
Expand All @@ -88,10 +90,9 @@ private AlteredTest(Path originalTest, Predicate<String> insertCondition) throws
boolean ret = true;
it.previous();
if (it.hasPrevious()) {
ret = insertCondition.test(it.previous());
ret = insertCondition.test(it.previous(), it.next());
}
it.next();
it.next();
return ret;
};
}
Expand Down Expand Up @@ -246,18 +247,18 @@ public T next() {
}
private final Node<Function<String, String>> replacers;
private final Node<String> insertables;
private final Predicate<String> insertCondition;
private final BiPredicate<String, String> insertCondition;

/** Initializes a builder for error inserters. */
public Builder() {
this(null, null, s -> true);
this(null, null, (s0, s1) -> true);
}

/** Construct a builder with the given replacers and insertables. */
private Builder(
Node<Function<String, String>> replacers,
Node<String> insertables,
Predicate<String> insertCondition
BiPredicate<String, String> insertCondition
) {
this.replacers = replacers;
this.insertables = insertables;
Expand Down Expand Up @@ -295,9 +296,10 @@ public Builder insertable(String line) {
}

/**
* Record that for any line X, insertCondition(X) is a necessary condition that a line may be inserted after X.
* Record that for any lines X, Y, insertCondition(X, Y) is a necessary condition that a line may be inserted
* between X and Y.
*/
public Builder insertCondition(Predicate<String> insertCondition) {
public Builder insertCondition(BiPredicate<String, String> insertCondition) {
return new Builder(replacers, insertables, insertCondition.and(insertCondition));
}

Expand All @@ -317,13 +319,13 @@ public ErrorInserter get(Random random) {
private final Random random;
private final ImmutableList<Function<String, String>> replacers;
private final ImmutableList<String> insertables;
private final Predicate<String> insertCondition;
private final BiPredicate<String, String> insertCondition;

private ErrorInserter(
Random random,
ImmutableList<Function<String, String>> replacers,
ImmutableList<String> insertables,
Predicate<String> insertCondition
BiPredicate<String, String> insertCondition
) {
this.random = random;
this.replacers = replacers;
Expand Down
Loading