This is an automated email from the ASF dual-hosted git repository. sruehl pushed a commit to branch feat/codegen_comment_support in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 3bfe27cd2a8180a62c0e7549cc842a6a4cedbd6f Author: Sebastian Rühl <[email protected]> AuthorDate: Mon Sep 8 12:43:54 2025 +0200 feat(codegen): comment support --- .../plugins/codegenerator/language/mspec/MSpec.g4 | 16 +- .../definitions/DefaultComplexTypeDefinition.java | 9 + .../mspec/model/fields/DefaultAbstractField.java | 10 + .../mspec/model/fields/DefaultArrayField.java | 9 + .../mspec/model/fields/DefaultAssertField.java | 9 + .../mspec/model/fields/DefaultChecksumField.java | 9 + .../mspec/model/fields/DefaultConstField.java | 9 + .../model/fields/DefaultDiscriminatorField.java | 10 + .../mspec/model/fields/DefaultEnumField.java | 9 + .../language/mspec/model/fields/DefaultField.java | 11 + .../mspec/model/fields/DefaultImplicitField.java | 9 + .../model/fields/DefaultManualArrayField.java | 9 + .../mspec/model/fields/DefaultManualField.java | 9 + .../mspec/model/fields/DefaultOptionalField.java | 9 + .../mspec/model/fields/DefaultPaddingField.java | 9 + .../mspec/model/fields/DefaultPeekField.java | 22 +- .../mspec/model/fields/DefaultReservedField.java | 9 + .../mspec/model/fields/DefaultSimpleField.java | 10 + .../mspec/model/fields/DefaultStateField.java | 9 + .../mspec/model/fields/DefaultSwitchField.java | 9 + .../mspec/model/fields/DefaultTypedField.java | 10 +- .../mspec/model/fields/DefaultTypedNamedField.java | 9 + .../mspec/model/fields/DefaultUnknownField.java | 10 + .../mspec/model/fields/DefaultValidationField.java | 14 +- .../mspec/model/fields/DefaultVirtualField.java | 11 + .../mspec/parser/MessageFormatListener.java | 492 +++++++++++++-------- 26 files changed, 546 insertions(+), 205 deletions(-) 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 2659e08e3b..41a40a4a5b 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 @@ -19,7 +19,16 @@ grammar MSpec; */ file - : contantsDefinition? globalsDefinition? contextDefintion? complexTypeDefinition* EOF + : (LINE_COMMENT | BLOCK_COMMENT | WS)* + contantsDefinition? + (LINE_COMMENT | BLOCK_COMMENT | WS)* + globalsDefinition? + (LINE_COMMENT | BLOCK_COMMENT | WS)* + contextDefintion? + (LINE_COMMENT | BLOCK_COMMENT | WS)* + complexTypeDefinition* + (LINE_COMMENT | BLOCK_COMMENT | WS)* + EOF ; contantsDefinition @@ -416,12 +425,13 @@ STRING_CHARACTER // Stuff we just want to ignore + LINE_COMMENT - : '//' ~[\r\n]* -> channel(HIDDEN) + : '//' ~[\r\n]* ; BLOCK_COMMENT - : '/*' .*? '*/' -> channel(HIDDEN) + : '/*' .*? '*/' ; WS diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/definitions/DefaultComplexTypeDefinition.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/definitions/DefaultComplexTypeDefinition.java index 45d08d3ae3..31672e4807 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/definitions/DefaultComplexTypeDefinition.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/definitions/DefaultComplexTypeDefinition.java @@ -28,6 +28,15 @@ import java.util.stream.Collectors; public class DefaultComplexTypeDefinition extends DefaultTypeDefinition implements ComplexTypeDefinition { + private String comment; + public void setComment(String comment) { + this.comment = comment; + } + + public String getComment() { + return comment; + } + private final boolean isAbstract; private final List<Field> fields; protected ComplexTypeDefinition parentType; diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultAbstractField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultAbstractField.java index 5fd413ef42..cbd1408a07 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultAbstractField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultAbstractField.java @@ -25,10 +25,20 @@ import java.util.Map; public class DefaultAbstractField extends DefaultTypedNamedField implements AbstractField { + private String comment; + public DefaultAbstractField(Map<String, Term> attributes, String name) { super(attributes, name); } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + @Override public String toString() { return "DefaultAbstractField{} " + super.toString(); diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultArrayField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultArrayField.java index d31efd0e06..ea60bfba45 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultArrayField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultArrayField.java @@ -28,6 +28,7 @@ import java.util.Objects; public class DefaultArrayField extends DefaultTypedNamedField implements ArrayField { + private String comment; private final LoopType loopType; private final Term loopExpression; @@ -37,6 +38,14 @@ public class DefaultArrayField extends DefaultTypedNamedField implements ArrayFi this.loopExpression = Objects.requireNonNull(loopExpression); } + public void setComment(String comment) { + this.comment = comment; + } + + public String getComment() { + return comment; + } + public LoopType getLoopType() { return loopType; } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultAssertField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultAssertField.java index bb36b97330..8807ca984a 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultAssertField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultAssertField.java @@ -26,6 +26,7 @@ import java.util.Objects; public class DefaultAssertField extends DefaultTypedNamedField implements AssertField { + private String comment; private final Term conditionExpression; public DefaultAssertField(Map<String, Term> attributes, String name, Term conditionExpression) { @@ -33,6 +34,14 @@ public class DefaultAssertField extends DefaultTypedNamedField implements Assert this.conditionExpression = Objects.requireNonNull(conditionExpression); } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + public Term getConditionExpression() { return conditionExpression; } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultChecksumField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultChecksumField.java index 84d35449d6..b85a1d53b1 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultChecksumField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultChecksumField.java @@ -26,6 +26,7 @@ import java.util.*; public class DefaultChecksumField extends DefaultTypedNamedField implements ChecksumField { + private String comment; private final Term checksumExpression; public DefaultChecksumField(Map<String, Term> attributes, SimpleTypeReference type, String name, Term checksumExpression) { @@ -34,6 +35,14 @@ public class DefaultChecksumField extends DefaultTypedNamedField implements Chec this.type = type; } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + public Term getChecksumExpression() { return checksumExpression; } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultConstField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultConstField.java index a2b892310c..31ce07ca84 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultConstField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultConstField.java @@ -26,12 +26,21 @@ import java.util.*; public class DefaultConstField extends DefaultTypedNamedField implements ConstField { + private String comment; private final Literal referenceValue; public DefaultConstField(Map<String, Term> attributes, String name, Literal referenceValue) { super(attributes, name); this.referenceValue = Objects.requireNonNull(referenceValue); } + + public void setComment(String comment) { + this.comment = comment; + } + + public String getComment() { + return comment; + } public Literal getReferenceValue() { return referenceValue; diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultDiscriminatorField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultDiscriminatorField.java index df0e8b2312..9c9acc7a27 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultDiscriminatorField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultDiscriminatorField.java @@ -25,10 +25,20 @@ import java.util.*; public class DefaultDiscriminatorField extends DefaultTypedNamedField implements DiscriminatorField { + private String comment; + public DefaultDiscriminatorField(Map<String, Term> attributes, String name) { super(attributes, name); } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + @Override public String toString() { return "DefaultDiscriminatorField{} " + super.toString(); diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultEnumField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultEnumField.java index 541b946b2a..e95eebafc7 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultEnumField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultEnumField.java @@ -27,6 +27,7 @@ import java.util.Objects; public class DefaultEnumField extends DefaultTypedNamedField implements EnumField { + private String comment; private final String fieldName; public DefaultEnumField(Map<String, Term> attributes, EnumTypeReference type, String name, String fieldName) { @@ -35,6 +36,14 @@ public class DefaultEnumField extends DefaultTypedNamedField implements EnumFiel this.type = type; } + public void setComment(String comment) { + this.comment = comment; + } + + public String getComment() { + return comment; + } + public String getFieldName() { return fieldName; } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultField.java index 8107d4faf4..633f9c5115 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultField.java @@ -25,15 +25,26 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; + public abstract class DefaultField { + protected String comment; protected TypeDefinition owner; protected final Map<String, Term> attributes; + protected DefaultField(Map<String, Term> attributes) { this.attributes = Objects.requireNonNull(attributes); } + public void setComment(String comment) { + this.comment = comment; + } + + public String getComment() { + return comment; + } + public void setOwner(TypeDefinition owner) { this.owner = owner; } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultImplicitField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultImplicitField.java index 9cbc024791..aac2fb9624 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultImplicitField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultImplicitField.java @@ -26,6 +26,7 @@ import java.util.*; public class DefaultImplicitField extends DefaultTypedNamedField implements ImplicitField { + private String comment; private final Term serializeExpression; public DefaultImplicitField(Map<String, Term> attributes, SimpleTypeReference type, String name, Term serializeExpression) { @@ -34,6 +35,14 @@ public class DefaultImplicitField extends DefaultTypedNamedField implements Impl this.type = type; } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + public Term getSerializeExpression() { return serializeExpression; } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultManualArrayField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultManualArrayField.java index 27ad02022f..99bdd0d335 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultManualArrayField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultManualArrayField.java @@ -28,6 +28,7 @@ import java.util.Objects; public class DefaultManualArrayField extends DefaultTypedNamedField implements ManualArrayField { + private String comment; private final LoopType loopType; private final Term loopExpression; private final Term parseExpression; @@ -43,6 +44,14 @@ public class DefaultManualArrayField extends DefaultTypedNamedField implements M this.lengthExpression = Objects.requireNonNull(lengthExpression); } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + public LoopType getLoopType() { return loopType; } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultManualField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultManualField.java index eb4af3c927..fca44bb489 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultManualField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultManualField.java @@ -26,6 +26,7 @@ import java.util.Objects; public class DefaultManualField extends DefaultTypedNamedField implements ManualField { + private String comment; private final Term parseExpression; private final Term serializeExpression; private final Term lengthExpression; @@ -37,6 +38,14 @@ public class DefaultManualField extends DefaultTypedNamedField implements Manual this.lengthExpression = Objects.requireNonNull(lengthExpression); } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + public Term getParseExpression() { return parseExpression; } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultOptionalField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultOptionalField.java index 1afb91a064..9fabc52788 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultOptionalField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultOptionalField.java @@ -27,6 +27,7 @@ import java.util.Optional; public class DefaultOptionalField extends DefaultTypedNamedField implements OptionalField { + private String comment; private final Term conditionExpression; public DefaultOptionalField(Map<String, Term> attributes, String name, Term conditionExpression) { @@ -34,6 +35,14 @@ public class DefaultOptionalField extends DefaultTypedNamedField implements Opti this.conditionExpression = conditionExpression; } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + public Optional<Term> getConditionExpression() { return Optional.ofNullable(conditionExpression); } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultPaddingField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultPaddingField.java index 1284c3ee33..424f98be98 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultPaddingField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultPaddingField.java @@ -27,6 +27,7 @@ import java.util.Objects; public class DefaultPaddingField extends DefaultTypedNamedField implements PaddingField { + private String comment; private final Term paddingValue; private final Term paddingCondition; @@ -45,6 +46,14 @@ public class DefaultPaddingField extends DefaultTypedNamedField implements Paddi return paddingCondition; } + public void setComment(String comment) { + this.comment = comment; + } + + public String getComment() { + return comment; + } + @Override public String toString() { return "DefaultPaddingField{" + diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultPeekField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultPeekField.java index f8f035449e..c389a042e9 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultPeekField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultPeekField.java @@ -27,6 +27,7 @@ import java.util.Optional; public class DefaultPeekField extends DefaultTypedNamedField implements PeekField { + private String comment; private final Term offsetExpression; public DefaultPeekField(Map<String, Term> attributes, String name, Term offsetExpression) { @@ -38,18 +39,29 @@ public class DefaultPeekField extends DefaultTypedNamedField implements PeekFiel return Optional.ofNullable(offsetExpression); } + public void setComment(String comment) { + this.comment = comment; + } + + public String getComment() { + return comment; + } + @Override public String toString() { return "DefaultPeekField{" + - "offsetExpression=" + offsetExpression + - "} " + super.toString(); + "offsetExpression=" + offsetExpression + + "} " + super.toString(); } @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - if (!super.equals(o)) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + if (!super.equals(o)) + return false; DefaultPeekField that = (DefaultPeekField) o; return Objects.equals(offsetExpression, that.offsetExpression); } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultReservedField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultReservedField.java index e638bc2309..b1a6019882 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultReservedField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultReservedField.java @@ -26,6 +26,7 @@ import java.util.*; public class DefaultReservedField extends DefaultTypedField implements ReservedField { + private String comment; private final Object referenceValue; public DefaultReservedField(Map<String, Term> attributes, SimpleTypeReference type, Object referenceValue) { @@ -34,6 +35,14 @@ public class DefaultReservedField extends DefaultTypedField implements ReservedF this.type = type; } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + public Object getReferenceValue() { return referenceValue; } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultSimpleField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultSimpleField.java index 2d495c142c..b5c743f731 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultSimpleField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultSimpleField.java @@ -25,10 +25,20 @@ import java.util.Map; public class DefaultSimpleField extends DefaultTypedNamedField implements SimpleField { + private String comment; + public DefaultSimpleField(Map<String, Term> attributes, String name) { super(attributes, name); } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + @Override public String toString() { return "DefaultSimpleField{} " + super.toString(); diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultStateField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultStateField.java index 95b0f21096..dc6a40ac6d 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultStateField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultStateField.java @@ -26,6 +26,7 @@ import java.util.Objects; public class DefaultStateField extends DefaultTypedNamedField implements StateField { + private String comment; private final Term valueExpression; public DefaultStateField(Map<String, Term> attributes, String name, Term valueExpression) { @@ -36,6 +37,14 @@ public class DefaultStateField extends DefaultTypedNamedField implements StateFi public Term getValueExpression() { return valueExpression; } + + public void setComment(String comment) { + this.comment = comment; + } + + public String getComment() { + return comment; + } @Override public String toString() { diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultSwitchField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultSwitchField.java index 08f7b1d950..21fcf965b1 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultSwitchField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultSwitchField.java @@ -28,6 +28,7 @@ import java.util.*; public class DefaultSwitchField extends DefaultField implements SwitchField { + private String comment; private final List<VariableLiteral> variableLiterals; private final List<DiscriminatedComplexTypeDefinition> cases; @@ -37,6 +38,14 @@ public class DefaultSwitchField extends DefaultField implements SwitchField { this.cases = new LinkedList<>(); } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + public List<VariableLiteral> getDiscriminatorExpressions() { return variableLiterals; } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultTypedField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultTypedField.java index 0b2a3f5f39..8f9fa58958 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultTypedField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultTypedField.java @@ -28,14 +28,22 @@ import java.util.concurrent.CompletionStage; public abstract class DefaultTypedField extends DefaultField { + private String comment; protected TypeReference type; - protected final CompletableFuture<TypeReference> typeReferenceCompletionStage = new CompletableFuture<>(); public DefaultTypedField(Map<String, Term> attributes) { super(attributes); } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + public TypeReference getType() { if (type == null) { throw new IllegalStateException("type not set"); diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultTypedNamedField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultTypedNamedField.java index 4bebc74471..28ed65224d 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultTypedNamedField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultTypedNamedField.java @@ -25,6 +25,7 @@ import java.util.Objects; public class DefaultTypedNamedField extends DefaultTypedField { + private String comment; protected final String name; public DefaultTypedNamedField(Map<String, Term> attributes, String name) { @@ -32,6 +33,14 @@ public class DefaultTypedNamedField extends DefaultTypedField { this.name = name; } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + public String getName() { return name; } diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultUnknownField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultUnknownField.java index e078db313c..ea594de5fa 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultUnknownField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultUnknownField.java @@ -26,11 +26,21 @@ import java.util.*; public class DefaultUnknownField extends DefaultTypedField implements UnknownField { + private String comment; + public DefaultUnknownField(Map<String, Term> attributes, SimpleTypeReference type) { super(attributes); this.type = type; } + public String getComment() { + return comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + @Override public String toString() { return "DefaultUnknownField{} " + super.toString(); diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultValidationField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultValidationField.java index 8900dbfb6f..07e3c2f27d 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultValidationField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultValidationField.java @@ -18,7 +18,6 @@ */ package org.apache.plc4x.plugins.codegenerator.language.mspec.model.fields; -import org.apache.plc4x.plugins.codegenerator.types.fields.Field; import org.apache.plc4x.plugins.codegenerator.types.fields.ValidationField; import org.apache.plc4x.plugins.codegenerator.types.terms.Term; @@ -26,11 +25,12 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; -public class DefaultValidationField extends DefaultField implements ValidationField, Field { +public class DefaultValidationField extends DefaultField implements ValidationField { + + private String comment; private final Term validationExpression; private final String description; - private final boolean shouldFail; public DefaultValidationField(Map<String, Term> attributes, Term validationExpression, String description, boolean shouldFail) { @@ -66,6 +66,14 @@ public class DefaultValidationField extends DefaultField implements ValidationFi public Optional<Term> getAttribute(String s) { return Optional.empty(); } + + public void setComment(String comment) { + this.comment = comment; + } + + public String getComment() { + return comment; + } @Override public String toString() { diff --git a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultVirtualField.java b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultVirtualField.java index edfd21c20a..094d6eac97 100644 --- a/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultVirtualField.java +++ b/code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/fields/DefaultVirtualField.java @@ -1,3 +1,4 @@ + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -25,6 +26,8 @@ import java.util.*; public class DefaultVirtualField extends DefaultTypedNamedField implements VirtualField { + private String comment; + private final Term valueExpression; public DefaultVirtualField(Map<String, Term> attributes, String name, Term valueExpression) { @@ -35,6 +38,14 @@ public class DefaultVirtualField extends DefaultTypedNamedField implements Virtu public Term getValueExpression() { return valueExpression; } + + public void setComment(String comment) { + this.comment = comment; + } + + public String getComment() { + return comment; + } @Override public String toString() { 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 8e7ce7e6c4..87074a8bd0 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 @@ -20,6 +20,7 @@ package org.apache.plc4x.plugins.codegenerator.language.mspec.parser; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.RuleContext; +import org.antlr.v4.runtime.tree.TerminalNode; import org.apache.commons.io.IOUtils; import org.apache.plc4x.plugins.codegenerator.language.mspec.LazyTypeDefinitionConsumer; import org.apache.plc4x.plugins.codegenerator.language.mspec.MSpecBaseListener; @@ -41,7 +42,6 @@ import org.apache.plc4x.plugins.codegenerator.types.terms.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.InputStream; import java.nio.charset.Charset; import java.util.*; import java.util.concurrent.CompletableFuture; @@ -73,6 +73,13 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType private final Stack<String> currentTypeName = new Stack<>(); + // Track the last seen comment and its line number + private String pendingComment = null; + private int pendingCommentLine = -1; + + // For storing comments for complex types between enter/exit + private final Map<MSpecParser.ComplexTypeContext, String> complexTypeComments = new HashMap<>(); + public MessageFormatListener() { types = new HashMap<>(); typeDefinitionConsumers = new HashMap<>(); @@ -89,6 +96,41 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType enumContexts = new LinkedList<>(); } + // Helper: Attach comment if directly above or on the same line + private String consumePendingComment(ParserRuleContext ctx) { + if (pendingComment == null) + return null; + var defLine = ctx.getStart().getLine(); + // Only attach if comment is on the previous line or same line (end-of-line + // comment) + if (pendingCommentLine == defLine - 1 || pendingCommentLine == defLine) { + var comment = pendingComment; + pendingComment = null; + pendingCommentLine = -1; + return comment; + } + return null; + } + + // Listen for comment tokens in the parse tree + @Override + public void visitTerminal(TerminalNode node) { + var token = node.getSymbol(); + if (token == null) + return; + switch (token.getType()) { + case MSpecParser.LINE_COMMENT: + case MSpecParser.BLOCK_COMMENT: + // Save the comment and its line number + pendingComment = token.getText(); + pendingCommentLine = token.getLine(); + break; + default: + break; + } + super.visitTerminal(node); + } + @Override public void enterContantsDefinition(MSpecParser.ContantsDefinitionContext ctx) { currentTypeName.push("Constants"); @@ -107,9 +149,9 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType String typeName = "Constants"; // If the type has subtypes, it's an abstract type. - final List<Field> fields = parserContexts.pop(); - DefaultConstantsTypeDefinition type = new DefaultConstantsTypeDefinition( - typeName, Collections.emptyMap(), Collections.emptyList(), fields); + var fields = parserContexts.pop(); + var type = new DefaultConstantsTypeDefinition(typeName, Collections.emptyMap(), Collections.emptyList(), + fields); // Link the fields and the complex types. if (fields != null) { fields.forEach(field -> ((DefaultField) field).setOwner(type)); @@ -145,65 +187,69 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType // Set a map of attributes that should be set for all fields. Map<String, Term> curBatchSetAttributes = new HashMap<>(); // Add all attributes defined in the current batchSet field. - for (MSpecParser.AttributeContext attributeContext : ctx.attributes.attribute()) { - Term attributeExpression = getExpressionTerm(attributeContext.value); + for (var attributeContext : ctx.attributes.attribute()) { + var attributeExpression = getExpressionTerm(attributeContext.value); curBatchSetAttributes.put(attributeContext.name.getText(), attributeExpression); } // Make the new Map the top of the stack. batchSetAttributes.push(curBatchSetAttributes); + // Attach comment if present (store for use in exit) + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) { + complexTypeComments.put(ctx, comment); + } + if ("enum".equals(ctx.getChild(0).getText())) { - List<EnumValue> enumContext = new LinkedList<>(); + var enumContext = new LinkedList<EnumValue>(); enumContexts.push(enumContext); } else { - List<Field> parserContext = new LinkedList<>(); + var parserContext = new LinkedList<Field>(); parserContexts.push(parserContext); } } @Override public void exitComplexType(MSpecParser.ComplexTypeContext ctx) { - String typeName = getIdString(ctx.name); - List<Argument> parserArguments = null; - if (ctx.params != null) { - parserArguments = getParserArguments(ctx.params.argument()); - } + var typeName = getIdString(ctx.name); + var parserArguments = ctx.params != null ? getParserArguments(ctx.params.argument()) : null; - final Map<String, Term> attributes = batchSetAttributes.peek(); + var attributes = batchSetAttributes.peek(); // Handle enum types. if ("enum".equals(ctx.getChild(0).getText())) { - SimpleTypeReference type = (ctx.type != null) ? getSimpleTypeReference(ctx.type) : null; - List<EnumValue> enumValues = getEnumValues(); + var type = ctx.type != null ? getSimpleTypeReference(ctx.type) : null; + var enumValues = getEnumValues(); if (type == null) { // in case there is no type we default to uint32 type = new DefaultIntegerTypeReference(SimpleTypeReference.SimpleBaseType.UINT, 32); } - DefaultEnumTypeDefinition enumType = new DefaultEnumTypeDefinition(typeName, type, attributes, enumValues, - parserArguments); + var enumType = new DefaultEnumTypeDefinition(typeName, type, attributes, enumValues, parserArguments); dispatchType(typeName, enumType); enumContexts.pop(); - } else if (ctx.dataIoTypeSwitch != null) { // Handle data-io types. - SwitchField switchField = getSwitchField(); - DefaultDataIoTypeDefinition type = new DefaultDataIoTypeDefinition(typeName, attributes, parserArguments, switchField); + } else if (ctx.dataIoTypeSwitch != null) { // Handle data-io types. + var switchField = getSwitchField(); + var type = new DefaultDataIoTypeDefinition(typeName, attributes, parserArguments, switchField); dispatchType(typeName, type); // Set the parent type for all sub-types. if (switchField != null) { for (DiscriminatedComplexTypeDefinition subtype : switchField.getCases()) { - if (subtype instanceof DefaultDiscriminatedComplexTypeDefinition) { - LOGGER.debug("Setting parent {} for {}", type, subtype); - ((DefaultDiscriminatedComplexTypeDefinition) subtype).setParentType(type); + if (subtype instanceof DefaultDiscriminatedComplexTypeDefinition ddctd) { + LOGGER.debug("Setting parent {} for {}", type, ddctd); + ddctd.setParentType(type); } } } parserContexts.pop(); } else { // Handle all other types. // If the type has sub-types it's an abstract type. - SwitchField switchField = getSwitchField(); - boolean abstractType = switchField != null; - final List<Field> fields = parserContexts.pop(); - DefaultComplexTypeDefinition type = new DefaultComplexTypeDefinition( - typeName, attributes, parserArguments, abstractType, fields); + var switchField = getSwitchField(); + var abstractType = switchField != null; + var fields = parserContexts.pop(); + var type = new DefaultComplexTypeDefinition(typeName, attributes, parserArguments, abstractType, fields); + // Attach comment if present + var comment = complexTypeComments.remove(ctx); + if (comment != null) type.setComment(comment); // Link the fields and the complex types. if (fields != null) { fields.forEach(field -> ((DefaultField) field).setOwner(type)); @@ -220,10 +266,10 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType Optional<SwitchField> switchField = type.getSwitchField(); if (switchField.isPresent()) { for (DiscriminatedComplexTypeDefinition subtype : switchField.get().getCases()) { - if (subtype instanceof DefaultDiscriminatedComplexTypeDefinition) { - LOGGER.debug("Setting parent {} for {}", type, subtype); - ((DefaultDiscriminatedComplexTypeDefinition) subtype).setParentType(type); - setParentRelationship((DefaultDiscriminatedComplexTypeDefinition) subtype); + if (subtype instanceof DefaultDiscriminatedComplexTypeDefinition ddct) { + LOGGER.debug("Setting parent {} for {}", type, ddct); + ddct.setParentType(type); + setParentRelationship(ddct); } } } @@ -232,7 +278,7 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterBatchSetDefinition(MSpecParser.BatchSetDefinitionContext ctx) { // Set a map of attributes that should be set for all fields. - Map<String, Term> curBatchSetAttributes = new HashMap<>(); + var curBatchSetAttributes = new HashMap<String, Term>(); // Add all attributes of the lower layers and initialize the new map with it. if (!batchSetAttributes.empty()) { curBatchSetAttributes.putAll(batchSetAttributes.peek()); @@ -254,8 +300,8 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterAbstractField(MSpecParser.AbstractFieldContext ctx) { - String name = getIdString(ctx.name); - DefaultAbstractField field = new DefaultAbstractField(getAttributes(ctx), name); + var name = getIdString(ctx.name); + var field = new DefaultAbstractField(getAttributes(ctx), name); getTypeReference(ctx.type).whenComplete((typeReference, throwable) -> { if (throwable != null) { // TODO: proper error collection in type context error bucket @@ -271,10 +317,13 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterArrayField(MSpecParser.ArrayFieldContext ctx) { - String name = getIdString(ctx.name); - ArrayField.LoopType loopType = ArrayField.LoopType.valueOf(ctx.loopType.getText().toUpperCase()); - Term loopExpression = getExpressionTerm(ctx.loopExpression); - DefaultArrayField field = new DefaultArrayField(getAttributes(ctx), name, loopType, loopExpression); + var name = getIdString(ctx.name); + var loopType = ArrayField.LoopType.valueOf(ctx.loopType.getText().toUpperCase()); + var loopExpression = getExpressionTerm(ctx.loopExpression); + var field = new DefaultArrayField(getAttributes(ctx), name, loopType, loopExpression); + // Attach comment if present + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); getTypeReference(ctx.type).whenComplete((typeReference, throwable) -> { if (throwable != null) { // TODO: proper error collection in type context error bucket @@ -290,9 +339,9 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterAssertField(MSpecParser.AssertFieldContext ctx) { - String name = getIdString(ctx.name); - Term conditionExpression = getExpressionTerm(ctx.condition); - DefaultAssertField field = new DefaultAssertField(getAttributes(ctx), name, conditionExpression); + var name = getIdString(ctx.name); + var conditionExpression = getExpressionTerm(ctx.condition); + var field = new DefaultAssertField(getAttributes(ctx), name, conditionExpression); getTypeReference(ctx.type).whenComplete((typeReference, throwable) -> { if (throwable != null) { // TODO: proper error collection in type context error bucket @@ -308,10 +357,12 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterChecksumField(MSpecParser.ChecksumFieldContext ctx) { - SimpleTypeReference type = getSimpleTypeReference(ctx.type); - String name = getIdString(ctx.name); - Term checksumExpression = getExpressionTerm(ctx.checksumExpression); - Field field = new DefaultChecksumField(getAttributes(ctx), type, name, checksumExpression); + var type = getSimpleTypeReference(ctx.type); + var name = getIdString(ctx.name); + var checksumExpression = getExpressionTerm(ctx.checksumExpression); + var field = new DefaultChecksumField(getAttributes(ctx), type, name, checksumExpression); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -319,8 +370,10 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterConstField(MSpecParser.ConstFieldContext ctx) { - String name = getIdString(ctx.name); - DefaultConstField field = new DefaultConstField(getAttributes(ctx), name, getValueLiteral(ctx.expected)); + var name = getIdString(ctx.name); + var field = new DefaultConstField(getAttributes(ctx), name, getValueLiteral(ctx.expected)); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); if (ctx.type.dataType() != null) { field.setType(getSimpleTypeReference(ctx.type.dataType())); } else { @@ -340,8 +393,10 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterDiscriminatorField(MSpecParser.DiscriminatorFieldContext ctx) { - String name = getIdString(ctx.name); - DefaultDiscriminatorField field = new DefaultDiscriminatorField(getAttributes(ctx), name); + var name = getIdString(ctx.name); + var field = new DefaultDiscriminatorField(getAttributes(ctx), name); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); getTypeReference(ctx.type).whenComplete((typeReference, throwable) -> { if (throwable != null) { // TODO: proper error collection in type context error bucket @@ -357,15 +412,17 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterEnumField(MSpecParser.EnumFieldContext ctx) { - String typeRefName = ctx.type.complexTypeReference.getText(); - DefaultEnumTypeReference type = new DefaultEnumTypeReference(typeRefName, null); + var typeRefName = ctx.type.complexTypeReference.getText(); + var type = new DefaultEnumTypeReference(typeRefName, null); setOrScheduleTypeDefinitionConsumer(typeRefName, type::setTypeDefinition); - String name = getIdString(ctx.name); + var name = getIdString(ctx.name); String fieldName = null; if (ctx.fieldName != null) { fieldName = getIdString(ctx.fieldName); } - Field field = new DefaultEnumField(getAttributes(ctx), type, name, fieldName); + var field = new DefaultEnumField(getAttributes(ctx), type, name, fieldName); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -373,10 +430,12 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterImplicitField(MSpecParser.ImplicitFieldContext ctx) { - SimpleTypeReference type = getSimpleTypeReference(ctx.type); - String name = getIdString(ctx.name); - Term serializeExpression = getExpressionTerm(ctx.serializeExpression); - Field field = new DefaultImplicitField(getAttributes(ctx), type, name, serializeExpression); + var type = getSimpleTypeReference(ctx.type); + var name = getIdString(ctx.name); + var serializeExpression = getExpressionTerm(ctx.serializeExpression); + var field = new DefaultImplicitField(getAttributes(ctx), type, name, serializeExpression); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null ) field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -386,13 +445,13 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType public void enterManualArrayField(MSpecParser.ManualArrayFieldContext ctx) { String name = getIdString(ctx.name); ManualArrayField.LoopType loopType = ManualArrayField.LoopType.valueOf( - ctx.loopType.getText().toUpperCase()); + ctx.loopType.getText().toUpperCase()); Term loopExpression = getExpressionTerm(ctx.loopExpression); Term parseExpression = getExpressionTerm(ctx.parseExpression); Term serializeExpression = getExpressionTerm(ctx.serializeExpression); Term lengthExpression = getExpressionTerm(ctx.lengthExpression); DefaultManualArrayField field = new DefaultManualArrayField(getAttributes(ctx), name, loopType, loopExpression, - parseExpression, serializeExpression, lengthExpression); + parseExpression, serializeExpression, lengthExpression); getTypeReference(ctx.type).whenComplete((typeReference, throwable) -> { if (throwable != null) { // TODO: proper error collection in type context error bucket @@ -401,6 +460,9 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType } field.setType(new DefaultArrayTypeReference(typeReference)); }); + String comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) + field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -412,8 +474,9 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType Term parseExpression = getExpressionTerm(ctx.parseExpression); Term serializeExpression = getExpressionTerm(ctx.serializeExpression); Term lengthExpression = getExpressionTerm(ctx.lengthExpression); - DefaultManualField field = new DefaultManualField(getAttributes(ctx), name, parseExpression, serializeExpression, - lengthExpression); + DefaultManualField field = new DefaultManualField(getAttributes(ctx), name, parseExpression, + serializeExpression, + lengthExpression); getTypeReference(ctx.type).whenComplete((typeReference, throwable) -> { if (throwable != null) { // TODO: proper error collection in type context error bucket @@ -422,6 +485,9 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType } field.setType(typeReference); }); + String comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) + field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -434,7 +500,7 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType if (ctx.condition != null) { conditionExpression = getExpressionTerm(ctx.condition); } - DefaultOptionalField field = new DefaultOptionalField(getAttributes(ctx), name, conditionExpression); + var field = new DefaultOptionalField(getAttributes(ctx), name, conditionExpression); getTypeReference(ctx.type).whenComplete((typeReference, throwable) -> { if (throwable != null) { // TODO: proper error collection in type context error bucket @@ -443,6 +509,8 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType } field.setType(typeReference); }); + String comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -450,11 +518,13 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterPaddingField(MSpecParser.PaddingFieldContext ctx) { - SimpleTypeReference type = getSimpleTypeReference(ctx.type); - String name = getIdString(ctx.name); - Term paddingValue = getExpressionTerm(ctx.paddingValue); - Term timesPadding = getExpressionTerm(ctx.timesPadding); - Field field = new DefaultPaddingField(getAttributes(ctx), type, name, paddingValue, timesPadding); + var type = getSimpleTypeReference(ctx.type); + var name = getIdString(ctx.name); + var paddingValue = getExpressionTerm(ctx.paddingValue); + var timesPadding = getExpressionTerm(ctx.timesPadding); + var field = new DefaultPaddingField(getAttributes(ctx), type, name, paddingValue, timesPadding); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -462,12 +532,12 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterPeekField(MSpecParser.PeekFieldContext ctx) { - String name = getIdString(ctx.name); + var name = getIdString(ctx.name); Term offsetExpression = null; if (ctx.offset != null) { offsetExpression = getExpressionTerm(ctx.offset); } - DefaultPeekField field = new DefaultPeekField(getAttributes(ctx), name, offsetExpression); + var field = new DefaultPeekField(getAttributes(ctx), name, offsetExpression); getTypeReference(ctx.type).whenComplete((typeReference, throwable) -> { if (throwable != null) { // TODO: proper error collection in type context error bucket @@ -476,6 +546,8 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType } field.setType(typeReference); }); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -483,9 +555,11 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterReservedField(MSpecParser.ReservedFieldContext ctx) { - SimpleTypeReference type = getSimpleTypeReference(ctx.type); - String expected = getExprString(ctx.expected); - Field field = new DefaultReservedField(getAttributes(ctx), type, expected); + var type = getSimpleTypeReference(ctx.type); + var expected = getExprString(ctx.expected); + var field = new DefaultReservedField(getAttributes(ctx), type, expected); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -493,8 +567,8 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterSimpleField(MSpecParser.SimpleFieldContext ctx) { - String name = getIdString(ctx.name); - DefaultSimpleField field = new DefaultSimpleField(getAttributes(ctx), name); + var name = getIdString(ctx.name); + var field = new DefaultSimpleField(getAttributes(ctx), name); getTypeReference(ctx.type).whenComplete((typeReference, throwable) -> { if (throwable != null) { // TODO: proper error collection in type context error bucket @@ -503,6 +577,8 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType } field.setType(typeReference); }); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -510,31 +586,37 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterStateField(MSpecParser.StateFieldContext ctx) { - String name = getIdString(ctx.name); + var name = getIdString(ctx.name); // Get the type information from the parents arguments. - ParserRuleContext parent = ctx.getParent().getParent().getParent(); - if (!(parent instanceof MSpecParser.ComplexTypeContext) && !(parent instanceof MSpecParser.CaseStatementContext)) { + var parent = ctx.getParent().getParent().getParent(); + if (!(parent instanceof MSpecParser.ComplexTypeContext) + && !(parent instanceof MSpecParser.CaseStatementContext)) { throw new RuntimeException("state fields must only be defined in complex types"); } Optional<MSpecParser.ArgumentContext> argumentContext; - if (parent instanceof MSpecParser.ComplexTypeContext) { - MSpecParser.ComplexTypeContext complexTypeContext = (MSpecParser.ComplexTypeContext) parent; - argumentContext = complexTypeContext.params.argument().stream().filter(argContext -> argContext.name.getText().equalsIgnoreCase(name)).findFirst(); + if (parent instanceof MSpecParser.ComplexTypeContext complexTypeContext) { + argumentContext = complexTypeContext.params.argument().stream() + .filter(argContext -> argContext.name.getText().equalsIgnoreCase(name)).findFirst(); } else { RuleContext curContext = parent; - while((curContext.parent != null) && !(curContext.parent instanceof MSpecParser.ComplexTypeContext)) { + while ((curContext.parent != null) && !(curContext.parent instanceof MSpecParser.ComplexTypeContext)) { curContext = curContext.parent; } if (curContext.parent == null) { - throw new RuntimeException("state fields must refer to arguments by using the same name. Parent context is null."); + throw new RuntimeException( + "state fields must refer to arguments by using the same name. Parent context is null."); } - MSpecParser.ComplexTypeContext complexTypeContext = (MSpecParser.ComplexTypeContext) curContext.parent; - argumentContext = complexTypeContext.params.argument().stream().filter(argContext -> argContext.name.getText().equalsIgnoreCase(name)).findFirst(); + var complexTypeContext = (MSpecParser.ComplexTypeContext) curContext.parent; + argumentContext = complexTypeContext.params.argument().stream() + .filter(argContext -> argContext.name.getText().equalsIgnoreCase(name)).findFirst(); } - MSpecParser.TypeReferenceContext type = argumentContext.orElseThrow(() -> new RuntimeException("state fields must refer to arguments by using the same name.")).type; + var type = argumentContext.orElseThrow( + () -> new RuntimeException("state fields must refer to arguments by using the same name.")).type; // The variable term is always just a direct reference to the parser argument. - Term valueExpression = new DefaultVariableLiteral(name, null, null, null); - DefaultStateField field = new DefaultStateField(getAttributes(ctx), name, valueExpression); + var valueExpression = new DefaultVariableLiteral(name, null, null, null); + var field = new DefaultStateField(getAttributes(ctx), name, valueExpression); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); getTypeReference(type).whenComplete((typeReference, throwable) -> { if (throwable != null) { // TODO: proper error collection in type context error bucket @@ -550,10 +632,12 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterTypeSwitchField(MSpecParser.TypeSwitchFieldContext ctx) { - List<VariableLiteral> variableLiterals = ctx.discriminators.variableLiteral().stream() - .map(this::getVariableLiteral) - .collect(Collectors.toList()); - DefaultSwitchField field = new DefaultSwitchField(getAttributes(ctx), variableLiterals); + var variableLiterals = ctx.discriminators.variableLiteral().stream() + .map(this::getVariableLiteral) + .collect(Collectors.toList()); + var field = new DefaultSwitchField(getAttributes(ctx), variableLiterals); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -561,8 +645,10 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterUnknownField(MSpecParser.UnknownFieldContext ctx) { - SimpleTypeReference type = getSimpleTypeReference(ctx.type); - Field field = new DefaultUnknownField(getAttributes(ctx), type); + var type = getSimpleTypeReference(ctx.type); + var field = new DefaultUnknownField(getAttributes(ctx), type); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -570,8 +656,8 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterValidationField(MSpecParser.ValidationFieldContext ctx) { - Term validationExpression = getExpressionTerm(ctx.validationExpression); - boolean shouldFail = true; + var validationExpression = getExpressionTerm(ctx.validationExpression); + var shouldFail = true; if (ctx.shouldFail != null) { shouldFail = "true".equalsIgnoreCase(ctx.shouldFail.getText()); } @@ -579,7 +665,9 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType if (ctx.description != null) { description = ctx.description.getText(); } - Field field = new DefaultValidationField(getAttributes(ctx), validationExpression, description, shouldFail); + var field = new DefaultValidationField(getAttributes(ctx), validationExpression, description, shouldFail); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); if (parserContexts.peek() != null) { parserContexts.peek().add(field); } @@ -587,9 +675,11 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterVirtualField(MSpecParser.VirtualFieldContext ctx) { - String name = getIdString(ctx.name); - Term valueExpression = getExpressionTerm(ctx.valueExpression); - DefaultVirtualField field = new DefaultVirtualField(getAttributes(ctx), name, valueExpression); + var name = getIdString(ctx.name); + var valueExpression = getExpressionTerm(ctx.valueExpression); + var field = new DefaultVirtualField(getAttributes(ctx), name, valueExpression); + var comment = consumePendingComment((ParserRuleContext) ctx); + if (comment != null) field.setComment(comment); getTypeReference(ctx.type).whenComplete((typeReference, throwable) -> { if (throwable != null) { // TODO: proper error collection in type context error bucket @@ -608,11 +698,11 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType List<Field> parserContext = new LinkedList<>(); // Calculate the name of the current type - String namePrefix = ""; - if(ctx.nameWildcard != null) { + var namePrefix = ""; + if (ctx.nameWildcard != null) { namePrefix = getCurrentTypeName(); } - String typeName = namePrefix + ctx.name.getText(); + var typeName = namePrefix + ctx.name.getText(); currentTypeName.push(typeName); // For DataIo we don't generate types. @@ -625,24 +715,29 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void exitCaseStatement(MSpecParser.CaseStatementContext ctx) { - String typeName = currentTypeName.pop(); + var typeName = currentTypeName.pop(); // For DataIo we don't generate types. if (ctx.parent.parent instanceof MSpecParser.DataIoDefinitionContext) { currentTypeName.push(typeName); typeName = ctx.name.getText(); } - boolean abstractType = getSwitchField() != null; + var abstractType = getSwitchField() != null; - final Map<String, Term> attributes = batchSetAttributes.peek(); + var attributes = batchSetAttributes.peek(); - List<Argument> parserArguments = new LinkedList<>(); + var parserArguments = new LinkedList<Argument>(); // For DataIO types, add all the arguments from the parent type. -/* if (!(ctx.parent.parent.parent.parent instanceof MSpecParser.ComplexTypeContext) - && ((MSpecParser.ComplexTypeContext) ctx.parent.parent.parent).params != null) { - parserArguments.addAll(getParserArguments( - ((MSpecParser.ComplexTypeContext) ctx.parent.parent.parent).params.argument())); - }*/ + /* + * if (!(ctx.parent.parent.parent.parent instanceof + * MSpecParser.ComplexTypeContext) + * && ((MSpecParser.ComplexTypeContext) ctx.parent.parent.parent).params != + * null) { + * parserArguments.addAll(getParserArguments( + * ((MSpecParser.ComplexTypeContext) + * ctx.parent.parent.parent).params.argument())); + * } + */ // Add all eventually existing local arguments. if (ctx.argumentList() != null) { parserArguments.addAll(getParserArguments(ctx.argumentList().argument())); @@ -651,27 +746,26 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType List<Term> discriminatorValues; if (ctx.discriminatorValues != null) { discriminatorValues = ctx.discriminatorValues.expression().stream() - .map(this::getExpressionTerm) - .collect(Collectors.toList()); + .map(this::getExpressionTerm) + .collect(Collectors.toList()); } else { discriminatorValues = Collections.emptyList(); } - final List<Field> fields = parserContexts.pop(); - DefaultDiscriminatedComplexTypeDefinition type = - new DefaultDiscriminatedComplexTypeDefinition(typeName, attributes, parserArguments, abstractType, - fields, discriminatorValues); + var fields = parserContexts.pop(); + var type = new DefaultDiscriminatedComplexTypeDefinition(typeName, attributes, parserArguments, abstractType, fields, discriminatorValues); // Link the fields and the complex types. if (fields != null) { fields.forEach(field -> ((DefaultField) field).setOwner(type)); } - // For DataIO we don't need to generate the sub-types as these will be PlcValues. + // For DataIO we don't need to generate the sub-types as these will be + // PlcValues. if (!(ctx.parent.parent instanceof MSpecParser.DataIoDefinitionContext)) { dispatchType(typeName, type); } // Add the type to the switch field definition. - DefaultSwitchField switchField = getSwitchField(); + var switchField = getSwitchField(); if (switchField == null) { throw new RuntimeException("This shouldn't have happened"); } @@ -680,26 +774,27 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType @Override public void enterEnumValueDefinition(MSpecParser.EnumValueDefinitionContext ctx) { - String value = (ctx.valueExpression != null) ? unquoteString(ctx.valueExpression.getText()) : null; - String name = ctx.name.getText(); + var value = (ctx.valueExpression != null) ? unquoteString(ctx.valueExpression.getText()) : null; + var name = ctx.name.getText(); Map<String, String> constants = null; if (ctx.constantValueExpressions != null) { - MSpecParser.ComplexTypeContext parentCtx = (MSpecParser.ComplexTypeContext) ctx.parent; - int numConstantValues = parentCtx.params.argument().size(); - int numExpressionValues = ctx.constantValueExpressions.expression().size(); - // This only works if we provide exactly the same number of expressions as we defined constants + var parentCtx = (MSpecParser.ComplexTypeContext) ctx.parent; + var numConstantValues = parentCtx.params.argument().size(); + var numExpressionValues = ctx.constantValueExpressions.expression().size(); + // This only works if we provide exactly the same number of expressions as we + // defined constants if (numConstantValues != numExpressionValues) { throw new RuntimeException("Number of constant value expressions doesn't match the number of " + - "defined constants. Expecting " + numConstantValues + " but got " + numExpressionValues); + "defined constants. Expecting " + numConstantValues + " but got " + numExpressionValues); } // Build a map of the constant expressions (With the constant name as key) constants = new HashMap<>(); for (int i = 0; i < numConstantValues; i++) { - MSpecParser.ArgumentContext argumentContext = parentCtx.params.argument(i); - String constantName = argumentContext.name.getText(); - MSpecParser.ExpressionContext expression = ctx.constantValueExpressions.expression(i); - String constant = unquoteString(expression.getText()); + var argumentContext = parentCtx.params.argument(i); + var constantName = argumentContext.name.getText(); + var expression = ctx.constantValueExpressions.expression(i); + var constant = unquoteString(expression.getText()); // String expressions are double escaped if (constant != null && constant.startsWith("\"")) { constant = unquoteString(constant); @@ -707,18 +802,18 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType constants.put(constantName, constant); } } - List<EnumValue> enumValues = Objects.requireNonNull(this.enumContexts.peek()); + var enumValues = Objects.requireNonNull(this.enumContexts.peek()); if (value == null) { // If no values are specified we count - String counted = "0"; + var counted = "0"; if (enumValues.size() > 0) { - String previousValue = enumValues.get(enumValues.size() - 1).getValue(); - int parsedPreviousValue = Integer.parseInt(previousValue); + var previousValue = enumValues.get(enumValues.size() - 1).getValue(); + var parsedPreviousValue = Integer.parseInt(previousValue); counted = "" + (parsedPreviousValue + 1); } value = counted; } - final DefaultEnumValue enumValue = new DefaultEnumValue(value, name, constants); + var enumValue = new DefaultEnumValue(value, name, constants); enumValues.add(enumValue); } @@ -726,45 +821,48 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType if (expressionContext.ASTERISK() != null) { return WildcardTerm.INSTANCE; } - String expressionString = getExprString(expressionContext); + var expressionString = getExprString(expressionContext); Objects.requireNonNull(expressionString, "Expression string should not be null"); - InputStream inputStream = IOUtils.toInputStream(expressionString, Charset.defaultCharset()); + var inputStream = IOUtils.toInputStream(expressionString, Charset.defaultCharset()); Objects.requireNonNull(getCurrentTypeName(), "expression term can only occur within a type"); - ExpressionStringParser parser = new ExpressionStringParser(this, getCurrentTypeName()); + var parser = new ExpressionStringParser(this, getCurrentTypeName()); try { return parser.parse(inputStream); } catch (Exception e) { throw new RuntimeException(String.format("Error parsing expression: '%s' at line %d column %d", - expressionString, expressionContext.start.getLine(), expressionContext.start.getStartIndex()), e); + expressionString, expressionContext.start.getLine(), expressionContext.start.getStartIndex()), e); } } private VariableLiteral getVariableLiteral(MSpecParser.VariableLiteralContext variableLiteralContext) { // TODO: make nullsafe - final String variableLiteral = variableLiteralContext.getText(); - InputStream inputStream = IOUtils.toInputStream(variableLiteral, Charset.defaultCharset()); - ExpressionStringParser parser = new ExpressionStringParser(this, getCurrentTypeName()); + var variableLiteral = variableLiteralContext.getText(); + var inputStream = IOUtils.toInputStream(variableLiteral, Charset.defaultCharset()); + var parser = new ExpressionStringParser(this, getCurrentTypeName()); try { - // As this come from a VariableLiteralContext we know that it is a VariableLiteral + // As this come from a VariableLiteralContext we know that it is a + // VariableLiteral return (VariableLiteral) parser.parse(inputStream); } catch (Exception e) { throw new RuntimeException(String.format("Error parsing variable literal: '%s' at line %d column %d", - variableLiteral, variableLiteralContext.start.getLine(), variableLiteralContext.start.getStartIndex()), e); + variableLiteral, variableLiteralContext.start.getLine(), + variableLiteralContext.start.getStartIndex()), e); } } private Literal getValueLiteral(MSpecParser.ValueLiteralContext valueLiteralContext) { // TODO: make nullsafe - final String valueLiteralContextText = valueLiteralContext.getText(); - InputStream inputStream = IOUtils.toInputStream(valueLiteralContextText, Charset.defaultCharset()); - ExpressionStringParser parser = new ExpressionStringParser(this, getCurrentTypeName()); + var valueLiteralContextText = valueLiteralContext.getText(); + var inputStream = IOUtils.toInputStream(valueLiteralContextText, Charset.defaultCharset()); + var parser = new ExpressionStringParser(this, getCurrentTypeName()); try { // As this come from a ValueLiteralContext we know that it is a Literal return (Literal) parser.parse(inputStream); } catch (Exception e) { throw new RuntimeException(String.format("Error parsing variable literal: '%s' at line %d column %d", - valueLiteralContextText, valueLiteralContext.start.getLine(), valueLiteralContext.start.getStartIndex()), e); + valueLiteralContextText, valueLiteralContext.start.getLine(), + valueLiteralContext.start.getStartIndex()), e); } } @@ -773,18 +871,20 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType return CompletableFuture.completedFuture(getSimpleTypeReference(ctx.simpleTypeReference)); } else { CompletableFuture<TypeReference> typeReferenceCompletableFuture = new CompletableFuture<>(); - String typeRefName = ctx.complexTypeReference.getText(); + var typeRefName = ctx.complexTypeReference.getText(); setOrScheduleTypeDefinitionConsumer(typeRefName, typeDefinition -> { if (typeDefinition.isDataIoTypeDefinition()) { - DefaultDataIoTypeReference value = new DefaultDataIoTypeReference(typeRefName, getParams(ctx.params)); + var value = new DefaultDataIoTypeReference(typeRefName, + getParams(ctx.params)); value.setTypeDefinition(typeDefinition); typeReferenceCompletableFuture.complete(value); } else if (typeDefinition.isComplexTypeDefinition()) { - DefaultComplexTypeReference value = new DefaultComplexTypeReference(typeRefName, getParams(ctx.params)); + var value = new DefaultComplexTypeReference(typeRefName, + getParams(ctx.params)); value.setTypeDefinition(typeDefinition); typeReferenceCompletableFuture.complete(value); } else if (typeDefinition.isEnumTypeDefinition()) { - DefaultEnumTypeReference value = new DefaultEnumTypeReference(typeRefName, getParams(ctx.params)); + var value = new DefaultEnumTypeReference(typeRefName, getParams(ctx.params)); value.setTypeDefinition(typeDefinition); typeReferenceCompletableFuture.complete(value); } else { @@ -796,12 +896,12 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType } private SimpleTypeReference getSimpleTypeReference(MSpecParser.DataTypeContext ctx) { - SimpleTypeReference.SimpleBaseType simpleBaseType = - SimpleTypeReference.SimpleBaseType.valueOf(ctx.base.getText().toUpperCase()); + var simpleBaseType = SimpleTypeReference.SimpleBaseType + .valueOf(ctx.base.getText().toUpperCase()); // String types need an additional length expression. if (simpleBaseType == SimpleTypeReference.SimpleBaseType.VSTRING) { if (ctx.length != null) { - Term lengthExpression = getExpressionTerm(ctx.length); + var lengthExpression = getExpressionTerm(ctx.length); return new DefaultVstringTypeReference(simpleBaseType, lengthExpression); } else { return new DefaultVstringTypeReference(simpleBaseType, null); @@ -810,21 +910,23 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType switch (simpleBaseType) { case INT: case UINT: - int integerSize = Integer.parseInt(ctx.size.getText()); + var integerSize = Integer.parseInt(ctx.size.getText()); return new DefaultIntegerTypeReference(simpleBaseType, integerSize); case VINT: { - final Map<String, Term> attributes = getAttributes(ctx.parent.parent); + var attributes = getAttributes(ctx.parent.parent); SimpleTypeReference propertyType; - int propertySizeInBits = 32; + var propertySizeInBits = 32; if (attributes.containsKey("propertySizeInBits")) { - final Term propertySizeInBitsTerm = attributes.get("propertySizeInBits"); + var propertySizeInBitsTerm = attributes.get("propertySizeInBits"); if (!(propertySizeInBitsTerm instanceof NumericLiteral)) { - throw new RuntimeException("'propertySizeInBits' attribute is required to be a numeric literal"); + throw new RuntimeException( + "'propertySizeInBits' attribute is required to be a numeric literal"); } - NumericLiteral propertySizeInBitsLiteral = (NumericLiteral) propertySizeInBitsTerm; + var propertySizeInBitsLiteral = (NumericLiteral) propertySizeInBitsTerm; propertySizeInBits = propertySizeInBitsLiteral.getNumber().intValue(); } - propertyType = new DefaultIntegerTypeReference(SimpleTypeReference.SimpleBaseType.INT, propertySizeInBits); + propertyType = new DefaultIntegerTypeReference(SimpleTypeReference.SimpleBaseType.INT, + propertySizeInBits); return new DefaultVintegerTypeReference(simpleBaseType, propertyType); } case VUINT: { @@ -834,12 +936,14 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType 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"); + throw new RuntimeException( + "'propertySizeInBits' attribute is required to be a numeric literal"); } - NumericLiteral propertySizeInBitsLiteral = (NumericLiteral) propertySizeInBitsTerm; + var propertySizeInBitsLiteral = (NumericLiteral) propertySizeInBitsTerm; propertySizeInBits = propertySizeInBitsLiteral.getNumber().intValue(); } - propertyType = new DefaultIntegerTypeReference(SimpleTypeReference.SimpleBaseType.UINT, propertySizeInBits); + propertyType = new DefaultIntegerTypeReference(SimpleTypeReference.SimpleBaseType.UINT, + propertySizeInBits); return new DefaultVintegerTypeReference(simpleBaseType, propertyType); } case FLOAT: @@ -863,7 +967,7 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType } private DefaultSwitchField getSwitchField() { - for (Field field : Objects.requireNonNull(parserContexts.peek())) { + for (var field : Objects.requireNonNull(parserContexts.peek())) { if (field instanceof DefaultSwitchField) { return (DefaultSwitchField) field; } @@ -877,19 +981,19 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType private List<Argument> getParserArguments(List<MSpecParser.ArgumentContext> params) { return params.stream() - .map(argumentContext -> { - DefaultArgument argument = new DefaultArgument(getIdString(argumentContext.name)); - getTypeReference(argumentContext.type).whenComplete((typeReference, throwable) -> { - if (throwable != null) { - // TODO: proper error collection in type context error bucket - LOGGER.debug("Error setting type for {}", argument, throwable); - return; - } - argument.setType(typeReference); - }); - return argument; - }) - .collect(Collectors.toList()); + .map(argumentContext -> { + DefaultArgument argument = new DefaultArgument(getIdString(argumentContext.name)); + getTypeReference(argumentContext.type).whenComplete((typeReference, throwable) -> { + if (throwable != null) { + // TODO: proper error collection in type context error bucket + LOGGER.debug("Error setting type for {}", argument, throwable); + return; + } + argument.setType(typeReference); + }); + return argument; + }) + .collect(Collectors.toList()); } private List<Term> getParams(MSpecParser.MultipleExpressionsContext params) { @@ -897,14 +1001,14 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType return null; } return params.expression().stream() - .map(this::getExprString) - .map(this::parseExpression) - .collect(Collectors.toList()); + .map(this::getExprString) + .map(this::parseExpression) + .collect(Collectors.toList()); } private Term parseExpression(String expressionString) { - InputStream inputStream = IOUtils.toInputStream(expressionString, Charset.defaultCharset()); - ExpressionStringParser parser = new ExpressionStringParser(this, getCurrentTypeName()); + var inputStream = IOUtils.toInputStream(expressionString, Charset.defaultCharset()); + var parser = new ExpressionStringParser(this, getCurrentTypeName()); try { return parser.parse(inputStream); } catch (Exception e) { @@ -913,7 +1017,7 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType } private String getCurrentTypeName() { - if(currentTypeName.isEmpty()) { + if (currentTypeName.isEmpty()) { return null; } return currentTypeName.peek(); @@ -962,7 +1066,8 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType if (types.containsKey(typeName)) { LOGGER.warn("{} being overridden", typeName); - // TODO: we need to implement replace logic... means we need to replace all old references with the new one in that case otherwise we just get an exception + // TODO: we need to implement replace logic... means we need to replace all old + // references with the new one in that case otherwise we just get an exception } types.put(typeName, type); @@ -976,11 +1081,11 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType } private void consumerDispatchType(String typeName, TypeDefinition type) { - List<Consumer<TypeDefinition>> waitingConsumers = typeDefinitionConsumers.getOrDefault(typeName, new LinkedList<>()); + var waitingConsumers = typeDefinitionConsumers.getOrDefault(typeName, new LinkedList<>()); LOGGER.debug("{} waiting for {}", waitingConsumers.size(), typeName); - Iterator<Consumer<TypeDefinition>> consumerIterator = waitingConsumers.iterator(); - List<Consumer<TypeDefinition>> removedItems = new ArrayList<>(); + var consumerIterator = waitingConsumers.iterator(); + var removedItems = new ArrayList<Consumer<TypeDefinition>>(); while (consumerIterator.hasNext()) { Consumer<TypeDefinition> setter = consumerIterator.next(); @@ -999,14 +1104,15 @@ public class MessageFormatListener extends MSpecBaseListener implements LazyType public void setOrScheduleTypeDefinitionConsumer(String typeRefName, Consumer<TypeDefinition> setTypeDefinition) { LOGGER.debug("set or schedule {}", typeRefName); - TypeDefinition typeDefinition = types.get(typeRefName); + var typeDefinition = types.get(typeRefName); if (typeDefinition != null) { LOGGER.debug("{} present so setting for {}", typeRefName, setTypeDefinition); setTypeDefinition.accept(typeDefinition); } else { // put up order if (LOGGER.isDebugEnabled()) { - LOGGER.debug("{} already waiting for {}", typeDefinitionConsumers.getOrDefault(typeRefName, new LinkedList<>()).size(), typeRefName); + LOGGER.debug("{} already waiting for {}", + typeDefinitionConsumers.getOrDefault(typeRefName, new LinkedList<>()).size(), typeRefName); } typeDefinitionConsumers.putIfAbsent(typeRefName, new LinkedList<>()); typeDefinitionConsumers.get(typeRefName).add(setTypeDefinition);
