-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_comments.r
259 lines (238 loc) · 6.88 KB
/
transform_comments.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# transform Google R style formatted function documentations to roxygen2 style format
# below is example output in roxygen2 format
#' Add together two numbers
#'
#' @param x A number
#' @param y A number
#' @return The sum of \code{x} and \code{y}
# below is example input in Google R style format
# Add together two numbers
#
# Args:
#. x: A number
#. y: A number
#
# Returns:
#. The sum of x and y
catnl <- function(line) {
# cat for input+newline
cat(paste(line, "\n", sep=""))
}
lineEmpty <- function(line) {
# return TRUE if line is empty after trimming
#
# Args:
# line: string
#
# Returns:
# TRUE if line is empty
if(trimws(line, which = "left")=="") {TRUE} else {FALSE}
}
lineStartsWith <- function(line, char) {
# return TRUE if line starts with char
#
# Args:
# line: string
# char: single character
#
# Returns:
# TRUE if line begins with char after trimming
stopifnot(nchar(char) == 1)
line <- trimws(line, which="left")
if(substr(line, 1, 1) == char) {TRUE} else {FALSE}
}
printLines <- function(lines) {
# print all lines in input
#
# Args:
# lines: list of strings
#
# Returns:
# TRUE
lapply(lines, catnl)
return(TRUE)
}
roxygenesis <- function(line, add=NULL) {
# Transform eg "# foo" to "#' foo"
#
# Etymology:
# This is a genesis for function documentation.
#
# Args:
# line: function document line, begins with # always
# add: add this part after "#' "
#
# Returns:
# transform to roxygen style
line <- trimws(line, which="left")
stopifnot(substr(line, 1, 1)=="#")
line_start <- "#' "
if(!is.null(add)) {
line_start <- paste0("#' ", add, " ")
}
gsub("^#(#){0,1}[[:blank:]]*", line_start, line)
}
labelFunDocLine <- function(line) {
# give label such as "args" to a line of function documentation
#
# Args:
# line: Google R style guide function document line
#
# Returns:
# label in set (args, arg_name, returns, other)
# remove white spaces from the start of line
line <- trimws(line, which="left")
has_args <- grepl("^#*[[:blank:]]*Args:[[:blank:]]*", line)
has_returns <- grepl("^#*[[:blank:]]*Returns:[[:blank:]]*", line)
# support only function arguments up to 50 characters
has_argument_name <- (
grepl("^#*[[:blank:]]*(\\w|\\.|_){1,50}:[[:blank:]]*", line)
)
if(has_returns) {
return("returns")
}
if(has_args) {
return("args")
}
if(has_argument_name) {
return("arg_name")
}
return("other")
}
labelAllFunDocLines <- function(fun_doc) {
# label each line in function documentation
#
# Logic:
# 1) Give label (args, arg_name, returns, other) to each line independently
# 2) Relabel line after returns to return_body
#
# Args:
# fun_doc: list of function documentation lines
#
# Returns:
# label for each line from set (other, args, arg_name, returns, return_body)
labels <- lapply(fun_doc, labelFunDocLine)
stopifnot(length(labels) == length(fun_doc))
# mark the line after returns
suppressWarnings(
return_index <- min(which(labels == "returns"))
)
if(length(return_index) >= 1 && length(labels) >= return_index +1 ) {
labels[[return_index + 1]] <- "return_body"
}
return(labels)
}
transformFunDocLine <- function(line, label) {
# Transform function document line
#
# Args:
# line: function document line
# label: one of (other, args, arg_name, returns, return_body)
#
# Returns:
# Transformed function document line.
# eg " # foo" -> "#' foo"
# remove "Args:" and "Returns:" lines
out <- NULL
if(label == "other") {
out <- roxygenesis(line)
}
if(label == "arg_name") {
out <- roxygenesis(line, add = "@param")
}
if(label == "return_body") {
out <- roxygenesis(line, add = "@return")
}
return(out)
}
transformFunDoc <- function(fun_doc, add_export=FALSE) {
# Transform Google style R function document to roxygen2 style.
# See above for examples.
#
# Args:
# fun_doc: list of function document lines
# add_export: if TRUE then add @export
#
# Returns:
# List of transformed function document lines.
# label all lines
labels <- labelAllFunDocLines(fun_doc)
transformed_fun_doc <- mapply(transformFunDocLine, fun_doc, labels)
transformed_fun_doc <- unlist(transformed_fun_doc)
if(add_export & length(transformed_fun_doc) > 0) {
transformed_fun_doc <- append(transformed_fun_doc, "#' @export", 1)
}
return(transformed_fun_doc)
}
printTransformedLines <- function(lines, add_export=FALSE) {
# transform lines to roxygen2 document format and print them
#
# Args:
# lines: vector of code string lines
# add_export: if TRUE then add @export to each function document
#
# Returns:
# TRUE, prints to stdout
# Logic:
# step A: print lines until "function" keyword is found
# step B: store (to S1) lines until "{" character is found
# step C: if first -non-empty line starts with # then go to D, otherwise print
# the stored S1 lines and go to A
# step D: store (to S2) function document lines until line does not begin with #
# step E: transform function document lines lines at S2, print S1, and go to A
store_fun_def <- list()
store_fun_doc <- list()
state <- "find function"
for (i in 1:length(lines)) {
line <- lines[[i]]
if(state == "find function") {
# print lines until "function" keyword is found
if(grepl("function(", line, fixed=TRUE)) {
state <- "find {"
} else {
catnl(line)
}
}
if(state == "find {") {
#store lines until { is found and function body starts
store_fun_def <- c(store_fun_def, line)
if(grepl("\\{", line)) {
state <- "find #"
}
} else if(state == "find #") {
# find start of function documentation
if(lineEmpty(line)) {
# if line is empty, just store it
store_fun_def <- c(store_fun_def, line)
} else if(lineStartsWith(line, "#")) {
# else if line starts with # then start storing the documentation
state <- "store function documentation"
} else {
# no function documentation found, just print what we have found so far
printLines(store_fun_def)
store_fun_def <- list()
state <- "find function"
}
}
if(state == "store function documentation") {
if(lineStartsWith(line, "#")) {
# store function documentation starting with #
store_fun_doc <- c(store_fun_doc, line)
} else {
# function documentation has ended, print transformed documentation and
# continue
transformed_fun_doc <- transformFunDoc(store_fun_doc, add_export)
printLines(transformed_fun_doc)
store_fun_doc <- list()
printLines(store_fun_def)
store_fun_def <- list()
catnl(line)
state <- "find function"
}
}
}
# print any remaining stored lines as such
printLines(store_fun_def)
printLines(store_fun_doc)
return(TRUE)
}