From romain at r-enthusiasts.com  Fri May 13 08:30:06 2011
From: romain at r-enthusiasts.com (Romain Francois)
Date: Fri, 13 May 2011 08:30:06 +0200
Subject: [Rd] Recursively parsing srcrefs
In-Reply-To: <BANLkTimkrQ3AXViGgnxB6hgVN90abXcL5Q@mail.gmail.com>
References: <BANLkTikpgh27=EJGnmfrmuQ7QBh1vV-Qkw@mail.gmail.com>	<4DCC28F6.2090502@gmail.com>
	<BANLkTimkrQ3AXViGgnxB6hgVN90abXcL5Q@mail.gmail.com>
Message-ID: <4DCCCFEE.7050506@r-enthusiasts.com>

Le 12/05/11 21:59, Hadley Wickham a ?crit :
>
>>> Is it possible to "recursively" parse srcrefs to match the recursive
>>> structure of the underlying code?  I'm interested in this because it's
>>
>> I don't understand what you mean by that.  It is certainly possible to walk
>> through nested srcrefs, to zoom in on a particular location; that's what
>> findLineNum() does.
>
> Does the example below not help?  Given the whole function, I want to
> be able to walk down the call tree, finding the matching src refs as I
> go.  i.e. given f, how do I get f_inside?
>
> f<- function(x = T) {
>    # This is a comment
>    if (x)                  return(4)
>    if (emergency_status()) return(T)
> }
>
> f_inside<- parse(text = "
>    # This is a comment
>    if (x)                  return(4)
>    if (emergency_status()) return(T)
> ")
>
> findLineNum doesn't quite do what I want - it works on the text of the
> srcref, not on the parse tree.
>
> Here's another go at explaining what I want:
>
> h<- quote(
>    1 # one
>    + # plus
>    2 # two
> )
>
> h[[1]] extracts +.  What can I do to extract "+ # plus" (on an object
> created in the appropriate manner to keep the srcref)?  Is that even
> possible?

Maybe the parser package can help you.

 > x <- parser( "/tmp/test.R" )
 > attr( x, "data" )
    line1 col1 byte1 line2 col2 byte2 token id parent top_level 
   token.desc terminal   text
1      1    0     0     1    1     1   263  1      3         0 
      SYMBOL     TRUE      h
2      1    1     1     1    3     3   265  2     32         0 
LEFT_ASSIGN     TRUE     <-
3      1    0     0     1    1     1    77  3     32         0 
        expr    FALSE
4      1    4     4     1    9     9   296  5      7         0 
SYMBOL_FUNCTION_CALL     TRUE  quote
5      1    9     9     1   10    10    40  6     30         0 
         '('     TRUE      (
6      1    4     4     1    9     9    77  7     30         0 
        expr    FALSE
7      2    3     3     2    4     4   261 10     11         0 
   NUM_CONST     TRUE      1
8      2    3     3     2    4     4    77 11     27         0 
        expr    FALSE
9      2    5     5     2   10    10   289 13     27         0 
     COMMENT     TRUE  # one
10     3    3     3     3    4     4    43 16     27         0 
         '+'     TRUE      +
11     3    5     5     3   11    11   289 18     27         0 
     COMMENT     TRUE # plus
12     4    3     3     4    4     4   261 21     22         0 
   NUM_CONST     TRUE      2
13     4    3     3     4    4     4    77 22     27         0 
        expr    FALSE
14     4    5     5     4   10    10   289 24     30         0 
     COMMENT     TRUE  # two
15     5    0     0     5    1     1    41 26     30         0 
         ')'     TRUE      )
16     2    3     3     4    4     4    77 27     30         0 
        expr    FALSE
17     1    4     4     5    1     1    77 30     32         0 
        expr    FALSE
18     1    0     0     5    1     1    77 32      0         0 
        expr    FALSE

Romain

> My eventual goal is something like
>
> f<- function(x) {
>    # This is my function
>    T
> }
>
> g<- fix_logical_abbreviations(f)
>
> which would be equivalent to
>
> g<- function(x) {
>    # This is my function
>    TRUE
> }
>
>
>> That last display looks like a bug indeed.  I'll take a look.
>
> The key seems to be a leading newline:
>
> parse(text = "\nx")
> parse(text = "x")
>
> Hadley
>
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
http://romain-francois.com
|- http://bit.ly/hdKhCy : Rcpp article in JSS
|- http://bit.ly/elZJRJ : Montpellier Comedie Club - Avril 2011
`- http://bit.ly/fhqbRC : Rcpp workshop in Chicago on April 28th



[Rd] Recursively parsing srcrefs
Romain Francois