SCM

SCM Repository

[inlinedocs] Diff of /pkg/inlinedocs/R/parsers.R
ViewVC logotype

Diff of /pkg/inlinedocs/R/parsers.R

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 109, Mon Jun 7 08:42:15 2010 UTC revision 163, Tue Nov 16 12:06:29 2010 UTC
# Line 1  Line 1 
1    ### combine lists or character strings
2    combine <- function(x,y)UseMethod("combine")
3    
4    ### combine character strings by pasting them together
5    combine.character <- function(x,y)
6        paste(x,y,sep="\n")
7    
8    ### combine lists by adding elements or adding to existing elements
9    combine.list <- function(x,y){
10      toadd <- !names(y)%in%names(x)
11      toup <- names(y)[names(y)%in%names(x)]
12      x[names(y)[toadd]] <- y[toadd]
13      for(up in toup)x[[up]] <- combine(x[[up]],y[[up]])
14      return(x)
15    ### A list, same type as x, but with added elements from y.
16    }
17    
18    ### Prefix for code comments used with grep and gsub.
19    prefix <- "^[ \t]*###[ \t]"
20    
21    decomment <- function
22    ### Remove comment prefix and join lines of code to form a
23    ### documentation string.
24    (comments
25    ### Character vector of prefixed comment lines.
26     ){
27      paste(gsub(prefix,"",comments),collapse="\n")
28    ### String without prefixes or newlines.
29    }
30    
31    forall <- function
32  ### For each object in the package that satisfies the criterion  ### For each object in the package that satisfies the criterion
33  ### checked by subfun, parse source using FUN and return the resulting  ### checked by subfun, parse source using FUN and return the resulting
34  ### documentation list.  ### documentation list.
 forall <- function  
