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 389 - (view) (download)

1 : tdhock 336 do.not.generate <- structure(function
2 : tdhock 335 ### Make a Parser Function used to indicate that certain Rd files
3 :     ### should not be generated.
4 :     (...
5 :     ### Character strings indicating Rd files without the .Rd suffix.
6 :     ){
7 :     filenames <- c(...)
8 :     function(docs,...){
9 :     for(fn in filenames){
10 :     docs[[fn]] <- list()
11 :     }
12 :     docs$.overwrite <- TRUE
13 :     docs
14 :     }
15 :     ### A Parser Function that will delete items from the outer
16 :     ### Documentation List.
17 : tdhock 336 },ex=function(){
18 :     silly.pkg <- system.file("silly",package="inlinedocs")
19 :     owd <- setwd(tempdir())
20 :     file.copy(silly.pkg,".",recursive=TRUE)
21 : tdhock 335
22 : tdhock 336 ## define a custom Parser Function that will not generate some Rd
23 :     ## files
24 :     custom <- do.not.generate("silly-package","Silly-class")
25 :     parsers <- c(default.parsers,list(exclude=custom))
26 :    
27 :     ## At first, no Rd files in the man subdirectory.
28 :     man.dir <- file.path("silly","man")
29 :     dir(man.dir)
30 :    
31 :     ## Running package.skeleton.dx will generate bare-bones files for
32 :     ## those specified in do.not.generate, if they do not exist.
33 :     package.skeleton.dx("silly",parsers)
34 :     Rd.files <- c("silly-package.Rd","Silly-class.Rd","silly.example.Rd")
35 :     Rd.paths <- file.path(man.dir,Rd.files)
36 :     stopifnot(all(file.exists(Rd.paths)))
37 :    
38 :     ## Save the modification times of the Rd files
39 :     old <- file.info(Rd.paths)$mtime
40 : tdhock 356
41 :     ## make sure there is at least 2 seconds elapsed, which is the
42 :     ## resolution for recording times on windows file systems.
43 :     Sys.sleep(4)
44 : tdhock 336
45 :     ## However, it will NOT generate Rd for files specified in
46 :     ## do.not.generate, if they DO exist already.
47 :     package.skeleton.dx("silly",parsers)
48 :     mtimes <- data.frame(old,new=file.info(Rd.paths)$mtime)
49 :     rownames(mtimes) <- Rd.files
50 :     mtimes$changed <- mtimes$old != mtimes$new
51 :     print(mtimes)
52 :     stopifnot(mtimes["silly-package.Rd","changed"]==FALSE)
53 :     stopifnot(mtimes["Silly-class.Rd","changed"]==FALSE)
54 :     stopifnot(mtimes["silly.example.Rd","changed"]==TRUE)
55 :    
56 :     unlink("silly",recursive=TRUE)
57 :     setwd(owd)
58 :     })
59 :    
60 : tdhock 376 ### combine NULL objects.
61 : markus 370 combine.NULL<-function(x,y){
62 :     if (class(x) == "NULL"){
63 :     # print(paste("mm x=",x))
64 :     # print(paste("mm class(x)=",class(x)))
65 :     x=list("")
66 :     }
67 :     if (class(y) == "NULL"){
68 :     # print(paste("mm y=",y))
69 :     # print(paste("mm class(y)=",class(y)))
70 :     y=list("")
71 :     }
72 :     return(combine(x,y))
73 :     }
74 : tdhock 376
75 :     ### combine lists or character strings
76 : markus 370 combine <- function(x,y){
77 :     UseMethod("combine")
78 :     }
79 : tdhock 110
80 :     ### combine character strings by pasting them together
81 :     combine.character <- function(x,y)
82 :     paste(x,y,sep="\n")
83 :    
84 :     ### combine lists by adding elements or adding to existing elements
85 :     combine.list <- function(x,y){
86 : tdhock 200 toadd <- if(".overwrite"%in%names(y)){
87 :     y <- y[names(y)!=".overwrite"]
88 :     rep(TRUE,length(y))
89 :     }else{
90 :     !names(y)%in%names(x)
91 :     }
92 : tdhock 185 toup <- names(y)[!toadd]
93 : tdhock 110 x[names(y)[toadd]] <- y[toadd]
94 :     for(up in toup)x[[up]] <- combine(x[[up]],y[[up]])
95 : tdhock 200 x
96 : tdhock 110 ### A list, same type as x, but with added elements from y.
97 :     }
98 :    
99 : tdhock 302
100 :     getSource <- function
101 :     ### Extract a function's source code.
102 :     (fun.obj
103 :     ### A function.
104 :     ) {
105 :     srcref <- attr(fun.obj, "srcref")
106 : tdhock 308 if (!is.null(srcref)) {
107 :     ##unlist(strsplit(as.character(srcref), "\n"))
108 :     as.character(srcref)
109 :     }
110 : tdhock 302 else attr(fun.obj, "source")
111 :     ### Source code lines as a character vector.
112 :     }
113 :    
114 : tdhock 110 ### Prefix for code comments used with grep and gsub.
115 : tdhock 257 prefix <- "^[ \t]*###[ \t]*"
116 : tdhock 110
117 :     decomment <- function
118 :     ### Remove comment prefix and join lines of code to form a
119 :     ### documentation string.
120 :     (comments
121 :     ### Character vector of prefixed comment lines.
122 :     ){
123 : tdhock 185 gsub(prefix,"",comments)
124 : tdhock 110 ### String without prefixes or newlines.
125 :     }
126 :    
127 : tdhock 162 forall <- function
128 : tdhock 158 ### For each object in the package that satisfies the criterion
129 :     ### checked by subfun, parse source using FUN and return the resulting
130 : tdhock 87 ### documentation list.
131 :     (FUN,
132 :     ### Function to apply to each element in the package.
133 :     subfun=function(x)TRUE
134 :     ### Function to select subsets of elements of the package, such as
135 :     ### is.function. subfun(x)==TRUE means FUN will be applied to x and
136 :     ### the result will be returned.
137 :     ){
138 : tdhock 138 FUN <- FUN
139 :     f <- function(objs,docs,...){
140 : tdhock 300 if(length(objs)==0)return(list())
141 : tdhock 87 objs <- objs[sapply(objs,subfun)]
142 :     L <- list()
143 : tdhock 162 on.exit(cat(sprintf("Parser Function failed on %s\n",N)))
144 : tdhock 204 for(N in union(names(docs),names(objs))){
145 : tdhock 87 o <- objs[[N]]
146 : tdhock 302 L[[N]] <- FUN(src=getSource(o),
147 : tdhock 89 name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)
148 : tdhock 87 }
149 : tdhock 162 on.exit()## remove warning message
150 : tdhock 87 L
151 :     }
152 : tdhock 138 class(f) <- c("allfun","function")
153 :     f
154 : tdhock 87 ### A Parser Function.
155 :     }
156 :    
157 : tdhock 138 ### Print method for functions constructed using forall.
158 :     print.allfun <- function(x,...){
159 :     e <- environment(x)
160 :     cat("Function to apply to every element.\nselector:")
161 :     print(e$subfun)
162 :     cat("processor:")
163 :     print(e$FUN)
164 :     }
165 :    
166 : tdhock 87 ### For each function in the package, do something.
167 :     forfun <- function(FUN)forall(FUN,is.function)
168 :    
169 : tdhock 163 kill.prefix.whitespace <- function
170 :     ### Figure out what the whitespace preceding the example code is, and
171 :     ### then delete that from every line.
172 :     (ex
173 :     ### character vector of example code lines.
174 :     ){
175 :     tlines <- gsub("\\s*","",ex)
176 :     ##tlines <- gsub("#.*","",tlines)
177 :     prefixes <- unique(gsub("\\S.*","",ex[tlines!=""]))
178 :     FIND <- prefixes[which.min(nchar(prefixes))]
179 :     ## Eliminate leading tabulations or 2/4 spaces
180 :     sub(FIND, "", ex)
181 :     ### Character vector of code lines with preceding whitespace removed.
182 :     }
183 :    
184 : tdhock 175 prefixed.lines <- structure(function(src,...){
185 : tdhock 138 ### The primary mechanism of inline documentation is via consecutive
186 :     ### groups of lines matching the specified prefix regular expression
187 :     ### "\code{^### }" (i.e. lines beginning with "\code{### }") are
188 :     ### collected as follows into documentation sections:\describe{
189 :     ### \item{description}{group starting at line 2 in the code}
190 :     ### \item{arguments}{group following each function argument}
191 :     ### \item{value}{group ending at the penultimate line of the code}}
192 :     ### These may be added to by use of the \code{##<<} constructs
193 :     ### described below.
194 :     clines <- grep(prefix,src)
195 :     if(length(clines)==0)return(list())
196 :     bounds <- which(diff(clines)!=1)
197 :     starts <- c(1,bounds+1)
198 :     ends <- c(bounds,length(clines))
199 :     ## detect body of function using paren matching
200 : tdhock 159 code <- gsub("#.*","",src)
201 :     f <- function(ch)cumsum(nchar(gsub(sprintf("[^%s]",ch),"",code)))
202 : tdhock 138 parens <- f("(")-f(")")
203 :     body.begin <- which(diff(parens)<0 & parens[-1]==0)+2
204 : tdhock 175 if(length(body.begin)==0)body.begin <- 1 ## rare cases
205 : tdhock 138 is.arg <- function(){
206 : tdhock 159 gres <- grep("^\\s*#",src[start-1],perl=TRUE)
207 :     0 == length(gres) && start<=body.begin
208 :     }
209 : tdhock 138 res <- list()
210 :     for(i in seq_along(starts)){
211 :     start <- clines[starts[i]]
212 :     end <- clines[ends[i]]
213 : tdhock 304 processed <- gsub("#.*","",gsub("[ }]","",src[(end+1):length(src)]))
214 :     lab <- if(all(processed==""))"value"
215 : tdhock 138 else if(start==2)"description"
216 :     else if(is.arg()){
217 :     ##twutz: strip leading white spaces and brackets and ,
218 : kmpont 305 arg <- gsub("^[ \t(,]*", "", src[start - 1])
219 : tdhock 138 arg <- gsub("^([^=,]*)[=,].*", "\\1", arg)
220 :     ##twutz: remove trailing whitespaces
221 : kmpont 305 arg <- gsub("^([^ \t]*)([ \t]+)$","\\1",arg)
222 : tdhock 336 arg <- gsub("...", "\\dots", arg, fixed = TRUE)
223 : tdhock 138 paste("item{",arg,"}",sep="")
224 :     } else {
225 :     next;
226 :     }
227 :     res[[lab]] <- decomment(src[start:end])
228 :     }
229 :     res
230 : tdhock 175 },ex=function(){
231 :     test <- function
232 : tdhock 191 ### the description
233 : tdhock 175 (x,
234 :     ### the first argument
235 :     y ##<< another argument
236 :     ){
237 :     5
238 :     ### the return value
239 :     ##seealso<< foobar
240 : tdhock 138 }
241 : tdhock 302 src <- getSource(test)
242 : tdhock 175 prefixed.lines(src)
243 :     extract.xxx.chunks(src)
244 :     })
245 : tdhock 138
246 : tdhock 154 extract.xxx.chunks <- function # Extract documentation from a function
247 : tdhock 87 ### Given source code of a function, return a list describing inline
248 :     ### documentation in that source code.
249 : tdhock 154 (src,
250 :     ### The source lines of the function to examine, as a character
251 :     ### vector.
252 :     name.fun="(unnamed function)",
253 : tdhock 87 ### The name of the function/chunk to use in warning messages.
254 : tdhock 154 ...
255 :     ### ignored.
256 : tdhock 95 ){
257 : tdhock 87 res <- list()
258 :     ##details<< For simple functions/arguments, the argument may also be
259 :     ## documented by appending \code{##<<} comments on the same line as the
260 :     ## argument name. Mixing this mechanism with \code{###} comment lines for
261 :     ## the same argument is likely to lead to confusion, as the \code{###}
262 :     ## lines are processed first.
263 :     #arg.pat <- paste("^[^=,#]*?([\\w\\.]+)\\s*([=,].*|\\)\\s*)?",
264 :     # "<<\\s*(\\S.*?)\\s*$",
265 :     # sep="##") # paste avoids embedded trigger fooling the system
266 :     #tw: removed first comma
267 :     arg.pat <- paste("^[^=#]*?([\\w\\.]+)\\s*([=,].*|\\)\\s*)?",
268 :     "<<\\s*(\\S.*?)\\s*$",
269 :     sep="##") # paste avoids embedded trigger fooling the system
270 :    
271 :     skeleton.fields <- c("alias","details","keyword","references","author",
272 :     "note","seealso","value","title","description",
273 :     "describe","end")
274 :     ##details<< Additionally, consecutive sections of \code{##} comment
275 :     ## lines beginning with \code{##}\emph{xxx}\code{<<} (where
276 :     ## \emph{xxx} is one of the fields: \code{alias}, \code{details},
277 :     ## \code{keyword}, \code{references}, \code{author}, \code{note},
278 :     ## \code{seealso}, \code{value}, \code{title} or \code{description})
279 :     ## are accumulated and inserted in the relevant part of the .Rd
280 :     ## file.
281 :     ##
282 :     ## For \code{value}, \code{title}, \code{description} and function
283 :     ## arguments, these \emph{append} to any text from "prefix"
284 :     ## (\code{^### }) comment lines, irrespective of the order in the
285 :     ## source.
286 :     ##
287 :     ## When documenting S4 classes, documentation from \code{details}
288 :     ## sections will appear under a section \code{Objects from the Class}. That
289 :     ## section typically includes information about construction methods
290 :     ## as well as other description of class objects (but note that the
291 :     ## class Slots are documented in a separate section).
292 :    
293 :     ## but this should not appear, because separated by a blank line
294 :     extra.regexp <- paste("^\\s*##(",paste(skeleton.fields,collapse="|"),
295 :     ")<<\\s*(.*)$",sep="")
296 :     cont.re <- "^\\s*##\\s*"
297 :     in.describe <- 0
298 :     first.describe <- FALSE
299 :     k <- 1
300 :     in.chunk <- FALSE
301 :     end.chunk <- function(field,payload)
302 :     {
303 :     if ( "alias" == field ){
304 :     ##note<< \code{alias} extras are automatically split at new lines.
305 :     payload <- gsub("\\n+","\\}\n\\\\alias\\{",payload,perl=TRUE)
306 :     chunk.sep <- "}\n\\alias{"
307 :     } else if ( "keyword" == field ){
308 :     ##keyword<< documentation utilities
309 :     ##note<< \code{keyword} extras are automatically split at white space,
310 :     ## as all the valid keywords are single words.
311 :     payload <- gsub("\\s+","\\}\n\\\\keyword\\{",payload,perl=TRUE)
312 :     chunk.sep <- "}\n\\keyword{"
313 :     } else if ( "title" == field ){
314 :     chunk.sep <- " "
315 :     } else if ( "description" == field ){
316 :     chunk.sep <- "\n"
317 :     } else {
318 :     ##details<< Each separate extra section appears as a new
319 :     ## paragraph except that: \itemize{\item empty sections (no
320 :     ## matter how many lines) are ignored;\item \code{alias} and
321 :     ## \code{keyword} sections have special rules;\item
322 :     ## \code{description} should be brief, so all such sections
323 :     ## are concatenated as one paragraph;\item \code{title} should
324 :     ## be one line, so any extra \code{title} sections are
325 :     ## concatenated as a single line with spaces separating the
326 :     ## sections.}
327 :     chunk.sep <- "\n\n"
328 :     }
329 :     chunk.res <- NULL
330 : tdhock 191 if ( !grepl("^\\s*$",payload,perl=TRUE) )
331 : tdhock 87 chunk.res <-
332 :     if ( is.null(res[[field]]) ) payload
333 :     else paste(res[[field]], payload, sep=chunk.sep)
334 :     invisible(chunk.res)
335 :     }
336 : tdhock 154 while ( k <= length(src) ){
337 :     line <- src[k]
338 : tdhock 308 ##print(line)
339 :     ##if(grepl("^$",line))browser()
340 : tdhock 191 if ( grepl(extra.regexp,line,perl=TRUE) ){
341 : tdhock 87 ## we have a new extra chunk - first get field name and any payload
342 :     new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)
343 :     new.contents <- gsub(extra.regexp,"\\2",line,perl=TRUE)
344 : tdhock 308 ##cat(new.field,"\n-----\n",new.contents,"\n\n")
345 : tdhock 87 ##details<< As a special case, the construct \code{##describe<<} causes
346 :     ## similar processing to the main function arguments to be
347 :     ## applied in order to construct a describe block within the
348 :     ## documentation, for example to describe the members of a
349 :     ## list. All subsequent "same line" \code{##<<} comments go into that
350 :     ## block until terminated by a subsequent \code{##}\emph{xxx}\code{<<} line.
351 :     if ( "describe" == new.field ){
352 :     ##details<< Such regions may be nested, but not in such a way
353 : tdhock 307 ## that the first element in a \code{describe} is another
354 :     ## \code{describe}. Thus there must be at least one
355 :     ## \code{##<<} comment between each pair of
356 :     ## \code{##describe<<} comments.
357 : tdhock 87 if ( first.describe ){
358 :     stop("consecutive ##describe<< at line",k,"in",name.fun)
359 :     } else {
360 :     if ( nzchar(new.contents) ){
361 :     if ( is.null(payload) || 0 == nzchar(payload) ){
362 :     payload <- new.contents
363 :     } else {
364 :     payload <- paste(payload,new.contents,sep="\n\n")
365 :     }
366 :     }
367 :     first.describe <- TRUE
368 :     }
369 :     } else if ( "end" == new.field ){
370 :     ##details<< When nested \code{describe} blocks are used, a comment-only
371 :     ## line with \code{##end<<} terminates the current level only; any
372 :     ## other valid \code{##}\emph{xxx}\code{<<} line terminates
373 :     ## all open describe blocks.
374 :     if ( in.describe>0 ){
375 :     ## terminate current \item and \describe block only
376 :     if ( "value" == cur.field && 1 == in.describe ){
377 :     payload <- paste(payload,"}",sep="")
378 :     } else {
379 :     payload <- paste(payload,"}\n}",sep="")
380 :     }
381 :     in.describe <- in.describe-1;
382 :     } else {
383 :     warning("mismatched ##end<< at line ",k," in ",name.fun)
384 :     }
385 :     if ( nzchar(new.contents) ){
386 :     if ( nzchar(payload) ){
387 :     payload <- paste(payload,new.contents,sep="\n")
388 :     } else {
389 :     payload <- new.contents
390 :     }
391 :     }
392 :     } else {
393 :     ## terminate all open \describe blocks (+1 because of open item)
394 :     if ( 0 < in.describe ){
395 :     if ( "value" != cur.field ){ # value is implicit describe block
396 :     payload <- paste(payload,"}",sep="")
397 :     }
398 :     while ( in.describe>0 ){
399 :     payload <- paste(payload,"}",sep="\n")
400 :     in.describe <- in.describe-1;
401 :     }
402 :     }
403 :     ## finishing any existing payload
404 :     if ( in.chunk ) res[[cur.field]] <- end.chunk(cur.field,payload)
405 :     in.chunk <- TRUE
406 :     cur.field <- new.field
407 :     payload <- new.contents
408 :     ##note<< The "value" section of a .Rd file is implicitly a describe
409 :     ## block and \code{##}\code{value}\code{<<} acts accordingly. Therefore
410 :     ## it automatically enables the describe block itemization (##<< after
411 :     ## list entries).
412 :     if ( "value" == new.field ){
413 :     first.describe <- TRUE;
414 :     }
415 :     }
416 : tdhock 191 } else if ( in.chunk && grepl(cont.re,line,perl=TRUE) ){
417 : tdhock 87 ## append this line to current chunk
418 : tdhock 191 if ( !grepl(prefix,line,perl=TRUE) ){
419 : tdhock 87 ##describe<< Any lines with "\code{### }" at the left hand
420 :     ## margin within the included chunks are handled separately,
421 :     ## so if they appear in the documentation they will appear
422 :     ## before the \code{##}\emph{xxx}\code{<}\code{<} chunks.
423 :     ### This one should not appear.
424 :     stripped <- gsub(cont.re,"",line,perl=TRUE)
425 :     if ( nzchar(payload) ){
426 :     payload <- paste(payload,stripped,sep="\n")
427 :     } else {
428 :     payload <- stripped
429 :     }
430 :     }
431 : tdhock 191 } else if ( grepl(arg.pat,line,perl=TRUE) ){
432 : tdhock 87 not.describe <- (0==in.describe && !first.describe)
433 :     if ( in.chunk && not.describe){
434 :     res[[cur.field]] <- end.chunk(cur.field,payload)
435 :     }
436 :     comment <- gsub(arg.pat,"\\3",line,perl=TRUE);
437 :     arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)
438 :     in.chunk <- TRUE
439 :     if ( not.describe ){
440 : tdhock 115 ## TDH 2010-06-18 For item{}s in the documentation list names,
441 :     ## we don't need to have a backslash before, so delete it.
442 :     arg <- gsub("^[\\]+","",arg)
443 : tdhock 336 cur.field <- gsub("...","\\dots",arg,fixed=TRUE) ##special case for dots
444 : tdhock 87 payload <- comment
445 :     } else {
446 :     ## this is a describe block, so we need to paste with existing
447 :     ## payload as a new \item.
448 :     if ( first.describe ){
449 :     ## for first item, need to add describe block starter
450 :     if ( "value" == cur.field ){
451 :     payload <- paste(payload,"\n",arg,"{",sep="")
452 :     } else {
453 :     payload <- paste(payload,"\\describe{\n",arg,"{",sep="")
454 :     }
455 :     first.describe <- FALSE
456 :     in.describe <- in.describe+1
457 :     } else {
458 :     ## subsequent item - terminate existing and start new
459 :     payload <- paste(payload,"}\n",arg,"{",sep="")
460 :     }
461 :     if ( nzchar(comment) ){
462 :     payload <- paste(payload,comment,sep="")
463 :     }
464 :     }
465 :     } else if ( in.chunk ){
466 :     if ( 0 == in.describe && !first.describe ){
467 :     ## reached an end to current field, but need to wait if in.describe
468 :     res[[cur.field]] <- end.chunk(cur.field,payload)
469 :     in.chunk <- FALSE
470 :     cur.field <- NULL
471 :     payload <- NULL
472 :     }
473 :     }
474 :     k <- k+1
475 :     }
476 :     ## finishing any existing payload
477 :     if ( 0 < in.describe ){
478 :     if ( "value" != cur.field ){ # value is implicit describe block
479 :     payload <- paste(payload,"}",sep="")
480 :     }
481 :     while ( in.describe>0 ){
482 :     payload <- paste(payload,"}",sep="\n")
483 :     in.describe <- in.describe-1;
484 :     }
485 :     }
486 :     if ( in.chunk ) res[[cur.field]] <- end.chunk(cur.field,payload)
487 :     res
488 :     ### Named list of character strings extracted from comments. For each
489 :     ### name N we will look for N\{...\} in the Rd file and replace it
490 :     ### with the string in this list (implemented in modify.Rd.file).
491 :     }
492 :    
493 : kmpont 212 leadingS3generic <- function # check whether function name is an S3 generic
494 :     ### Determines whether a function name looks like an S3 generic function
495 :     (name, ##<< name of function
496 :     env, ##<< environment to search for additional generics
497 :     ...) ##<< ignored here
498 :     {
499 :     ##details<< This function is one of the default parsers, but exposed as
500 :     ## possibly of more general interest. Given a function name of the form
501 :     ## x.y.z it looks for the generic function x applying to objects of class
502 :     ## y.z and also for generic function x.y applying to objects of class z.
503 :     ##
504 :     parts <- strsplit(name, ".", fixed = TRUE)[[1]]
505 :     l <- length(parts)
506 :     if (l > 1) {
507 :     for (i in 1:(l - 1)) {
508 :     ## Look for a generic function (known by the system or defined
509 :     ## in the package) that matches that part of the function name
510 :     generic <- paste(parts[1:i], collapse = ".")
511 : tdhock 389 if (any(generic %in% getKnownS3generics()) ||
512 :     findGeneric(generic, env) != "") {
513 : kmpont 305 object <- paste(parts[(i + 1):l], collapse = ".")
514 : kmpont 212 ##details<< Assumes that the first name which matches any known
515 :     ## generics is the target generic function, so if both x and x.y
516 :     ## are generic functions, will assume generic x applying to objects
517 :     ## of class y.z
518 :     ##value<< If a matching generic found returns a list with a single component:
519 :     return(list(.s3method=c(generic, object))) ##<< a character vector containing generic name and object name.
520 :     }
521 :     }
522 :     }
523 :     ##value<< If no matching generic functions are found, returns an empty list.
524 :     list()
525 :     }
526 :    
527 : tdhock 154 ### Parsers for each function that are constructed automatically. This
528 :     ### is a named list, and each element is a parser function for an
529 :     ### individual object.
530 :     forfun.parsers <-
531 :     list(prefixed.lines=prefixed.lines,
532 :     extract.xxx.chunks=extract.xxx.chunks,
533 :     ## title from first line of function def
534 : tdhock 191 title.from.firstline=function(src,...){
535 : tdhock 195 first <- src[1]
536 :     if(!is.character(first))return(list())
537 :     if(!grepl("#",first))return(list())
538 :     list(title=gsub("[^#]*#\\s*(.*)","\\1",first,perl=TRUE))
539 : tdhock 154 },
540 :     ## PhG: it is tests/FUN.R!!! I would like more flexibility here
541 :     ## please, let me choose which dir to use for examples!
542 :     ## Get examples for FUN from the file tests/FUN.R
543 :     examples.from.testfile=function(name,...){
544 :     tsubdir <- getOption("inlinedocs.exdir")
545 :     if (is.null(tsubdir)) tsubdir <- "tests" # Default value
546 :     tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))
547 :     if(file.exists(tfile))
548 : tdhock 185 list(examples=readLines(tfile))
549 : tdhock 154 else list()
550 :     },
551 :     definition.from.source=function(doc,src,...){
552 :     def <- doc$definition
553 :     is.empty <- function(x)is.null(x)||x==""
554 :     if(is.empty(def) && !is.empty(src))
555 : tdhock 185 list(definition=src)
556 : tdhock 154 else list()
557 :     })
558 :    
559 :     ### List of Parser Functions that can be applied to any object.
560 :     forall.parsers <-
561 :     list(## Fill in author from DESCRIPTION and titles.
562 :     author.from.description=function(desc,...){
563 : tdhock 308 list(author=desc[,"Author"])
564 : tdhock 154 },
565 :     ## The format section sometimes causes problems, so erase it.
566 :     erase.format=function(...){
567 :     list(format="")
568 :     },
569 :     ## Convert the function name to a title.
570 :     title.from.name=function(name,doc,...){
571 :     if("title"%in%names(doc))list() else
572 :     list(title=gsub("[._]"," ",name))
573 :     },
574 :     ## PhG: here is what I propose for examples code in the 'ex' attribute
575 :     examples.in.attr = function (name, o, ...) {
576 :     ex <- attr(o, "ex")
577 :     if (!is.null(ex)) {
578 :     ## Special case for code contained in a function
579 :     if (inherits(ex, "function")) {
580 :     ## If source is available, start from there
581 : tdhock 302 src <- getSource(ex)
582 : tdhock 154 if (!is.null(src)) {
583 :     ex <- src
584 :     } else { ## Use the body of the function
585 :     ex <- deparse(body(ex))
586 :     }
587 :     ## Eliminate leading and trailing code
588 :     ex <- ex[-c(1, length(ex))]
589 : tdhock 303 if(ex[1]=="{")ex <- ex[-1]
590 : tdhock 163 ## all the prefixes
591 :     ex <- kill.prefix.whitespace(ex)
592 : tdhock 154 ## Add an empty line before and after example
593 :     ex <- c("", ex, "")
594 :     }
595 : tdhock 185 list(examples = ex)
596 : tdhock 154 } else list()
597 : tdhock 200 },collapse=function(doc,...){
598 :     L <- lapply(doc,paste,collapse="\n")
599 :     L$.overwrite <- TRUE
600 :     L
601 : kmpont 212 },tag.s3methods=leadingS3generic
602 : tdhock 196 )
603 : tdhock 154
604 :     ### List of parser functions that operate on single objects. This list
605 :     ### is useful for testing these functions.
606 : tdhock 196 lonely <- structure(c(forall.parsers,forfun.parsers),ex=function(){
607 : tdhock 154 f <- function # title
608 :     ### description
609 :     (x, ##<< arg x
610 :     y
611 :     ### arg y
612 :     ){
613 :     ##value<< a list with elements
614 :     list(x=x, ##<< original x value
615 :     y=y, ##<< original y value
616 :     sum=x+y) ##<< their sum
617 :     ##end<<
618 :     }
619 : tdhock 302 src <- getSource(f)
620 : tdhock 154 lonely$extract.xxx.chunks(src)
621 :     lonely$prefixed.lines(src)
622 : tdhock 196 })
623 : tdhock 154
624 :     extra.code.docs <- function # Extract documentation from code chunks
625 :     ### Parse R code to extract inline documentation from comments around
626 :     ### each function. These are not able to be retreived simply by
627 :     ### looking at the "source" attribute. This is a Parser Function that
628 :     ### can be used in the parser list of package.skeleton.dx(). TODO:
629 :     ### Modularize this into separate Parsers Functions for S4 classes,
630 :     ### prefixes, ##<<blocks, etc. Right now it is not very clean!
631 :     (code,
632 :     ### Code lines in a character vector containing multiple R objects to
633 :     ### parse for documentation.
634 :     objs,
635 :     ### The objects defined in the code.
636 :     ...
637 :     ### ignored
638 :     ){
639 :     parsed <- extract.file.parse(code)
640 :     extract.docs.try <- function(o,on)
641 :     {
642 :     ## Note: we could use parsed information here too, but that
643 :     ## would produce different results for setMethodS3 etc.
644 :     doc <- list()
645 :     if ( !is.null(parsed[[on]]) ){
646 :     if ( !is.na(parsed[[on]]@code[1]) ){ # no code given for generics
647 : tdhock 185 doc$definition <- paste(parsed[[on]]@code)
648 : tdhock 154 }
649 :     if(!"description"%in%names(doc) && !is.na(parsed[[on]]@description) ){
650 :     doc$description <- parsed[[on]]@description
651 :     }
652 : kmpont 212 ## if ( "setMethodS3" == parsed[[on]]@created ){
653 :     ## gen <- leadingS3generic(on,topenv())
654 :     ## if ( 0 < length(gen) ){
655 :     ## doc$.s3method <- gen$.s3method
656 :     ## cat("S3method(",gen$.s3method[1],",",gen$.s3method[2],")\n",sep="")
657 :     ## }
658 :     ## }
659 : tdhock 154 }
660 :     if("title" %in% names(doc) && !"description" %in% names(doc) ){
661 :     ## For short functions having both would duplicate, but a
662 :     ## description is required. Therefore automatically copy title
663 :     ## across to avoid errors at package build time.
664 :     doc$description <- doc$title
665 :     }
666 :     doc
667 :     }
668 :     extract.docs <- function(on){
669 :     res <- try({o <- objs[[on]]
670 :     extract.docs.try(o, on)},FALSE)
671 :     if(class(res)=="try-error"){
672 :     cat("Failed to extract docs for: ",on,"\n\n")
673 :     list()
674 :     } else if(0 == length(res) && inherits(objs[[on]],"standardGeneric")){
675 :     NULL
676 :     } else if(0 == length(res) && "function" %in% class(o)
677 : tdhock 302 && 1 == length(osource <- getSource(o))
678 : tdhock 191 && grepl(paste("UseMethod(",on,")",sep="\""),osource)
679 : tdhock 154 ){
680 :     ## phew - this should only pick up R.oo S3 generic definitions like:
681 :     ## attr(*, "source")= chr "function(...) UseMethod(\"select\")"
682 :     NULL
683 :     } else res
684 :     }
685 :     doc.names <- names(objs)
686 :     res <- sapply(doc.names,extract.docs,simplify=FALSE)
687 :     ## Special processing for S4 classes as they do not appear in normal ls()
688 :     for ( nn in names(parsed) ){
689 :     if ( parsed[[nn]]@created == "setClass" ){
690 :     S4class.docs <- extract.docs.setClass(parsed[[nn]])
691 :     docname <- paste(nn,"class",sep="-")
692 :     if ( is.null(res[[docname]]) ){
693 :     res[[docname]] <- S4class.docs
694 :     doc.names <- c(doc.names,docname)
695 :     } else {
696 :     stop(nn," appears as both S4 class and some other definition")
697 :     }
698 :     }
699 :     }
700 :     inherit.docs <- function(on){
701 :     in.res <- res[[on]]
702 :     if ( !is.null(parsed[[on]]) ){
703 :     for ( parent in parsed[[on]]@parent ){
704 :     if ( !is.na(parent) ){
705 :     if ( is.null(in.res) ){
706 :     in.res <- res[[parent]]
707 :     } else if ( parent %in% names(res) ){
708 :     parent.docs <- res[[parent]]
709 :     for ( nn in names(parent.docs) ){
710 :     if ( !nn %in% names(in.res) ){
711 :     in.res[[nn]] <- parent.docs[[nn]]
712 :     }
713 :     }
714 :     }
715 :     }
716 :     }
717 :     }
718 :     invisible(in.res)
719 :     }
720 :     all.done <- FALSE
721 :     while ( !all.done ){
722 :     res1 <- sapply(doc.names,inherit.docs,simplify=FALSE)
723 :     all.done <- identical(res1,res)
724 :     res <- res1
725 :     }
726 :     ## now strip out any generics (which have value NULL in res):
727 :     res.not.null <- sapply(res,function(x){!is.null(x)})
728 :     if ( 0 < length(res.not.null) && length(res.not.null) < length(res) ){
729 :     res <- res[res.not.null]
730 :     }
731 :     res
732 :     ### named list of lists, one for each object to document.
733 :     }
734 :    
735 :     ### List of parsers to use by default with package.skeleton.dx.
736 :     default.parsers <-
737 :     c(extra.code.docs=extra.code.docs, ## TODO: cleanup!
738 :     sapply(forfun.parsers,forfun),
739 :     edit.package.file=function(desc,...){
740 :     in.details <- setdiff(colnames(desc),"Description")
741 : tdhock 185 details <- sprintf("%s: \\tab %s\\cr",in.details,desc[,in.details])
742 : tdhock 154 L <-
743 :     list(list(title=desc[,"Title"],
744 :     description=desc[,"Description"],
745 : tdhock 195 `tabular{ll}`=details))
746 : tdhock 154 names(L) <- paste(desc[,"Package"],"-package",sep="")
747 :     L
748 : tdhock 197 },
749 :     sapply(forall.parsers,forall)
750 : tdhock 186 )
751 : tdhock 154
752 : tdhock 87 setClass("DocLink", # Link documentation among related functions
753 :     ### The \code{.DocLink} class provides the basis for hooking together
754 :     ### documentation of related classes/functions/objects. The aim is that
755 : kmpont 212 ### documentation sections missing from the child are inherited from
756 :     ### the parent class.
757 : tdhock 87 representation(name="character", ##<< name of object
758 :     created="character", ##<< how created
759 :     parent="character", ##<< parent class or NA
760 :     code="character", ##<< actual source lines
761 :     description="character") ##<< preceding description block
762 :     )
763 :    
764 :     extract.file.parse <- function # File content analysis
765 : tdhock 356 ### Using the base \code{parse} function, analyse the file to link
766 : tdhock 87 ### preceding "prefix" comments to each active chunk. Those comments form
767 :     ### the default description for that chunk. The analysis also looks for
768 :     ### S4 class "setClass" calls and R.oo setConstructorS3 and setMethodS3
769 :     ### calls in order to link the documentation of those properly.
770 :     (code
771 :     ### Lines of R source code in a character vector - note that any
772 :     ### nested \code{source} statements are \emph{ignored} when scanning
773 :     ### for class definitions.
774 :     ){
775 :     res <- list()
776 :     old.opt <- options(keep.source=TRUE)
777 :     parsed <- try(parse(text=code))
778 :     options(old.opt)
779 :     if ( inherits(parsed,"try-error") ){
780 :     stop("parse failed with error:\n",parsed)
781 :     }
782 :     chunks <- attr(parsed,"srcref")
783 :     last.end <- 0
784 :     for ( k in 1:length(parsed) ){
785 :     start <- chunks[[k]][1]
786 :     ##details<< If the definition chunk does not contain a
787 :     ## description, any immediately preceding sequence consecutive
788 :     ## "prefix" lines will be used instead.
789 :     default.description <- NULL
790 :     while ( start > last.end+1
791 : tdhock 191 && grepl(prefix,code[start-1],perl=TRUE) ){
792 : tdhock 87 start <- start-1
793 :     }
794 :     if ( start < chunks[[k]][1] ){
795 :     default.description <- decomment(code[start:(chunks[[k]][1]-1)])
796 :     } else {
797 :     default.description <- NA_character_;
798 :     }
799 :     ##details<< Class and method definitions can take several forms,
800 :     ## determined by expression type: \describe{
801 :     ## \item{assignment (<-)}{Ordinary assignment of value/function;}
802 :     ## \item{setClass}{Definition of S4 class;}
803 :     ## \item{setConstructorS3}{Definition of S3 class using R.oo package;}
804 :     ## \item{setMethodS3}{Definition of method for S3 class using R.oo package.}}
805 :     ## Additionally, the value may be a name of a function defined elsewhere,
806 :     ## in which case the documentation should be copied from that other definition.
807 :     ## This is handled using the concept of documentation links.
808 :     lang <- parsed[[k]]
809 :     chars <- as.character(lang)
810 :     expr.type <- chars[1]
811 :     parent <- NA_character_
812 :    
813 :     if ( expr.type == "<-" || expr.type == "setConstructorS3" || expr.type == "setClass" ){
814 :     object.name <- chars[2]
815 :     ## If the function definition is not embedded within the call, then
816 :     ## the parent is that function. Test whether the the third value
817 :     ## looks like a name and add it to parents if so.
818 : tdhock 191 if ( grepl("^[\\._\\w]+$",chars[3],perl=TRUE) ){
819 : tdhock 87 parent <- chars[3]
820 :     }
821 :     res[[object.name]] <- new("DocLink",name=object.name,
822 :     created=expr.type,
823 :     parent=parent,
824 :     code=paste(chunks[[k]],sep=""),
825 :     description=default.description)
826 : tomaschwut 383 } else if ( expr.type == "setMethodS3" || expr.type == "R.methodsS3::setMethodS3"){
827 : tdhock 87 ##details<< The \code{setMethodS3} calls introduce additional
828 :     ## complexity: they will define an additional S3 generic (which
829 :     ## needs documentation to avoid warnings at package build time)
830 :     ## unless one already exists. This also is handled by "linking"
831 :     ## documentation. A previously unseen generic is linked to the
832 :     ## first defining instances, subsequent definitions of that generic
833 :     ## also link back to the first defining instance.
834 :     generic.name <- chars[2]
835 :     object.name <- paste(generic.name,chars[3],sep=".")
836 :     if ( is.null(res[[generic.name]]) ){
837 : tdhock 356 ## TDH 9 April 2012 Do NOT add \\link in generic.desc below,
838 :     ## since it causes problems on R CMD check.
839 :     ##* checking Rd cross-references ... WARNING
840 :     ##Error in find.package(package, lib.loc) :
841 :     ## there is no package called ‘MASS’
842 :     ##Calls: <Anonymous> -> lapply -> FUN -> find.package
843 :    
844 :     generic.desc <-
845 :     paste("Generic method behind \\code{",object.name,"}",sep="")
846 : tdhock 87 res[[generic.name]] <- new("DocLink",
847 :     name=generic.name,
848 :     created=expr.type,
849 :     parent=object.name,
850 :     code=NA_character_,
851 :     description=generic.desc)
852 :     } else {
853 :     parent <- res[[generic.name]]@parent
854 :     }
855 :     ## If the function definition is not embedded within the call, then
856 :     ## the parent is that function. Test whether the the fourth value
857 :     ## looks like a name and add it to parents if so.
858 : tdhock 191 if ( grepl("^[\\._\\w]+$",chars[4],perl=TRUE) ){
859 : tdhock 87 parent <- c(chars[4],parent)
860 :     }
861 :     res[[object.name]] <- new("DocLink",name=object.name,
862 :     created=expr.type,
863 :     parent=parent,
864 :     code=paste(chunks[[k]],sep=""),
865 :     description=default.description)
866 :     } else {
867 :     ## Not sure what to do with these yet. Need to deal with setMethod, setAs etc.
868 :     }
869 :     }
870 :     invisible(res)
871 :     ### Returns an invisible list of .DocLink objects.
872 :     }
873 :    
874 :     extract.docs.setClass <- function # S4 class inline documentation
875 :     ### Using the same conventions as for functions, definitions of S4 classes
876 :     ### in the form \code{setClass("classname",\dots)} are also located and
877 :     ### scanned for inline comments.
878 : tdhock 93 (doc.link
879 : tdhock 356 ### DocLink object as created by \code{extract.file.parse}.
880 : tdhock 87 ### Note that \code{source} statements are \emph{ignored} when scanning for
881 :     ### class definitions.
882 : tdhock 93 ){
883 : tdhock 87 chunk.source <- doc.link@code
884 :     ##details<<
885 :     ## Extraction of S4 class documentation is currently limited to expressions
886 :     ## within the source code which have first line starting with
887 :     ## \code{setClass("classname"}. These are located from the source file
888 :     ## (allowing also for white space around the \code{setClass} and \code{(}).
889 :     ## Note that \code{"classname"} must be a quoted character string;
890 :     ## expressions returning such a string are not matched.
891 :     class.name <- doc.link@name
892 :    
893 :     ##details<< For class definitions, the slots (elements of the
894 :     ## \code{representation} list) fill the role of function
895 :     ## arguments, so may be documented by \code{##<<} comments on
896 :     ## the same line or \code{### } comments at the beginning of the
897 :     ## following line.
898 :     f.n <- paste(class.name,"class",sep="-")
899 : tdhock 154 docs <- extract.xxx.chunks(chunk.source,f.n)
900 : tdhock 138 ## also apply source parsing functions that I separated out into
901 :     ## separate functions
902 :     docs <- combine(docs,lonely$prefixed.lines(chunk.source))
903 :     docs$title <- lonely$title.from.firstline(chunk.source)
904 : tdhock 87 ##details<<
905 : kmpont 305 ## If there is no explicit title on the first line of setClass, then
906 :     ## one is made up from the class name.
907 :     if ( 0 == length(docs$title) ){
908 :     docs$title <- list(title=paste(class.name,"S4 class"))
909 :     }
910 :     ##details<<
911 : tdhock 87 ## The class definition skeleton includes an \code{Objects from the Class}
912 :     ## section, to which any \code{##details<<} documentation chunks are
913 :     ## written. It is given a vanilla content if there are no specific
914 :     ## \code{##details<<} documentation chunks.
915 :     if ( is.null(docs[["details"]]) ){
916 :     docs[["details"]] <-
917 :     paste("Objects can be created by calls of the form \\code{new(",
918 :     class.name," ...)}",sep="")
919 :     }
920 :     docs[["section{Objects from the Class}"]] <- docs[["details"]]
921 :     ## seealso has a skeleton line not marked by ~ .. ~, so have to suppress
922 :     if ( is.null(docs[["seealso"]]) ){
923 :     docs[["seealso"]] <- ""
924 :     }
925 :     if ( is.null(docs[["alias"]]) ){
926 :     docs[["alias"]] <- class.name
927 :     }
928 :     if ( is.null(docs[["description"]]) ){
929 :     docs[["description"]] <- doc.link@description
930 :     }
931 :     invisible(docs)
932 :     }
933 : tdhock 105
934 : tdhock 109 apply.parsers <- function
935 :     ### Parse code to r objs, then run all the parsers and return the
936 :     ### documentation list.
937 : tdhock 105 (code,
938 :     ### Character vector of code lines.
939 : tdhock 109 parsers=default.parsers,
940 : tdhock 105 ### List of Parser Functions.
941 : tdhock 109 verbose=FALSE,
942 : tdhock 105 ### Echo names of Parser Functions?
943 :     ...
944 :     ### Additional arguments to pass to Parser Functions.
945 :     ){
946 :     e <- new.env()
947 : kmpont 212 ## KMP 2011-03-09 fix problem with DocLink when inlinedocs ran on itself
948 :     ## Error in assignClassDef(Class, classDef, where) :
949 :     ## Class "DocLink" has a locked definition in package "inlinedocs"
950 :     ## Traced to "where" argument in setClassDef which defaults to topenv()
951 :     ## which in turn is inlinedocs when processing inlinedocs package, hence
952 :     ## the clash. The following works (under R 2.12.2), so that the topenv()
953 :     ## now finds e before finding the inlinedocs environment.
954 : kmpont 213 old <- options(keep.source=TRUE,topLevelEnvironment=e)
955 : tdhock 109 on.exit(options(old))
956 : tdhock 110 exprs <- parse(text=code)
957 : tdhock 248 ## TDH 2011-04-07 set this so that no warnings about creating a fake
958 :     ## package when we try to process S4 classes defined in code
959 :     e$.packageName <- "inlinedocs.processor"
960 : tdhock 152 for (i in exprs){
961 : tdhock 248 eval(i, e)
962 : tdhock 152 }
963 : tdhock 105 objs <- sapply(ls(e),get,e,simplify=FALSE)
964 :    
965 :     docs <- list()
966 : kmpont 305
967 : tdhock 109 ## apply parsers in sequence to code and objs
968 : tdhock 312 if(verbose)cat("Applying parsers:\n")
969 : tdhock 105 for(i in seq_along(parsers)){
970 :     N <- names(parsers[i])
971 : markus 370 #mm if(verbose){
972 : tdhock 105 if(is.character(N) && N!=""){
973 : markus 370 cat(" this is parser:",N,"\n",sep="")
974 : tdhock 312 }else cat('.\n')
975 : markus 370 #mm }
976 : tdhock 105 p <- parsers[[i]]
977 :     ## This is the argument list that each parser receives:
978 : tdhock 200 L <- p(code=code,objs=objs,docs=docs,env=e,...)
979 : markus 370 # print("mm point1")
980 :     #save(docs,L,file="/home/mm/SoilR/scripts/docs_L.RData")
981 : markus 324 #print(paste(L,"\n"))
982 : tdhock 335 #if(N=="exclude")browser()
983 : markus 370 docs <- combine(docs,L) #mm
984 : tdhock 105 }
985 : tdhock 196 ## post-process to collapse all character vectors
986 :     for(i in seq_along(docs)){
987 :     for(j in seq_along(docs[[i]])){
988 : tdhock 198 if(names(docs[[i]])[j]!=".s3method")
989 : tdhock 196 docs[[i]][[j]] <- paste(docs[[i]][[j]],collapse="\n")
990 :     }
991 : markus 324 }
992 : tdhock 105 if(verbose)cat("\n")
993 : markus 324 return(docs)
994 : tdhock 105 ### A list of extracted documentation from code.
995 :     }
996 : tdhock 109
997 : tdhock 138 ### Names of Parser Functions that operate on the desc arg.
998 :     descfile.names <- c("author.from.description","edit.package.file")
999 :    
1000 :     ### Names of Parser Functions that do NOT use the desc arg.
1001 :     non.descfile.names <-
1002 :     names(default.parsers)[!names(default.parsers)%in%descfile.names]
1003 :    
1004 : tdhock 118 ### Parsers that operate only on R code, independently of the
1005 :     ### description file.
1006 : tdhock 138 nondesc.parsers <- default.parsers[non.descfile.names]
1007 : tdhock 118
1008 : tdhock 209 extract.docs.file <- structure(function
1009 : tdhock 109 ### Apply all parsers relevant to extract info from just 1 code file.
1010 :     (f,
1011 :     ### File name of R code to read and parse.
1012 : tdhock 203 parsers=NULL,
1013 : tdhock 109 ### Parser Functions to use to parse the code and extract
1014 :     ### documentation.
1015 :     ...
1016 :     ### Other arguments to pass to Parser Functions.
1017 :     ){
1018 : tdhock 203 if(is.null(parsers))parsers <- nondesc.parsers
1019 : tdhock 109 apply.parsers(readLines(f),parsers,verbose=FALSE,...)
1020 : tdhock 209 },ex=function(){
1021 :     f <- system.file("silly","R","silly.R",package="inlinedocs")
1022 : tdhock 248 extract.docs.file(f)
1023 : tdhock 209 })
1024 : tdhock 248

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