Duncan Murdoch wrote:
On 12/02/2009 7:01 AM, Romain Francois wrote:
Hello,
Consider this file (/tmp/test.R) :
<file>
f <- function( x, y = 2 ){
z <- x + y
print( z )
}
</file>
I get this in R 2.7.2 :
> p <- parse( "/tmp/test.R" )
> str( attr( p, "srcref" ) )
List of 1
$ :Class 'srcref' atomic [1:4] 1 1 4 1
.. ..- attr(*, "srcfile")=Class 'srcfile' length 4 <environment>
and this in R-devel :
> p <- parse( "/tmp/test.R" )
> str( attr(p, "srcref") )
List of 1
$ :Class 'srcref' atomic [1:6] 1 1 4 1 1 1
.. ..- attr(*, "srcfile")=Class 'srcfile' <environment: 0x946b944>
What are the two last numbers ?
The original design for srcref gave 4 entries: start line, start byte,
stop line, stop byte. However, in multibyte strings, bytes don't
correspond to columns, so error messages could often report the wrong
location according to what a user sees in an editor. To support the
more useful error messages in R-devel, I added two more values: start
column and stop column. With pure ASCII text these will be the same
as start byte and stop byte; with UTF-8 text and non-ASCII characters
they will be be different. Other multibyte encodings are only
supported if the platform can convert them to UTF-8 (and are not well
tested; error reports would be welcome, if there's a way to improve
the performance.)
If you are using these for error reports, I recommend using the two
new values. If you are trying to retrieve the text from the source
file, use the originals.
Duncan Murdoch
Thank you Duncan,
I am using this to massage the output of "parse" into a data frame to
represent it as a tree
(see http://addictedtor.free.fr/misc/sidekick.png)
> cat( readLines( "/tmp/test.R" ), sep = "\n" )
f <- function( x, y = 2 ){
z <- x + y
g <- function( x ){
print( x )
xx <- x + 1
}
g( x )
}
>
> sidekick( "/tmp/test.R", encoding = "utf-8" )
id parent mode srcref1 srcref2 srcref3 srcref4
description
1 1 0 function 1 1 8 1 f <- function(x, y
= 2) {
2 2 1 name 1 26 1
26 {
3 3 1 call 2 2 2 11 z <-
x + y
4 4 1 function 3 2 6 2 g <-
function(x) {
5 5 1 call 7 2 7
7 g(x)
6 6 4 name 3 20 3
20 {
7 7 4 call 4 4 4 13
print(x)
8 8 4 call 5 4 5 14 xx <-
x + 1
--
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel