1- # ' Check whether a parse table corresponds to a certain expression
1+ # ' What is a parse table representing?
22# '
3- # ' @param pd A parse table.
3+ # ' Check whether a parse table corresponds to a certain expression .
44# ' @name pd_is
5+ # '
6+ # ' @param pd A parse table.
7+ # ' @param tilde_pos Integer vector indicating row-indices that should be
8+ # ' checked for tilde. See 'Details'.
9+ # '
10+ # ' @family third-party style guide helpers
511# ' @keywords internal
612NULL
713
8- # ' @describeIn pd_is Checks whether `pd` contains an expression wrapped in
9- # ' curly brackets.
10- # ' @keywords internal
14+ # ' @describeIn pd_is Checks whether `pd` contains an expression wrapped in curly brackets.
15+ # ' @examples
16+ # ' code <- "if (TRUE) { 1 }"
17+ # ' pd <- compute_parse_data_nested(code)
18+ # ' is_curly_expr(pd)
19+ # ' child_of_child <- pd$child[[1]]$child[[5]]
20+ # ' is_curly_expr(child_of_child)
21+ # '
22+ # ' @export
1123is_curly_expr <- function (pd ) {
1224 if (is.null(pd )) {
1325 return (FALSE )
1426 }
1527 pd $ token [1L ] == " '{'"
1628}
1729
30+ # ' @describeIn pd_is Checks whether `pd` contains a `for` loop.
31+ # ' @examples
32+ # ' code <- "for (i in 1:5) print(1:i)"
33+ # ' pd <- compute_parse_data_nested(code)
34+ # ' is_for_expr(pd)
35+ # ' is_for_expr(pd$child[[1]])
36+ # '
37+ # ' @export
1838is_for_expr <- function (pd ) {
1939 pd $ token [1L ] == " FOR"
2040}
2141
2242# ' @describeIn pd_is Checks whether `pd` contains is a conditional expression.
23- # ' @keywords internal
24- is_cond_expr <- function (pd ) {
43+ # ' @examples
44+ # ' code <- "if (TRUE) x <- 1 else x <- 0"
45+ # ' pd <- compute_parse_data_nested(code)
46+ # ' is_conditional_expr(pd)
47+ # ' is_conditional_expr(pd$child[[1]])
48+ # '
49+ # ' @export
50+ is_conditional_expr <- function (pd ) {
2551 pd $ token [1L ] == " IF"
2652}
2753
28- # ' @describeIn pd_is Checks whether `pd` contains is a while loop.
29- # ' @keywords internal
54+ # ' @describeIn pd_is Checks whether `pd` contains a ` while` loop.
55+ # ' @export
3056is_while_expr <- function (pd ) {
3157 pd $ token [1L ] == " WHILE"
3258}
3359
60+
3461# ' @describeIn pd_is Checks whether `pd` is a function call.
35- # ' @keywords internal
62+ # ' @examples
63+ # ' code <- "x <- list(1:3)"
64+ # ' pd <- compute_parse_data_nested(code)
65+ # ' is_function_call(pd)
66+ # ' child_of_child <- pd$child[[1]]$child[[3]]
67+ # ' is_function_call(child_of_child)
68+ # '
69+ # ' @export
3670is_function_call <- function (pd ) {
3771 if (is.null(pd )) {
3872 return (FALSE )
@@ -44,56 +78,69 @@ is_function_call <- function(pd) {
4478}
4579
4680# ' @describeIn pd_is Checks whether `pd` is a function declaration.
47- # ' @keywords internal
48- is_function_dec <- function (pd ) {
81+ # ' @examples
82+ # ' code <- "foo <- function() NULL"
83+ # ' pd <- compute_parse_data_nested(code)
84+ # ' is_function_declaration(pd)
85+ # ' child_of_child <- pd$child[[1]]$child[[3]]
86+ # ' is_function_declaration(child_of_child)
87+ # '
88+ # ' @export
89+ is_function_declaration <- function (pd ) {
4990 if (is.null(pd )) {
5091 return (FALSE )
5192 }
5293 pd $ token [1L ] == " FUNCTION"
5394}
5495
5596# ' @describeIn pd_is Checks for every token whether or not it is a comment.
56- # ' @keywords internal
97+ # ' @examples
98+ # ' code <- "x <- 1 # TODO: check value"
99+ # ' pd <- compute_parse_data_nested(code)
100+ # ' is_comment(pd)
101+ # '
102+ # ' @export
57103is_comment <- function (pd ) {
58104 if (is.null(pd )) {
59105 return (FALSE )
60106 }
61107 pd $ token == " COMMENT"
62108}
63109
64-
65-
66- # ' Check whether a parse table contains a tilde
67- # '
68- # '
69- # ' @param pd A parse table.
70- # ' @param tilde_pos Integer vector indicating row-indices that should be
71- # ' checked for tilde. See 'Details'.
72- # '
110+ # ' @describeIn pd_is Checks whether `pd` contains a tilde.
73111# ' @details
74112# ' A tilde is on the top row in the parse table if it is an asymmetric tilde
75113# ' expression (like `~column`), in the second row if it is a symmetric tilde
76114# ' expression (like `a~b`).
77- # ' @keywords internal
115+ # ' @examples
116+ # ' code <- "lm(wt ~ mpg, mtcars)"
117+ # ' pd <- compute_parse_data_nested(code)
118+ # ' is_tilde_expr(pd$child[[1]]$child[[3]])
119+ # ' is_symmetric_tilde_expr(pd$child[[1]]$child[[3]])
120+ # ' is_asymmetric_tilde_expr(pd$child[[1]]$child[[3]])
121+ # '
122+ # ' @export
78123is_tilde_expr <- function (pd , tilde_pos = c(1L , 2L )) {
79124 if (is.null(pd ) || nrow(pd ) == 1L ) {
80125 return (FALSE )
81126 }
82127 any(pd $ token [tilde_pos ] == " '~'" )
83128}
84129
85- # ' @rdname is_tilde_expr
130+ # ' @describeIn pd_is If `pd` contains a tilde, checks whether it is asymmetrical.
131+ # ' @export
86132is_asymmetric_tilde_expr <- function (pd ) {
87133 is_tilde_expr(pd , tilde_pos = 1L )
88134}
89135
90- # ' @rdname is_tilde_expr
136+ # ' @describeIn pd_is If `pd` contains a tilde, checks whether it is symmetrical.
137+ # ' @export
91138is_symmetric_tilde_expr <- function (pd ) {
92139 is_tilde_expr(pd , tilde_pos = 2L )
93140}
94141
95142is_subset_expr <- function (pd ) {
96- if (is.null(pd ) || nrow(pd ) == 1 ) {
143+ if (is.null(pd ) || nrow(pd ) == 1L ) {
97144 return (FALSE )
98145 }
99146 pd $ token [2L ] %in% subset_token_opening
@@ -152,7 +199,7 @@ contains_else_expr_that_needs_braces <- function(pd) {
152199 non_comment_after_else <- next_non_comment(pd , else_idx )
153200 sub_expr <- pd $ child [[non_comment_after_else ]]
154201 # needs braces if NOT if_condition, NOT curly expr
155- ! is_cond_expr (sub_expr ) && ! is_curly_expr(sub_expr )
202+ ! is_conditional_expr (sub_expr ) && ! is_curly_expr(sub_expr )
156203 } else {
157204 FALSE
158205 }
0 commit comments