diff --git a/Parser/spec/Fortran2008Rewrites.jrag b/Parser/spec/Fortran2008Rewrites.jrag new file mode 100644 index 0000000000000000000000000000000000000000..6a4fd68384077d5ba766bafb70d0af0a2270e32d --- /dev/null +++ b/Parser/spec/Fortran2008Rewrites.jrag @@ -0,0 +1,89 @@ +aspect AmbiguityResoler { + rewrite Designator_OR_FunctionReference { + when ( !hasSubstringRange_OR_ActualArgSpecList() && !hasSubstringRange() ) + to ArraySection_Designator { + return new ArraySection_Designator(new ArraySection(getDataRef(), new Opt<SubstringRange>())); + } + } + rewrite ArraySection_Designator { + when (!getArraySection().hasSubstringRange() + && getArraySection().getDataRef().getPartRefList().numChildren() == 1 + && getArraySection().getDataRef().getPartRef(0).getSectionSubscriptList().numChildren() == 0 + && !getArraySection().getDataRef().getPartRef(0).hasImageSelector()) + to ObjectName_Designator { + return new ObjectName_Designator(new ObjectName(getArraySection().getDataRef().getPartRef(0).getName())); + } + when (getArraySection().isArrayElement()) + to ArrayElement_Designator { + return new ArrayElement_Designator(new ArrayElement(getArraySection().getDataRef())); + } + } + + // TODO: to implement this, an Expr.rank() attribute is required + // rewrite VectorSectionSubscript { + // when ( getVectorSubscript().getExpr().rank() == 0) + // to SimpleSectionSubscript { + // return new SimpleSectionSubscript(new Subscript(getVectorSubscript().getExpr())); + // } + // } + + rewrite AccLoopStmt { + when (containedCollapseOne() != null) + to AccLoopStmt { + ASTNode parent = getParent(); + if(parent!=null){ + int i = containedCollapseOne().getParent().getIndexOfChild(containedCollapseOne()); + if(i>-1){ + parent.removeChild(i); + } + + } + return this; + } + } + + // TODO support the collapse optimisation in all Acc Statements + syn AccCollapseClause AccStmt.containedCollapseOne(); + eq AccStmt.containedCollapseOne() = null; + eq AccLoopStmt.containedCollapseOne() { + for (AccClause clause: getAccClauseList()) { + if (clause.isCollapseOne() != null) { + return clause.isCollapseOne(); + } + } + return null; + } + + syn AccCollapseClause AccClause.isCollapseOne(); + eq AccClause.isCollapseOne() = null; + eq AccCollapseClause.isCollapseOne() { + if (getCount().getDigitString().getDigits().equals("1")) { + return this; + } else { + return null; + } + } + + /** + * C624 (R618) Exactly one part-ref shall have nonzero rank, and either the final part-ref shall + * have a section-subscript-list with nonzero rank, another part-ref shall have nonzero rank, or + * the complex-part-designator shall be an array. + * TODO: C624 is too relaxed + */ + syn boolean ArraySection.isArrayElement() { + if (hasSubstringRange()) { + return false; + } + for (PartRef partRef : getDataRef().getPartRefList()) { + if (!partRef.hasZeroRank()) { + return true; + } + } + return false; + } + + syn boolean PartRef.hasZeroRank() { + return getSectionSubscriptList().numChildren() == 0; + } + +} \ No newline at end of file diff --git a/Parser/test-data/rewrites/A_Designator_OR_FunctionReference.f90 b/Parser/test-data/rewrites/A_Designator_OR_FunctionReference.f90 new file mode 100644 index 0000000000000000000000000000000000000000..9e538611bcbf523c4fb7a46af069a0a2c33dcd53 --- /dev/null +++ b/Parser/test-data/rewrites/A_Designator_OR_FunctionReference.f90 @@ -0,0 +1,4 @@ + +mat(i,j) = 0 + +end diff --git a/Parser/test/org/tud/forty/test/RewriteTest.java b/Parser/test/org/tud/forty/test/RewriteTest.java new file mode 100644 index 0000000000000000000000000000000000000000..cea2846c12678fdbb993202cf79a71b19c6aaa5c --- /dev/null +++ b/Parser/test/org/tud/forty/test/RewriteTest.java @@ -0,0 +1,26 @@ +package org.tud.forty.test; + +import java.io.File; +import java.util.Iterator; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import org.tud.forty.ast.Root; + +public class RewriteTest extends TestBase { + + @DataProvider(name = "rewrites") + public static Iterator<Object[]> fortranRuleProvider() { + return ruleProvider("test-data/rewrites"); + } + + @Test(dataProvider = "rewrites", groups = {"parser"}) + public void testRuleParser(File f) throws Exception { + printFile(f); + testParse(f, false, true, false, true, Root.class); + } + + @Test(dataProvider = "rewrites", groups = {"prettyprinter"}) + public void testRulePrinter(File f) throws Exception { + testParse(f, true, false, false, true, Root.class); + } +}