Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 6 additions & 36 deletions lfc/core/src/main/java/org/lflang/LinguaFranca.xtext
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
/* The Lingua Franca grammar. */

/*************
Copyright (c) 2020, The University of California at Berkeley.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***************/

/**
* Grammar for Lingua Franca.
* A note of caution: extending this grammar with productions that introduce
Expand Down Expand Up @@ -205,7 +178,7 @@ Reaction:
('(' (triggers+=TriggerRef (',' triggers+=TriggerRef)*)? ')')
( => sources+=VarRef (',' sources+=VarRef)*)?
('->' effects+=VarRefOrModeTransition (',' effects+=VarRefOrModeTransition)*)?
(code=Code)? (stp=STP)? (deadline=Deadline)? (maxWait=MaxWait)? (delimited?=';')?
(code=Code)? (maxWait=MaxWait)? (deadline=Deadline)? (delimited?=';')?
;

TriggerRef:
Expand All @@ -223,11 +196,8 @@ Watchdog:
('->' effects+=VarRefOrModeTransition (',' effects+=VarRefOrModeTransition)*)?
code=Code;

STP:
('STP' | 'STAA' ) '(' value=Expression ')' code=Code;

MaxWait:
('maxwait') '(' value=Expression ')' (code=Code)?;
('STP' | 'STAA' | 'maxwait') ('(' value=Expression ')')? (code=Code)?;

Preamble:
(visibility=Visibility)? 'preamble' code=Code;
Expand Down Expand Up @@ -278,7 +248,7 @@ Element:
keyvalue=KeyValuePairs
| array=Array
| literal=Literal
| (time=INT unit=TimeUnit)
| time=Time
| id=Path;

///////// Pieces
Expand Down Expand Up @@ -306,7 +276,7 @@ Assignment:
*/
Parameter:
(attributes+=Attribute)*
name=ID (':' type=Type)?
name=(ID | 'maxwait') (':' type=Type)?
init=Initializer?
;

Expand Down Expand Up @@ -343,7 +313,7 @@ ParameterReference:
;

Time:
(interval=INT unit=TimeUnit)
(interval=INT unit=TimeUnit) | forever=Forever | never=Never
;

Port:
Expand Down Expand Up @@ -405,7 +375,7 @@ Never:
;

Literal:
STRING | CHAR_LIT | SignedFloat | SignedInt | Boolean | Forever | Never
STRING | CHAR_LIT | SignedFloat | SignedInt | Boolean
;

Boolean:
Expand Down
32 changes: 26 additions & 6 deletions lfc/core/src/main/java/org/lflang/TimeUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@

package org.lflang;

