Skip to content

Commit 5f6b4e6

Browse files
authored
Merge pull request #303 from lf-lang/time-syntax
Align with changes in lingua-franca master
2 parents b4be6a2 + b51f4a0 commit 5f6b4e6

File tree

9 files changed

+166
-137
lines changed

9 files changed

+166
-137
lines changed

lfc/core/src/main/java/org/lflang/LinguaFranca.xtext

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,3 @@
1-
/* The Lingua Franca grammar. */
2-
3-
/*************
4-
Copyright (c) 2020, The University of California at Berkeley.
5-
6-
Redistribution and use in source and binary forms, with or without modification,
7-
are permitted provided that the following conditions are met:
8-
9-
1. Redistributions of source code must retain the above copyright notice,
10-
this list of conditions and the following disclaimer.
11-
12-
2. Redistributions in binary form must reproduce the above copyright notice,
13-
this list of conditions and the following disclaimer in the documentation
14-
and/or other materials provided with the distribution.
15-
16-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
20-
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23-
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26-
***************/
27-
281
/**
292
* Grammar for Lingua Franca.
303
* A note of caution: extending this grammar with productions that introduce
@@ -205,7 +178,7 @@ Reaction:
205178
('(' (triggers+=TriggerRef (',' triggers+=TriggerRef)*)? ')')
206179
( => sources+=VarRef (',' sources+=VarRef)*)?
207180
('->' effects+=VarRefOrModeTransition (',' effects+=VarRefOrModeTransition)*)?
208-
(code=Code)? (stp=STP)? (deadline=Deadline)? (maxWait=MaxWait)? (delimited?=';')?
181+
(code=Code)? (maxWait=MaxWait)? (deadline=Deadline)? (delimited?=';')?
209182
;
210183

211184
TriggerRef:
@@ -223,11 +196,8 @@ Watchdog:
223196
('->' effects+=VarRefOrModeTransition (',' effects+=VarRefOrModeTransition)*)?
224197
code=Code;
225198

226-
STP:
227-
('STP' | 'STAA' ) '(' value=Expression ')' code=Code;
228-
229199
MaxWait:
230-
('maxwait') '(' value=Expression ')' (code=Code)?;
200+
('STP' | 'STAA' | 'maxwait') ('(' value=Expression ')')? (code=Code)?;
231201

232202
Preamble:
233203
(visibility=Visibility)? 'preamble' code=Code;
@@ -278,7 +248,7 @@ Element:
278248
keyvalue=KeyValuePairs
279249
| array=Array
280250
| literal=Literal
281-
| (time=INT unit=TimeUnit)
251+
| time=Time
282252
| id=Path;
283253

284254
///////// Pieces
@@ -306,7 +276,7 @@ Assignment:
306276
*/
307277
Parameter:
308278
(attributes+=Attribute)*
309-
name=ID (':' type=Type)?
279+
name=(ID | 'maxwait') (':' type=Type)?
310280
init=Initializer?
311281
;
312282

@@ -343,7 +313,7 @@ ParameterReference:
343313
;
344314

345315
Time:
346-
(interval=INT unit=TimeUnit)
316+
(interval=INT unit=TimeUnit) | forever=Forever | never=Never
347317
;
348318

349319
Port:
@@ -405,7 +375,7 @@ Never:
405375
;
406376

407377
Literal:
408-
STRING | CHAR_LIT | SignedFloat | SignedInt | Boolean | Forever | Never
378+
STRING | CHAR_LIT | SignedFloat | SignedInt | Boolean
409379
;
410380

411381
Boolean:

lfc/core/src/main/java/org/lflang/TimeUnit.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424

2525
package org.lflang;
2626

