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 2022/06/09 17:00:45 UTC
[plc4x] branch develop updated: chore(code-gen): Added the concept of var-length int and uint to mspec.
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new ba50273699 chore(code-gen): Added the concept of var-length int and uint to mspec.
ba50273699 is described below
commit ba5027369907a26d2ed16863746cfc0cd8475eb1
Author: cdutz <ch...@c-ware.de>
AuthorDate: Thu Jun 9 19:00:36 2022 +0200
chore(code-gen): Added the concept of var-length int and uint to mspec.
---
.../plugins/codegenerator/language/mspec/MSpec.g4 | 2 ++
.../references/DefaultVintegerTypeReference.java | 38 ++++++++++++++++++++++
.../mspec/parser/MessageFormatListener.java | 31 ++++++++++++++++++
.../src/test/resources/mspec.example | 7 ++++
4 files changed, 78 insertions(+)
diff --git a/code-generation/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4 b/code-generation/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4
index ddbc405da1..75acfee572 100644
--- a/code-generation/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4
+++ b/code-generation/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4
@@ -161,7 +161,9 @@ dataType
: base='bit'
| base='byte'
| base='int' size=INTEGER_LITERAL
+ | base='vint'
| base='uint' size=INTEGER_LITERAL
+ | base='vuint'
| base='float' size=INTEGER_LITERAL
| base='ufloat' size=INTEGER_LITERAL
| base='string' size=INTEGER_LITERAL
diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/references/DefaultVintegerTypeReference.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/references/DefaultVintegerTypeReference.java
new file mode 100644
index 0000000000..fd33aeccb9
--- /dev/null
+++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/references/DefaultVintegerTypeReference.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.plc4x.plugins.codegenerator.language.mspec.model.references;
+
+import org.apache.plc4x.plugins.codegenerator.types.references.SimpleTypeReference;
+import org.apache.plc4x.plugins.codegenerator.types.references.VintegerTypeReference;
+
+public class DefaultVintegerTypeReference extends AbstractSimpleTypeReference implements VintegerTypeReference {
+
+ private final SimpleTypeReference propertyType;
+
+ public DefaultVintegerTypeReference(SimpleBaseType baseType, SimpleTypeReference propertyType) {
+ super(baseType, -1);
+ this.propertyType = propertyType;
+ }
+
+ @Override
+ public SimpleTypeReference getPropertyTypeReference() {
+ return propertyType;
+ }
+
+}
diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
index addf2802fe..36ca5a464c 100644
--- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
+++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/parser/MessageFormatListener.java
@@ -38,6 +38,7 @@ import org.apache.plc4x.plugins.codegenerator.types.fields.ManualArrayField;
import org.apache.plc4x.plugins.codegenerator.types.fields.SwitchField;
import org.apache.plc4x.plugins.codegenerator.types.references.*;
import org.apache.plc4x.plugins.codegenerator.types.terms.Literal;
+import org.apache.plc4x.plugins.codegenerator.types.terms.NumericLiteral;
import org.apache.plc4x.plugins.codegenerator.types.terms.Term;
import org.apache.plc4x.plugins.codegenerator.types.terms.VariableLiteral;
import org.slf4j.Logger;
@@ -687,6 +688,36 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType
case UINT:
int integerSize = Integer.parseInt(ctx.size.getText());
return new DefaultIntegerTypeReference(simpleBaseType, integerSize);
+ case VINT: {
+ final Map<String, Term> attributes = getAttributes(ctx.parent.parent);
+ SimpleTypeReference propertyType;
+ int propertySizeInBits = 32;
+ if (attributes.containsKey("propertySizeInBits")) {
+ final Term propertySizeInBitsTerm = attributes.get("propertySizeInBits");
+ if(!(propertySizeInBitsTerm instanceof NumericLiteral)) {
+ throw new RuntimeException("'propertySizeInBits' attribute is required to be a numeric literal");
+ }
+ NumericLiteral propertySizeInBitsLiteral = (NumericLiteral) propertySizeInBitsTerm;
+ propertySizeInBits = propertySizeInBitsLiteral.getNumber().intValue();
+ }
+ propertyType = new DefaultIntegerTypeReference(SimpleTypeReference.SimpleBaseType.INT,propertySizeInBits);
+ return new DefaultVintegerTypeReference(simpleBaseType, propertyType);
+ }
+ case VUINT: {
+ final Map<String, Term> attributes = getAttributes(ctx.parent.parent);
+ SimpleTypeReference propertyType;
+ int propertySizeInBits = 32;
+ if (attributes.containsKey("propertySizeInBits")) {
+ final Term propertySizeInBitsTerm = attributes.get("propertySizeInBits");
+ if(!(propertySizeInBitsTerm instanceof NumericLiteral)) {
+ throw new RuntimeException("'propertySizeInBits' attribute is required to be a numeric literal");
+ }
+ NumericLiteral propertySizeInBitsLiteral = (NumericLiteral) propertySizeInBitsTerm;
+ propertySizeInBits = propertySizeInBitsLiteral.getNumber().intValue();
+ }
+ propertyType = new DefaultIntegerTypeReference(SimpleTypeReference.SimpleBaseType.UINT,propertySizeInBits);
+ return new DefaultVintegerTypeReference(simpleBaseType, propertyType);
+ }
case FLOAT:
case UFLOAT:
int floatSize = Integer.parseInt(ctx.size.getText());
diff --git a/code-generation/protocol-base-mspec/src/test/resources/mspec.example b/code-generation/protocol-base-mspec/src/test/resources/mspec.example
index a0afe0d567..e75d55d21f 100644
--- a/code-generation/protocol-base-mspec/src/test/resources/mspec.example
+++ b/code-generation/protocol-base-mspec/src/test/resources/mspec.example
@@ -362,4 +362,11 @@
//Confirm implicit can be used in string length
[implicit uint 32 stringLength 'stringValue.lengthInBytes']
[simple vstring 'stringLength' stringValue]
+]
+
+[type VariableIntegerType
+ [simple vint variableLengthInt ]
+ [simple vuint variableLengthUnsignedInt ]
+ [simple vint variableLengthIntWithNonDefaultProperty propertySizeInBits='8' ]
+ [simple vuint variableLengthUnsignedInt propertySizeInBits='16']
]
\ No newline at end of file