You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2019/10/14 16:08:37 UTC

[plc4x] 01/03: - Updated the mspec format to allow non-value enum types (Ones without a "getValue" and "valueOf" method.

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch feature/plc-simulator
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit d9539a7d47a084dcd66d7571e7d7deaec487cf33
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon Oct 14 17:58:04 2019 +0200

    - Updated the mspec format to allow non-value enum types (Ones without a "getValue" and "valueOf" method.
---
 .../src/main/resources/templates/java/enum-template.ftlh | 16 +++++++++-------
 .../plc4x/plugins/codegenerator/language/mspec/MSpec.g4  |  5 ++---
 .../language/mspec/parser/MessageFormatListener.java     |  4 ++--
 3 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/build-utils/language-java/src/main/resources/templates/java/enum-template.ftlh b/build-utils/language-java/src/main/resources/templates/java/enum-template.ftlh
index 62c4f34..4f4cbd7 100644
--- a/build-utils/language-java/src/main/resources/templates/java/enum-template.ftlh
+++ b/build-utils/language-java/src/main/resources/templates/java/enum-template.ftlh
@@ -48,12 +48,12 @@ import java.util.Map;
 public enum ${typeName} {
 
 <#list type.enumValues as enumValue>
-    ${enumValue.name}((${helper.getLanguageTypeName(type.type, true)}) ${enumValue.value}<#if type.constantNames?has_content>, <#list type.constantNames as constantName>(${helper.getLanguageTypeName(type.getConstantType(constantName), true)}) ${helper.escapeValue(type.getConstantType(constantName), enumValue.getConstant(constantName))?no_esc}<#sep>, </#sep></#list></#if>)<#sep>,
+    ${enumValue.name}(<#if type.type?has_content>(${helper.getLanguageTypeName(type.type, true)}) ${enumValue.value}</#if><#if (type.constantNames?has_content && type.type?has_content)>, </#if><#list type.constantNames as constantName>(${helper.getLanguageTypeName(type.getConstantType(constantName), true)}) ${helper.escapeValue(type.getConstantType(constantName), enumValue.getConstant(constantName))?no_esc}<#sep>, </#sep></#list>)<#sep>,
 </#sep></#list>;
 
     private static final Logger logger = LoggerFactory.getLogger(${typeName}.class);
 
-    private static final Map<${helper.getLanguageTypeName(type.type, false)}, ${typeName}> map;
+    <#if type.type?has_content>private static final Map<${helper.getLanguageTypeName(type.type, false)}, ${typeName}> map;
     static {
         map = new HashMap<>();
         for (${typeName} value : ${typeName}.values()) {
@@ -61,15 +61,15 @@ public enum ${typeName} {
         }
     }
 
-    private ${helper.getLanguageTypeName(type.type, true)} value;
+    private ${helper.getLanguageTypeName(type.type, true)} value;</#if>
     <#if type.constantNames?has_content>
         <#list type.constantNames as constantName>
     private ${helper.getLanguageTypeName(type.getConstantType(constantName), true)} ${constantName};
         </#list>
     </#if>
 
-    ${typeName}(${helper.getLanguageTypeName(type.type, true)} value<#if type.constantNames?has_content>, <#list type.constantNames as constantName>${helper.getLanguageTypeName(type.getConstantType(constantName), true)} ${constantName}<#sep>, </#sep></#list></#if>) {
-        this.value = value;
+    ${typeName}(<#if type.type?has_content>${helper.getLanguageTypeName(type.type, true)} value</#if><#if (type.constantNames?has_content && type.type?has_content)>, </#if><#list type.constantNames as constantName>${helper.getLanguageTypeName(type.getConstantType(constantName), true)} ${constantName}<#sep>, </#sep></#list>) {
+        <#if type.type?has_content>this.value = value;</#if>
         <#if type.constantNames?has_content>
             <#list type.constantNames as constantName>
         this.${constantName} = ${constantName};
@@ -77,9 +77,10 @@ public enum ${typeName} {
         </#if>
     }
 
-    public ${helper.getLanguageTypeName(type.type, true)} getValue() {
+    <#if type.type?has_content>public ${helper.getLanguageTypeName(type.type, true)} getValue() {
         return value;
     }
+    </#if>
 
     <#if type.constantNames?has_content>
         <#list type.constantNames as constantName>
@@ -89,11 +90,12 @@ public enum ${typeName} {
 
         </#list>
     </#if>
-    public static ${typeName} valueOf(${helper.getLanguageTypeName(type.type, true)} value) {
+    <#if type.type?has_content>public static ${typeName} valueOf(${helper.getLanguageTypeName(type.type, true)} value) {
         if (!map.containsKey(value)) {
             logger.error("No ${typeName} for value {}", value);
         }
         return map.get(value);
     }
+    </#if>
 
 }
\ No newline at end of file
diff --git a/build-utils/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4 b/build-utils/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4
index 8d058ca..75a09de 100644
--- a/build-utils/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4
+++ b/build-utils/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4
@@ -30,7 +30,7 @@ complexTypeDefinition
 complexType
  : 'type' name=idExpression (LBRACKET params=argumentList RBRACKET)? fieldDefinition+
  | 'discriminatedType' name=idExpression (LBRACKET params=argumentList RBRACKET)? fieldDefinition+
- | 'enum' type=typeReference name=idExpression (LBRACKET params=argumentList RBRACKET)? enumValues=enumValueDefinition+
+ | 'enum' (type=typeReference)? name=idExpression (LBRACKET params=argumentList RBRACKET)? enumValues=enumValueDefinition+
  ;
 
 
@@ -112,10 +112,9 @@ virtualField
  ;
 
 enumValueDefinition
- : LBRACKET valueExpression=expression name=IDENTIFIER (LBRACKET constantValueExpressions=multipleExpressions RBRACKET)? RBRACKET
+ : LBRACKET (valueExpression=expression)? name=IDENTIFIER (LBRACKET constantValueExpressions=multipleExpressions RBRACKET)? RBRACKET
  ;
 
-
 typeReference
  : complexTypeReference=IDENTIFIER
  | simpleTypeReference=dataType
diff --git a/build-utils/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java b/build-utils/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
index 74ff1cf..2556408 100644
--- a/build-utils/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
+++ b/build-utils/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
@@ -86,7 +86,7 @@ public class MessageFormatListener extends MSpecBaseListener {
         }
 
         if(ctx.enumValues != null) {
-            TypeReference type = getTypeReference(ctx.type);
+            TypeReference type = (ctx.type != null) ? getTypeReference(ctx.type) : null;
             EnumValue[] enumValues = getEnumValues();
             DefaultEnumTypeDefinition enumType = new DefaultEnumTypeDefinition(typeName, type, enumValues,
                 parserArguments, null);
@@ -343,7 +343,7 @@ public class MessageFormatListener extends MSpecBaseListener {
 
     @Override
     public void enterEnumValueDefinition(MSpecParser.EnumValueDefinitionContext ctx) {
-        String value = unquoteString(ctx.valueExpression.getText());
+        String value = (ctx.valueExpression != null) ? unquoteString(ctx.valueExpression.getText()) : null;
         String name = ctx.name.getText();
         Map<String, String> constants = null;
         if(ctx.constantValueExpressions != null) {