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

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