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

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