Skip to content

Commit

Permalink
Merge pull request #1984 from lf-lang/update-formatter
Browse files Browse the repository at this point in the history
Avoid squeezing reaction, method, or preamble bodies onto a single line
  • Loading branch information
petervdonovan authored Sep 3, 2023
2 parents ca412c5 + ba2fa9c commit ff64ce4
Show file tree
Hide file tree
Showing 440 changed files with 2,010 additions and 677 deletions.
4 changes: 3 additions & 1 deletion cli/lff/src/test/java/org/lflang/cli/LffCliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public class LffCliTest {
/** moo */
// this is a humbug reaction
reaction(a) -> humbug {= /* it reacts like this*/ react react =}
reaction(a) -> humbug {=
/* it reacts like this*/ react react
=}
}
"""),
List.of(
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/lflang/ast/FormattingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static Function<EObject, String> renderer(Target target) {
* with the assumption that the target language is {@code target}.
*/
public static String render(EObject object, int lineLength, Target target, boolean codeMapTags) {
MalleableString ms = ToLf.instance.doSwitch(object);
MalleableString ms = new ToLf().doSwitch(object);
String singleLineCommentPrefix = target.getSingleLineCommentPrefix();
ms.findBestRepresentation(
() -> ms.render(INDENTATION, singleLineCommentPrefix, codeMapTags, null),
Expand Down
26 changes: 19 additions & 7 deletions core/src/main/java/org/lflang/ast/ToLf.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.lflang.ast;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -88,13 +89,11 @@ public class ToLf extends LfSwitch<MalleableString> {
private static final Pattern KEEP_FORMAT_COMMENT =
Pattern.compile("\\s*(//|#)\\s*keep-format\\s*");

/// public instance initialized when loading the class
public static final ToLf instance = new ToLf();

// private constructor
private ToLf() {
super();
}
/**
* The eObjects in the syntax tree on the path from the root up to and including the current
* eObject.
*/
private final ArrayDeque<EObject> callStack = new ArrayDeque<>();

@Override
public MalleableString caseArraySpec(ArraySpec spec) {
Expand All @@ -104,6 +103,13 @@ public MalleableString caseArraySpec(ArraySpec spec) {

@Override
public MalleableString doSwitch(EObject eObject) {
callStack.push(eObject);
var ret = doSwitchHelper(eObject);
callStack.pop();
return ret;
}

private MalleableString doSwitchHelper(EObject eObject) {
ICompositeNode node = NodeModelUtils.findActualNodeFor(eObject);
if (node == null) return super.doSwitch(eObject);
var ancestorComments = getAncestorComments(node);
Expand Down Expand Up @@ -257,6 +263,12 @@ public MalleableString caseCode(Code code) {
if (content.lines().count() > 1 || content.contains("#") || content.contains("//")) {
return multilineRepresentation;
}
if (callStack.stream()
.anyMatch(
it -> it instanceof Reaction || it instanceof Preamble || it instanceof Method)
&& !content.isBlank()) {
return MalleableString.anyOf(multilineRepresentation);
}
return MalleableString.anyOf(singleLineRepresentation, multilineRepresentation);
}

Expand Down
16 changes: 8 additions & 8 deletions core/src/main/java/org/lflang/ast/ToText.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private ToText() {

@Override
public String caseArraySpec(ArraySpec spec) {
return ToLf.instance.doSwitch(spec).toString();
return new ToLf().doSwitch(spec).toString();
}

@Override
Expand Down Expand Up @@ -77,41 +77,41 @@ public String caseCode(Code code) {

@Override
public String caseBracedListExpression(BracedListExpression object) {
return ToLf.instance.caseBracedListExpression(object).toString();
return new ToLf().caseBracedListExpression(object).toString();
}

@Override
public String caseHost(Host host) {
return ToLf.instance.caseHost(host).toString();
return new ToLf().caseHost(host).toString();
}

@Override
public String caseLiteral(Literal l) {
return ToLf.instance.caseLiteral(l).toString();
return new ToLf().caseLiteral(l).toString();
}

@Override
public String caseParameterReference(ParameterReference p) {
return ToLf.instance.caseParameterReference(p).toString();
return new ToLf().caseParameterReference(p).toString();
}

@Override
public String caseTime(Time t) {
return ToLf.instance.caseTime(t).toString();
return new ToLf().caseTime(t).toString();
}

@Override
public String caseType(Type type) {
if (type.getCode() != null) {
return caseCode(type.getCode());
}
return ToLf.instance.caseType(type).toString();
return new ToLf().caseType(type).toString();
}

@Override
public String caseTypeParm(TypeParm t) {
if (t.getCode() != null) return doSwitch(t.getCode());
return ToLf.instance.caseTypeParm(t).toString();
return new ToLf().caseTypeParm(t).toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ private String createParameterLabel(ParameterInstance param) {
if (param.getOverride() != null) {
b.append(" = ");
var init = param.getActualValue();
b.append(ToLf.instance.doSwitch(init));
b.append(new ToLf().doSwitch(init));
}
return b.toString();
}
Expand Down Expand Up @@ -1523,7 +1523,7 @@ private String createStateVariableLabel(StateVar variable) {
b.append(":").append(t.toOriginalText());
}
if (variable.getInit() != null) {
b.append(ToLf.instance.doSwitch(variable.getInit()));
b.append(new ToLf().doSwitch(variable.getInit()));
}
return b.toString();
}
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/ActionDelay.lf
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ reactor GeneratedDelay {
lf_schedule(act, MSEC(0));
=}

reaction(act) -> y_out {= lf_set(y_out, self->y_state); =}
reaction(act) -> y_out {=
lf_set(y_out, self->y_state);
=}
}

reactor Source {
output out: int

reaction(startup) -> out {= lf_set(out, 1); =}
reaction(startup) -> out {=
lf_set(out, 1);
=}
}

reactor Sink {
Expand Down
4 changes: 3 additions & 1 deletion test/C/src/ActionWithNoReaction.lf
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ main reactor ActionWithNoReaction {
timer t(0, 1 sec)
f.y -> p.x after 10 msec

reaction(t) -> f.x {= lf_set(f.x, 42); =}
reaction(t) -> f.x {=
lf_set(f.x, 42);
=}
}
8 changes: 6 additions & 2 deletions test/C/src/After.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ reactor foo {
input x: int
output y: int

reaction(x) -> y {= lf_set(y, 2*x->value); =}
reaction(x) -> y {=
lf_set(y, 2*x->value);
=}
}

reactor print {
Expand Down Expand Up @@ -47,5 +49,7 @@ main reactor After {
timer t(0, 1 sec)
f.y -> p.x after 10 msec

reaction(t) -> f.x {= lf_set(f.x, 42); =}
reaction(t) -> f.x {=
lf_set(f.x, 42);
=}
}
8 changes: 6 additions & 2 deletions test/C/src/AfterCycles.lf
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ target C
reactor Source {
output out: unsigned

reaction(startup) -> out {= lf_set(out, 1); =}
reaction(startup) -> out {=
lf_set(out, 1);
=}
}

reactor Work {
input in: unsigned
output out: unsigned

reaction(in) -> out {= lf_set(out, in->value); =}
reaction(in) -> out {=
lf_set(out, in->value);
=}
}

main reactor AfterCycles {
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/AfterZero.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ reactor foo {
input x: int
output y: int

reaction(x) -> y {= SET(y, 2*x->value); =}
reaction(x) -> y {=
SET(y, 2*x->value);
=}
}

reactor print {
Expand Down Expand Up @@ -52,5 +54,7 @@ main reactor {
timer t(0, 1 sec)
f.y -> p.x after 0

reaction(t) -> f.x {= SET(f.x, 42); =}
reaction(t) -> f.x {=
SET(f.x, 42);
=}
}
4 changes: 3 additions & 1 deletion test/C/src/Alignment.lf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ reactor Source {
state count: int = 1
timer t(0, 100 msec)

reaction(t) -> out {= lf_set(out, self->count++); =}
reaction(t) -> out {=
lf_set(out, self->count++);
=}
}

reactor Sieve {
Expand Down
4 changes: 3 additions & 1 deletion test/C/src/CompositionGain.lf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ reactor Wrapper {
main reactor CompositionGain {
wrapper = new Wrapper()

reaction(startup) -> wrapper.x {= lf_set(wrapper.x, 42); =}
reaction(startup) -> wrapper.x {=
lf_set(wrapper.x, 42);
=}

reaction(wrapper.y) {=
printf("Received %d\n", wrapper.y->value);
Expand Down
4 changes: 3 additions & 1 deletion test/C/src/DanglingOutput.lf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ reactor Source {
output out: int
timer t

reaction(t) -> out {= lf_set(out, 1); =}
reaction(t) -> out {=
lf_set(out, 1);
=}
}

reactor Gain {
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/DeadlineAnytime.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ reactor A {
reaction(startup) -> a {=
self->i = 0;
while (!lf_check_deadline(self, true));
=} deadline(10 msec) {= lf_schedule(a, 0); =}
=} deadline(10 msec) {=
lf_schedule(a, 0);
=}

reaction(a) {= self->i = 42; =}
reaction(a) {=
self->i = 42;
=}

reaction(shutdown) {=
if (self->i == 42) {
Expand Down
12 changes: 9 additions & 3 deletions test/C/src/DeadlineInherited.lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ target C {
threading: false
}

preamble {= extern int global_cnt; =}
preamble {=
extern int global_cnt;
=}

reactor NoDeadline {
preamble {= int global_cnt = 0; =}
preamble {=
int global_cnt = 0;
=}
timer t(0 msec, 100 msec)

reaction(t) {= global_cnt++; =}
reaction(t) {=
global_cnt++;
=}
}

reactor WithDeadline {
Expand Down
12 changes: 9 additions & 3 deletions test/C/src/DeadlinePriority.lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ target C {
threading: false
}

preamble {= extern int global_cnt; =}
preamble {=
extern int global_cnt;
=}

reactor NoDeadline {
preamble {= int global_cnt = 0; =}
preamble {=
int global_cnt = 0;
=}
timer t(0 msec, 100 msec)

reaction(t) {= global_cnt++; =}
reaction(t) {=
global_cnt++;
=}
}

reactor WithDeadline {
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/DeadlineWithAfterDelay.lf
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ target C {
threading: false
}

preamble {= extern int global_cnt; =}
preamble {=
extern int global_cnt;
=}

reactor Source {
preamble {= int global_cnt = 0; =}
preamble {=
int global_cnt = 0;
=}

output out: int
timer t(0 msec, 100 msec)
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/DeadlineWithBanks.lf
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ target C {
build-type: Debug
}

preamble {= extern volatile int global_cnt; =}
preamble {=
extern volatile int global_cnt;
=}

reactor Bank(bank_index: int = 0) {
preamble {= volatile int global_cnt = 0; =}
preamble {=
volatile int global_cnt = 0;
=}

timer t(0, 100 msec)
output out: int
Expand Down
8 changes: 6 additions & 2 deletions test/C/src/DeadlineZero.lf
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ reactor Detector {
reaction(trigger) {=
printf("ERROR: failed to detect zero-duration deadline at iteration %d.\n", self->cnt);
exit(1);
=} deadline(0 msec) {= self->cnt++; =}
=} deadline(0 msec) {=
self->cnt++;
=}
}

reactor Generator {
output pulse: int
timer t(0, 100 msec)

reaction(t) -> pulse {= lf_set(pulse, 0); =}
reaction(t) -> pulse {=
lf_set(pulse, 0);
=}
}

main reactor {
Expand Down
Loading

0 comments on commit ff64ce4

Please sign in to comment.