From 7fe30fc5da4a6b848b5eb7b0e0df97c1a6b3b06b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Bispo?= Date: Wed, 1 Jan 2025 17:39:27 +0000 Subject: [PATCH] Improves parsing of LangSpecV2, removes LangSpecV1 refs in AbstractJoinPointClassGenerator --- .../language/specification/dsl/Attribute.java | 27 ++- .../specification/dsl/types/EnumDef.java | 10 +- .../specification/dsl/types/LiteralEnum.java | 11 +- .../lara/langspec/LangSpecsXmlParser.java | 16 +- .../generator/generator/BaseGenerator.java | 77 ++++--- .../AbstractJoinPointClassGenerator.java | 36 ++-- .../java/helpers/AttributesEnumGenerator.java | 20 +- .../generator/java/utils/GeneratorUtils.java | 196 ++++++++++++------ 8 files changed, 233 insertions(+), 160 deletions(-) diff --git a/LanguageSpecification/src/org/lara/language/specification/dsl/Attribute.java b/LanguageSpecification/src/org/lara/language/specification/dsl/Attribute.java index 643c09629..64133b931 100644 --- a/LanguageSpecification/src/org/lara/language/specification/dsl/Attribute.java +++ b/LanguageSpecification/src/org/lara/language/specification/dsl/Attribute.java @@ -1,11 +1,11 @@ /** * Copyright 2016 SPeCS. - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. under the License. @@ -13,24 +13,22 @@ package org.lara.language.specification.dsl; +import org.lara.language.specification.dsl.types.IType; + import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; -import org.lara.language.specification.dsl.types.ArrayType; -import org.lara.language.specification.dsl.types.IType; -import org.lara.language.specification.dsl.types.PrimitiveClasses; - /** * A basic class that contains a type and a name - * - * @author tiago * + * @author tiago */ public class Attribute extends BaseNode implements Comparable { private Declaration declaration; private List parameters; + private List defs; public Attribute(IType type, String name) { this(type, name, new ArrayList<>()); @@ -39,8 +37,9 @@ public Attribute(IType type, String name) { public Attribute(IType type, String name, List parameters) { declaration = new Declaration(type, name); this.parameters = parameters; + this.defs = new ArrayList<>(); } - + public void addParameter(IType type, String name) { addParameter(type, name, ""); } @@ -49,6 +48,14 @@ public void addParameter(IType type, String name, String defaultValue) { parameters.add(new Parameter(type, name, defaultValue)); } + public void addDef(Def def) { + defs.add(def); + } + + public List getDefs() { + return defs; + } + public IType getType() { return declaration.getType(); } diff --git a/LanguageSpecification/src/org/lara/language/specification/dsl/types/EnumDef.java b/LanguageSpecification/src/org/lara/language/specification/dsl/types/EnumDef.java index 2584ee349..fb172646a 100644 --- a/LanguageSpecification/src/org/lara/language/specification/dsl/types/EnumDef.java +++ b/LanguageSpecification/src/org/lara/language/specification/dsl/types/EnumDef.java @@ -1,11 +1,11 @@ /** * Copyright 2016 SPeCS. - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. under the License. @@ -13,12 +13,12 @@ package org.lara.language.specification.dsl.types; +import org.lara.language.specification.dsl.BaseNode; + import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; -import org.lara.language.specification.dsl.BaseNode; - public class EnumDef extends BaseNode implements IType { private String name; diff --git a/LanguageSpecification/src/org/lara/language/specification/dsl/types/LiteralEnum.java b/LanguageSpecification/src/org/lara/language/specification/dsl/types/LiteralEnum.java index 2144341b2..ced1bcf30 100644 --- a/LanguageSpecification/src/org/lara/language/specification/dsl/types/LiteralEnum.java +++ b/LanguageSpecification/src/org/lara/language/specification/dsl/types/LiteralEnum.java @@ -1,11 +1,11 @@ /** * Copyright 2016 SPeCS. - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. under the License. @@ -77,6 +77,9 @@ public String getType() { @Override public String toString() { - return values.stream().collect(Collectors.joining("| ", "[", "]")); + //return values.stream().collect(Collectors.joining("| ", "[", "]")); + + // To keep compatibility with previous code, since it is dependent on this format + return values.stream().collect(Collectors.joining(",", "{", "}")); } } diff --git a/LanguageSpecification/src/pt/up/fe/specs/lara/langspec/LangSpecsXmlParser.java b/LanguageSpecification/src/pt/up/fe/specs/lara/langspec/LangSpecsXmlParser.java index c5d7041ef..0201e551a 100644 --- a/LanguageSpecification/src/pt/up/fe/specs/lara/langspec/LangSpecsXmlParser.java +++ b/LanguageSpecification/src/pt/up/fe/specs/lara/langspec/LangSpecsXmlParser.java @@ -245,9 +245,23 @@ private static Attribute getAttribute(XmlElement attributeNode, LanguageSpecific parameterNode.getAttribute("name"), parameterNode.getAttribute("default")); } + var defNodes = attributeNode.getElementsByName("def"); + + for (var defNode : defNodes) { + // If def does not have a type, use the attribute type + newAttribute.addDef(parseDef(defNode, type)); + } + return newAttribute; } + private static Def parseDef(XmlElement defNode, String attributeType) { + // Check if it has an optional 'type' + var type = defNode.getAttribute("type", attributeType); + + return new Def(type); + } + private static String getType(XmlElement node) { // Default type is "void" return node.getAttribute("type", "void"); @@ -293,7 +307,7 @@ private static List