Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the C++ roxygen parser #295

Closed
kevinushey opened this issue Oct 13, 2014 · 4 comments
Closed

Use the C++ roxygen parser #295

kevinushey opened this issue Oct 13, 2014 · 4 comments

Comments

@kevinushey
Copy link
Contributor

@kevinushey kevinushey commented Oct 13, 2014

Wire it up where appropriate.

@hadley
Copy link
Member

@hadley hadley commented Oct 5, 2015

What needs to be done here?

@kevinushey
Copy link
Contributor Author

@kevinushey kevinushey commented Oct 6, 2015

The C++ parser introduced two main entry points, roxygen2:::preparse_file(<file>) and roxygen2:::preparse_block(<block>). The goal was for this to be a (near) drop-in replacement for parse.preref, so that those functions could potentially be used in that situation.

E.g.

lines <- "#' @param foo A parameter.\n#' @param bar Another parameter.\n"
roxygen2:::parse.preref(lines)
roxygen2:::preparse_block(lines)

Comparing output,

> lines <- "#' @param foo A parameter.\n#' @param bar Another parameter.\n"
> roxygen2:::parse.preref(lines)
$param
$param$name
[1] "foo"

$param$description
[1] "A parameter.\n#' "


$param
$param$name
[1] "bar"

$param$description
[1] "Another parameter."


> roxygen2:::preparse_block(lines)
$param
[1] "foo A parameter."
attr(,"file")
[1] ""
attr(,"row")
[1] 1

$param
[1] "bar Another parameter."
attr(,"file")
[1] ""
attr(,"row")
[1] 2

[[3]]
[1] ""
attr(,"file")
[1] ""
attr(,"row")
[1] 2

attr(,"delimiter")
[1] "#' "

I think early on you indicated that you wanted the preparse_block to do as little as possible, hence the contents of the @param tag aren't actually split up. That said, I'm open to amending the implementation of the preparse functions to generate output that's useful.

@hadley
Copy link
Member

@hadley hadley commented Oct 6, 2015

It seems to be a bit too accepting currently:

preparse_block("\n  # This file was generated by Rcpp::compileAttributes\n  # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393\n")
#> $introduction
#> [1] "# This file was generated by Rcpp::compileAttributes\n# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393\n"
#> attr(,"file")
#> [1] ""
#> attr(,"row")
#> [1] 1
#> 
#> attr(,"delimiter")
#> [1] "\n  "

This should return NULL because there's no roxygen there

@hadley
Copy link
Member

@hadley hadley commented Oct 6, 2015

Revised parse.file will need to look something like this:

parse_file <- function(file, env) {
  parsed <- parse(file = file, keep.source = TRUE)
  if (length(parsed) == 0) return()

  refs <- getSrcref(parsed)
  comment_refs <- comments(refs)

  extract <- function(call, ref, comment_ref) {
    comment_ref2 <- list(filename = file, lloc = as.vector(comment_ref))

    block <- preparse_block(paste(as.character(comment_ref), collapse = ""))
    if (is.null(block)) return()

    preref <- parse_elements(block)
    preref$object <- object_from_call(call, env, preref)
    preref$srcref <- list(filename = file, lloc = as.vector(ref))
    add_defaults(preref)
  }

  Map(extract, parsed, refs, comment_refs)
}

Would be nice if preparse_block() could take a srcref directly.

@hadley hadley closed this in 36a726f Oct 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants