From f27833a9efae2df06e96b525c6cdf40b34becaae Mon Sep 17 00:00:00 2001 From: Tomas Stupka <tomas.stupka@oracle.com> Date: Tue, 31 Jan 2017 13:20:46 +0100 Subject: [PATCH] deparse adds too many right parentheses when structure expressions has more attributes --- .../oracle/truffle/r/runtime/RDeparse.java | 79 +++++++++---------- .../r/test/builtins/TestBuiltin_deparse.java | 9 ++- 2 files changed, 47 insertions(+), 41 deletions(-) diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java index 426f5b569e..9c8ae0753b 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java @@ -904,50 +904,49 @@ public class RDeparse { append("structure("); return () -> { DynamicObject attrs = ((RAttributable) obj).getAttributes(); - if (attrs != null) { - Iterator<RAttributesLayout.RAttribute> iter = RAttributesLayout.asIterable(attrs).iterator(); - while (iter.hasNext()) { - RAttributesLayout.RAttribute attr = iter.next(); - // TODO ignore function source attribute - String attrName = attr.getName(); - append(", "); - String dotName = null; - switch (attrName) { - case "dimnames": - dotName = ".Dimnames"; - break; - case "dim": - dotName = ".Dim"; - break; - case "names": - dotName = ".Names"; - break; - case "tsp": - dotName = ".Tsp"; - break; - case "levels": - dotName = ".Label"; - break; - - default: { - opts = SIMPLEDEPARSE; - if (attrName.contains(" ")) { - append('"'); - append(attrName); - append('"'); - } else { - append(attrName); - } + assert attrs != null; + Iterator<RAttributesLayout.RAttribute> iter = RAttributesLayout.asIterable(attrs).iterator(); + while (iter.hasNext()) { + RAttributesLayout.RAttribute attr = iter.next(); + // TODO ignore function source attribute + String attrName = attr.getName(); + append(", "); + String dotName = null; + switch (attrName) { + case "dimnames": + dotName = ".Dimnames"; + break; + case "dim": + dotName = ".Dim"; + break; + case "names": + dotName = ".Names"; + break; + case "tsp": + dotName = ".Tsp"; + break; + case "levels": + dotName = ".Label"; + break; + + default: { + opts = SIMPLEDEPARSE; + if (attrName.contains(" ")) { + append('"'); + append(attrName); + append('"'); + } else { + append(attrName); } } - if (dotName != null) { - append(dotName); - } - append(" = "); - appendValue(attr.getValue()); - append(')'); } + if (dotName != null) { + append(dotName); + } + append(" = "); + appendValue(attr.getValue()); } + append(')'); }; } else { return () -> { diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_deparse.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_deparse.java index b1d19e945d..70eb09d783 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_deparse.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_deparse.java @@ -4,7 +4,7 @@ * http://www.gnu.org/licenses/gpl-2.0.html * * Copyright (c) 2012-2014, Purdue University - * Copyright (c) 2013, 2016, Oracle and/or its affiliates + * Copyright (c) 2013, 2017, Oracle and/or its affiliates * * All rights reserved. */ @@ -276,6 +276,13 @@ public class TestBuiltin_deparse extends TestBase { "-0.0000000000000001", "-1.545234523452345252523452345", "-Inf", "c(1L,2L,3L)", "c(1,2,3)", "c(NA_integer_, 1L,2L,3L)", "c(1L,2L,3L, NA_integer_)", "c(3L,2L,1L)", "c(-2L,-1L,0L,1L)"}; + @Test + public void testDeparseStructure() { + assertEval("{ deparse(structure(.Data=c(1,2))) }"); + assertEval("{ deparse(structure(.Data=c(1,2), attr1=c(1))) }"); + assertEval("{ deparse(structure(.Data=c(1,2), attr1=c(1), attr2=c(2))) }"); + } + @Test public void testDeparse() { assertEval(template("deparse(%0)", VALUES)); -- GitLab