Skip to content

Commit 0d52bde

Browse files
committed
feat(codegen): comment support
1 parent 3430da0 commit 0d52bde

File tree

47 files changed

+444
-346
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+444
-346
lines changed

code-generation/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,8 @@ STRING_CHARACTER
414414
: ~["\\\r\n]
415415
;
416416
417-
// Stuff we just want to ignore
418-
419417
LINE_COMMENT
420-
: '//' ~[\r\n]* -> channel(HIDDEN)
418+
: ('//' ~[\r\n]*)+ -> channel(HIDDEN)
421419
;
422420
423421
BLOCK_COMMENT

code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/expression/ExpressionStringListener.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,13 @@ public void exitExpressionString(ExpressionParser.ExpressionStringContext ctx) {
7373
if (roots.size() != 1) {
7474
throw new RuntimeException("Expression can only contain one root term.");
7575
}
76-
root = roots.get(0);
76+
root = roots.getFirst();
7777
}
7878

7979
/////////////////////////////////////////////////////////////////////////////////////////
8080
// Literals
81-
/////////////////////////////////////////////////////////////////////////////////////////
81+
82+
/// //////////////////////////////////////////////////////////////////////////////////////
8283

8384
@Override
8485
public void exitNullExpression(ExpressionParser.NullExpressionContext ctx) {
@@ -284,7 +285,8 @@ public void exitIdentifierSegmentRest(ExpressionParser.IdentifierSegmentRestCont
284285

285286
/////////////////////////////////////////////////////////////////////////////////////////
286287
// Unary Terms
287-
/////////////////////////////////////////////////////////////////////////////////////////
288+
289+
/// //////////////////////////////////////////////////////////////////////////////////////
288290

289291
@Override
290292
public void enterNotExpression(ExpressionParser.NotExpressionContext ctx) {
@@ -321,7 +323,8 @@ public void exitExpressionExpression(ExpressionParser.ExpressionExpressionContex
321323

322324
/////////////////////////////////////////////////////////////////////////////////////////
323325
// Binary Terms
324-
/////////////////////////////////////////////////////////////////////////////////////////
326+
327+
/// //////////////////////////////////////////////////////////////////////////////////////
325328

326329
@Override
327330
public void enterOrExpression(ExpressionParser.OrExpressionContext ctx) {
@@ -435,7 +438,8 @@ public void exitMultExpression(ExpressionParser.MultExpressionContext ctx) {
435438

436439
/////////////////////////////////////////////////////////////////////////////////////////
437440
// Ternary Terms
438-
/////////////////////////////////////////////////////////////////////////////////////////
441+
442+
/// //////////////////////////////////////////////////////////////////////////////////////
439443

440444
@Override
441445
public void enterIfExpression(ExpressionParser.IfExpressionContext ctx) {
@@ -450,13 +454,14 @@ public void exitIfExpression(ExpressionParser.IfExpressionContext ctx) {
450454

451455
/////////////////////////////////////////////////////////////////////////////////////////
452456
// Helpers
453-
/////////////////////////////////////////////////////////////////////////////////////////
457+
458+
/// //////////////////////////////////////////////////////////////////////////////////////
454459

455460
private UnaryTerm getUnaryTerm(String op, List<Term> terms) {
456461
if (terms.size() != 1) {
457462
throw new RuntimeException(op + " should be a unary operation");
458463
}
459-
Term a = terms.get(0);
464+
Term a = terms.getFirst();
460465
return new DefaultUnaryTerm(a, op);
461466
}
462467

code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/definitions/DefaultArgument.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@
2727

2828
public class DefaultArgument implements Argument {
2929

30+
protected final CompletableFuture<TypeReference> typeReferenceCompletionStage = new CompletableFuture<>();
3031
private final String name;
31-
3232
private TypeReference type;
3333

34-
protected final CompletableFuture<TypeReference> typeReferenceCompletionStage = new CompletableFuture<>();
35-
3634
public DefaultArgument(String name) {
3735
this.name = Objects.requireNonNull(name);
3836
}

code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/definitions/DefaultComplexTypeDefinition.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public class DefaultComplexTypeDefinition extends DefaultTypeDefinition implemen
3232
private final List<Field> fields;
3333
protected ComplexTypeDefinition parentType;
3434

35-
public DefaultComplexTypeDefinition(String name, Map<String, Term> attributes, List<Argument> parserArguments, boolean isAbstract, List<Field> fields) {
36-
super(name, attributes, parserArguments);
35+
public DefaultComplexTypeDefinition(String name, Map<String, Term> attributes, List<Argument> parserArguments, boolean isAbstract, List<Field> fields, String comment) {
36+
super(name, attributes, parserArguments, comment);
3737
this.isAbstract = isAbstract;
3838
this.fields = Objects.requireNonNull(fields);
3939
}
@@ -48,7 +48,7 @@ public void setParentType(ComplexTypeDefinition parentType) {
4848

4949
public Optional<List<Argument>> getAllParserArguments() {
5050
List<Argument> allArguments = new ArrayList<>(getParserArguments().orElse(Collections.emptyList()));
51-
if(getParentType().isPresent()) {
51+
if (getParentType().isPresent()) {
5252
ComplexTypeDefinition parent = getParentType().get();
5353
allArguments.addAll(parent.getAllParserArguments().orElse(Collections.emptyList()));
5454
}

code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/definitions/DefaultConstantsTypeDefinition.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,22 @@
1818
*/
1919
package org.apache.plc4x.plugins.codegenerator.language.mspec.model.definitions;
2020

21-
import org.apache.plc4x.plugins.codegenerator.types.definitions.Argument;
2221
import org.apache.plc4x.plugins.codegenerator.types.definitions.ConstantsTypeDefinition;
2322
import org.apache.plc4x.plugins.codegenerator.types.fields.ConstField;
2423
import org.apache.plc4x.plugins.codegenerator.types.fields.Field;
2524
import org.apache.plc4x.plugins.codegenerator.types.fields.PropertyField;
2625
import org.apache.plc4x.plugins.codegenerator.types.fields.VirtualField;
27-
import org.apache.plc4x.plugins.codegenerator.types.terms.Term;
2826

27+
import java.util.Collections;
2928
import java.util.List;
30-
import java.util.Map;
3129
import java.util.stream.Collectors;
3230

3331
public class DefaultConstantsTypeDefinition extends DefaultTypeDefinition implements ConstantsTypeDefinition {
3432

3533
private final List<Field> fields;
3634

37-
public DefaultConstantsTypeDefinition(String name, Map<String, Term> attributes, List<Argument> parserArguments, List<Field> fields) {
38-
super(name, attributes, parserArguments);
35+
public DefaultConstantsTypeDefinition(String name, List<Field> fields, String comment) {
36+
super(name, Collections.emptyMap(), Collections.emptyList(), comment);
3937
this.fields = fields;
4038
}
4139

code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/definitions/DefaultDataIoTypeDefinition.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,20 @@ public class DefaultDataIoTypeDefinition extends DefaultComplexTypeDefinition im
3434
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDataIoTypeDefinition.class);
3535
private final SwitchField switchField;
3636
private TypeReference type;
37-
public DefaultDataIoTypeDefinition(String name, Map<String, Term> attributes, List<Argument> parserArguments, SwitchField switchField) {
38-
super(name, attributes, parserArguments, false, List.of(switchField));
37+
38+
public DefaultDataIoTypeDefinition(String name, Map<String, Term> attributes, List<Argument> parserArguments, SwitchField switchField, String comment) {
39+
super(name, attributes, parserArguments, false, List.of(switchField), comment);
3940
this.switchField = Objects.requireNonNull(switchField);
40-
if (parserArguments.size() < 1) {
41+
if (parserArguments.isEmpty()) {
4142
throw new IllegalStateException();
4243
}
43-
((DefaultArgument) parserArguments.get(0)).getTypeReferenceCompletionStage().whenComplete((typeReference, throwable) -> {
44+
((DefaultArgument) parserArguments.getFirst()).getTypeReferenceCompletionStage().whenComplete((typeReference, throwable) -> {
4445
if (throwable != null) {
4546
// TODO: proper error collection in type context error bucket
46-
LOGGER.debug("Error setting type for {}", parserArguments.get(0), throwable);
47+
LOGGER.debug("Error setting type for {}", parserArguments.getFirst(), throwable);
4748
return;
4849
}
49-
this.type = Objects.requireNonNull(parserArguments.get(0).getType());
50+
this.type = Objects.requireNonNull(parserArguments.getFirst().getType());
5051
});
5152
}
5253

code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/definitions/DefaultDiscriminatedComplexTypeDefinition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public class DefaultDiscriminatedComplexTypeDefinition extends DefaultComplexTyp
3333

3434
private final List<Term> discriminatorValueTerms;
3535

36-
public DefaultDiscriminatedComplexTypeDefinition(String name, Map<String, Term> attributes, List<Argument> parserArguments, boolean isAbstract, List<Field> fields, List<Term> discriminatorValueTerms) {
37-
super(name, attributes, parserArguments, isAbstract, fields);
36+
public DefaultDiscriminatedComplexTypeDefinition(String name, Map<String, Term> attributes, List<Argument> parserArguments, boolean isAbstract, List<Field> fields, List<Term> discriminatorValueTerms, String comment) {
37+
super(name, attributes, parserArguments, isAbstract, fields, comment);
3838
this.discriminatorValueTerms = Objects.requireNonNull(discriminatorValueTerms);
3939
}
4040

code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/definitions/DefaultEnumTypeDefinition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public class DefaultEnumTypeDefinition extends DefaultTypeDefinition implements
4040
private final Map<String, TypeReference> constants;
4141

4242
public DefaultEnumTypeDefinition(String name, SimpleTypeReference type, Map<String, Term> attributes, List<EnumValue> enumValues,
43-
List<Argument> parserArgument) {
44-
super(name, attributes, parserArgument);
43+
List<Argument> parserArgument, String comment) {
44+
super(name, attributes, parserArgument, comment);
4545
this.type = Objects.requireNonNull(type);
4646
this.enumValues = Objects.requireNonNull(enumValues);
4747
this.constants = new HashMap<>();

code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/definitions/DefaultEnumValue.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ public class DefaultEnumValue implements EnumValue {
2929
private final String value;
3030
private final String name;
3131
private final Map<String, String> constants;
32+
private final String comment;
3233

33-
public DefaultEnumValue(String value, String name, Map<String, String> constants) {
34+
public DefaultEnumValue(String value, String name, Map<String, String> constants, String comment) {
3435
this.value = Objects.requireNonNull(value);
3536
this.name = Objects.requireNonNull(name);
3637
this.constants = constants;
38+
this.comment = comment;
3739
}
3840

3941
@Override
@@ -54,6 +56,11 @@ public Optional<String> getConstant(String name) {
5456
return Optional.ofNullable(constants.get(name));
5557
}
5658

59+
@Override
60+
public Optional<String> getComment() {
61+
return Optional.ofNullable(comment);
62+
}
63+
5764
@Override
5865
public String toString() {
5966
return "DefaultEnumValue{" +

code-generation/protocol-base-mspec/src/main/java/org/apache/plc4x/plugins/codegenerator/language/mspec/model/definitions/DefaultTypeDefinition.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
public abstract class DefaultTypeDefinition {
2828

2929
protected final String name;
30-
private final Map<String, Term> attributes;
3130
protected final List<Argument> parserArguments;
31+
protected final String comment;
32+
private final Map<String, Term> attributes;
3233

33-
public DefaultTypeDefinition(String name, Map<String, Term> attributes, List<Argument> parserArguments) {
34+
public DefaultTypeDefinition(String name, Map<String, Term> attributes, List<Argument> parserArguments, String comment) {
3435
this.name = Objects.requireNonNull(name);
3536
this.attributes = attributes;
3637
this.parserArguments = parserArguments;
38+
this.comment = comment;
3739
}
3840

3941
public String getName() {
@@ -59,12 +61,17 @@ public Optional<List<Argument>> getAllParserArguments() {
5961
return Optional.of(allArguments);
6062
}
6163

64+
public Optional<String> getComment() {
65+
return Optional.ofNullable(comment);
66+
}
67+
6268
@Override
6369
public String toString() {
6470
return "DefaultTypeDefinition{" +
6571
"name='" + name + '\'' +
6672
", attributes=" + attributes +
6773
", parserArguments=" + parserArguments +
74+
", comment='" + comment + '\'' +
6875
'}';
6976
}
7077

@@ -73,11 +80,14 @@ public boolean equals(Object o) {
7380
if (this == o) return true;
7481
if (o == null || getClass() != o.getClass()) return false;
7582
DefaultTypeDefinition that = (DefaultTypeDefinition) o;
76-
return name.equals(that.name) && Objects.equals(attributes, that.attributes) && Objects.equals(parserArguments, that.parserArguments);
83+
return name.equals(that.name) &&
84+
Objects.equals(attributes, that.attributes) &&
85+
Objects.equals(parserArguments, that.parserArguments) &&
86+
comment.equals(that.comment);
7787
}
7888

7989
@Override
8090
public int hashCode() {
81-
return Objects.hash(name, attributes, parserArguments);
91+
return Objects.hash(name, attributes, parserArguments, comment);
8292
}
8393
}

0 commit comments

Comments
 (0)