From radford at cs.toronto.edu  Tue Jan  5 13:00:38 2016
From: radford at cs.toronto.edu (Radford Neal)
Date: Tue, 5 Jan 2016 07:00:38 -0500
Subject: [Rd] deparse with parentheses for SUBSET
In-Reply-To: <mailman.23.1451991604.1814.r-devel@r-project.org>
References: <mailman.23.1451991604.1814.r-devel@r-project.org>
Message-ID: <20160105120038.GA31897@cs.toronto.edu>

> 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
      below:
      
        > p<-parse()
        ?"abc;xyz"
        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
      R-3.2.2):
      
       > parse()
       ?'\12a\x.'
       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).



[Rd] deparse with parentheses for SUBSET
Radford Neal