-
Notifications
You must be signed in to change notification settings - Fork 73
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
Expose internals used with other style guides #1043
Changes from all commits
59958a1
880a145
fce1680
5bfd6db
8544a3c
d5d2546
24684f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,72 @@ | ||
#' Check whether a parse table corresponds to a certain expression | ||
#' What is a parse table representing? | ||
#' | ||
#' @param pd A parse table. | ||
#' Check whether a parse table corresponds to a certain expression. | ||
#' @name pd_is | ||
#' | ||
#' @param pd A parse table. | ||
#' @param tilde_pos Integer vector indicating row-indices that should be | ||
#' checked for tilde. See 'Details'. | ||
#' | ||
#' @family third-party style guide helpers | ||
#' @keywords internal | ||
NULL | ||
|
||
#' @describeIn pd_is Checks whether `pd` contains an expression wrapped in | ||
#' curly brackets. | ||
#' @keywords internal | ||
#' @describeIn pd_is Checks whether `pd` contains an expression wrapped in curly brackets. | ||
#' @examples | ||
#' code <- "if (TRUE) { 1 }" | ||
#' pd <- compute_parse_data_nested(code) | ||
#' is_curly_expr(pd) | ||
#' child_of_child <- pd$child[[1]]$child[[5]] | ||
#' is_curly_expr(child_of_child) | ||
#' | ||
#' @export | ||
is_curly_expr <- function(pd) { | ||
if (is.null(pd)) { | ||
return(FALSE) | ||
} | ||
pd$token[1L] == "'{'" | ||
} | ||
|
||
#' @describeIn pd_is Checks whether `pd` contains a `for` loop. | ||
#' @examples | ||
#' code <- "for (i in 1:5) print(1:i)" | ||
#' pd <- compute_parse_data_nested(code) | ||
#' is_for_expr(pd) | ||
#' is_for_expr(pd$child[[1]]) | ||
#' | ||
#' @export | ||
is_for_expr <- function(pd) { | ||
pd$token[1L] == "FOR" | ||
} | ||
|
||
#' @describeIn pd_is Checks whether `pd` contains is a conditional expression. | ||
#' @keywords internal | ||
is_cond_expr <- function(pd) { | ||
#' @examples | ||
#' code <- "if (TRUE) x <- 1 else x <- 0" | ||
#' pd <- compute_parse_data_nested(code) | ||
#' is_conditional_expr(pd) | ||
#' is_conditional_expr(pd$child[[1]]) | ||
#' | ||
#' @export | ||
is_conditional_expr <- function(pd) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a better name than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But why do we need to change all the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no sorry I read your comment wrongly (which is why I deleted it). I thought you wanted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Okay, then this should be ready for your review. I don't have any additional changes in mind. |
||
pd$token[1L] == "IF" | ||
} | ||
|
||
#' @describeIn pd_is Checks whether `pd` contains is a while loop. | ||
#' @keywords internal | ||
#' @describeIn pd_is Checks whether `pd` contains a `while` loop. | ||
#' @export | ||
is_while_expr <- function(pd) { | ||
pd$token[1L] == "WHILE" | ||
} | ||
|
||
|
||
#' @describeIn pd_is Checks whether `pd` is a function call. | ||
#' @keywords internal | ||
#' @examples | ||
#' code <- "x <- list(1:3)" | ||
#' pd <- compute_parse_data_nested(code) | ||
#' is_function_call(pd) | ||
#' child_of_child <- pd$child[[1]]$child[[3]] | ||
#' is_function_call(child_of_child) | ||
#' | ||
#' @export | ||
is_function_call <- function(pd) { | ||
if (is.null(pd)) { | ||
return(FALSE) | ||
|
@@ -44,56 +78,69 @@ is_function_call <- function(pd) { | |
} | ||
|
||
#' @describeIn pd_is Checks whether `pd` is a function declaration. | ||
#' @keywords internal | ||
is_function_dec <- function(pd) { | ||
#' @examples | ||
#' code <- "foo <- function() NULL" | ||
#' pd <- compute_parse_data_nested(code) | ||
#' is_function_declaration(pd) | ||
#' child_of_child <- pd$child[[1]]$child[[3]] | ||
#' is_function_declaration(child_of_child) | ||
#' | ||
#' @export | ||
is_function_declaration <- function(pd) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a better name than |
||
if (is.null(pd)) { | ||
return(FALSE) | ||
} | ||
pd$token[1L] == "FUNCTION" | ||
} | ||
|
||
#' @describeIn pd_is Checks for every token whether or not it is a comment. | ||
#' @keywords internal | ||
#' @examples | ||
#' code <- "x <- 1 # TODO: check value" | ||
#' pd <- compute_parse_data_nested(code) | ||
#' is_comment(pd) | ||
#' | ||
#' @export | ||
is_comment <- function(pd) { | ||
if (is.null(pd)) { | ||
return(FALSE) | ||
} | ||
pd$token == "COMMENT" | ||
} | ||
|
||
|
||
|
||
#' Check whether a parse table contains a tilde | ||
#' | ||
#' | ||
#' @param pd A parse table. | ||
#' @param tilde_pos Integer vector indicating row-indices that should be | ||
#' checked for tilde. See 'Details'. | ||
#' | ||
#' @describeIn pd_is Checks whether `pd` contains a tilde. | ||
#' @details | ||
#' A tilde is on the top row in the parse table if it is an asymmetric tilde | ||
#' expression (like `~column`), in the second row if it is a symmetric tilde | ||
#' expression (like `a~b`). | ||
#' @keywords internal | ||
#' @examples | ||
#' code <- "lm(wt ~ mpg, mtcars)" | ||
#' pd <- compute_parse_data_nested(code) | ||
#' is_tilde_expr(pd$child[[1]]$child[[3]]) | ||
#' is_symmetric_tilde_expr(pd$child[[1]]$child[[3]]) | ||
#' is_asymmetric_tilde_expr(pd$child[[1]]$child[[3]]) | ||
#' | ||
#' @export | ||
is_tilde_expr <- function(pd, tilde_pos = c(1L, 2L)) { | ||
if (is.null(pd) || nrow(pd) == 1L) { | ||
return(FALSE) | ||
} | ||
any(pd$token[tilde_pos] == "'~'") | ||
} | ||
|
||
#' @rdname is_tilde_expr | ||
#' @describeIn pd_is If `pd` contains a tilde, checks whether it is asymmetrical. | ||
#' @export | ||
is_asymmetric_tilde_expr <- function(pd) { | ||
is_tilde_expr(pd, tilde_pos = 1L) | ||
} | ||
|
||
#' @rdname is_tilde_expr | ||
#' @describeIn pd_is If `pd` contains a tilde, checks whether it is symmetrical. | ||
#' @export | ||
is_symmetric_tilde_expr <- function(pd) { | ||
is_tilde_expr(pd, tilde_pos = 2L) | ||
} | ||
|
||
is_subset_expr <- function(pd) { | ||
if (is.null(pd) || nrow(pd) == 1) { | ||
if (is.null(pd) || nrow(pd) == 1L) { | ||
return(FALSE) | ||
} | ||
pd$token[2L] %in% subset_token_opening | ||
|
@@ -152,7 +199,7 @@ contains_else_expr_that_needs_braces <- function(pd) { | |
non_comment_after_else <- next_non_comment(pd, else_idx) | ||
sub_expr <- pd$child[[non_comment_after_else]] | ||
# needs braces if NOT if_condition, NOT curly expr | ||
!is_cond_expr(sub_expr) && !is_curly_expr(sub_expr) | ||
!is_conditional_expr(sub_expr) && !is_curly_expr(sub_expr) | ||
} else { | ||
FALSE | ||
} | ||
|
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.
The blank lines are deliberate.
Without them, all examples are squished together like a centipede, and it's hard to tell where one ends and the other starts.