Skip to content

Commit

Permalink
feat: mspec extension with parameterized type refs, assert, try, const
Browse files Browse the repository at this point in the history
1. parameters on type refs
    with that change it is now possible to target a discriminated child in advance.
2. assert keyword
    with that change it is possible to throw a ParserAssertException (in java, or errors in other languages). This field is similar to a const but instead of a ParseException a ParserAssertException is thrown. In contrast to a const the check expression can be dynamic (e.g. virtual fields now working on develop)
3. try keyword to prefix fields:
    with that change it is possible to try to parse some content and in case an assert fails it resets the buffer.
4. const is now extended to type reference
   this change allows enums to be used as const values.
  • Loading branch information
sruehl committed Sep 26, 2021
1 parent ef35531 commit 22c9f00
Show file tree
Hide file tree
Showing 122 changed files with 3,666 additions and 405 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,20 @@ private Optional<TypeReference> getDiscriminatorType(ComplexTypeDefinition paren
* @return list of symbolic names for the discriminators.
*/
public List<String> getDiscriminatorNames() {
TypeDefinition baseType = thisType;
if (thisType.getParentType() != null) {
baseType = thisType.getParentType();
return getDiscriminatorNames(thisType);
}

/**
* Get an ordered list of generated names for the discriminators.
* These names can be used to access the type definitions as well as well as the values.
*
* @param baseType the type to get the discriminator Names from
*
* @return list of symbolic names for the discriminators.
*/
public List<String> getDiscriminatorNames(TypeDefinition baseType) {
if (baseType.getParentType() != null) {
baseType = baseType.getParentType();
}
final SwitchField switchField = getSwitchField(baseType);
List<String> discriminatorNames = new ArrayList<>();
Expand Down Expand Up @@ -722,9 +733,21 @@ public boolean isNonDiscriminatorField(String discriminatorName) {
* @return true if a field with the given name already exists in the same type.
*/
public boolean isDiscriminatorField(String discriminatorName) {
List<String> names = getDiscriminatorNames();
return isDiscriminatorField(thisType, discriminatorName);
}

/**
* Check if there's any field with the given name.
* This is required to suppress the generation of a virtual field
* in case a discriminated field is providing the information.
*
* @param discriminatorName name of the virtual name
* @return true if a field with the given name already exists in the same type.
*/
public boolean isDiscriminatorField(TypeDefinition typeDefinition, String discriminatorName) {
List<String> names = getDiscriminatorNames(typeDefinition);
if (names != null) {
return getDiscriminatorNames().stream()
return names.stream()
.anyMatch(field -> field.equals(discriminatorName));
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ private String toExpression(TypeReference fieldType, Term term, Argument[] parse
return tracer + "\"" + ((StringLiteral) term).getValue() + "\"";
} else if (term instanceof VariableLiteral) {
tracer = tracer.dive("variable literal instanceOf");
return tracer+toVariableExpression(fieldType, (VariableLiteral) term, parserArguments, serializerArguments, serialize, suppressPointerAccess);
return tracer + toVariableExpression(fieldType, (VariableLiteral) term, parserArguments, serializerArguments, serialize, suppressPointerAccess);
} else {
throw new RuntimeException("Unsupported Literal type " + term.getClass().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.go.GoLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.ComplexTypeDefinition" -->
<#-- Declare the name and type of variables declared locally inside the template -->
<#-- @ftlvariable name="arrayField" type="org.apache.plc4x.plugins.codegenerator.types.fields.ArrayField" -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.go.GoLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.EnumTypeDefinition" -->
<#-- @ftlvariable name="simpleTypeReference" type="org.apache.plc4x.plugins.codegenerator.types.references.SimpleTypeReference" -->
${helper.fileName(protocolName, languageName, outputFlavor)?replace(".", "/")}/model/${type.name}.go
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.go.GoLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.ComplexTypeDefinition" -->
<#-- Declare the name and type of variables declared locally inside the template -->
<#-- @ftlvariable name="arrayField" type="org.apache.plc4x.plugins.codegenerator.types.fields.ArrayField" -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.go.GoLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.ComplexTypeDefinition" -->
<#-- Declare the name and type of variables declared locally inside the template -->
<#-- @ftlvariable name="arrayField" type="org.apache.plc4x.plugins.codegenerator.types.fields.ArrayField" -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,9 @@ public String toParseExpression(TypedField field, Term term, Argument[] parserAr
return tracer + toExpression(field, term, variableLiteral -> tracer.dive("variableExpressionGenerator") + toVariableParseExpression(field, variableLiteral, parserArguments));
}

public String toSerializationExpression(TypedField field, Term term, Argument[] serializerArgments) {
public String toSerializationExpression(TypedField field, Term term, Argument[] serializerArguments) {
Tracer tracer = Tracer.start("toSerializationExpression");
return tracer + toExpression(field, term, variableLiteral -> tracer.dive("variableExpressionGenerator") + toVariableSerializationExpression(field, variableLiteral, serializerArgments));
return tracer + toExpression(field, term, variableLiteral -> tracer.dive("variableExpressionGenerator") + toVariableSerializationExpression(field, variableLiteral, serializerArguments));
}

private String toExpression(TypedField field, Term term, Function<VariableLiteral, String> variableExpressionGenerator) {
Expand Down Expand Up @@ -640,6 +640,10 @@ private String toExpression(TypedField field, Term term, Function<VariableLitera
}
}

public String toVariableEnumAccessExpression(VariableLiteral variableLiteral) {
return variableLiteral.getName();
}

private String toVariableParseExpression(TypedField field, VariableLiteral variableLiteral, Argument[] parserArguments) {
Tracer tracer = Tracer.start("toVariableParseExpression");
// CAST expressions are special as we need to add a ".class" to the second parameter in Java.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.java.JavaLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.ComplexTypeDefinition" -->
<#-- Declare the name and type of variables declared locally inside the template -->
<#-- @ftlvariable name="arrayField" type="org.apache.plc4x.plugins.codegenerator.types.fields.ArrayField" -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.java.JavaLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
${helper.packageName(protocolName, languageName, outputFlavor)?replace(".", "/")}/types/package-info.java
/*
* Licensed to the Apache Software Foundation (ASF) under one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.java.JavaLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.EnumTypeDefinition" -->
${helper.packageName(protocolName, languageName, outputFlavor)?replace(".", "/")}/types/${type.name}.java
/*
Expand Down
Loading

0 comments on commit 22c9f00

Please sign in to comment.