import static org.lflang.util.CollectionUtil.immutableSetOf;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -53,22 +52,43 @@ public enum TimeUnit {
HOUR("hour", "h", "hours"),
/** Day. */
DAY("day", "d", "days"),
WEEK("week", "weeks"),
WEEK("week", "wk", "weeks"),
;

private final Set<String> allNames;
private final String canonicalName;
private final String siName;

TimeUnit(String canonicalName, String... aliases) {
/**
* Construct a time unit.
*
* @param canonicalName The name used in the generated code for the unit.
* @param siName The SI unit name, if there is one, and otherwise a short name.
* @param aliases Any number of alternative names for the unit.
*/
TimeUnit(String canonicalName, String siName, String... aliases) {
this.canonicalName = canonicalName;
this.allNames = immutableSetOf(canonicalName, aliases);
this.siName = siName;
var all = new LinkedHashSet<String>();
all.add(canonicalName);
all.add(siName);
all.addAll(Arrays.asList(aliases));
this.allNames = all;
}

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

/** Returns the name that is preferred when displaying this unit. */
public static String staticGetCanonicalName(TimeUnit unit) {
if (unit == null) {
return null;
}
return unit.getCanonicalName();
}

/** Returns true if the given name is one of the aliases of this unit. */
public boolean hasAlias(String name) {
return allNames.contains(name);
Expand Down Expand Up @@ -105,6 +125,6 @@ public static List<String> list() {

@Override
public String toString() {
return this.canonicalName;
return this.siName;
}
}
64 changes: 37 additions & 27 deletions lfc/core/src/main/java/org/lflang/TimeValue.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,31 @@
/*************
* Copyright (c) 2019, The University of California at Berkeley.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***************/

package org.lflang;

import org.lflang.lf.Time;

/**
* Represents an amount of time (a duration).
*
* @author Marten Lohstroh
* @author Clément Fournier - TU Dresden, INSA Rennes
* @ingroup Utilities
*/
public final class TimeValue implements Comparable<TimeValue> {

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

/** The minimum value of this type. */
public static final TimeValue MIN_VALUE = new TimeValue(Long.MIN_VALUE, TimeUnit.NANO);

/** A time value equal to zero. */
public static final TimeValue ZERO = new TimeValue(0, null);

/** A time value representing NEVER, which is less than any other time value. */
public static final TimeValue NEVER = new TimeValue(Long.MIN_VALUE, TimeUnit.NANO);

/** A time value representing FOREVER which is greater than any other time value. */
public static final TimeValue FOREVER = new TimeValue(Long.MAX_VALUE, TimeUnit.NANO);

/**
* Primitive numerical representation of this time value, to be interpreted in terms the
* associated time unit.
Expand All @@ -57,7 +44,9 @@ public final class TimeValue implements Comparable<TimeValue> {
/**
* Create a new time value.
*
* @throws IllegalArgumentException If time is non-zero and the unit is null
* @param time The time value.
* @param unit The unit of the time value.
* @throws IllegalArgumentException If time is non-zero and the unit is null.
*/
public TimeValue(long time, TimeUnit unit) {
if (unit == null && time != 0) {
Expand All @@ -67,6 +56,25 @@ public TimeValue(long time, TimeUnit unit) {
this.unit = unit;
}

/**
* Create a new time value.
*
* @param time The time AST node..
* @throws IllegalArgumentException If time is non-zero and the unit is null.
*/
public TimeValue(Time time) {
if (time == null || time.getNever() != null) {
this.time = Long.MIN_VALUE;
this.unit = TimeUnit.NANO;
} else if (time.getForever() != null) {
this.time = Long.MAX_VALUE;
this.unit = TimeUnit.NANO;
} else {
this.time = time.getInterval();
this.unit = TimeUnit.fromName(time.getUnit());
}
}

@Override
public boolean equals(Object t1) {
if (t1 instanceof TimeValue) {
Expand Down Expand Up @@ -173,7 +181,9 @@ public static TimeValue fromNanoSeconds(long ns) {

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

/** Return the latest of both values. */
Expand Down
14 changes: 7 additions & 7 deletions lfc/core/src/main/java/org/lflang/ast/ASTUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ public static Integer toInteger(Element e) {
* @param e The element to be rendered as a time value.
*/
public static TimeValue toTimeValue(Element e) {
return new TimeValue(e.getTime(), TimeUnit.fromName(e.getUnit()));
return new TimeValue(e.getTime());
}

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

/**
Expand Down Expand Up @@ -882,10 +882,12 @@ else if (list.size() == 1) {
*/
public static Element toElement(TimeValue tv) {
Element e = LfFactory.eINSTANCE.createElement();
e.setTime((int) tv.time);
Time time = LfFactory.eINSTANCE.createTime();
time.setInterval((int) tv.time);
if (tv.unit != null) {
e.setUnit(tv.unit.toString());
time.setUnit(tv.unit.toString());
}
e.setTime(time);
return e;
}

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

/** Assuming that the given expression denotes a valid time literal, return a time value. */
/** Assuming that the given expression denotes a valid time, return a time value. */
public static TimeValue getLiteralTimeValue(Expression expr) {
if (expr instanceof Time) {
return toTimeValue((Time) expr);
} else if (expr instanceof Literal && isZero(((Literal) expr).getLiteral())) {
return TimeValue.ZERO;
} else if (expr instanceof Literal && isForever(((Literal) expr).getLiteral())) {
return TimeValue.MAX_VALUE;
} else {
return null;
}
Expand Down
Loading
Loading