SCM

SCM Repository

[inlinedocs] Annotation of /pkg/inlinedocs/R/parsers.R
ViewVC logotype

Annotation of /pkg/inlinedocs/R/parsers.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 108 - (view) (download)

1 : tdhock 87 ### For each object in the package that satisfies the criterion
2 :     ### checked by subfun, parse source using FUN and return the resulting
3 :     ### documentation list.
4 :     forall <- function
5 :     (FUN,
6 :     ### Function to apply to each element in the package.
7 :     subfun=function(x)TRUE
8 :     ### Function to select subsets of elements of the package, such as
9 :     ### is.function. subfun(x)==TRUE means FUN will be applied to x and
10 :     ### the result will be returned.
11 :     ){
12 :     function(objs,docs,...){
13 :     objs <- objs[sapply(objs,subfun)]
14 :     L <- list()
15 : tdhock 93 for(N in names(docs)){
16 : tdhock 87 o <- objs[[N]]
17 : tdhock 89 L[[N]] <- FUN(src=attr(o,"source"),
18 :     name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)
19 : tdhock 87 }
20 :     L
21 :     }
22 :     ### A Parser Function.
23 :     }
24 :    
25 :     ### For each function in the package, do something.
26 :     forfun <- function(FUN)forall(FUN,is.function)
27 :    
28 : tdhock 99 ### Parsers for each object/function that are constructed
29 :     ### automatically. This is a named list, each element is a list of 2
30 :     ### elements: forfun/forall, then a parser function for an individual
31 :     ### object.
32 : tdhock 89 forall.parsers <-
33 :     list(## Extract lots of info from normal functions.
34 :     parsefun=list(forfun,function(src,name,...){
35 :     extract.docs.fun(src,name)
36 :     }),
37 :     ## Fill in author from DESCRIPTION and titles.
38 :     author.from.description=list(forall,function(desc,...){
39 :     list(author=desc[,"Maintainer"])
40 :     }),
41 :     ## The format section sometimes causes problems, so erase it.
42 :     erase.format=list(forall,function(...){
43 :     list(format="")
44 :     }),
45 :     ## Convert the function name to a title.
46 :     title.from.name=list(forall,function(name,doc,...){
47 :     if("title"%in%names(doc))list() else
48 :     list(title=gsub("[._]"," ",name))
49 :     }),
50 : phgrosjean 108 # PhG: it is tests/FUN.R!!! I would like more flexibility here
51 :     # please, let me choose which dir to use for examples!
52 :     ## Get examples for FUN from the file tests/FUN.R
53 : tdhock 89 examples.from.testfile=list(forfun,function(name,...){
54 : phgrosjean 108 tsubdir <- getOption("inlinedocs.exdir")
55 :     if (is.null(tsubdir)) tsubdir <- "tests" # Default value
56 :     tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))
57 : tdhock 89 if(file.exists(tfile))
58 :     list(examples=paste(readLines(tfile),collapse="\n"))
59 :     else list()
60 :     }),
61 :     ## Get examples from inline definitions after return()
62 : phgrosjean 108 # PhG: this does not work well! Think at these situations:
63 :     # 1) You have multiple return() in the code of your function,
64 :     # 2) You have return() appearing is some example code, ...
65 :     # I can hardly propose a hack here. The whole code of the function
66 :     # must be parsed, and one must determine which one is the last line
67 :     # of code that is actually executed.
68 :     #
69 :     # I make two propositions here
70 :     # 1) to keep the same mechanism that has the advantage of simplicity
71 :     # but to use a special tag ##examples<< or #{{{examples to separate
72 :     # function code from examples explicitly, and
73 :     # 2) to place the example in an "ex" attribute attached to the function
74 :     # (see next parser). That solution will be also interesting for
75 :     # documenting datasets, something not done yet by inlinedocs!
76 :     examples.after.return = list(forfun, function(name, src, ...) {
77 :     # Look for the examples mark
78 :     m <- grep("##examples<<|#\\{\\{\\{examples", src)
79 :     if (!length(m)) return(list())
80 :     if (length(m) > 1)
81 :     warning("More than one examples tag for ", name, ". Taking the last one")
82 :     m <- m[length(m)]
83 :     # Look for the lines containing return value comments just before
84 :     r <- grep("\\s*### ", src[1:(m-1)])
85 :     if (!length(r)) {
86 :     value <- NULL
87 :     } else {
88 :     # Only take consecutive lines before the mark
89 :     keep <- rev((m - rev(r)) == 1:length(r))
90 :     if (!any(keep)) {
91 :     value <- NULL
92 :     } else {
93 :     value <- decomment(src[r[keep]])
94 :     }
95 :     }
96 :     # Collect now the example code beneath the mark
97 :     ex <- src[(m + 1):(length(src) - 1)]
98 :     # Possibly eliminate a #}}} tag
99 :     ex <- ex[!grepl("#}}}", ex)]
100 :     # Eliminate leading tabulations or four spaces
101 :     ex <- sub("^\t| ", "", ex)
102 :     # Add an empty line before and after example
103 :     ex <- c("", ex, "")
104 :     # Return examples and value
105 :     list(examples = paste(ex, collapse = "\n"), value = value)
106 :     }),
107 :     # PhG: here is what I propose for examples code in the 'ex' attribute
108 :     examples.in.attr = list(forfun, function (name, o, ...) {
109 :     ex <- attr(o, "ex")
110 :     if (!is.null(ex)) {
111 :     # Special case for code contained in a function
112 :     if (inherits(ex, "function")) {
113 :     # If source is available, start from there
114 :     src <- attr(ex, "source")
115 :     if (!is.null(src)) {
116 :     ex <- src
117 :     } else { # Use the body of the function
118 :     ex <- deparse(body(ex))
119 :     }
120 :     # Eliminate leading and trailing code
121 :     ex <- ex[-c(1, length(ex))]
122 :     # Eliminate leading tabulations or four spaces
123 :     ex <- sub("^\t| ", "", ex)
124 :     # Add an empty line before and after example
125 :     ex <- c("", ex, "")
126 :     }
127 :     list(examples = paste(ex, collapse = "\n"))
128 :     } else list()
129 :     }))
130 : tdhock 87
131 : tdhock 89 ### List of parser functions that operate on single objects. This list
132 :     ### is useful for testing these functions, ie
133 : tdhock 99 ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")
134 : tdhock 89 lonely <- sapply(forall.parsers,function(L)L[[2]])
135 : tdhock 87
136 :     extract.docs.file <- function # Extract documentation from a file
137 :     ### Parse R code to extract inline documentation from comments around
138 :     ### each function. These are not able to be retreived simply by
139 :     ### looking at the "source" attribute. This is a Parser Function that
140 : tdhock 106 ### can be used in the parser list of package.skeleton.dx(). TODO:
141 : phgrosjean 108 ### Modularize this into separate Parsers Functions for S4 classes,
142 :     ### prefixes, ##<<blocks, etc. Right now it is not very clean!
143 : tdhock 87 (code,
144 :     ### Code lines in a character vector containing multiple R objects to
145 :     ### parse for documentation.
146 :     objs,
147 :     ### The objects defined in the code.
148 :     ...
149 :     ### ignored
150 :     ){
151 :     parsed <- extract.file.parse(code)
152 :     extract.docs.try <- function(o,on)
153 :     {
154 :     ## Note: we could use parsed information here too, but that
155 :     ## would produce different results for setMethodS3 etc.
156 :     doc <- list()
157 :     if ( !is.null(parsed[[on]]) ){
158 :     if ( !is.na(parsed[[on]]@code[1]) ){ # no code given for generics
159 :     doc$definition <- paste(parsed[[on]]@code,collapse="\n")
160 :     }
161 :     if(!"description"%in%names(doc) && !is.na(parsed[[on]]@description) ){
162 :     doc$description <- parsed[[on]]@description
163 :     }
164 :     if ( "setMethodS3" == parsed[[on]]@created ){
165 : phgrosjean 108 # PhG: this may be wrong! It does not catch correctly how the method
166 :     # must be splitted in case of methods containing dots. for instance,
167 :     # as.data.frame.matrix must be split into: m1 = as.data.frame and
168 :     # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!
169 :     pattern <- "^([^\\.]+)\\.(.*)$"
170 : tdhock 87 doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),
171 :     m2 <- gsub(pattern,"\\2",on,perl=TRUE))
172 :     if ( 0 < length(grep("\\W",m1,perl=TRUE)) ){
173 : phgrosjean 108 m1 <- paste("`",m1,"`",sep="")
174 : tdhock 87 }
175 :     cat("S3method(",m1,",",m2,")\n",sep="")
176 :     }
177 :     }
178 :     if("title" %in% names(doc) && !"description" %in% names(doc) ){
179 :     ## For short functions having both would duplicate, but a
180 :     ## description is required. Therefore automatically copy title
181 :     ## across to avoid errors at package build time.
182 :     doc$description <- doc$title
183 :     }
184 :     doc
185 :     }
186 :     extract.docs <- function(on){
187 :     res <- try({o <- objs[[on]]
188 :     extract.docs.try(o, on)},FALSE)
189 :     if(class(res)=="try-error"){
190 :     cat("Failed to extract docs for: ",on,"\n\n")
191 :     list()
192 :     } else if(0 == length(res) && inherits(objs[[on]],"standardGeneric")){
193 :     NULL
194 :     } else if(0 == length(res) && "function" %in% class(o)
195 :     && 1 == length(osource <- attr(o,"source"))
196 :     && 1 == length(grep(paste("UseMethod(",on,")",sep="\""),osource))
197 :     ){
198 :     ## phew - this should only pick up R.oo S3 generic definitions like:
199 :     ## attr(*, "source")= chr "function(...) UseMethod(\"select\")"
200 :     NULL
201 :     } else res
202 :     }
203 :     doc.names <- names(objs)
204 :     res <- sapply(doc.names,extract.docs,simplify=FALSE)
205 :     ## Special processing for S4 classes as they do not appear in normal ls()
206 :     for ( nn in names(parsed) ){
207 :     if ( parsed[[nn]]@created == "setClass" ){
208 :     S4class.docs <- extract.docs.setClass(parsed[[nn]])
209 :     docname <- paste(nn,"class",sep="-")
210 :     if ( is.null(res[[docname]]) ){
211 :     res[[docname]] <- S4class.docs
212 :     doc.names <- c(doc.names,docname)
213 :     } else {
214 :     stop(nn," appears as both S4 class and some other definition")
215 :     }
216 :     }
217 :     }
218 :     inherit.docs <- function(on){
219 :     in.res <- res[[on]]
220 :     if ( !is.null(parsed[[on]]) ){
221 :     for ( parent in parsed[[on]]@parent ){
222 :     if ( !is.na(parent) ){
223 :     if ( is.null(in.res) ){
224 :     in.res <- res[[parent]]
225 :     } else if ( parent %in% names(res) ){
226 :     parent.docs <- res[[parent]]
227 :     for ( nn in names(parent.docs) ){
228 :     if ( !nn %in% names(in.res) ){
229 :     in.res[[nn]] <- parent.docs[[nn]]
230 :     }
231 :     }
232 :     }
233 :     }
234 :     }
235 :     }
236 :     invisible(in.res)
237 :     }
238 :     all.done <- FALSE
239 :     while ( !all.done ){
240 :     res1 <- sapply(doc.names,inherit.docs,simplify=FALSE)
241 :     all.done <- identical(res1,res)
242 :     res <- res1
243 :     }
244 :     ## now strip out any generics (which have value NULL in res):
245 :     res.not.null <- sapply(res,function(x){!is.null(x)})
246 :     if ( 0 < length(res.not.null) && length(res.not.null) < length(res) ){
247 :     res <- res[res.not.null]
248 :     }
249 :     res
250 :     ### named list of lists, one for each object to document.
251 :     }
252 :    
253 : tdhock 89 ### List of parsers to use by default with package.skeleton.dx.
254 : tdhock 93 default.parsers <- c(extract.docs.file=extract.docs.file,
255 :     sapply(forall.parsers,function(L)L[[1]](L[[2]])))
256 : tdhock 87
257 :     extract.docs.fun <- function # Extract documentation from a function
258 :     ### Given source code of a function, return a list describing inline
259 :     ### documentation in that source code.
260 :     (code,
261 :     ### The function to examine.
262 :     name.fun
263 :     ### The name of the function/chunk to use in warning messages.
264 : tdhock 95 ){
265 : tdhock 87 res <- list()
266 :     clines <- grep(prefix,code)
267 :     if(length(grep("#",code[1]))){
268 :     res$title <- gsub("[^#]*#\\s*(.*)","\\1",code[1],perl=TRUE)
269 :     }
270 :     if(length(clines) > 0){
271 :     ##details<<
272 :     ## The primary mechanism is that consecutive groups of lines matching
273 :     ## the specified prefix regular expression "\code{^### }" (i.e. lines
274 :     ## beginning with "\code{### }") are collected
275 :     ## as follows into documentation sections:\describe{
276 :     ## \item{description}{group starting at line 2 in the code}
277 :     ## \item{arguments}{group following each function argument}
278 :     ## \item{value}{group ending at the penultimate line of the code}}
279 :     ## These may be added to by use of the \code{##<<} constructs described
280 :     ## below.
281 :     bounds <- which(diff(clines)!=1)
282 :     starts <- c(1,bounds+1)
283 :     ends <- c(bounds,length(clines))
284 :     for(i in seq_along(starts)){
285 :     start <- clines[starts[i]]
286 :     end <- clines[ends[i]]
287 :     lab <- if(end+1==length(code))"value"
288 :     else if(start==2)"description"
289 :     else if ( 0 == length(grep("^\\s*#",code[start-1],perl=TRUE)) ){
290 :     #arg <- gsub("^[ (]*","",code[start-1])
291 :     #arg <- gsub("^([^=,]*)[=,].*","\\1",arg)
292 :     #arg <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots
293 :     arg <- gsub("^[ \t(,]*", "", code[start - 1]) #twutz: strip leading white spaces and brackets and ,
294 :     arg <- gsub("^([^=,]*)[=,].*", "\\1", arg)
295 :     arg <- gsub("^([^ \t]*)([ \t]+)$","\\1",arg) #twutz: remove trailing whitespaces
296 :     arg <- gsub("...", "\\dots", arg, fix = TRUE)
297 :     paste("item{",arg,"}",sep="")
298 :     } else {
299 :     next;
300 :     }
301 :     res[[lab]] <- decomment(code[start:end])
302 :     }
303 :     }
304 :     ##details<< For simple functions/arguments, the argument may also be
305 :     ## documented by appending \code{##<<} comments on the same line as the
306 :     ## argument name. Mixing this mechanism with \code{###} comment lines for
307 :     ## the same argument is likely to lead to confusion, as the \code{###}
308 :     ## lines are processed first.
309 :     #arg.pat <- paste("^[^=,#]*?([\\w\\.]+)\\s*([=,].*|\\)\\s*)?",
310 :     # "<<\\s*(\\S.*?)\\s*$",
311 :     # sep="##") # paste avoids embedded trigger fooling the system
312 :     #tw: removed first comma
313 :     arg.pat <- paste("^[^=#]*?([\\w\\.]+)\\s*([=,].*|\\)\\s*)?",
314 :     "<<\\s*(\\S.*?)\\s*$",
315 :     sep="##") # paste avoids embedded trigger fooling the system
316 :    
317 :     skeleton.fields <- c("alias","details","keyword","references","author",
318 :     "note","seealso","value","title","description",
319 :     "describe","end")
320 :     ##details<< Additionally, consecutive sections of \code{##} comment
321 :     ## lines beginning with \code{##}\emph{xxx}\code{<<} (where
322 :     ## \emph{xxx} is one of the fields: \code{alias}, \code{details},
323 :     ## \code{keyword}, \code{references}, \code{author}, \code{note},
324 :     ## \code{seealso}, \code{value}, \code{title} or \code{description})
325 :     ## are accumulated and inserted in the relevant part of the .Rd
326 :     ## file.
327 :     ##
328 :     ## For \code{value}, \code{title}, \code{description} and function
329 :     ## arguments, these \emph{append} to any text from "prefix"
330 :     ## (\code{^### }) comment lines, irrespective of the order in the
331 :     ## source.
332 :     ##
333 :     ## When documenting S4 classes, documentation from \code{details}
334 :     ## sections will appear under a section \code{Objects from the Class}. That
335 :     ## section typically includes information about construction methods
336 :     ## as well as other description of class objects (but note that the
337 :     ## class Slots are documented in a separate section).
338 :    
339 :     ## but this should not appear, because separated by a blank line
340 :     extra.regexp <- paste("^\\s*##(",paste(skeleton.fields,collapse="|"),
341 :     ")<<\\s*(.*)$",sep="")
342 :     cont.re <- "^\\s*##\\s*"
343 :     in.describe <- 0
344 :     first.describe <- FALSE
345 :     k <- 1
346 :     in.chunk <- FALSE
347 :     end.chunk <- function(field,payload)
348 :     {
349 :     if ( "alias" == field ){
350 :     ##note<< \code{alias} extras are automatically split at new lines.
351 :     payload <- gsub("\\n+","\\}\n\\\\alias\\{",payload,perl=TRUE)
352 :     chunk.sep <- "}\n\\alias{"
353 :     } else if ( "keyword" == field ){
354 :     ##keyword<< documentation utilities
355 :     ##note<< \code{keyword} extras are automatically split at white space,
356 :     ## as all the valid keywords are single words.
357 :     payload <- gsub("\\s+","\\}\n\\\\keyword\\{",payload,perl=TRUE)
358 :     chunk.sep <- "}\n\\keyword{"
359 :     } else if ( "title" == field ){
360 :     chunk.sep <- " "
361 :     } else if ( "description" == field ){
362 :     chunk.sep <- "\n"
363 :     } else {
364 :     ##details<< Each separate extra section appears as a new
365 :     ## paragraph except that: \itemize{\item empty sections (no
366 :     ## matter how many lines) are ignored;\item \code{alias} and
367 :     ## \code{keyword} sections have special rules;\item
368 :     ## \code{description} should be brief, so all such sections
369 :     ## are concatenated as one paragraph;\item \code{title} should
370 :     ## be one line, so any extra \code{title} sections are
371 :     ## concatenated as a single line with spaces separating the
372 :     ## sections.}
373 :     chunk.sep <- "\n\n"
374 :     }
375 :     chunk.res <- NULL
376 :     if ( 0 == length(grep("^\\s*$",payload,perl=TRUE)) )
377 :     chunk.res <-
378 :     if ( is.null(res[[field]]) ) payload
379 :     else paste(res[[field]], payload, sep=chunk.sep)
380 :     invisible(chunk.res)
381 :     }
382 :     while ( k <= length(code) ){
383 :     line <- code[k]
384 :     if ( 0 < length(grep(extra.regexp,line,perl=TRUE) ) ){
385 :     ## we have a new extra chunk - first get field name and any payload
386 :     new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)
387 :     new.contents <- gsub(extra.regexp,"\\2",line,perl=TRUE)
388 :    
389 :     ##details<< As a special case, the construct \code{##describe<<} causes
390 :     ## similar processing to the main function arguments to be
391 :     ## applied in order to construct a describe block within the
392 :     ## documentation, for example to describe the members of a
393 :     ## list. All subsequent "same line" \code{##<<} comments go into that
394 :     ## block until terminated by a subsequent \code{##}\emph{xxx}\code{<<} line.
395 :     if ( "describe" == new.field ){
396 :     ##details<< Such regions may be nested, but not in such a way
397 :     ## that the first element in a \code{describe} is another \code{describe}.
398 :     ## Thus there must be at least one \code{##<<} comment between each
399 :     ## pair of \code{##describe<<} comments.
400 :     if ( first.describe ){
401 :     stop("consecutive ##describe<< at line",k,"in",name.fun)
402 :     } else {
403 :     if ( nzchar(new.contents) ){
404 :     if ( is.null(payload) || 0 == nzchar(payload) ){
405 :     payload <- new.contents
406 :     } else {
407 :     payload <- paste(payload,new.contents,sep="\n\n")
408 :     }
409 :     }
410 :     first.describe <- TRUE
411 :     }
412 :     } else if ( "end" == new.field ){
413 :     ##details<< When nested \code{describe} blocks are used, a comment-only
414 :     ## line with \code{##end<<} terminates the current level only; any
415 :     ## other valid \code{##}\emph{xxx}\code{<<} line terminates
416 :     ## all open describe blocks.
417 :     if ( in.describe>0 ){
418 :     ## terminate current \item and \describe block only
419 :     if ( "value" == cur.field && 1 == in.describe ){
420 :     payload <- paste(payload,"}",sep="")
421 :     } else {
422 :     payload <- paste(payload,"}\n}",sep="")
423 :     }
424 :     in.describe <- in.describe-1;
425 :     } else {
426 :     warning("mismatched ##end<< at line ",k," in ",name.fun)
427 :     }
428 :     if ( nzchar(new.contents) ){
429 :     if ( nzchar(payload) ){
430 :     payload <- paste(payload,new.contents,sep="\n")
431 :     } else {
432 :     payload <- new.contents
433 :     }
434 :     }
435 :     } else {
436 :     ## terminate all open \describe blocks (+1 because of open item)
437 :     if ( 0 < in.describe ){
438 :     if ( "value" != cur.field ){ # value is implicit describe block
439 :     payload <- paste(payload,"}",sep="")
440 :     }
441 :     while ( in.describe>0 ){
442 :     payload <- paste(payload,"}",sep="\n")
443 :     in.describe <- in.describe-1;
444 :     }
445 :     }
446 :     ## finishing any existing payload
447 :     if ( in.chunk ) res[[cur.field]] <- end.chunk(cur.field,payload)
448 :     in.chunk <- TRUE
449 :     cur.field <- new.field
450 :     payload <- new.contents
451 :     ##note<< The "value" section of a .Rd file is implicitly a describe
452 :     ## block and \code{##}\code{value}\code{<<} acts accordingly. Therefore
453 :     ## it automatically enables the describe block itemization (##<< after
454 :     ## list entries).
455 :     if ( "value" == new.field ){
456 :     first.describe <- TRUE;
457 :     }
458 :     }
459 :     } else if ( in.chunk && 0<length(grep(cont.re,line,perl=TRUE)) ){
460 :     ## append this line to current chunk
461 :     if ( 0 == length(grep(prefix,line,perl=TRUE)) ){
462 :     ##describe<< Any lines with "\code{### }" at the left hand
463 :     ## margin within the included chunks are handled separately,
464 :     ## so if they appear in the documentation they will appear
465 :     ## before the \code{##}\emph{xxx}\code{<}\code{<} chunks.
466 :     ### This one should not appear.
467 :     stripped <- gsub(cont.re,"",line,perl=TRUE)
468 :     if ( nzchar(payload) ){
469 :     payload <- paste(payload,stripped,sep="\n")
470 :     } else {
471 :     payload <- stripped
472 :     }
473 :     }
474 :     } else if ( 0 < length(grep(arg.pat,line,perl=TRUE)) ){
475 :     not.describe <- (0==in.describe && !first.describe)
476 :     if ( in.chunk && not.describe){
477 :     res[[cur.field]] <- end.chunk(cur.field,payload)
478 :     }
479 :     comment <- gsub(arg.pat,"\\3",line,perl=TRUE);
480 :     arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)
481 :     in.chunk <- TRUE
482 :     if ( not.describe ){
483 :     cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots
484 :     payload <- comment
485 :     } else {
486 :     ## this is a describe block, so we need to paste with existing
487 :     ## payload as a new \item.
488 :     if ( first.describe ){
489 :     ## for first item, need to add describe block starter
490 :     if ( "value" == cur.field ){
491 :     payload <- paste(payload,"\n",arg,"{",sep="")
492 :     } else {
493 :     payload <- paste(payload,"\\describe{\n",arg,"{",sep="")
494 :     }
495 :     first.describe <- FALSE
496 :     in.describe <- in.describe+1
497 :     } else {
498 :     ## subsequent item - terminate existing and start new
499 :     payload <- paste(payload,"}\n",arg,"{",sep="")
500 :     }
501 :     if ( nzchar(comment) ){
502 :     payload <- paste(payload,comment,sep="")
503 :     }
504 :     }
505 :     } else if ( in.chunk ){
506 :     if ( 0 == in.describe && !first.describe ){
507 :     ## reached an end to current field, but need to wait if in.describe
508 :     res[[cur.field]] <- end.chunk(cur.field,payload)
509 :     in.chunk <- FALSE
510 :     cur.field <- NULL
511 :     payload <- NULL
512 :     }
513 :     }
514 :     k <- k+1
515 :     }
516 :     ## finishing any existing payload
517 :     if ( 0 < in.describe ){
518 :     if ( "value" != cur.field ){ # value is implicit describe block
519 :     payload <- paste(payload,"}",sep="")
520 :     }
521 :     while ( in.describe>0 ){
522 :     payload <- paste(payload,"}",sep="\n")
523 :     in.describe <- in.describe-1;
524 :     }
525 :     }
526 :     if ( in.chunk ) res[[cur.field]] <- end.chunk(cur.field,payload)
527 :     res
528 :     ### Named list of character strings extracted from comments. For each
529 :     ### name N we will look for N\{...\} in the Rd file and replace it
530 :     ### with the string in this list (implemented in modify.Rd.file).
531 :     }
532 :    
533 :     setClass("DocLink", # Link documentation among related functions
534 :     ### The \code{.DocLink} class provides the basis for hooking together
535 :     ### documentation of related classes/functions/objects. The aim is that
536 :     ### documentation sections missing from the child are
537 :     representation(name="character", ##<< name of object
538 :     created="character", ##<< how created
539 :     parent="character", ##<< parent class or NA
540 :     code="character", ##<< actual source lines
541 :     description="character") ##<< preceding description block
542 :     )
543 :    
544 :     extract.file.parse <- function # File content analysis
545 :     ### Using the base \code{\link{parse}} function, analyse the file to link
546 :     ### preceding "prefix" comments to each active chunk. Those comments form
547 :     ### the default description for that chunk. The analysis also looks for
548 :     ### S4 class "setClass" calls and R.oo setConstructorS3 and setMethodS3
549 :     ### calls in order to link the documentation of those properly.
550 :     (code
551 :     ### Lines of R source code in a character vector - note that any
552 :     ### nested \code{source} statements are \emph{ignored} when scanning
553 :     ### for class definitions.
554 :     ){
555 :     res <- list()
556 :     old.opt <- options(keep.source=TRUE)
557 :     parsed <- try(parse(text=code))
558 :     options(old.opt)
559 :     if ( inherits(parsed,"try-error") ){
560 :     stop("parse failed with error:\n",parsed)
561 :     }
562 :     chunks <- attr(parsed,"srcref")
563 :     last.end <- 0
564 :     for ( k in 1:length(parsed) ){
565 :     start <- chunks[[k]][1]
566 :     ##details<< If the definition chunk does not contain a
567 :     ## description, any immediately preceding sequence consecutive
568 :     ## "prefix" lines will be used instead.
569 :     default.description <- NULL
570 :     while ( start > last.end+1
571 :     && 1 == length(grep(prefix,code[start-1],perl=TRUE)) ){
572 :     start <- start-1
573 :     }
574 :     if ( start < chunks[[k]][1] ){
575 :     default.description <- decomment(code[start:(chunks[[k]][1]-1)])
576 :     } else {
577 :     default.description <- NA_character_;
578 :     }
579 :     ##details<< Class and method definitions can take several forms,
580 :     ## determined by expression type: \describe{
581 :     ## \item{assignment (<-)}{Ordinary assignment of value/function;}
582 :     ## \item{setClass}{Definition of S4 class;}
583 :     ## \item{setConstructorS3}{Definition of S3 class using R.oo package;}
584 :     ## \item{setMethodS3}{Definition of method for S3 class using R.oo package.}}
585 :     ## Additionally, the value may be a name of a function defined elsewhere,
586 :     ## in which case the documentation should be copied from that other definition.
587 :     ## This is handled using the concept of documentation links.
588 :     lang <- parsed[[k]]
589 :     chars <- as.character(lang)
590 :     expr.type <- chars[1]
591 :     parent <- NA_character_
592 :    
593 :     if ( expr.type == "<-" || expr.type == "setConstructorS3" || expr.type == "setClass" ){
594 :     object.name <- chars[2]
595 :     ## If the function definition is not embedded within the call, then
596 :     ## the parent is that function. Test whether the the third value
597 :     ## looks like a name and add it to parents if so.
598 :     if ( 1 == length(grep("^[\\._\\w]+$",chars[3],perl=TRUE)) ){
599 :     parent <- chars[3]
600 :     }
601 :     res[[object.name]] <- new("DocLink",name=object.name,
602 :     created=expr.type,
603 :     parent=parent,
604 :     code=paste(chunks[[k]],sep=""),
605 :     description=default.description)
606 :     } else if ( expr.type == "setMethodS3" ){
607 :     ##details<< The \code{setMethodS3} calls introduce additional
608 :     ## complexity: they will define an additional S3 generic (which
609 :     ## needs documentation to avoid warnings at package build time)
610 :     ## unless one already exists. This also is handled by "linking"
611 :     ## documentation. A previously unseen generic is linked to the
612 :     ## first defining instances, subsequent definitions of that generic
613 :     ## also link back to the first defining instance.
614 :     generic.name <- chars[2]
615 :     object.name <- paste(generic.name,chars[3],sep=".")
616 :     if ( is.null(res[[generic.name]]) ){
617 :     generic.desc <- paste("Generic method behind \\code{\\link{",object.name,"}}",sep="")
618 :     res[[generic.name]] <- new("DocLink",
619 :     name=generic.name,
620 :     created=expr.type,
621 :     parent=object.name,
622 :     code=NA_character_,
623 :     description=generic.desc)
624 :     } else {
625 :     parent <- res[[generic.name]]@parent
626 :     }
627 :     ## If the function definition is not embedded within the call, then
628 :     ## the parent is that function. Test whether the the fourth value
629 :     ## looks like a name and add it to parents if so.
630 :     if ( 1 == length(grep("^[\\._\\w]+$",chars[4],perl=TRUE)) ){
631 :     parent <- c(chars[4],parent)
632 :     }
633 :     res[[object.name]] <- new("DocLink",name=object.name,
634 :     created=expr.type,
635 :     parent=parent,
636 :     code=paste(chunks[[k]],sep=""),
637 :     description=default.description)
638 :     } else {
639 :     ## Not sure what to do with these yet. Need to deal with setMethod, setAs etc.
640 :     }
641 :     }
642 :     invisible(res)
643 :     ### Returns an invisible list of .DocLink objects.
644 :     }
645 :    
646 :     extract.docs.setClass <- function # S4 class inline documentation
647 :     ### Using the same conventions as for functions, definitions of S4 classes
648 :     ### in the form \code{setClass("classname",\dots)} are also located and
649 :     ### scanned for inline comments.
650 : tdhock 93 (doc.link
651 : tdhock 87 ### DocLink object as created by \code{\link{extract.file.parse}}.
652 :     ### Note that \code{source} statements are \emph{ignored} when scanning for
653 :     ### class definitions.
654 : tdhock 93 ){
655 : tdhock 87 chunk.source <- doc.link@code
656 :     ##details<<
657 :     ## Extraction of S4 class documentation is currently limited to expressions
658 :     ## within the source code which have first line starting with
659 :     ## \code{setClass("classname"}. These are located from the source file
660 :     ## (allowing also for white space around the \code{setClass} and \code{(}).
661 :     ## Note that \code{"classname"} must be a quoted character string;
662 :     ## expressions returning such a string are not matched.
663 :     class.name <- doc.link@name
664 :    
665 :     ##details<< For class definitions, the slots (elements of the
666 :     ## \code{representation} list) fill the role of function
667 :     ## arguments, so may be documented by \code{##<<} comments on
668 :     ## the same line or \code{### } comments at the beginning of the
669 :     ## following line.
670 :     f.n <- paste(class.name,"class",sep="-")
671 :     docs <- extract.docs.fun(chunk.source,f.n)
672 :     ##details<<
673 :     ## The class definition skeleton includes an \code{Objects from the Class}
674 :     ## section, to which any \code{##details<<} documentation chunks are
675 :     ## written. It is given a vanilla content if there are no specific
676 :     ## \code{##details<<} documentation chunks.
677 :     if ( is.null(docs[["details"]]) ){
678 :     docs[["details"]] <-
679 :     paste("Objects can be created by calls of the form \\code{new(",
680 :     class.name," ...)}",sep="")
681 :     }
682 :     docs[["section{Objects from the Class}"]] <- docs[["details"]]
683 :     ## seealso has a skeleton line not marked by ~ .. ~, so have to suppress
684 :     if ( is.null(docs[["seealso"]]) ){
685 :     docs[["seealso"]] <- ""
686 :     }
687 :     if ( is.null(docs[["alias"]]) ){
688 :     docs[["alias"]] <- class.name
689 :     }
690 :     if ( is.null(docs[["description"]]) ){
691 :     docs[["description"]] <- doc.link@description
692 :     }
693 :     invisible(docs)
694 :     }
695 : tdhock 105
696 :     extract.docs.code <- function
697 :     ### Write code to a file and parse it to r objs, then run all the
698 :     ### parsers and return the documentation list.
699 :     (code,
700 :     ### Character vector of code lines.
701 :     parsers,
702 :     ### List of Parser Functions.
703 :     verbose=TRUE,
704 :     ### Echo names of Parser Functions?
705 :     ...
706 :     ### Additional arguments to pass to Parser Functions.
707 :     ){
708 :     code.file <- tempfile()
709 :     writeLines(code,code.file)
710 :     e <- new.env()
711 :     old <- options(keep.source.pkgs=TRUE)
712 :     tryCatch(suppressWarnings(sys.source(code.file,e)),error=function(e){
713 :     stop("source ",code.file," failed with error:\n",e)
714 :     })
715 :     options(old)
716 :     objs <- sapply(ls(e),get,e,simplify=FALSE)
717 :    
718 :     ## apply parsers in sequence to code and objs
719 :     docs <- list()
720 : phgrosjean 108
721 :     # PhG: Automatically determine who is S3 methods
722 :     for (name in names(objs)) {
723 :     parts <- strsplit(name, ".", fixed = TRUE)[[1]]
724 :     l <- length(parts)
725 :     if (l > 1) {
726 :     for (i in 1:(l - 1)) {
727 :     # Look for a generic function (known by the system or defined
728 :     # in the package) that matches that part of the function name
729 :     generic <- paste(parts[1:i], collapse = ".")
730 :     if (any(generic %in% utils:::getKnownS3generics()) ||
731 :     utils:::findGeneric(generic, e) != "") {
732 :     object <- paste(parts[(i + 1):l], collapse = ".")
733 :     docs[[name]]$s3method <- c(generic, object)
734 :     break
735 :     }
736 :     }
737 :     }
738 :     }
739 : tdhock 105 for(i in seq_along(parsers)){
740 :     N <- names(parsers[i])
741 :     if(verbose){
742 :     if(is.character(N) && N!=""){
743 :     cat(N," ",sep="")
744 :     }else cat('. ')
745 :     }
746 :     p <- parsers[[i]]
747 :     ## This is the argument list that each parser receives:
748 :     L <- p(code=code,objs=objs,docs=docs,...)
749 :     docs <- combine(docs,L)
750 :     }
751 :     if(verbose)cat("\n")
752 :     docs
753 :     ### A list of extracted documentation from code.
754 :     }

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business University of Wisconsin - Madison Powered By FusionForge