Adrian, Indeed, this has come up in a few places, but as Gabor says, there is no such thing as right hand assignment at any point after parsing is complete. This means the only feasible way to detect it, which a few projects do I believe, is process the code while it is still raw text, before it goes into the parser, and have clever enough regular expressions. The next question, then, is why are you trying to detect right assignment. Doing so can be arguably useful fo linting, its true. Otherwise, though, because its not really a "real thing" when the R code is being executed, its not something thats generally meaningful to detect in most cases. Best, ~G On Mon, Apr 13, 2020 at 12:52 AM G?bor Cs?rdi <csardi.gabor at gmail.com> wrote:> That parser already flips -> to <- before creating the parse tree. > > Gabor > > On Mon, Apr 13, 2020 at 8:39 AM Adrian Du?a <dusa.adrian at gmail.com> wrote: > > > > I searched and tried for hours, to no avail although it looks simple. > > > > (function(x) substitute(x))(A <- B) > > #A <- B > > > > (function(x) substitute(x))(A -> B) > > # B <- A > > > > In the first example, A occurs on the LHS, but in the second example A > is somehow evaluated as if it occured on the RHS, despite my understanding > that substitute() returns the unevaluated parse tree. > > > > Is there any way, or is it even possible to detect the right hand > assignment, to determine whether A occurs on the LHS? > > > > Thanks in advance for any hint, > > Adrian > > > > ? > > Adrian Dusa > > University of Bucharest > > Romanian Social Data Archive > > Soseaua Panduri nr. 90-92 > > 050663 Bucharest sector 5 > > Romania > > https://adriandusa.eu > > > > ______________________________________________ > > R-devel at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]
On Mon, Apr 13, 2020 at 9:23 AM Gabriel Becker <gabembecker at gmail.com> wrote: [...]> This means the only feasible way to detect it, which a few projects do I believe, is process the code while it is still raw text, before it goes into the parser, and have clever enough regular expressions.Well, especially considering R's news raw strings with user defined delimiters, regular expressions are not the best here, I think. OTOH the source references do keep the right assignment. So if you can re-parse the data, you can detect them: ? x <- parse(text = "A -> B", keep.source=FALSE) ? x expression(B <- A) ? x <- parse(text = "A -> B") ? x expression(A -> B) ? getParseData(x) line1 col1 line2 col2 id parent token terminal text 7 1 1 1 6 7 0 expr FALSE 1 1 1 1 1 1 3 SYMBOL TRUE A 3 1 1 1 1 3 7 expr FALSE 2 1 3 1 4 2 7 RIGHT_ASSIGN TRUE -> 4 1 6 1 6 4 6 SYMBOL TRUE B 6 1 6 1 6 6 7 expr FALSE Gabor [...]
Thank you for your replies, this actually has little to do with the regular R code but more to signal what in my package QCA is referred to as a necessity relation A <- B (A is necessary for B) and sufficiency A -> B (A is sufficient for B). If switched by the parser, A -> B becomes B <- A which makes B necessary for A, while the intention is to signal sufficiency for B. Capturing in a quoted string is trivial, but I am now experimenting with substitute() to allow unquoted expressions. This is especially useful when selecting A and B from the columns of a data frame, using: c(A, B) instead of c("A", "B") with a lot more quotes for more complex expressions using more columns. I would be grateful for any pointer to a project that processes the code while it is still raw text. I could maybe learn from their code and adapt to my use case. Best wishes, Adrian> On 13 Apr 2020, at 11:23, Gabriel Becker <gabembecker at gmail.com> wrote: > > Adrian, > > Indeed, this has come up in a few places, but as Gabor says, there is no such thing as right hand assignment at any point after parsing is complete. > > This means the only feasible way to detect it, which a few projects do I believe, is process the code while it is still raw text, before it goes into the parser, and have clever enough regular expressions. > > The next question, then, is why are you trying to detect right assignment. Doing so can be arguably useful fo linting, its true. Otherwise, though, because its not really a "real thing" when the R code is being executed, its not something thats generally meaningful to detect in most cases. > > Best, > ~G > > On Mon, Apr 13, 2020 at 12:52 AM G?bor Cs?rdi <csardi.gabor at gmail.com> wrote: > That parser already flips -> to <- before creating the parse tree. > > Gabor > > On Mon, Apr 13, 2020 at 8:39 AM Adrian Du?a <dusa.adrian at gmail.com> wrote: > > > > I searched and tried for hours, to no avail although it looks simple. > > > > (function(x) substitute(x))(A <- B) > > #A <- B > > > > (function(x) substitute(x))(A -> B) > > # B <- A > > > > In the first example, A occurs on the LHS, but in the second example A is somehow evaluated as if it occured on the RHS, despite my understanding that substitute() returns the unevaluated parse tree. > > > > Is there any way, or is it even possible to detect the right hand assignment, to determine whether A occurs on the LHS? > > > > Thanks in advance for any hint, > > Adrian > > > > ? > > Adrian Dusa > > University of Bucharest > > Romanian Social Data Archive > > Soseaua Panduri nr. 90-92 > > 050663 Bucharest sector 5 > > Romania > > https://adriandusa.eu > > > > ______________________________________________ > > R-devel at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel? Adrian Dusa University of Bucharest Romanian Social Data Archive Soseaua Panduri nr. 90-92 050663 Bucharest sector 5 Romania https://adriandusa.eu
Using => and <= instead of -> and <- would make things easier, although the precedence would be different. Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Apr 13, 2020 at 1:43 AM Adrian Du?a <dusa.adrian at gmail.com> wrote:> Thank you for your replies, this actually has little to do with the > regular R code but more to signal what in my package QCA is referred to as > a necessity relation A <- B (A is necessary for B) and sufficiency A -> B > (A is sufficient for B). > > If switched by the parser, A -> B becomes B <- A which makes B necessary > for A, while the intention is to signal sufficiency for B. > > Capturing in a quoted string is trivial, but I am now experimenting with > substitute() to allow unquoted expressions. > > This is especially useful when selecting A and B from the columns of a > data frame, using: c(A, B) instead of c("A", "B") with a lot more quotes > for more complex expressions using more columns. > > I would be grateful for any pointer to a project that processes the code > while it is still raw text. I could maybe learn from their code and adapt > to my use case. > > Best wishes, > Adrian > > > On 13 Apr 2020, at 11:23, Gabriel Becker <gabembecker at gmail.com> wrote: > > > > Adrian, > > > > Indeed, this has come up in a few places, but as Gabor says, there is no > such thing as right hand assignment at any point after parsing is complete. > > > > This means the only feasible way to detect it, which a few projects do I > believe, is process the code while it is still raw text, before it goes > into the parser, and have clever enough regular expressions. > > > > The next question, then, is why are you trying to detect right > assignment. Doing so can be arguably useful fo linting, its true. > Otherwise, though, because its not really a "real thing" when the R code is > being executed, its not something thats generally meaningful to detect in > most cases. > > > > Best, > > ~G > > > > On Mon, Apr 13, 2020 at 12:52 AM G?bor Cs?rdi <csardi.gabor at gmail.com> > wrote: > > That parser already flips -> to <- before creating the parse tree. > > > > Gabor > > > > On Mon, Apr 13, 2020 at 8:39 AM Adrian Du?a <dusa.adrian at gmail.com> > wrote: > > > > > > I searched and tried for hours, to no avail although it looks simple. > > > > > > (function(x) substitute(x))(A <- B) > > > #A <- B > > > > > > (function(x) substitute(x))(A -> B) > > > # B <- A > > > > > > In the first example, A occurs on the LHS, but in the second example A > is somehow evaluated as if it occured on the RHS, despite my understanding > that substitute() returns the unevaluated parse tree. > > > > > > Is there any way, or is it even possible to detect the right hand > assignment, to determine whether A occurs on the LHS? > > > > > > Thanks in advance for any hint, > > > Adrian > > > > > > ? > > > Adrian Dusa > > > University of Bucharest > > > Romanian Social Data Archive > > > Soseaua Panduri nr. 90-92 > > > 050663 Bucharest sector 5 > > > Romania > > > https://adriandusa.eu > > > > > > ______________________________________________ > > > R-devel at r-project.org mailing list > > > https://stat.ethz.ch/mailman/listinfo/r-devel > > > > ______________________________________________ > > R-devel at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > > ? > Adrian Dusa > University of Bucharest > Romanian Social Data Archive > Soseaua Panduri nr. 90-92 > 050663 Bucharest sector 5 > Romania > https://adriandusa.eu > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]