35  (FUN,  (FUN,
36  ### Function to apply to each element in the package.  ### Function to apply to each element in the package.
37   subfun=function(x)TRUE   subfun=function(x)TRUE
# Line 9  Line 39 
39  ### is.function. subfun(x)==TRUE means FUN will be applied to x and  ### is.function. subfun(x)==TRUE means FUN will be applied to x and
40  ### the result will be returned.  ### the result will be returned.
41   ){   ){
42    function(objs,docs,...){    FUN <- FUN
43      f <- function(objs,docs,...){
44      objs <- objs[sapply(objs,subfun)]      objs <- objs[sapply(objs,subfun)]
45      L <- list()      L <- list()
46        on.exit(cat(sprintf("Parser Function failed on %s\n",N)))
47      for(N in names(docs)){      for(N in names(docs)){
48        o <- objs[[N]]        o <- objs[[N]]
49        L[[N]] <- FUN(src=attr(o,"source"),        L[[N]] <- FUN(src=attr(o,"source"),
50                      name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)                      name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)
51      }      }
52        on.exit()## remove warning message
53      L      L
54    }    }
55      class(f) <- c("allfun","function")
56      f
57  ### A Parser Function.  ### A Parser Function.
58  }  }
59    
60    ### Print method for functions constructed using forall.
61    print.allfun <- function(x,...){
62      e <- environment(x)
63      cat("Function to apply to every element.\nselector:")
64      print(e$subfun)
65      cat("processor:")
66      print(e$FUN)
67    }
68    
69  ### For each function in the package, do something.  ### For each function in the package, do something.
70  forfun <- function(FUN)forall(FUN,is.function)  forfun <- function(FUN)forall(FUN,is.function)
71    
72  ### Parsers for each object/function that are constructed  kill.prefix.whitespace <- function
73  ### automatically. This is a named list, each element is a list of 2  ### Figure out what the whitespace preceding the example code is, and
74  ### elements: forfun/forall, then a parser function for an individual  ### then delete that from every line.
75  ### object.  (ex
76  forall.parsers <-  ### character vector of example code lines.
77    list(## Extract lots of info from normal functions.   ){
78         parsefun=list(forfun,function(src,name,...){    tlines <- gsub("\\s*","",ex)
79           extract.docs.fun(src,name)    ##tlines <- gsub("#.*","",tlines)
80         }),    prefixes <- unique(gsub("\\S.*","",ex[tlines!=""]))
81         ## Fill in author from DESCRIPTION and titles.    FIND <- prefixes[which.min(nchar(prefixes))]
82         author.from.description=list(forall,function(desc,...){    ## Eliminate leading tabulations or 2/4 spaces
83           list(author=desc[,"Maintainer"])    sub(FIND, "", ex)
84         }),  ### Character vector of code lines with preceding whitespace removed.
85         ## The format section sometimes causes problems, so erase it.  }
86         erase.format=list(forall,function(...){  
87           list(format="")  examples.after.return <- function
88         }),  ### Get examples from inline definitions after return()
89         ## Convert the function name to a title.  ### PhG: this does not work well! Think of these situations:
90         title.from.name=list(forall,function(name,doc,...){  ### 1) You have multiple return() in the code of your function,
91           if("title"%in%names(doc))list() else  ### 2) You have return() appearing is some example code, ...
92           list(title=gsub("[._]"," ",name))  ### I can hardly propose a hack here. The whole code of the function
93         }),  ### must be parsed, and one must determine which one is the last line
94         # PhG: it is tests/FUN.R!!! I would like more flexibility here  ### of code that is actually executed.
95             # please, let me choose which dir to use for examples!  ###
96             ## Get examples for FUN from the file tests/FUN.R  ### I make two propositions here
97         examples.from.testfile=list(forfun,function(name,...){  ### 1) to keep the same mechanism that has the advantage of simplicity
98           tsubdir <- getOption("inlinedocs.exdir")  ###    but to use a special tag
99                   if (is.null(tsubdir)) tsubdir <- "tests"       # Default value  ###    ##examples<< or #{{{examples to separate
100                   tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))  ###    function code from examples explicitly, and
101           if(file.exists(tfile))  ### 2) to place the example in an "ex" attribute
102             list(examples=paste(readLines(tfile),collapse="\n"))  ###    attached to the function
103           else list()  ###    (see next parser). That solution will be also interesting for
104         }),  ###    documenting datasets, something not done yet by inlinedocs!
105         ## Get examples from inline definitions after return()  (src,name="",...) {
106             # PhG: this does not work well! Think at these situations:    ## Look for the examples mark
107             # 1) You have multiple return() in the code of your function,    m <- grep("^\\s*(##examples<<|#\\{\\{\\{examples)", src)
            # 2) You have return() appearing is some example code, ...  
            # I can hardly propose a hack here. The whole code of the function  
            # must be parsed, and one must determine which one is the last line  
            # of code that is actually executed.  
            #  
            # I make two propositions here  
            # 1) to keep the same mechanism that has the advantage of simplicity  
            #    but to use a special tag ##examples<< or #{{{examples to separate  
            #    function code from examples explicitly, and  
            # 2) to place the example in an "ex" attribute attached to the function  
            #    (see next parser). That solution will be also interesting for  
            #    documenting datasets, something not done yet by inlinedocs!  
            examples.after.return = list(forfun, function(name, src, ...) {  
                         # Look for the examples mark  
                         m <- grep("##examples<<|#\\{\\{\\{examples", src)  
108                          if (!length(m)) return(list())                          if (!length(m)) return(list())
109                          if (length(m) > 1)                          if (length(m) > 1)
110                                  warning("More than one examples tag for ", name, ". Taking the last one")      warning("More than one examples tag for ", name,
111                ". Taking the last one")
112                          m <- m[length(m)]                          m <- m[length(m)]
113                          # Look for the lines containing return value comments just before    ## Look for the lines containing return value comments just before
114                          r <- grep("\\s*### ", src[1:(m-1)])                          r <- grep("\\s*### ", src[1:(m-1)])
115                          if (!length(r)) {                          if (!length(r)) {
116                                  value <- NULL                                  value <- NULL
117                          } else {                          } else {
118                                  # Only take consecutive lines before the mark        ## Only take consecutive lines before the mark
119                                  keep <- rev((m - rev(r)) == 1:length(r))                                  keep <- rev((m - rev(r)) == 1:length(r))
120                                  if (!any(keep)) {                                  if (!any(keep)) {
121                                          value <- NULL                                          value <- NULL
# Line 93  Line 123 
123                                          value <- decomment(src[r[keep]])                                          value <- decomment(src[r[keep]])
124                                  }                                  }
125                          }                          }
126                          # Collect now the example code beneath the mark    ## Collect now the example code beneath the mark
127                          ex <- src[(m + 1):(length(src) - 1)]                          ex <- src[(m + 1):(length(src) - 1)]
128                          # Possibly eliminate a #}}} tag    ## Possibly eliminate a #}}} tag
129                          ex <- ex[!grepl("#}}}", ex)]                          ex <- ex[!grepl("#}}}", ex)]
130                          # Eliminate leading tabulations or four spaces    ## Eliminate leading tabulations or four spaces
131                          ex <- sub("^\t|    ", "", ex)    ex <- kill.prefix.whitespace(ex)
132                          # Add an empty line before and after example    ## Add an empty line before and after example
133                          ex <- c("", ex, "")                          ex <- c("", ex, "")
134                          # Return examples and value    ## Return examples and value
135                          list(examples = paste(ex, collapse = "\n"), value = value)                          list(examples = paste(ex, collapse = "\n"), value = value)
            }),  
            # PhG: here is what I propose for examples code in the 'ex' attribute  
            examples.in.attr = list(forfun, function (name, o, ...) {  
                         ex <- attr(o, "ex")  
                         if (!is.null(ex)) {  
                                 # Special case for code contained in a function  
                                 if (inherits(ex, "function")) {  
                                         # If source is available, start from there  
                                         src <- attr(ex, "source")  
                                         if (!is.null(src)) {  
                                                 ex <- src  
                                         } else { # Use the body of the function  
                                                 ex <- deparse(body(ex))  
                                         }  
                                         # Eliminate leading and trailing code  
                                         ex <- ex[-c(1, length(ex))]  
                                         # Eliminate leading tabulations or four spaces  
                                         ex <- sub("^\t|    ", "", ex)  
                                         # Add an empty line before and after example  
                                         ex <- c("", ex, "")  
                                 }  
                                 list(examples = paste(ex, collapse = "\n"))  
                         } else list()  
            }))  
   
 ### List of parser functions that operate on single objects. This list  
 ### is useful for testing these functions, ie  
 ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")  
 lonely <- sapply(forall.parsers,function(L)L[[2]])  
   
 extra.code.docs <- function # Extract documentation from code chunks  
 ### Parse R code to extract inline documentation from comments around  
 ### each function. These are not able to be retreived simply by  
 ### looking at the "source" attribute. This is a Parser Function that  
 ### can be used in the parser list of package.skeleton.dx(). TODO:  
 ### Modularize this into separate Parsers Functions for S4 classes,  
 ### prefixes, ##<<blocks, etc. Right now it is not very clean!  
 (code,  
 ### Code lines in a character vector containing multiple R objects to  
 ### parse for documentation.  
  objs,  
 ### The objects defined in the code.  
  ...  
 ### ignored  
  ){  
   parsed <- extract.file.parse(code)  
   extract.docs.try <- function(o,on)  
     {  
       ## Note: we could use parsed information here too, but that  
       ## would produce different results for setMethodS3 etc.  
       doc <- list()  
       if ( !is.null(parsed[[on]]) ){  
         if ( !is.na(parsed[[on]]@code[1]) ){ # no code given for generics  
           doc$definition <- paste(parsed[[on]]@code,collapse="\n")  
         }  
         if(!"description"%in%names(doc) && !is.na(parsed[[on]]@description) ){  
           doc$description <- parsed[[on]]@description  
         }  
         if ( "setMethodS3" == parsed[[on]]@created ){  
           # PhG: this may be wrong! It does not catch correctly how the method  
                   # must be splitted in case of methods containing dots. for instance,  
                   # as.data.frame.matrix must be split into: m1 = as.data.frame and  
                   # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!  
                   pattern <- "^([^\\.]+)\\.(.*)$"  
           doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),  
               m2 <- gsub(pattern,"\\2",on,perl=TRUE))  
           if ( 0 < length(grep("\\W",m1,perl=TRUE)) ){  
                           m1 <- paste("`",m1,"`",sep="")  
           }  
           cat("S3method(",m1,",",m2,")\n",sep="")  
         }  
       }  
       if("title" %in% names(doc) && !"description" %in% names(doc) ){  
         ## For short functions having both would duplicate, but a  
         ## description is required. Therefore automatically copy title  
         ## across to avoid errors at package build time.  
         doc$description <- doc$title  
       }  
       doc  
     }  
   extract.docs <- function(on){  
     res <- try({o <- objs[[on]]  
                 extract.docs.try(o, on)},FALSE)  
     if(class(res)=="try-error"){  
       cat("Failed to extract docs for: ",on,"\n\n")  
       list()  
     } else if(0 == length(res) && inherits(objs[[on]],"standardGeneric")){  
       NULL  
     } else if(0 == length(res) && "function" %in% class(o)  
               && 1 == length(osource <- attr(o,"source"))  
               && 1 == length(grep(paste("UseMethod(",on,")",sep="\""),osource))  
               ){  
       ## phew - this should only pick up R.oo S3 generic definitions like:  
       ## attr(*, "source")= chr "function(...) UseMethod(\"select\")"  
       NULL  
     } else res  
   }  
   doc.names <- names(objs)  
   res <- sapply(doc.names,extract.docs,simplify=FALSE)  
   ## Special processing for S4 classes as they do not appear in normal ls()  
   for ( nn in names(parsed) ){  
     if ( parsed[[nn]]@created == "setClass" ){  
       S4class.docs <- extract.docs.setClass(parsed[[nn]])  
       docname <- paste(nn,"class",sep="-")  
       if ( is.null(res[[docname]]) ){  
         res[[docname]] <- S4class.docs  
         doc.names <- c(doc.names,docname)  
       } else {  
         stop(nn," appears as both S4 class and some other definition")  
       }  
     }  
   }  
   inherit.docs <- function(on){  
     in.res <- res[[on]]  
     if ( !is.null(parsed[[on]]) ){  
       for ( parent in parsed[[on]]@parent ){  
         if ( !is.na(parent) ){  
           if ( is.null(in.res) ){  
             in.res <- res[[parent]]  
           } else if ( parent %in% names(res) ){  
             parent.docs <- res[[parent]]  
             for ( nn in names(parent.docs) ){  
               if ( !nn %in% names(in.res) ){  
                 in.res[[nn]] <- parent.docs[[nn]]  
               }  
             }  
           }  
         }  
       }  
     }  
     invisible(in.res)  
   }  
   all.done <- FALSE  
   while ( !all.done ){  
     res1 <- sapply(doc.names,inherit.docs,simplify=FALSE)  
     all.done <- identical(res1,res)  
     res <- res1  
   }  
   ## now strip out any generics (which have value NULL in res):  
   res.not.null <- sapply(res,function(x){!is.null(x)})  
   if ( 0 < length(res.not.null) && length(res.not.null) < length(res) ){  
     res <- res[res.not.null]  
   }  
   res  
 ### named list of lists, one for each object to document.  
