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

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