-
Notifications
You must be signed in to change notification settings - Fork 1
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
Parse attribute-style comments from source code #20
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a964ec5
Add some standard utilities
richfitz 840e3c1
Add helpers for reading parameters
richfitz 0d32776
Use helpers for data too
richfitz 2912362
Add basic metadata parsing
richfitz a3b54fc
Test metadata parsing
richfitz 2617071
Also need withr in tests
richfitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
## Each model will be marked up with decor-style comments, this allows | ||
## us to describe a model without needing to try and parse anything, | ||
## which is fraught at best with C++. | ||
## | ||
## The main things we need to know are: | ||
## | ||
## * what is the name of the class we are wrapping? | ||
## * what should the name be of the model exported to R (if different) | ||
## * what parameters does it accept (optional?) | ||
## * does the model support comparison with data? | ||
## * what data entries does it accept (optional?) | ||
## | ||
## Later, we'll want to do the same thing with GPU support if that | ||
## looks different, and possibly with MPI support, though with those | ||
## it might depend a bit on if we actually need much special there - | ||
## it might be worth looking at the GPU stuff again fairly soon | ||
## actually. | ||
parse_metadata <- function(filename, call = NULL) { | ||
data <- decor::cpp_decorations(files = filename) | ||
|
||
class <- parse_metadata_class(data, call) | ||
list(class = class, | ||
name = parse_metadata_name(data, call) %||% class, | ||
has_compare = parse_metadata_has_compare(data, call), | ||
parameters = parse_metadata_parameters(data, call)) | ||
} | ||
|
||
|
||
## All of the errors here could benefit from line number and context | ||
## information, but it's not that important as users should never see | ||
## these errors - models will mostly be written by odin | ||
parse_metadata_class <- function(data, call = NULL) { | ||
data <- find_attribute_value_single(data, "dust2::class", required = TRUE, | ||
call = call) | ||
if (length(data) != 1 || nzchar(names(data))) { | ||
cli::cli_abort( | ||
"Expected a single unnamed argument to '[[dust2::class()]]'", | ||
call = call) | ||
} | ||
if (!is.symbol(data[[1]])) { | ||
cli::cli_abort( | ||
"Expected an unquoted string argument to '[[dust2::class()]]'", | ||
call = call) | ||
} | ||
deparse(data[[1]]) | ||
} | ||
|
||
|
||
parse_metadata_name <- function(data, call = NULL) { | ||
data <- find_attribute_value_single(data, "dust2::name", required = FALSE, | ||
call = call) | ||
if (is.null(data)) { | ||
return(NULL) | ||
} | ||
if (length(data) != 1 || nzchar(names(data))) { | ||
cli::cli_abort( | ||
"Expected a single unnamed argument to '[[dust2::name()]]'", | ||
call = call) | ||
} | ||
if (!is.symbol(data[[1]])) { | ||
cli::cli_abort( | ||
"Expected an unquoted string argument to '[[dust2::name()]]'", | ||
call = call) | ||
} | ||
deparse(data[[1]]) | ||
} | ||
|
||
|
||
parse_metadata_has_compare <- function(data, call = NULL) { | ||
data <- find_attribute_value_single(data, "dust2::has_compare", | ||
required = FALSE, call = call) | ||
if (is.null(data)) { | ||
return(FALSE) | ||
} | ||
if (length(data) != 0) { | ||
cli::cli_abort( | ||
"Expected no arguments to '[[dust2::has_compare()]]'", | ||
call = call) | ||
} | ||
TRUE | ||
} | ||
|
||
|
||
parse_metadata_parameters <- function(data, call = NULL) { | ||
res <- data$params[data$decoration == "dust2::parameter"] | ||
ok <- vlapply(res, function(x) { | ||
length(x) == 1 && !nzchar(names(x)[[1]]) && is.symbol(x[[1]]) | ||
}) | ||
if (!all(ok)) { | ||
cli::cli_abort( | ||
paste("Expected an unnamed unquoted string argument to", | ||
"'[[dust2::parameter()]]'"), | ||
call = call) | ||
} | ||
data_frame(name = vcapply(res, function(x) deparse(x[[1]]))) | ||
} | ||
|
||
|
||
find_attribute_value_single <- function(data, name, required, call = NULL) { | ||
i <- data$decoration == name | ||
if (!any(i)) { | ||
if (required) { | ||
cli::cli_abort( | ||
"Attribute '[[{name}()]]' is required, but was not found", | ||
call = call) | ||
} | ||
return(NULL) | ||
} | ||
|
||
if (sum(i) > 1) { | ||
cli::cli_abort( | ||
"More than one '[[{name}()]]' attribute found", | ||
call = call) | ||
} | ||
|
||
data$params[[which(i)]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That
mustWork
arg name always amuses me. I might write some argsnot_that_bothered_if_it_works
in some of my functions.