[Rd] deparse with parentheses for SUBSET

Radford Neal radford at cs.toronto.edu
Tue Jan 5 13:00:38 CET 2016

> maybe there?s a reason for it, but the discrepancy between the
> handling of `[` and `$` in deparsing seems odd to me:
> > substitute(a[1], list(a = quote(x * y)))
> x * y[1]
> > substitute(a$b, list(a = quote(x * y)))
> (x * y)$b
> The former is still executed in the right order (`*` first, then
> `[`), which is not what you?d expect looking at the deparse result.

This is one of a large number of bugs in the deparser and parser that
are fixed in the latest version of pqR (of 2015-09-14), as a result of
a complete rewrite of the parser and extensive revisions to the
deparser.  As I communicated to this list, it would be reasonably easy
to incorporate the rewritten parser and deparser into R Core versions
of R.  You can get more details from http://www.pqr-project.org/NEWS.txt

The portion of the bugs section in that NEWS file relevant to parsing
and deparsing is shown below:

    o The new parser fixes bugs arising from the old parser's kludge to
      handle semicolons, illustrated by the incorrect output seen
        > p<-parse()
        Error in parse() : <stdin>:1:1: unexpected INCOMPLETE_STRING
        1: "abc;
        > p<-parse()
        ?8 #abc;xyz
        Error in parse() : <stdin>:1:7: unexpected end of input
        1: 8 #abc;

    o Fixed deparsing of complex numbers, which were always deparsed as
      the sum of a real and an imaginary part, even though the parser
      can only produce complex numbers that are pure imaginary.  For
      example, the following output was produced before:
        > deparse(quote(3*5i))
        [1] "3 * (0+5i)"

      This is now deparsed to "3 * 5i".  This bug exists in all R Core
      versions through at least R-3.2.2.

    o Fixed a number of bugs in the deparser that are illustrated by
      the following, which produce incorrect output as noted, in R Core
      versions through at least R-3.2.2:
        deparse(parse(text="`+`(a,b)[1]")[[1]])# Omits necessary parens
        deparse(quote(`[<-`(x,1)),control="S_compatible")  # unmatched " and '
        deparse(parse(text="a = b <- c")[[1]]) # Puts in unnecessary parens
        deparse(parse(text="a+!b")[[1]])       # Puts in unnecessary parens
        deparse(parse(text="?lm")[[1]])        # Doesn't know about ? operator
        deparse(parse(text="a:=b")[[1]])       # Doesn't know about := operator
        deparse(parse(text="a$'x'")[[1]])      # Conflates name and character
        deparse(parse(text="`*`(2)")[[1]])     # Result is syntactically invalid
        deparse(parse(text="`$`(a,b+2)")[[1]]) # Result is syntactically invalid
        e<-quote(if(x) X else Y); e[[3]]<-quote(if(T)3); deparse(e)# all here 
        e <- quote(f(x)); e[[2]] <- quote((a=1))[[2]]; deparse(e)  # and below 
        e <- quote(f(Q=x)); e[[2]] <- quote((a=1))[[2]]; deparse(e)# need parens
        e <- quote(while(x) 1); e[[2]] <- quote((a=1))[[2]]; deparse(e)
        e <- quote(if(x) 1 else 2); e[[2]] <- quote((a=1))[[2]]; deparse(e)
        e <- quote(for(x in y) 1); e[[3]] <- quote((a=1))[[2]]; deparse(e)

      In addition, the bug illustrated below was fixed, which was fixed
      (differently) in R-3.0.0:
        a<-quote(f(1,2)); a[[1]]<-function(x,y)x+y; deparse(a)  # Omits parens

    o Fixed the following bug (also in R Core versions to at least
       > parse()
       Error: '\x' used without hex digits in character string starting "'\1a\x"

      Note that the "2" has disappeared from the error message.  This
      bug also affected the results of getParseData.

    o Fixed a memory leak that can be seen by running the code below:
        > long <- paste0 (c('"', rep("1234567890",820), '\x."'), collapse="")
        > for (i in 1:1000000) try (e <- parse(text=long), silent=TRUE)

      The leak will not occur if 820 is changed to 810 in the above.
      This bug also exists in R Core versions to at least R-3.2.2.

    o Entering a string constant containing Unicode escapes that was
      9999 or 10000 characters long would produce an error message
      saying "String is too long (max 10000 chars)".  This has been
      fixed so that the maximum now really is 10000 characters.  (Also
      present in R Core versions, to at least R-3.2.2.)

    o Fixed a bug that caused the error caret in syntax error reports
      to be misplaced when more than one line of context was shown.
      This was supposedly fixed in R-3.0.2, but incorrectly, resulting
      in the error caret being misplaced when only one line of context
      is shown (in R Core versions to at least R-3.2.2).

More information about the R-devel mailing list