136  }  }
137    
138  ### List of parsers to use by default with package.skeleton.dx.  prefixed.lines <- function(src,...){
139  default.parsers <-  ### The primary mechanism of inline documentation is via consecutive
140    c(extra.code.docs=extra.code.docs,  ### groups of lines matching the specified prefix regular expression
141      sapply(forall.parsers,function(L)L[[1]](L[[2]])),  ### "\code{^### }" (i.e. lines beginning with "\code{### }") are
142      edit.package.file=function(desc,...){  ### collected as follows into documentation sections:\describe{
143        in.details <- setdiff(colnames(desc),"Description")  ### \item{description}{group starting at line 2 in the code}
144        details <- paste(paste(in.details,": \\tab ",desc[,in.details],"\\cr",  ### \item{arguments}{group following each function argument}
145                               sep=""),collapse="\n")  ### \item{value}{group ending at the penultimate line of the code}}
146        L <-  ### These may be added to by use of the \code{##<<} constructs
147          list(list(title=desc[,"Title"],  ### described below.
148                    description=desc[,"Description"],    clines <- grep(prefix,src)
149                    `tabular{ll}`=details,    if(length(clines)==0)return(list())
                   author=desc[,"Maintainer"]))  
       names(L) <- paste(desc[,"Package"],"-package",sep="")  
       L  
     })  
   
 extract.docs.fun <- function # Extract documentation from a function  
 ### Given source code of a function, return a list describing inline  
 ### documentation in that source code.  
 (code,  
 ### The function to examine.  
  name.fun  
 ### The name of the function/chunk to use in warning messages.  
  ){  
   res <- list()  
   clines <- grep(prefix,code)  
   if(length(grep("#",code[1]))){  
     res$title <- gsub("[^#]*#\\s*(.*)","\\1",code[1],perl=TRUE)  
   }  
   if(length(clines) > 0){  
     ##details<<  
     ## The primary mechanism is that consecutive groups of lines matching  
     ## the specified prefix regular expression "\code{^### }" (i.e. lines  
     ## beginning with "\code{### }") are collected  
     ## as follows into documentation sections:\describe{  
     ## \item{description}{group starting at line 2 in the code}  
     ## \item{arguments}{group following each function argument}  
     ## \item{value}{group ending at the penultimate line of the code}}  
     ## These may be added to by use of the \code{##<<} constructs described  
     ## below.  
150      bounds <- which(diff(clines)!=1)      bounds <- which(diff(clines)!=1)
151      starts <- c(1,bounds+1)      starts <- c(1,bounds+1)
152      ends <- c(bounds,length(clines))      ends <- c(bounds,length(clines))
153      ## detect body of function using paren matching
154      code <- gsub("#.*","",src)
155      f <- function(ch)cumsum(nchar(gsub(sprintf("[^%s]",ch),"",code)))
156      parens <- f("(")-f(")")
157      body.begin <- which(diff(parens)<0 & parens[-1]==0)+2
158      is.arg <- function(){
159        gres <- grep("^\\s*#",src[start-1],perl=TRUE)
160        0 == length(gres) && start<=body.begin
161      }
162      res <- list()
163      for(i in seq_along(starts)){      for(i in seq_along(starts)){
164        start <- clines[starts[i]]        start <- clines[starts[i]]
165        end <- clines[ends[i]]        end <- clines[ends[i]]
166        lab <- if(end+1==length(code))"value"      lab <- if(end+1==length(src))"value"
167        else if(start==2)"description"        else if(start==2)"description"
168        else if ( 0 == length(grep("^\\s*#",code[start-1],perl=TRUE)) ){      else if(is.arg()){
169           #arg <- gsub("^[ (]*","",code[start-1])        ##twutz: strip leading white spaces and brackets and ,
170           #arg <- gsub("^([^=,]*)[=,].*","\\1",arg)        arg <- gsub("^[ \t(,]*", "", src[start - 1])
          #arg <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots  
                  arg <- gsub("^[ \t(,]*", "", code[start - 1])  #twutz: strip leading white spaces and brackets and ,  
171                   arg <- gsub("^([^=,]*)[=,].*", "\\1", arg)                   arg <- gsub("^([^=,]*)[=,].*", "\\1", arg)
172                   arg <- gsub("^([^ \t]*)([ \t]+)$","\\1",arg)   #twutz: remove trailing whitespaces        ##twutz: remove trailing whitespaces
173          arg <- gsub("^([^ \t]*)([ \t]+)$","\\1",arg)
174                   arg <- gsub("...", "\\dots", arg, fix = TRUE)                   arg <- gsub("...", "\\dots", arg, fix = TRUE)
175           paste("item{",arg,"}",sep="")           paste("item{",arg,"}",sep="")
176         } else {         } else {
177           next;           next;
178         }         }
179        res[[lab]] <- decomment(code[start:end])      res[[lab]] <- decomment(src[start:end])
180      }      }
181      res
182    }    }
183    
184    extract.xxx.chunks <- function # Extract documentation from a function
185    ### Given source code of a function, return a list describing inline
186    ### documentation in that source code.
187    (src,
188    ### The source lines of the function to examine, as a character
189    ### vector.
190     name.fun="(unnamed function)",
191    ### The name of the function/chunk to use in warning messages.
192     ...
193    ### ignored.
194     ){
195      res <- list()
196    ##details<< For simple functions/arguments, the argument may also be    ##details<< For simple functions/arguments, the argument may also be
197    ## documented by appending \code{##<<} comments on the same line as the    ## documented by appending \code{##<<} comments on the same line as the
198    ## argument name. Mixing this mechanism with \code{###} comment lines for    ## argument name. Mixing this mechanism with \code{###} comment lines for
# Line 392  Line 271 
271            else paste(res[[field]], payload, sep=chunk.sep)            else paste(res[[field]], payload, sep=chunk.sep)
272        invisible(chunk.res)        invisible(chunk.res)
273      }      }
274    while ( k <= length(code) ){    while ( k <= length(src) ){
275      line <- code[k]      line <- src[k]
276      if ( 0 < length(grep(extra.regexp,line,perl=TRUE) ) ){      if ( 0 < length(grep(extra.regexp,line,perl=TRUE) ) ){
277        ## we have a new extra chunk - first get field name and any payload        ## we have a new extra chunk - first get field name and any payload
278        new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)        new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)
# Line 493  Line 372 
372        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)
373        in.chunk <- TRUE        in.chunk <- TRUE
374        if ( not.describe ){        if ( not.describe ){
375            ## TDH 2010-06-18 For item{}s in the documentation list names,
376            ## we don't need to have a backslash before, so delete it.
377            arg <- gsub("^[\\]+","",arg)
378          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots
379          payload <- comment          payload <- comment
380        } else {        } else {
# Line 543  Line 425 
425  ### with the string in this list (implemented in modify.Rd.file).  ### with the string in this list (implemented in modify.Rd.file).
426  }  }
427    
428    ### Parsers for each function that are constructed automatically. This
429    ### is a named list, and each element is a parser function for an
430    ### individual object.
431    forfun.parsers <-
432      list(prefixed.lines=prefixed.lines,
433           examples.after.return=examples.after.return,
434           extract.xxx.chunks=extract.xxx.chunks,
435           ## title from first line of function def
436           title.from.firstline=function(src,name,code,...){
437             if(length(grep("#",src[1]))){
438               list(title=gsub("[^#]*#\\s*(.*)","\\1",src[1],perl=TRUE))
439             } else list()
440           },
441           ## PhG: it is tests/FUN.R!!! I would like more flexibility here
442           ## please, let me choose which dir to use for examples!
443           ## Get examples for FUN from the file tests/FUN.R
444           examples.from.testfile=function(name,...){
445             tsubdir <- getOption("inlinedocs.exdir")
446             if (is.null(tsubdir)) tsubdir <- "tests"       # Default value
447             tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))
448             if(file.exists(tfile))
449               list(examples=paste(readLines(tfile),collapse="\n"))
450             else list()
451           },
452           definition.from.source=function(doc,src,...){
453             def <- doc$definition
454             is.empty <- function(x)is.null(x)||x==""
455             if(is.empty(def) && !is.empty(src))
456               list(definition=paste(src,collapse="\n"))
457             else list()
458           })
459    
460    ### List of Parser Functions that can be applied to any object.
461    forall.parsers <-
462      list(## Fill in author from DESCRIPTION and titles.
463           author.from.description=function(desc,...){
464             list(author=desc[,"Maintainer"])
465           },
466           ## The format section sometimes causes problems, so erase it.
467           erase.format=function(...){
468             list(format="")
469           },
470           ## Convert the function name to a title.
471           title.from.name=function(name,doc,...){
472             if("title"%in%names(doc))list() else
473             list(title=gsub("[._]"," ",name))
474           },
475           ## PhG: here is what I propose for examples code in the 'ex' attribute
476           examples.in.attr =  function (name, o, ...) {
477             ex <- attr(o, "ex")
478             if (!is.null(ex)) {
479               ## Special case for code contained in a function
480               if (inherits(ex, "function")) {
481                 ## If source is available, start from there
482                 src <- attr(ex, "source")
483                 if (!is.null(src)) {
484                   ex <- src
485                 } else { ## Use the body of the function
486                   ex <- deparse(body(ex))
487                 }
488                 ## Eliminate leading and trailing code
489                 ex <- ex[-c(1, length(ex))]
490                 ## all the prefixes
491                 ex <- kill.prefix.whitespace(ex)
492                 ## Add an empty line before and after example
493                 ex <- c("", ex, "")
494               }
495               list(examples = paste(ex, collapse = "\n"))
496             } else list()
497           })
498    
499    ### List of parser functions that operate on single objects. This list
500    ### is useful for testing these functions.
501    lonely <- c(forall.parsers,forfun.parsers)
502    attr(lonely,"ex") <- function(){
503      f <- function # title
504    ### description
505      (x, ##<< arg x
506       y
507    ### arg y
508       ){
509        ##value<< a list with elements
510        list(x=x, ##<< original x value
511             y=y, ##<< original y value
512             sum=x+y) ##<< their sum
513        ##end<<
514      }
515      src <- attr(f,"source")
516      lonely$extract.xxx.chunks(src)
517      lonely$prefixed.lines(src)
518    }
519    
520    extra.code.docs <- function # Extract documentation from code chunks
521    ### Parse R code to extract inline documentation from comments around
522    ### each function. These are not able to be retreived simply by
523    ### looking at the "source" attribute. This is a Parser Function that
524    ### can be used in the parser list of package.skeleton.dx(). TODO:
525    ### Modularize this into separate Parsers Functions for S4 classes,
526    ### prefixes, ##<<blocks, etc. Right now it is not very clean!
527    (code,
528    ### Code lines in a character vector containing multiple R objects to
529    ### parse for documentation.
530     objs,
531    ### The objects defined in the code.
532     ...
533    ### ignored
534     ){
535      parsed <- extract.file.parse(code)
536      extract.docs.try <- function(o,on)
537        {
538          ## Note: we could use parsed information here too, but that
539          ## would produce different results for setMethodS3 etc.
540          doc <- list()
541          if ( !is.null(parsed[[on]]) ){
542            if ( !is.na(parsed[[on]]@code[1]) ){ # no code given for generics
543              doc$definition <- paste(parsed[[on]]@code,collapse="\n")
544            }
545            if(!"description"%in%names(doc) && !is.na(parsed[[on]]@description) ){
546              doc$description <- parsed[[on]]@description
547            }
548            if ( "setMethodS3" == parsed[[on]]@created ){
549              # PhG: this may be wrong! It does not catch correctly how the method
550              # must be splitted in case of methods containing dots. for instance,
551              # as.data.frame.matrix must be split into: m1 = as.data.frame and
552              # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!
553                      pattern <- "^([^\\.]+)\\.(.*)$"
554              doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),
555                  m2 <- gsub(pattern,"\\2",on,perl=TRUE))
556              if ( 0 < length(grep("\\W",m1,perl=TRUE)) ){
557                              m1 <- paste("`",m1,"`",sep="")
558              }
559              cat("S3method(",m1,",",m2,")\n",sep="")
560            }
561          }
562          if("title" %in% names(doc) && !"description" %in% names(doc) ){
563            ## For short functions having both would duplicate, but a
564            ## description is required. Therefore automatically copy title
565            ## across to avoid errors at package build time.
566            doc$description <- doc$title
567          }
568          doc
569        }
570      extract.docs <- function(on){
571        res <- try({o <- objs[[on]]
572                    extract.docs.try(o, on)},FALSE)
573        if(class(res)=="try-error"){
574          cat("Failed to extract docs for: ",on,"\n\n")
575          list()
576        } else if(0 == length(res) && inherits(objs[[on]],"standardGeneric")){
577          NULL
578        } else if(0 == length(res) && "function" %in% class(o)
579                  && 1 == length(osource <- attr(o,"source"))
580                  && 1 == length(grep(paste("UseMethod(",on,")",sep="\""),osource))
581                  ){
582          ## phew - this should only pick up R.oo S3 generic definitions like:
583          ## attr(*, "source")= chr "function(...) UseMethod(\"select\")"
584          NULL
585        } else res
586      }
587      doc.names <- names(objs)
588      res <- sapply(doc.names,extract.docs,simplify=FALSE)
589      ## Special processing for S4 classes as they do not appear in normal ls()
590      for ( nn in names(parsed) ){
591        if ( parsed[[nn]]@created == "setClass" ){
592          S4class.docs <- extract.docs.setClass(parsed[[nn]])
593          docname <- paste(nn,"class",sep="-")
594          if ( is.null(res[[docname]]) ){
595            res[[docname]] <- S4class.docs
596            doc.names <- c(doc.names,docname)
597          } else {
598            stop(nn," appears as both S4 class and some other definition")
599          }
600        }
601      }
602      inherit.docs <- function(on){
603        in.res <- res[[on]]
604        if ( !is.null(parsed[[on]]) ){
605          for ( parent in parsed[[on]]@parent ){
606            if ( !is.na(parent) ){
607              if ( is.null(in.res) ){
608                in.res <- res[[parent]]
609              } else if ( parent %in% names(res) ){
610                parent.docs <- res[[parent]]
611                for ( nn in names(parent.docs) ){
612                  if ( !nn %in% names(in.res) ){
613                    in.res[[nn]] <- parent.docs[[nn]]
614                  }
615                }
616              }
617            }
618          }
619        }
620        invisible(in.res)
621      }
622      all.done <- FALSE
623      while ( !all.done ){
624        res1 <- sapply(doc.names,inherit.docs,simplify=FALSE)
625        all.done <- identical(res1,res)
626        res <- res1
627      }
628      ## now strip out any generics (which have value NULL in res):
629      res.not.null <- sapply(res,function(x){!is.null(x)})
630      if ( 0 < length(res.not.null) && length(res.not.null) < length(res) ){
631        res <- res[res.not.null]
632      }
633      res
634    ### named list of lists, one for each object to document.
635    }
636    
637    ### List of parsers to use by default with package.skeleton.dx.
638    default.parsers <-
639      c(extra.code.docs=extra.code.docs, ## TODO: cleanup!
640        sapply(forfun.parsers,forfun),
641        sapply(forall.parsers,forall),
642        edit.package.file=function(desc,...){
643          in.details <- setdiff(colnames(desc),"Description")
644          details <- paste(paste(in.details,": \\tab ",desc[,in.details],"\\cr",
645                                 sep=""),collapse="\n")
646          L <-
647            list(list(title=desc[,"Title"],
648                      description=desc[,"Description"],
649                      `tabular{ll}`=details,
650                      author=desc[,"Maintainer"]))
651          names(L) <- paste(desc[,"Package"],"-package",sep="")
652          L
653        })
654    
655  setClass("DocLink", # Link documentation among related functions  setClass("DocLink", # Link documentation among related functions
656  ### The \code{.DocLink} class provides the basis for hooking together  ### The \code{.DocLink} class provides the basis for hooking together
657  ### documentation of related classes/functions/objects. The aim is that  ### documentation of related classes/functions/objects. The aim is that
# Line 681  Line 790 
790    ## the same line or \code{### } comments at the beginning of the    ## the same line or \code{### } comments at the beginning of the
791    ## following line.    ## following line.
792    f.n <- paste(class.name,"class",sep="-")    f.n <- paste(class.name,"class",sep="-")
793    docs <- extract.docs.fun(chunk.source,f.n)    docs <- extract.xxx.chunks(chunk.source,f.n)
794      ## also apply source parsing functions that I separated out into
795      ## separate functions
796      docs <- combine(docs,lonely$prefixed.lines(chunk.source))
797      docs$title <- lonely$title.from.firstline(chunk.source)
798    ##details<<    ##details<<
799    ## The class definition skeleton includes an \code{Objects from the Class}    ## The class definition skeleton includes an \code{Objects from the Class}
800    ## section, to which any \code{##details<<} documentation chunks are    ## section, to which any \code{##details<<} documentation chunks are
# Line 719  Line 832 
832  ### Additional arguments to pass to Parser Functions.  ### Additional arguments to pass to Parser Functions.
833   ){   ){
834    e <- new.env()    e <- new.env()
835    old <- options(keep.source.pkgs=TRUE)    old <- options(keep.source=TRUE)
836    on.exit(options(old))    on.exit(options(old))
   ##tryCatch({  
837      exprs <- parse(text=code)      exprs <- parse(text=code)
838      for (i in exprs) eval(i, e)    for (i in exprs){
839  ##   },error=function(err){      tryCatch(eval(i, e),error=function(e){
840  ##     print(i)        ##print(e)
841  ##     stop("eval or parse failed with error:\n",err)      })
842  ##   })    }
843    objs <- sapply(ls(e),get,e,simplify=FALSE)    objs <- sapply(ls(e),get,e,simplify=FALSE)
844    
845    docs <- list()    docs <- list()
# Line 768  Line 880 
880  ### A list of extracted documentation from code.  ### A list of extracted documentation from code.
881  }  }
882    
883    ### Names of Parser Functions that operate on the desc arg.
884    descfile.names <- c("author.from.description","edit.package.file")
885    
886    ### Names of Parser Functions that do NOT use the desc arg.
887    non.descfile.names <-
888      names(default.parsers)[!names(default.parsers)%in%descfile.names]
889    
890    ### Parsers that operate only on R code, independently of the
891    ### description file.
892    nondesc.parsers <- default.parsers[non.descfile.names]
893    
894  extract.docs.file <- function  extract.docs.file <- function
895  ### Apply all parsers relevant to extract info from just 1 code file.  ### Apply all parsers relevant to extract info from just 1 code file.
896  (f,  (f,
897  ### File name of R code to read and parse.  ### File name of R code to read and parse.
898   parsers=list(extra.code.docs,default.parsers$parsefun),   parsers=nondesc.parsers,
899  ### Parser Functions to use to parse the code and extract  ### Parser Functions to use to parse the code and extract
900  ### documentation.  ### documentation.
901   ...   ...

Legend:
Removed from v.109  
changed lines
  Added in v.163

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge