Skip to content

Commit

Permalink
annotations can now accept any literal + fix in JS sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
brice-morin committed Jun 2, 2020
1 parent 9c06202 commit 892ef54
Show file tree
Hide file tree
Showing 47 changed files with 262 additions and 140 deletions.
17 changes: 17 additions & 0 deletions .project
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.thingml.root</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
Expand Up @@ -35,6 +35,7 @@
import org.thingml.xtext.thingML.PrimitiveType;
import org.thingml.xtext.thingML.ProvidedPort;
import org.thingml.xtext.thingML.State;
import org.thingml.xtext.thingML.StringLiteral;
import org.thingml.xtext.thingML.Thing;
import org.thingml.xtext.thingML.ThingMLModel;
import org.thingml.xtext.thingML.Type;
Expand All @@ -44,7 +45,7 @@ public abstract class LoadModelTestsCommon {
/* --- Some helper functions --- */
private void checkAnnotation(AnnotatedElement e, String type, String name, String value) {
for (PlatformAnnotation a : e.getAnnotations()) {
if (a.getName().equals(name) && a.getValue().equals(value))
if (a.getName().equals(name) && ((StringLiteral)a.getValue()).getStringValue().equals(value))
return;
}
fail("'"+type+"' has annotation @"+name+"='"+value+"'");
Expand Down
Expand Up @@ -79,7 +79,7 @@ public void generateImplementation(Thing thing, Context ctx) {
if (ctx.hasContextAnnotation("use_fifo", "true")) {
body.append("this.fifo = fifo;");
} else {
body.append("this.bus = (root === null)? new EventEmitter() : this.root.bus;");
body.append("this.bus = (root === null)? new EventEmitter() : root.bus;");
}

// Common constructor body
Expand Down
5 changes: 0 additions & 5 deletions compilers/official-network-plugins/pom.xml
Expand Up @@ -54,11 +54,6 @@
<artifactId>compilers.framework</artifactId>
<version>${thingml.version}</version>
</dependency>
<dependency>
<groupId>org.thingml</groupId>
<artifactId>compilers.debugGUI</artifactId>
<version>${thingml.version}</version>
</dependency>
<dependency>
<groupId>org.thingml</groupId>
<artifactId>compilers.registry</artifactId>
Expand Down
Expand Up @@ -41,6 +41,7 @@
import org.thingml.xtext.thingML.PlatformAnnotation;
import org.thingml.xtext.thingML.Port;
import org.thingml.xtext.thingML.Protocol;
import org.thingml.xtext.thingML.StringLiteral;
import org.thingml.xtext.thingML.Thing;
import org.thingml.xtext.thingML.ThingMLFactory;
import org.thingml.xtext.thingML.impl.ThingMLFactoryImpl;
Expand Down Expand Up @@ -82,7 +83,9 @@ private void addDependencies() {
factory = ThingMLFactoryImpl.init();
PlatformAnnotation pan = factory.createPlatformAnnotation();
pan.setName("add_c_libraries");
pan.setValue("mosquitto");
final StringLiteral lit = factory.createStringLiteral();
lit.setStringValue("mosquitto");
pan.setValue(lit);
cfg.getAnnotations().add(pan);
}
}
Expand Down
Expand Up @@ -30,6 +30,8 @@
import org.thingml.xtext.thingML.Message;
import org.thingml.xtext.thingML.Parameter;
import org.thingml.xtext.thingML.PlatformAnnotation;
import org.thingml.xtext.thingML.StringLiteral;
import org.thingml.xtext.thingML.ThingMLFactory;
import org.thingml.xtext.thingML.impl.ParameterImpl;

/**
Expand Down Expand Up @@ -165,7 +167,9 @@ public String generateSerialization(StringBuilder builder, String bufferName, Me
// Set the @json_message_name annotation
PlatformAnnotation jsonMsgName = EcoreUtil.copy(msg.getAnnotations().get(0));
jsonMsgName.setName("json_message_name");
jsonMsgName.setValue("observation");
final StringLiteral lit = ThingMLFactory.eINSTANCE.createStringLiteral();
lit.setStringValue("observation");
jsonMsgName.setValue(lit);
msg.getAnnotations().add(jsonMsgName);

// Generate JSON serialisation of modified message
Expand Down
Expand Up @@ -38,6 +38,7 @@
import org.thingml.xtext.thingML.PlatformAnnotation;
import org.thingml.xtext.thingML.Port;
import org.thingml.xtext.thingML.Protocol;
import org.thingml.xtext.thingML.StringLiteral;
import org.thingml.xtext.thingML.Thing;
import org.thingml.xtext.thingML.ThingMLFactory;
import org.thingml.xtext.thingML.impl.ThingMLFactoryImpl;
Expand Down Expand Up @@ -78,7 +79,9 @@ private void addDependencies() {
factory = ThingMLFactoryImpl.init();
PlatformAnnotation pan = factory.createPlatformAnnotation();
pan.setName("add_c_libraries");
pan.setValue("websockets");
final StringLiteral lit = factory.createStringLiteral();
lit.setStringValue("websockets");
pan.setValue(lit);
cfg.getAnnotations().add(pan);
}
}
Expand Down
6 changes: 0 additions & 6 deletions compilers/registry/pom.xml
Expand Up @@ -58,12 +58,6 @@
<version>${thingml.version}</version>
</dependency>

<dependency>
<groupId>org.thingml</groupId>
<artifactId>compilers.debugGUI</artifactId>
<version>${thingml.version}</version>
</dependency>

<dependency>
<groupId>org.thingml</groupId>
<artifactId>compilers.go</artifactId>
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.thingml.thingmltools.ThingMLTool;
import org.thingml.xtext.constraints.ThingMLHelpers;
import org.thingml.xtext.helpers.AnnotatedElementHelper;
import org.thingml.xtext.thingML.ByteLiteral;
import org.thingml.xtext.thingML.Function;
import org.thingml.xtext.thingML.Handler;
import org.thingml.xtext.thingML.Import;
Expand Down Expand Up @@ -123,40 +124,52 @@ public boolean compile(ThingMLModel model, String... options) {
final Message m = (Message) o;
final PlatformAnnotation ma = ThingMLFactory.eINSTANCE.createPlatformAnnotation();
ma.setName("id");
ma.setValue(Byte.toString(ByteHelper.messageID()));
final ByteLiteral id = ThingMLFactory.eINSTANCE.createByteLiteral();
id.setByteValue(ByteHelper.messageID());
ma.setValue(id);
m.getAnnotations().add(ma);
} else if (o instanceof Port) {
final Port port = (Port) o;
final PlatformAnnotation ma = ThingMLFactory.eINSTANCE.createPlatformAnnotation();
ma.setName("id");
ma.setValue(Byte.toString(ByteHelper.portID()));
final ByteLiteral id = ThingMLFactory.eINSTANCE.createByteLiteral();
id.setByteValue(ByteHelper.messageID());
ma.setValue(id);
port.getAnnotations().add(ma);
}else if (o instanceof Thing) {
final Thing thing = (Thing) o;
if (AnnotatedElementHelper.isDefined(thing, "monitor", "not") || !AnnotatedElementHelper.hasAnnotation(thing, "monitor")) continue;
final PlatformAnnotation ma = ThingMLFactory.eINSTANCE.createPlatformAnnotation();
ma.setName("id");
ma.setValue(Byte.toString(ByteHelper.thingID()));
final ByteLiteral id = ThingMLFactory.eINSTANCE.createByteLiteral();
id.setByteValue(ByteHelper.messageID());
ma.setValue(id);
thing.getAnnotations().add(ma);
} else if (o instanceof Function) {
final Function f = (Function) o;
if (f.isAbstract() || AnnotatedElementHelper.isDefined(f, "monitor", "not")) continue;
final PlatformAnnotation ma = ThingMLFactory.eINSTANCE.createPlatformAnnotation();
ma.setName("id");
ma.setValue(Byte.toString(ByteHelper.functionID()));
final ByteLiteral id = ThingMLFactory.eINSTANCE.createByteLiteral();
id.setByteValue(ByteHelper.messageID());
ma.setValue(id);
f.getAnnotations().add(ma);
} else if (o instanceof Property) {
final Property p = (Property) o;
if (AnnotatedElementHelper.isDefined(p, "monitor", "not")) continue;
final PlatformAnnotation ma = ThingMLFactory.eINSTANCE.createPlatformAnnotation();
ma.setName("id");
ma.setValue(Byte.toString(ByteHelper.varID()));
final ByteLiteral id = ThingMLFactory.eINSTANCE.createByteLiteral();
id.setByteValue(ByteHelper.messageID());
ma.setValue(id);
p.getAnnotations().add(ma);
} else if (o instanceof Handler) {
final Handler h = (Handler) o;
final PlatformAnnotation ma = ThingMLFactory.eINSTANCE.createPlatformAnnotation();
ma.setName("id");
ma.setValue(Byte.toString(ByteHelper.handlerID()));
final ByteLiteral id = ThingMLFactory.eINSTANCE.createByteLiteral();
id.setByteValue(ByteHelper.messageID());
ma.setValue(id);
h.getAnnotations().add(ma);
}
}
Expand Down
Expand Up @@ -281,11 +281,11 @@ public void generateMainAndInit(Configuration cfg, ThingMLModel model, Context c
datatypes.append("note bottom of " + t.getName() + " : ");
for(PlatformAnnotation a : t.getAnnotations()) {
datatypes.append("<b>@" + a.getName() + "</b>");
if (a.getValue() != null) {
if (a.getValue().length() > 16 || a.getValue().contains("\n"))
if (AnnotatedElementHelper.toString(a.getValue()) != null) {
if (AnnotatedElementHelper.toString(a.getValue()).length() > 16 || AnnotatedElementHelper.toString(a.getValue()).contains("\n"))
datatypes.append(" <color:royalBlue>\"...\"</color>\\n");
else
datatypes.append(" <color:royalBlue>\"" + a.getValue().replace("\n", "\\n") + "\"</color>\\n");
datatypes.append(" <color:royalBlue>\"" + AnnotatedElementHelper.toString(a.getValue()).replace("\n", "\\n") + "\"</color>\\n");
}
}
if (t.getAnnotations().size() > 0)
Expand Down
3 changes: 2 additions & 1 deletion language/thingml.ui.tests/META-INF/MANIFEST.MF
Expand Up @@ -12,7 +12,8 @@ Require-Bundle: thingml.ui,
org.eclipse.core.runtime,
org.eclipse.ui.workbench;resolution:=optional,
org.eclipse.xtext.testing,
org.eclipse.xtext.xbase.testing
org.eclipse.xtext.xbase.testing,
org.eclipse.xtext.ui.testing
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: org.thingml.xtext.ui.tests;x-internal=true
Import-Package: org.hamcrest.core,
Expand Down
Expand Up @@ -35,6 +35,7 @@ import org.thingml.xtext.thingML.InternalTransition
import org.thingml.xtext.thingML.ReceiveMessage
import org.thingml.xtext.thingML.Handler
import org.thingml.xtext.thingML.IntegerLiteral
import org.thingml.xtext.helpers.AnnotatedElementHelper

/**
* Provides labels for EObjects.
Expand Down Expand Up @@ -75,10 +76,10 @@ class ThingMLLabelProvider extends DefaultEObjectLabelProvider {
'outline/open iconic/paperclip-2x.png'
}
def text(PlatformAnnotation annotation) {
if (annotation.value.length < 16)
annotation.name + ': ' + annotation.value
if (AnnotatedElementHelper.toString(annotation.value).length < 16)
annotation.name + ': ' + AnnotatedElementHelper.toString(annotation.value)
else
annotation.name + ': ' + annotation.value.substring(0, 16) + "..."
annotation.name + ': ' + AnnotatedElementHelper.toString(annotation.value).substring(0, 16) + "..."
}

def image(Type datatype) {
Expand Down
3 changes: 2 additions & 1 deletion language/thingml/.classpath
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="src-gen"/>
<classpathentry kind="src" path="xtend-gen"/>
<classpathentry kind="src" path="src-gen"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="lib" path="antlr-generator-3.2.0-patch.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
1 change: 1 addition & 0 deletions language/thingml/.gitignore
@@ -0,0 +1 @@
*.jar
4 changes: 4 additions & 0 deletions language/thingml/README.MD
@@ -0,0 +1,4 @@
- You need to manually download https://download.itemis.com/antlr-generator-3.2.0-patch.jar
- In Eclipse, add it to your classpath

This file is no more distributed with XText (for Licensing issues) but is needed should you want to modify the language.
3 changes: 2 additions & 1 deletion language/thingml/src/org/thingml/annotations/Annotation.java
Expand Up @@ -18,6 +18,7 @@

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.thingml.xtext.thingML.Literal;
import org.thingml.xtext.thingML.ThingMLPackage;

public class Annotation {
Expand All @@ -37,7 +38,7 @@ public String getName() {
return name;
}

public boolean check(EObject source, String value) {
public boolean check(EObject source, Literal value) {
for(EClass clazz : scope) {
if (clazz.isInstance(source)) {
return true;
Expand Down
Expand Up @@ -237,7 +237,7 @@ public class AnnotationRegistry {
final String tc_name = "type_checker";
final String tc_desc = "Specifies the abstract type of a ThingML-defined primitive type. Used by the type checker.";
final EClass tc_scope[] = {ThingMLPackage.eINSTANCE.getType()};
final String tc_values[] = {"Byte", "Integer", "Boolean", "Character", "String", "Real", "Void", "Object"};
final String tc_values[] = {"Byte", "Integer", "Boolean", "Character", "String", "Real", "Void", "Object", "Any"};
final Annotation tc_annotation = new EnumAnnotation(tc_name, tc_desc, tc_scope, tc_values);
annotations.put(tc_name, tc_annotation);

Expand Down Expand Up @@ -377,6 +377,27 @@ public class AnnotationRegistry {
"Optional registry to publish NPM packages on other registries.",
new EClass[] {ThingMLPackage.eINSTANCE.getConfiguration()}
));

/** Testing **/
annotations.put("test_onlything", new Annotation(
"test_onlything",
"Only for testing annotations. Please ignore.",
new EClass[] {ThingMLPackage.eINSTANCE.getThing()}
));

annotations.put("test_integer", new IntegerAnnotation(
"test_integer",
"Only for testing annotations. Please ignore.",
new EClass[] {ThingMLPackage.eINSTANCE.getThing()},
false
));

annotations.put("test_enum", new EnumAnnotation(
"test_enum",
"Only for testing annotations. Please ignore.",
new EClass[] {ThingMLPackage.eINSTANCE.getThing()},
new String[] {"A", "B", "C"}
));
}

public static String toMD() {
Expand Down
Expand Up @@ -20,6 +20,8 @@

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.thingml.xtext.helpers.AnnotatedElementHelper;
import org.thingml.xtext.thingML.Literal;

public class EnumAnnotation extends Annotation {

Expand All @@ -29,12 +31,13 @@ public EnumAnnotation(String name, String description, EClass[] scope, String va
super(name, description, scope);
this.values = values;
}

@Override
public boolean check(EObject source, String value) {
public boolean check(EObject source, Literal value) {
if (!super.check(source, value)) return false;
for(String v : values) {
if (value.equals(v)) {
final String stringValue = AnnotatedElementHelper.toString(value);
if (v.equals(stringValue)) {
return true;
}
}
Expand Down
Expand Up @@ -18,6 +18,9 @@

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.thingml.xtext.helpers.AnnotatedElementHelper;
import org.thingml.xtext.thingML.IntegerLiteral;
import org.thingml.xtext.thingML.Literal;

public class IntegerAnnotation extends Annotation {

Expand All @@ -29,10 +32,10 @@ public IntegerAnnotation(String name, String description, EClass[] scope, boolea
}

@Override
public boolean check(EObject source, String value) {
public boolean check(EObject source, Literal value) {
if (!super.check(source, value)) return false;
try {
int v = Integer.parseInt(value);
long v = (value instanceof IntegerLiteral)? ((IntegerLiteral)value).getIntValue() : Integer.parseInt(AnnotatedElementHelper.toString(value));
if (onlyPositive) return v >= 0;
return true;
} catch (NumberFormatException nfe) {
Expand All @@ -45,7 +48,7 @@ public String toString() {
if (onlyPositive)
return super.toString() + " Valid values are positive integers.";
else
return super.toString() + " Valid values integers.";
return super.toString() + " Valid values are integers.";
}

}

0 comments on commit 892ef54

Please sign in to comment.