27-
import static org.lflang.util.CollectionUtil.immutableSetOf;
28-
2927
import java.util.Arrays;
28+
import java.util.LinkedHashSet;
3029
import java.util.List;
3130
import java.util.Set;
3231
import java.util.stream.Collectors;
@@ -53,22 +52,43 @@ public enum TimeUnit {
5352
HOUR("hour", "h", "hours"),
5453
/** Day. */
5554
DAY("day", "d", "days"),
56-
WEEK("week", "weeks"),
55+
WEEK("week", "wk", "weeks"),
5756
;
5857

5958
private final Set<String> allNames;
6059
private final String canonicalName;
60+
private final String siName;
6161

62-
TimeUnit(String canonicalName, String... aliases) {
62+
/**
63+
* Construct a time unit.
64+
*
65+
* @param canonicalName The name used in the generated code for the unit.
66+
* @param siName The SI unit name, if there is one, and otherwise a short name.
67+
* @param aliases Any number of alternative names for the unit.
68+
*/
69+
TimeUnit(String canonicalName, String siName, String... aliases) {
6370
this.canonicalName = canonicalName;
64-
this.allNames = immutableSetOf(canonicalName, aliases);
71+
this.siName = siName;
72+
var all = new LinkedHashSet<String>();
73+
all.add(canonicalName);
74+
all.add(siName);
75+
all.addAll(Arrays.asList(aliases));
76+
this.allNames = all;
6577
}
6678

6779
/** Returns the name that is preferred when displaying this unit. */
6880
public String getCanonicalName() {
6981
return canonicalName;
7082
}
7183

84+
/** Returns the name that is preferred when displaying this unit. */
85+
public static String staticGetCanonicalName(TimeUnit unit) {
86+
if (unit == null) {
87+
return null;
88+
}
89+
return unit.getCanonicalName();
90+
}
91+
7292
/** Returns true if the given name is one of the aliases of this unit. */
7393
public boolean hasAlias(String name) {
7494
return allNames.contains(name);
@@ -105,6 +125,6 @@ public static List<String> list() {
105125

106126
@Override
107127
public String toString() {
108-
return this.canonicalName;
128+
return this.siName;
109129
}
110130
}

lfc/core/src/main/java/org/lflang/TimeValue.java

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,31 @@
1-
/*************
2-
* Copyright (c) 2019, The University of California at Berkeley.
3-
*
4-
* Redistribution and use in source and binary forms, with or without modification,
5-
* are permitted provided that the following conditions are met:
6-
*
7-
* 1. Redistributions of source code must retain the above copyright notice,
8-
* this list of conditions and the following disclaimer.
9-
*
10-
* 2. Redistributions in binary form must reproduce the above copyright notice,
11-
* this list of conditions and the following disclaimer in the documentation
12-
* and/or other materials provided with the distribution.
13-
*
14-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15-
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16-
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18-
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19-
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20-
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21-
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22-
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23-
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24-
***************/
25-
261
package org.lflang;
272

3+
import org.lflang.lf.Time;
4+
285
/**
296
* Represents an amount of time (a duration).
307
*
318
* @author Marten Lohstroh
329
* @author Clément Fournier - TU Dresden, INSA Rennes
10+
* @ingroup Utilities
3311
*/
3412
public final class TimeValue implements Comparable<TimeValue> {
3513

3614
/** The maximum value of this type. This is approximately equal to 292 years. */
3715
public static final TimeValue MAX_VALUE = new TimeValue(Long.MAX_VALUE, TimeUnit.NANO);
3816

17+
/** The minimum value of this type. */
18+
public static final TimeValue MIN_VALUE = new TimeValue(Long.MIN_VALUE, TimeUnit.NANO);
19+
3920
/** A time value equal to zero. */
4021
public static final TimeValue ZERO = new TimeValue(0, null);
4122

23+
/** A time value representing NEVER, which is less than any other time value. */
24+
public static final TimeValue NEVER = new TimeValue(Long.MIN_VALUE, TimeUnit.NANO);
25+
26+
/** A time value representing FOREVER which is greater than any other time value. */
27+
public static final TimeValue FOREVER = new TimeValue(Long.MAX_VALUE, TimeUnit.NANO);
28+
4229
/**
4330
* Primitive numerical representation of this time value, to be interpreted in terms the
4431
* associated time unit.
@@ -57,7 +44,9 @@ public final class TimeValue implements Comparable<TimeValue> {
5744
/**
5845
* Create a new time value.
5946
*
60-
* @throws IllegalArgumentException If time is non-zero and the unit is null
47+
* @param time The time value.
48+
* @param unit The unit of the time value.
49+
* @throws IllegalArgumentException If time is non-zero and the unit is null.
6150
*/
6251
public TimeValue(long time, TimeUnit unit) {
6352
if (unit == null && time != 0) {
@@ -67,6 +56,25 @@ public TimeValue(long time, TimeUnit unit) {
6756
this.unit = unit;
6857
}
6958

59+
/**
60+
* Create a new time value.
61+
*
62+
* @param time The time AST node..
63+
* @throws IllegalArgumentException If time is non-zero and the unit is null.
64+
*/
65+
public TimeValue(Time time) {
66+
if (time == null || time.getNever() != null) {
67+
this.time = Long.MIN_VALUE;
68+
this.unit = TimeUnit.NANO;
69+
} else if (time.getForever() != null) {
70+
this.time = Long.MAX_VALUE;
71+
this.unit = TimeUnit.NANO;
72+
} else {
73+
this.time = time.getInterval();
74+
this.unit = TimeUnit.fromName(time.getUnit());
75+
}
76+
}
77+
7078
@Override
7179
public boolean equals(Object t1) {
7280
if (t1 instanceof TimeValue) {
@@ -173,7 +181,9 @@ public static TimeValue fromNanoSeconds(long ns) {
173181

174182
/** Return a string representation of this time value. */
175183
public String toString() {
176-
return unit != null ? time + " " + unit.getCanonicalName() : Long.toString(time);
184+
if (this.equals(MAX_VALUE)) return "forever";
185+
if (this.equals(MIN_VALUE)) return "never";
186+
return unit != null ? time + " " + unit.toString() : Long.toString(time);
177187
}
178188

179189
/** Return the latest of both values. */

lfc/core/src/main/java/org/lflang/ast/ASTUtils.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ public static Integer toInteger(Element e) {
739739
* @param e The element to be rendered as a time value.
740740
*/
741741
public static TimeValue toTimeValue(Element e) {
742-
return new TimeValue(e.getTime(), TimeUnit.fromName(e.getUnit()));
742+
return new TimeValue(e.getTime());
743743
}
744744

745745
/** Returns the time value represented by the given AST node. */
@@ -748,7 +748,7 @@ public static TimeValue toTimeValue(Time e) {
748748
// invalid unit, will have been reported by validator
749749
throw new IllegalArgumentException();
750750
}
751-
return new TimeValue(e.getInterval(), TimeUnit.fromName(e.getUnit()));
751+
return new TimeValue(e);
752752
}
753753

754754
/**
@@ -882,10 +882,12 @@ else if (list.size() == 1) {
882882
*/
883883
public static Element toElement(TimeValue tv) {
884884
Element e = LfFactory.eINSTANCE.createElement();
885-
e.setTime((int) tv.time);
885+
Time time = LfFactory.eINSTANCE.createTime();
886+
time.setInterval((int) tv.time);
886887
if (tv.unit != null) {
887-
e.setUnit(tv.unit.toString());
888+
time.setUnit(tv.unit.toString());
888889
}
890+
e.setTime(time);
889891
return e;
890892
}
891893

@@ -1148,14 +1150,12 @@ public static String generateVarRef(VarRef reference) {
11481150
return prefix + reference.getVariable().getName();
11491151
}
11501152

1151-
/** Assuming that the given expression denotes a valid time literal, return a time value. */
1153+
/** Assuming that the given expression denotes a valid time, return a time value. */
11521154
public static TimeValue getLiteralTimeValue(Expression expr) {
11531155
if (expr instanceof Time) {
11541156
return toTimeValue((Time) expr);
11551157
} else if (expr instanceof Literal && isZero(((Literal) expr).getLiteral())) {
11561158
return TimeValue.ZERO;
1157-
} else if (expr instanceof Literal && isForever(((Literal) expr).getLiteral())) {
1158-
return TimeValue.MAX_VALUE;
11591159
} else {
11601160
return null;
11611161
}

0 commit comments

Comments
 (0)