Skip to content

parse_all() fails with line directives #114

@ArcadeAntics

Description

@ArcadeAntics

I had a piece of code from a file, something like:

#line 11
LINENO()

where LINENO is a function I made that returns the line number of the executing expression in the executing script, and #line 11 is a line directive to indicate that the text is originally written starting on line 11 of the script. It erroneously returns something like this:

> evaluate:::parse_all(c("#line 11", "   LINENO()"))
             src     expr
1     #line 11\n         
2     LINENO()\n         
3           NA\n         
4           NA\n         
5           NA\n         
6           NA\n         
7           NA\n         
8           NA\n         
9           NA\n         
10          NA\n         
11            NA LINENO()
> 

I believe this is because evaluate:::parse_all.character does something like this:

    srcref <- attr(exprs, "srcref", exact = TRUE)
    pos <- do.call(rbind, lapply(srcref, unclass))[, c(1, 3), 
        drop = FALSE]

using the first_line and last_line of the srcref (which respects line directives) instead of the first_parsed and last_parsed (which just count lines). I think this error is easily solved by changing c(1, 3) to c(7, 8):

> x <- c("#line 11", "LINENO()")
> parse_all.character2 <- evaluate:::parse_all.character
> body(parse_all.character2)[[c(12, 3, 4)]]
c(1, 3)
> body(parse_all.character2)[[c(12, 3, 4)]] <- quote(c(7, 8))
> evaluate:::parse_all.character(x)
          src     expr
1  #line 11\n         
2  LINENO()\n         
3        NA\n         
4        NA\n         
5        NA\n         
6        NA\n         
7        NA\n         
8        NA\n         
9        NA\n         
10       NA\n         
11         NA LINENO()
>            parse_all.character2(x)
         src     expr
1 #line 11\n         
2   LINENO() LINENO()
> 

I took these details from the doc page ?srcfile. If you need any more details, I'm happy to oblige. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions