Unverified Commit f3a23c98 authored by Michael von Wenckstern's avatar Michael von Wenckstern Committed by GitHub
Browse files

Merge pull request #1 from EmbeddedMontiArc/Arrays

Arrays
parents b0a1a165 0eaf25f7
......@@ -27,7 +27,7 @@
<!-- == PROJECT COORDINATES ============================================= -->
<groupId>de.monticore.lang</groupId>
<artifactId>Tagging</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
<!-- == PROJECT DEPENDENCIES ============================================= -->
......
......@@ -32,6 +32,25 @@ grammar Tagging extends de.monticore.common.Common {
fragment token Recursion = '{' (~('{' | '}' | '"') | String | Recursion)+ '}';
fragment token TagToken = (~('"' | '{' | '}' | ';'))+;
NameWithArray = (Name ("[" start:IntLiteral (( ":" step:IntLiteral )? ":" end:IntLiteral )?"]")?);
ast NameWithArray =
method public String toString(){
String arrayPart = "";
arrayPart += start.isPresent() ? "" + start.get().getValue() : "";
arrayPart += step.isPresent() ? ":" + step.get().getValue() : "";
arrayPart += end.isPresent() ? ":" + end.get().getValue() : "";
return name + (arrayPart.equals("") ? "" : "[" + arrayPart + "]");
};
QualifiedNameWithArray = parts:( NameWithArray || ".")+;
ast QualifiedNameWithArray =
method public String toString(){
return de.se_rwth.commons.Joiners.DOT.join(getParts());
};
interface Scope;
ast Scope =
method public String getQualifiedNameString() {
......@@ -42,7 +61,7 @@ grammar Tagging extends de.monticore.common.Common {
};
// this one is only defined here, to get Parser Tags
NameScope implements Scope = QualifiedName;
NameScope implements Scope = qualifiedName:QualifiedNameWithArray;
ast NameScope =
method public String getQualifiedNameString() {
return getQualifiedName().toString();
......@@ -51,7 +70,7 @@ grammar Tagging extends de.monticore.common.Common {
return "NameScope";
};
ConnectorScope extends NameScope = source:QualifiedName "->" QualifiedName;
ConnectorScope extends NameScope = source:QualifiedNameWithArray "->" qualifiedName:QualifiedNameWithArray;
ast ConnectorScope =
method public String getQualifiedNameString() {
return getQualifiedName().toString();
......
......@@ -37,8 +37,9 @@ import java.util.Set;
import java.util.function.Predicate;
import de.monticore.ast.ASTNode;
import de.monticore.lang.tagging._ast.ASTTaggingUnit;
import de.monticore.lang.tagging._ast.*;
import de.monticore.lang.tagging._parser.TaggingParser;
import de.monticore.lang.tagging.helper.RangeFixer;
import de.monticore.symboltable.CommonScope;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
......@@ -130,6 +131,7 @@ public class TaggingResolver implements Scope {
}
if (ast.isPresent()) {
// todo check package conformity
RangeFixer.fixTaggingUnit(ast.get());
foundModels.add(ast.get());
}
});
......
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.tagging.helper;
import com.google.common.collect.Lists;
import de.monticore.lang.tagging._ast.*;
import de.monticore.literals.literals._ast.ASTIntLiteral;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
public class RangeFixer {
private RangeFixer() {
}
public static void fixTaggingUnit(ASTTaggingUnit astTaggingUnit) {
//Replace all range syntax scopes with expanded versions
astTaggingUnit.getTagBody().getTags()
.forEach(tag -> {
List<ASTScope> fixedScopes = tag.getScopes().stream()
.map(RangeFixer::expandScopeWithRange)
.flatMap(Collection::stream)
.collect(Collectors.toList());
tag.setScopes(fixedScopes);
});
}
public static List<ASTScope> expandScopeWithRange(ASTScope scope) {
if (scope.getScopeKind().equals("NameScope")) {
ASTNameScope nameScope = (ASTNameScope) scope;
List<ASTQualifiedNameWithArray> qualifiedNames = new ArrayList<>();
//add empty start name
qualifiedNames.add(ASTQualifiedNameWithArray.getBuilder().build());
for (ASTNameWithArray part : nameScope.getQualifiedName().getParts()) {
List<ASTNameWithArray> expandedParts = expandRangeInPart(part);
List<ASTQualifiedNameWithArray> expandedNames = new ArrayList<>();
for (ASTNameWithArray ep : expandedParts) {
for (ASTQualifiedNameWithArray name : qualifiedNames) {
ASTQualifiedNameWithArray tmpName = name.deepClone();
tmpName.getParts().add(ep);
expandedNames.add(tmpName);
}
}
qualifiedNames = expandedNames;
}
return qualifiedNames.stream().map(name -> ASTNameScope.getBuilder().qualifiedName(name).build()).collect(Collectors.toList());
} else {
return Lists.newArrayList(scope);
}
}
public static List<ASTNameWithArray> expandRangeInPart(ASTNameWithArray part) {
//Not a range
if (!part.getStart().isPresent() || !part.getEnd().isPresent()) {
return Lists.newArrayList(part);
}
List<ASTNameWithArray> result = new ArrayList<>();
int step = part.getStep().isPresent() ? part.getStep().get().getValue() : 1;
int start = part.getStart().get().getValue();
int end = part.getEnd().get().getValue();
for (int i = start; i <= end; i += step) {
ASTNameWithArray tmpNameWithArray = ASTNameWithArray.getBuilder()
.name(part.getName())
.start(ASTIntLiteral.getBuilder().source("" + i).build())
.build();
result.add(tmpNameWithArray);
}
return result;
}
}
......@@ -20,10 +20,24 @@
*/
package de.monticore.lang.montiarc.tagging;
import de.monticore.lang.tagging._ast.ASTScope;
import de.monticore.lang.tagging._ast.ASTTag;
import de.monticore.lang.tagging._ast.ASTTagElement;
import de.monticore.lang.tagging._ast.ASTTaggingUnit;
import de.monticore.lang.tagging._parser.TaggingParser;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Created by MichaelvonWenckstern on 13.06.2016.
*/
......@@ -65,5 +79,5 @@ public class ParserTagValueTest {
tags.getTagBody().getTags().forEach(t -> t.getTagElements().forEach(e -> System.out.println(e.getName() + ": " + e.getTagValue().orElse(""))));
// System.out.println(tags.getTags().size());
// System.out.println(tags.getTags().get(0).getTagValue().length());
} */
}*/
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment