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 89 - (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 :     for(N in names(objs)){
16 :     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 :     default.parsers <- c(sapply(forall.parsers,function(L)L[[1]](L[[2]])),
184 :     extract.docs.file=extract.docs.file)
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 :     )
194 :     {
195 :     res <- list()
196 :     clines <- grep(prefix,code)
197 :     if(length(grep("#",code[1]))){
198 :     res$title <- gsub("[^#]*#\\s*(.*)","\\1",code[1],perl=TRUE)
199 :     }
200 :     if(length(clines) > 0){
201 :     ##details<<
202 :     ## The primary mechanism is that consecutive groups of lines matching
203 :     ## the specified prefix regular expression "\code{^### }" (i.e. lines
204 :     ## beginning with "\code{### }") are collected
205 :     ## as follows into documentation sections:\describe{
206 :     ## \item{description}{group starting at line 2 in the code}
207 :     ## \item{arguments}{group following each function argument}
208 :     ## \item{value}{group ending at the penultimate line of the code}}
209 :     ## These may be added to by use of the \code{##<<} constructs described
210 :     ## below.
211 :     bounds <- which(diff(clines)!=1)
212 :     starts <- c(1,bounds+1)
213 :     ends <- c(bounds,length(clines))
214 :     for(i in seq_along(starts)){
215 :     start <- clines[starts[i]]
216 :     end <- clines[ends[i]]
217 :     lab <- if(end+1==length(code))"value"
218 :     else if(start==2)"description"
219 :     else if ( 0 == length(grep("^\\s*#",code[start-1],perl=TRUE)) ){
220 :     #arg <- gsub("^[ (]*","",code[start-1])
221 :     #arg <- gsub("^([^=,]*)[=,].*","\\1",arg)
222 :     #arg <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots
223 :     arg <- gsub("^[ \t(,]*", "", code[start - 1]) #twutz: strip leading white spaces and brackets and ,
224 :     arg <- gsub("^([^=,]*)[=,].*", "\\1", arg)
225 :     arg <- gsub("^([^ \t]*)([ \t]+)$","\\1",arg) #twutz: remove trailing whitespaces
226 :     arg <- gsub("...", "\\dots", arg, fix = TRUE)
227 :     paste("item{",arg,"}",sep="")
228 :     } else {
229 :     next;
230 :     }
231 :     res[[lab]] <- decomment(code[start:end])
232 :     }
233 :     }
234 :     ##details<< For simple functions/arguments, the argument may also be
235 :     ## documented by appending \code{##<<} comments on the same line as the
236 :     ## argument name. Mixing this mechanism with \code{###} comment lines for
237 :     ## the same argument is likely to lead to confusion, as the \code{###}
238 :     ## lines are processed first.
239 :     #arg.pat <- paste("^[^=,#]*?([\\w\\.]+)\\s*([=,].*|\\)\\s*)?",
240 :     # "<<\\s*(\\S.*?)\\s*$",
241 :     # sep="##") # paste avoids embedded trigger fooling the system
242 :     #tw: removed first comma
243 :     arg.pat <- paste("^[^=#]*?([\\w\\.]+)\\s*([=,].*|\\)\\s*)?",
244 :     "<<\\s*(\\S.*?)\\s*$",
245 :     sep="##") # paste avoids embedded trigger fooling the system
246 :    
247 :     skeleton.fields <- c("alias","details","keyword","references","author",
248 :     "note","seealso","value","title","description",
249 :     "describe","end")
250 :     ##details<< Additionally, consecutive sections of \code{##} comment
251 :     ## lines beginning with \code{##}\emph{xxx}\code{<<} (where
252 :     ## \emph{xxx} is one of the fields: \code{alias}, \code{details},
253 :     ## \code{keyword}, \code{references}, \code{author}, \code{note},
254 :     ## \code{seealso}, \code{value}, \code{title} or \code{description})
255 :     ## are accumulated and inserted in the relevant part of the .Rd
256 :     ## file.
257 :     ##
258 :     ## For \code{value}, \code{title}, \code{description} and function
259 :     ## arguments, these \emph{append} to any text from "prefix"
260 :     ## (\code{^### }) comment lines, irrespective of the order in the
261 :     ## source.
262 :     ##
263 :     ## When documenting S4 classes, documentation from \code{details}
264 :     ## sections will appear under a section \code{Objects from the Class}. That
265 :     ## section typically includes information about construction methods
266 :     ## as well as other description of class objects (but note that the
267 :     ## class Slots are documented in a separate section).
268 :    
269 :     ## but this should not appear, because separated by a blank line
270 :     extra.regexp <- paste("^\\s*##(",paste(skeleton.fields,collapse="|"),
271 :     ")<<\\s*(.*)$",sep="")
272 :     cont.re <- "^\\s*##\\s*"
273 :     in.describe <- 0
274 :     first.describe <- FALSE
275 :     k <- 1
276 :     in.chunk <- FALSE
277 :     end.chunk <- function(field,payload)
278 :     {
279 :     if ( "alias" == field ){
280 :     ##note<< \code{alias} extras are automatically split at new lines.
281 :     payload <- gsub("\\n+","\\}\n\\\\alias\\{",payload,perl=TRUE)
282 :     chunk.sep <- "}\n\\alias{"
283 :     } else if ( "keyword" == field ){
284 :     ##keyword<< documentation utilities
285 :     ##note<< \code{keyword} extras are automatically split at white space,
286 :     ## as all the valid keywords are single words.
287 :     payload <- gsub("\\s+","\\}\n\\\\keyword\\{",payload,perl=TRUE)
288 :     chunk.sep <- "}\n\\keyword{"
289 :     } else if ( "title" == field ){
290 :     chunk.sep <- " "
291 :     } else if ( "description" == field ){
292 :     chunk.sep <- "\n"
293 :     } else {
294 :     ##details<< Each separate extra section appears as a new
295 :     ## paragraph except that: \itemize{\item empty sections (no
296 :     ## matter how many lines) are ignored;\item \code{alias} and
297 :     ## \code{keyword} sections have special rules;\item
298 :     ## \code{description} should be brief, so all such sections
299 :     ## are concatenated as one paragraph;\item \code{title} should
300 :     ## be one line, so any extra \code{title} sections are
301 :     ## concatenated as a single line with spaces separating the
302 :     ## sections.}
303 :     chunk.sep <- "\n\n"
304 :     }
305 :     chunk.res <- NULL
306 :     if ( 0 == length(grep("^\\s*$",payload,perl=TRUE)) )
307 :     chunk.res <-
308 :     if ( is.null(res[[field]]) ) payload
309 :     else paste(res[[field]], payload, sep=chunk.sep)
310 :     invisible(chunk.res)
311 :     }
312 :     while ( k <= length(code) ){
313 :     line <- code[k]
314 :     if ( 0 < length(grep(extra.regexp,line,perl=TRUE) ) ){
315 :     ## we have a new extra chunk - first get field name and any payload
316 :     new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)
317 :     new.contents <- gsub(extra.regexp,"\\2",line,perl=TRUE)
318 :    
319 :     ##details<< As a special case, the construct \code{##describe<<} causes
320 :     ## similar processing to the main function arguments to be
321 :     ## applied in order to construct a describe block within the
322 :     ## documentation, for example to describe the members of a
323 :     ## list. All subsequent "same line" \code{##<<} comments go into that
324 :     ## block until terminated by a subsequent \code{##}\emph{xxx}\code{<<} line.
325 :     if ( "describe" == new.field ){
326 :     ##details<< Such regions may be nested, but not in such a way
327 :     ## that the first element in a \code{describe} is another \code{describe}.
328 :     ## Thus there must be at least one \code{##<<} comment between each
329 :     ## pair of \code{##describe<<} comments.
330 :     if ( first.describe ){
331 :     stop("consecutive ##describe<< at line",k,"in",name.fun)
332 :     } else {
333 :     if ( nzchar(new.contents) ){
334 :     if ( is.null(payload) || 0 == nzchar(payload) ){
335 :     payload <- new.contents
336 :     } else {
337 :     payload <- paste(payload,new.contents,sep="\n\n")
338 :     }
339 :     }
340 :     first.describe <- TRUE
341 :     }
342 :     } else if ( "end" == new.field ){
343 :     ##details<< When nested \code{describe} blocks are used, a comment-only
344 :     ## line with \code{##end<<} terminates the current level only; any
345 :     ## other valid \code{##}\emph{xxx}\code{<<} line terminates
346 :     ## all open describe blocks.
347 :     if ( in.describe>0 ){
348 :     ## terminate current \item and \describe block only
349 :     if ( "value" == cur.field && 1 == in.describe ){
350 :     payload <- paste(payload,"}",sep="")
351 :     } else {
352 :     payload <- paste(payload,"}\n}",sep="")
353 :     }
354 :     in.describe <- in.describe-1;
355 :     } else {
356 :     warning("mismatched ##end<< at line ",k," in ",name.fun)
357 :     }
358 :     if ( nzchar(new.contents) ){
359 :     if ( nzchar(payload) ){
360 :     payload <- paste(payload,new.contents,sep="\n")
361 :     } else {
362 :     payload <- new.contents
363 :     }
364 :     }
365 :     } else {
366 :     ## terminate all open \describe blocks (+1 because of open item)
367 :     if ( 0 < in.describe ){
368 :     if ( "value" != cur.field ){ # value is implicit describe block
369 :     payload <- paste(payload,"}",sep="")
370 :     }
371 :     while ( in.describe>0 ){
372 :     payload <- paste(payload,"}",sep="\n")
373 :     in.describe <- in.describe-1;
374 :     }
375 :     }
376 :     ## finishing any existing payload
377 :     if ( in.chunk ) res[[cur.field]] <- end.chunk(cur.field,payload)
378 :     in.chunk <- TRUE
379 :     cur.field <- new.field
380 :     payload <- new.contents
381 :     ##note<< The "value" section of a .Rd file is implicitly a describe
382 :     ## block and \code{##}\code{value}\code{<<} acts accordingly. Therefore
383 :     ## it automatically enables the describe block itemization (##<< after
384 :     ## list entries).
385 :     if ( "value" == new.field ){
386 :     first.describe <- TRUE;
387 :     }
388 :     }
389 :     } else if ( in.chunk && 0<length(grep(cont.re,line,perl=TRUE)) ){
390 :     ## append this line to current chunk
391 :     if ( 0 == length(grep(prefix,line,perl=TRUE)) ){
392 :     ##describe<< Any lines with "\code{### }" at the left hand
393 :     ## margin within the included chunks are handled separately,
394 :     ## so if they appear in the documentation they will appear
395 :     ## before the \code{##}\emph{xxx}\code{<}\code{<} chunks.
396 :     ### This one should not appear.
397 :     stripped <- gsub(cont.re,"",line,perl=TRUE)
398 :     if ( nzchar(payload) ){
399 :     payload <- paste(payload,stripped,sep="\n")
400 :     } else {
401 :     payload <- stripped
402 :     }
403 :     }
404 :     } else if ( 0 < length(grep(arg.pat,line,perl=TRUE)) ){
405 :     not.describe <- (0==in.describe && !first.describe)
406 :     if ( in.chunk && not.describe){
407 :     res[[cur.field]] <- end.chunk(cur.field,payload)
408 :     }
409 :     comment <- gsub(arg.pat,"\\3",line,perl=TRUE);
410 :     arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)
411 :     in.chunk <- TRUE
412 :     if ( not.describe ){
413 :     cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots
414 :     payload <- comment
415 :     } else {
416 :     ## this is a describe block, so we need to paste with existing
417 :     ## payload as a new \item.
418 :     if ( first.describe ){
419 :     ## for first item, need to add describe block starter
420 :     if ( "value" == cur.field ){
421 :     payload <- paste(payload,"\n",arg,"{",sep="")
422 :     } else {
423 :     payload <- paste(payload,"\\describe{\n",arg,"{",sep="")
424 :     }
425 :     first.describe <- FALSE
426 :     in.describe <- in.describe+1
427 :     } else {
428 :     ## subsequent item - terminate existing and start new
429 :     payload <- paste(payload,"}\n",arg,"{",sep="")
430 :     }
431 :     if ( nzchar(comment) ){
432 :     payload <- paste(payload,comment,sep="")
433 :     }
434 :     }
435 :     } else if ( in.chunk ){
436 :     if ( 0 == in.describe && !first.describe ){
437 :     ## reached an end to current field, but need to wait if in.describe
438 :     res[[cur.field]] <- end.chunk(cur.field,payload)
439 :     in.chunk <- FALSE
440 :     cur.field <- NULL
441 :     payload <- NULL
442 :     }
443 :     }
444 :     k <- k+1
445 :     }
446 :     ## finishing any existing payload
447 :     if ( 0 < in.describe ){
448 :     if ( "value" != cur.field ){ # value is implicit describe block
449 :     payload <- paste(payload,"}",sep="")
450 :     }
451 :     while ( in.describe>0 ){
452 :     payload <- paste(payload,"}",sep="\n")
453 :     in.describe <- in.describe-1;
454 :     }
455 :     }
456 :     if ( in.chunk ) res[[cur.field]] <- end.chunk(cur.field,payload)
457 :     res
458 :     ### Named list of character strings extracted from comments. For each
459 :     ### name N we will look for N\{...\} in the Rd file and replace it
460 :     ### with the string in this list (implemented in modify.Rd.file).
461 :     }
462 :    
463 :     setClass("DocLink", # Link documentation among related functions
464 :     ### The \code{.DocLink} class provides the basis for hooking together
465 :     ### documentation of related classes/functions/objects. The aim is that
466 :     ### documentation sections missing from the child are
467 :     representation(name="character", ##<< name of object
468 :     created="character", ##<< how created
469 :     parent="character", ##<< parent class or NA
470 :     code="character", ##<< actual source lines
471 :     description="character") ##<< preceding description block
472 :     )
473 :    
474 :     extract.file.parse <- function # File content analysis
475 :     ### Using the base \code{\link{parse}} function, analyse the file to link
476 :     ### preceding "prefix" comments to each active chunk. Those comments form
477 :     ### the default description for that chunk. The analysis also looks for
478 :     ### S4 class "setClass" calls and R.oo setConstructorS3 and setMethodS3
479 :     ### calls in order to link the documentation of those properly.
480 :     (code
481 :     ### Lines of R source code in a character vector - note that any
482 :     ### nested \code{source} statements are \emph{ignored} when scanning
483 :     ### for class definitions.
484 :     ){
485 :     res <- list()
486 :     old.opt <- options(keep.source=TRUE)
487 :     parsed <- try(parse(text=code))
488 :     options(old.opt)
489 :     if ( inherits(parsed,"try-error") ){
490 :     stop("parse failed with error:\n",parsed)
491 :     }
492 :     chunks <- attr(parsed,"srcref")
493 :     last.end <- 0
494 :     for ( k in 1:length(parsed) ){
495 :     start <- chunks[[k]][1]
496 :     ##details<< If the definition chunk does not contain a
497 :     ## description, any immediately preceding sequence consecutive
498 :     ## "prefix" lines will be used instead.
499 :     default.description <- NULL
500 :     while ( start > last.end+1
501 :     && 1 == length(grep(prefix,code[start-1],perl=TRUE)) ){
502 :     start <- start-1
503 :     }
504 :     if ( start < chunks[[k]][1] ){
505 :     default.description <- decomment(code[start:(chunks[[k]][1]-1)])
506 :     } else {
507 :     default.description <- NA_character_;
508 :     }
509 :     ##details<< Class and method definitions can take several forms,
510 :     ## determined by expression type: \describe{
511 :     ## \item{assignment (<-)}{Ordinary assignment of value/function;}
512 :     ## \item{setClass}{Definition of S4 class;}
513 :     ## \item{setConstructorS3}{Definition of S3 class using R.oo package;}
514 :     ## \item{setMethodS3}{Definition of method for S3 class using R.oo package.}}
515 :     ## Additionally, the value may be a name of a function defined elsewhere,
516 :     ## in which case the documentation should be copied from that other definition.
517 :     ## This is handled using the concept of documentation links.
518 :     lang <- parsed[[k]]
519 :     chars <- as.character(lang)
520 :     expr.type <- chars[1]
521 :     parent <- NA_character_
522 :    
523 :     if ( expr.type == "<-" || expr.type == "setConstructorS3" || expr.type == "setClass" ){
524 :     object.name <- chars[2]
525 :     ## If the function definition is not embedded within the call, then
526 :     ## the parent is that function. Test whether the the third value
527 :     ## looks like a name and add it to parents if so.
528 :     if ( 1 == length(grep("^[\\._\\w]+$",chars[3],perl=TRUE)) ){
529 :     parent <- chars[3]
530 :     }
531 :     res[[object.name]] <- new("DocLink",name=object.name,
532 :     created=expr.type,
533 :     parent=parent,
534 :     code=paste(chunks[[k]],sep=""),
535 :     description=default.description)
536 :     } else if ( expr.type == "setMethodS3" ){
537 :     ##details<< The \code{setMethodS3} calls introduce additional
538 :     ## complexity: they will define an additional S3 generic (which
539 :     ## needs documentation to avoid warnings at package build time)
540 :     ## unless one already exists. This also is handled by "linking"
541 :     ## documentation. A previously unseen generic is linked to the
542 :     ## first defining instances, subsequent definitions of that generic
543 :     ## also link back to the first defining instance.
544 :     generic.name <- chars[2]
545 :     object.name <- paste(generic.name,chars[3],sep=".")
546 :     if ( is.null(res[[generic.name]]) ){
547 :     generic.desc <- paste("Generic method behind \\code{\\link{",object.name,"}}",sep="")
548 :     res[[generic.name]] <- new("DocLink",
549 :     name=generic.name,
550 :     created=expr.type,
551 :     parent=object.name,
552 :     code=NA_character_,
553 :     description=generic.desc)
554 :     } else {
555 :     parent <- res[[generic.name]]@parent
556 :     }
557 :     ## If the function definition is not embedded within the call, then
558 :     ## the parent is that function. Test whether the the fourth value
559 :     ## looks like a name and add it to parents if so.
560 :     if ( 1 == length(grep("^[\\._\\w]+$",chars[4],perl=TRUE)) ){
561 :     parent <- c(chars[4],parent)
562 :     }
563 :     res[[object.name]] <- new("DocLink",name=object.name,
564 :     created=expr.type,
565 :     parent=parent,
566 :     code=paste(chunks[[k]],sep=""),
567 :     description=default.description)
568 :     } else {
569 :     ## Not sure what to do with these yet. Need to deal with setMethod, setAs etc.
570 :     }
571 :     }
572 :     invisible(res)
573 :     ### Returns an invisible list of .DocLink objects.
574 :     }
575 :    
576 :     extract.docs.setClass <- function # S4 class inline documentation
577 :     ### Using the same conventions as for functions, definitions of S4 classes
578 :     ### in the form \code{setClass("classname",\dots)} are also located and
579 :     ### scanned for inline comments.
580 :     (doc.link)
581 :     ### DocLink object as created by \code{\link{extract.file.parse}}.
582 :     ### Note that \code{source} statements are \emph{ignored} when scanning for
583 :     ### class definitions.
584 :     {
585 :     chunk.source <- doc.link@code
586 :     ##details<<
587 :     ## Extraction of S4 class documentation is currently limited to expressions
588 :     ## within the source code which have first line starting with
589 :     ## \code{setClass("classname"}. These are located from the source file
590 :     ## (allowing also for white space around the \code{setClass} and \code{(}).
591 :     ## Note that \code{"classname"} must be a quoted character string;
592 :     ## expressions returning such a string are not matched.
593 :     class.name <- doc.link@name
594 :    
595 :     ##details<< For class definitions, the slots (elements of the
596 :     ## \code{representation} list) fill the role of function
597 :     ## arguments, so may be documented by \code{##<<} comments on
598 :     ## the same line or \code{### } comments at the beginning of the
599 :     ## following line.
600 :     f.n <- paste(class.name,"class",sep="-")
601 :     docs <- extract.docs.fun(chunk.source,f.n)
602 :     ##details<<
603 :     ## The class definition skeleton includes an \code{Objects from the Class}
604 :     ## section, to which any \code{##details<<} documentation chunks are
605 :     ## written. It is given a vanilla content if there are no specific
606 :     ## \code{##details<<} documentation chunks.
607 :     if ( is.null(docs[["details"]]) ){
608 :     docs[["details"]] <-
609 :     paste("Objects can be created by calls of the form \\code{new(",
610 :     class.name," ...)}",sep="")
611 :     }
612 :     docs[["section{Objects from the Class}"]] <- docs[["details"]]
613 :     ## seealso has a skeleton line not marked by ~ .. ~, so have to suppress
614 :     if ( is.null(docs[["seealso"]]) ){
615 :     docs[["seealso"]] <- ""
616 :     }
617 :     if ( is.null(docs[["alias"]]) ){
618 :     docs[["alias"]] <- class.name
619 :     }
620 :     if ( is.null(docs[["description"]]) ){
621 :     docs[["description"]] <- doc.link@description
622 :     }
623 :     invisible(docs)
624 :     }

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