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

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

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