From murdoch@dunc@n @end|ng |rom gm@||@com  Fri Mar  1 12:00:42 2024
From: murdoch@dunc@n @end|ng |rom gm@||@com (Duncan Murdoch)
Date: Fri, 1 Mar 2024 06:00:42 -0500
Subject: [Rd] capture "->"
In-Reply-To: <CAJL_pogbYhhLxsXQN0jjm83sZybh1nO1wE6F+CwNV18F9EkYUA@mail.gmail.com>
References: <CAJL_pojF-Qc8YA0_eB+Ab9w3qD5sgMkjm0C3b0ooOmoOUy769w@mail.gmail.com>
 <ff87b48a-ccad-40ec-b79e-3fb4c945d3a9@gmail.com>
 <CAJL_pogbYhhLxsXQN0jjm83sZybh1nO1wE6F+CwNV18F9EkYUA@mail.gmail.com>
Message-ID: <79f6c330-e5bf-4ad4-9ccc-ed47ab8dcb69@gmail.com>

On 01/03/2024 5:25 a.m., Dmitri Popavenko wrote:
> Dear?Duncan,
> 
> On Fri, Mar 1, 2024 at 11:30?AM Duncan Murdoch <murdoch.duncan at gmail.com 
> <mailto:murdoch.duncan at gmail.com>> wrote:
> 
>     ...
>     If you parse it with srcrefs, you could look at the source.? The parser
>     doesn't record whether it was A -> B or B <- A anywhere else.
> 
> 
> Thank you, this gets me closer but it still needs a little push:
> 
>  > foo <- function(x) {
>  ? x <- substitute(x)
>  ? return(attr(x, "srcref")[[2]])
> }
> 
>  > foo(A -> B)
> NULL
> 
> This seems to work, however:
>  > foo({A -> B})
> A -> B
> 
> Is there a way to treat the formula as if it was enclosed between the 
> curly brackets?
> Dmitri

I was thinking more of you doing something like

  parse(text = "A -> B", keep.source = TRUE)

I forget what the exact rules are for attaching srcrefs to arguments of 
functions, but I do remember they are a little strange, because not 
every possible argument can accept a srcref attribute.  For example, you 
can't attach one to NULL, or to a name.

Srcrefs are also fairly big and building them is slow, so I think we 
tried to limit them to where they were needed, we didn't try to attach 
them to every subexpression, just one per statement.  Each expression 
within {} is a separate statement, so we get srcrefs attached to the {. 
But in "foo(A -> B)" probably you only get one on the foo call.

In some circumstances you could get the srcref on that call by looking 
at sys.call().  But then things are complicated again, because R doesn't 
attach srcrefs to things typed at the console, only to things that are 
sourced from files or text strings (and parsed with keep.source=TRUE).

So I think you should probably require input from a string or a file, or 
not expect foo(A -> B) to work without some decoration.

Duncan Murdoch



[Rd] capture "->"
Duncan Murdoch