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 87, Sat May 1 12:21:27 2010 UTC revision 195, Fri Feb 18 12:30:30 2011 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)[!toadd]
12      if("doc"%in%names(x))return(x$doc)
13      if("doc"%in%names(y))return(y$doc)
14      x[names(y)[toadd]] <- y[toadd]
15      for(up in toup)x[[up]] <- combine(x[[up]],y[[up]])
16      return(x)
17    ### A list, same type as x, but with added elements from y.
18    }
19    
20    ### Prefix for code comments used with grep and gsub.
21    prefix <- "^[ \t]*###[ \t]"
22    
23    decomment <- function
24    ### Remove comment prefix and join lines of code to form a
25    ### documentation string.
26    (comments
27    ### Character vector of prefixed comment lines.
28     ){
29      gsub(prefix,"",comments)
30    ### String without prefixes or newlines.
31    }
32    
33    forall <- function
34  ### For each object in the package that satisfies the criterion  ### For each object in the package that satisfies the criterion
35  ### checked by subfun, parse source using FUN and return the resulting  ### checked by subfun, parse source using FUN and return the resulting
36  ### documentation list.  ### documentation list.
 forall <- function  
37  (FUN,  (FUN,
38  ### Function to apply to each element in the package.  ### Function to apply to each element in the package.
39   subfun=function(x)TRUE   subfun=function(x)TRUE
# Line 9  Line 41 
41  ### 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
42  ### the result will be returned.  ### the result will be returned.
43   ){   ){
44    function(objs,docs,...){    FUN <- FUN
45      f <- function(objs,docs,...){
46      objs <- objs[sapply(objs,subfun)]      objs <- objs[sapply(objs,subfun)]
47      L <- list()      L <- list()
48      for(N in names(objs)){      on.exit(cat(sprintf("Parser Function failed on %s\n",N)))
49        for(N in names(docs)){
50        o <- objs[[N]]        o <- objs[[N]]
51        L[[N]] <- FUN(src=attr(o,"source"),name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)        L[[N]] <- FUN(src=attr(o,"source"),
52                        name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)
53      }      }
54        on.exit()## remove warning message
55      L      L
56    }    }
57      class(f) <- c("allfun","function")
58      f
59  ### A Parser Function.  ### A Parser Function.
60  }  }
61    
62    ### Print method for functions constructed using forall.
63    print.allfun <- function(x,...){
64      e <- environment(x)
65      cat("Function to apply to every element.\nselector:")
66      print(e$subfun)
67      cat("processor:")
68      print(e$FUN)
69    }
70    
71  ### For each function in the package, do something.  ### For each function in the package, do something.
72  forfun <- function(FUN)forall(FUN,is.function)  forfun <- function(FUN)forall(FUN,is.function)
73    
74  ### Get last line (test)  kill.prefix.whitespace <- function
75  parsefuns <- forfun(function(src,name,...){  ### Figure out what the whitespace preceding the example code is, and
76    extract.docs.fun(src,name)  ### then delete that from every line.
77  })  (ex
78    ### character vector of example code lines.
 ### Fill in author from DESCRIPTION and titles.  
 author.from.description <- forall(function(desc,...){  
   list(author=desc[,"Maintainer"])  
 })  
   
 ### The format section sometimes causes problems, so erase it.  
 erase.format <- forall(function(...){  
   list(format="")  
 })  
   
 ### Convert the function name to a title.  
 title.from.name <- forall(function(name,...){  
   list(title=gsub("[._]"," ",name))  
 })  
   
 ### Get examples for FUN from the file test/FUN.R  
 examples.from.testfile <- forfun(function(name,...){  
   tfile <- file.path("..","tests",paste(name,".R",sep=""))  
   if(file.exists(tfile))  
     list(examples=paste(readLines(tfile),collapse="\n"))  
   else list()  
 })  
   
 ### Get examples from inline definitions after return()  
 examples.after.return <- forfun(function(src,...){  
   rline <- grep("return([^)]*)",src)  
   if(length(rline)==0)return(list())  
   rline <- rline[length(rline)]  
   comment.line.nums <- grep(prefix,src)  
   if(!(comment.end <- rline+1)%in%comment.line.nums)return(list())  
   while(comment.end%in%comment.line.nums)comment.end <- comment.end+1  
   excode <- src[comment.end:(length(src)-1)]  
   list(examples=paste(gsub("^\\W*","",excode),collapse="\n"))  
 })  
   
 extract.docs.file <- function # Extract documentation from a file  
 ### 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().  
 (code,  
 ### Code lines in a character vector containing multiple R objects to  
 ### parse for documentation.  
  objs,  
 ### The objects defined in the code.  
  ...  
 ### ignored  
79   ){   ){
80    parsed <- extract.file.parse(code)    tlines <- gsub("\\s*","",ex)
81    extract.docs.try <- function(o,on)    ##tlines <- gsub("#.*","",tlines)
82      {    prefixes <- unique(gsub("\\S.*","",ex[tlines!=""]))
83        ## Note: we could use parsed information here too, but that    FIND <- prefixes[which.min(nchar(prefixes))]
84        ## would produce different results for setMethodS3 etc.    ## Eliminate leading tabulations or 2/4 spaces
85        doc <- list()    sub(FIND, "", ex)
86        if ( !is.null(parsed[[on]]) ){  ### Character vector of code lines with preceding whitespace removed.
         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 ){  
           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.  
87  }  }
88    
89  ### Default parsers to use with package.skeleton.dx  examples.after.return <- function
90  default.parsers <- list("parsefuns","extract.docs.file",  ### Get examples from inline definitions after return()
91                          "examples.after.return","examples.from.testfile",  ### PhG: this does not work well! Think of these situations:
92                          "author.from.description","erase.format","title.from.name")  ### 1) You have multiple return() in the code of your function,
93    ### 2) You have return() appearing is some example code, ...
94  extract.docs.fun <- function # Extract documentation from a function  ### I can hardly propose a hack here. The whole code of the function
95  ### Given source code of a function, return a list describing inline  ### must be parsed, and one must determine which one is the last line
96  ### documentation in that source code.  ### of code that is actually executed.
97  (code,  ###
98  ### The function to examine.  ### I make two propositions here
99   name.fun  ### 1) to keep the same mechanism that has the advantage of simplicity
100  ### The name of the function/chunk to use in warning messages.  ###    but to use a special tag
101   )  ###    ##examples<< or #{{{examples to separate
102  {  ###    function code from examples explicitly, and
103    res <- list()  ### 2) to place the example in an "ex" attribute
104    clines <- grep(prefix,code)  ###    attached to the function
105    if(length(grep("#",code[1]))){  ###    (see next parser). That solution will be also interesting for
106      res$title <- gsub("[^#]*#\\s*(.*)","\\1",code[1],perl=TRUE)  ###    documenting datasets, something not done yet by inlinedocs!
107    }  (src,name="",...) {
108    if(length(clines) > 0){    ## Look for the examples mark
109      ##details<<    m <- grep("^\\s*(##examples<<|#\\{\\{\\{examples)", src)
110      ## The primary mechanism is that consecutive groups of lines matching    if (!length(m)) return(list())
111      ## the specified prefix regular expression "\code{^### }" (i.e. lines    if (length(m) > 1)
112      ## beginning with "\code{### }") are collected      warning("More than one examples tag for ", name,
113      ## as follows into documentation sections:\describe{              ". Taking the last one")
114      ## \item{description}{group starting at line 2 in the code}    m <- m[length(m)]
115      ## \item{arguments}{group following each function argument}    ## Look for the lines containing return value comments just before
116      ## \item{value}{group ending at the penultimate line of the code}}    r <- grep("\\s*### ", src[1:(m-1)])
117      ## These may be added to by use of the \code{##<<} constructs described      if (!length(r)) {
118      ## below.        value <- NULL
119        } else {
120          ## Only take consecutive lines before the mark
121          keep <- rev((m - rev(r)) == 1:length(r))
122          if (!any(keep)) {
123            value <- NULL
124          } else {
125            value <- decomment(src[r[keep]])
126          }
127        }
128      ## Collect now the example code beneath the mark
129      ex <- src[(m + 1):(length(src) - 1)]
130      ## Possibly eliminate a #}}} tag
131      ex <- ex[!grepl("#}}}", ex)]
132      ## Eliminate leading tabulations or four spaces
133      ex <- kill.prefix.whitespace(ex)
134      ## Add an empty line before and after example
135      ex <- c("", ex, "")
136      ## Return examples and value
137      list(examples = ex, value = value)
138    }
139    
140    prefixed.lines <- structure(function(src,...){
141    ### The primary mechanism of inline documentation is via consecutive
142    ### groups of lines matching the specified prefix regular expression
143    ### "\code{^### }" (i.e. lines beginning with "\code{### }") are
144    ### collected as follows into documentation sections:\describe{
145    ### \item{description}{group starting at line 2 in the code}
146    ### \item{arguments}{group following each function argument}
147    ### \item{value}{group ending at the penultimate line of the code}}
148    ### These may be added to by use of the \code{##<<} constructs
149    ### described below.
150      clines <- grep(prefix,src)
151      if(length(clines)==0)return(list())
152      bounds <- which(diff(clines)!=1)      bounds <- which(diff(clines)!=1)
153      starts <- c(1,bounds+1)      starts <- c(1,bounds+1)
154      ends <- c(bounds,length(clines))      ends <- c(bounds,length(clines))
155      ## detect body of function using paren matching
156      code <- gsub("#.*","",src)
157      f <- function(ch)cumsum(nchar(gsub(sprintf("[^%s]",ch),"",code)))
158      parens <- f("(")-f(")")
159      body.begin <- which(diff(parens)<0 & parens[-1]==0)+2
160      if(length(body.begin)==0)body.begin <- 1 ## rare cases
161      is.arg <- function(){
162        gres <- grep("^\\s*#",src[start-1],perl=TRUE)
163        0 == length(gres) && start<=body.begin
164      }
165      res <- list()
166      for(i in seq_along(starts)){      for(i in seq_along(starts)){
167        start <- clines[starts[i]]        start <- clines[starts[i]]
168        end <- clines[ends[i]]        end <- clines[ends[i]]
169        lab <- if(end+1==length(code))"value"      lab <- if(all(grepl("^\\s*#",src[end:(length(src)-1)])))"value"
170        else if(start==2)"description"        else if(start==2)"description"
171        else if ( 0 == length(grep("^\\s*#",code[start-1],perl=TRUE)) ){      else if(is.arg()){
172           #arg <- gsub("^[ (]*","",code[start-1])        ##twutz: strip leading white spaces and brackets and ,
173           #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 ,  
174                   arg <- gsub("^([^=,]*)[=,].*", "\\1", arg)                   arg <- gsub("^([^=,]*)[=,].*", "\\1", arg)
175                   arg <- gsub("^([^ \t]*)([ \t]+)$","\\1",arg)   #twutz: remove trailing whitespaces        ##twutz: remove trailing whitespaces
176          arg <- gsub("^([^ \t]*)([ \t]+)$","\\1",arg)
177                   arg <- gsub("...", "\\dots", arg, fix = TRUE)                   arg <- gsub("...", "\\dots", arg, fix = TRUE)
178           paste("item{",arg,"}",sep="")           paste("item{",arg,"}",sep="")
179         } else {         } else {
180           next;           next;
181         }         }
182        res[[lab]] <- decomment(code[start:end])      res[[lab]] <- decomment(src[start:end])
     }  
183    }    }
184      res
185    },ex=function(){
186    test <- function
187    ### the description
188    (x,
189    ### the first argument
190     y ##<< another argument
191     ){
192      5
193    ### the return value
194    ##seealso<< foobar
195    }
196    src <- attr(test,"source")
197    prefixed.lines(src)
198    extract.xxx.chunks(src)
199    })
200    
201    extract.xxx.chunks <- function # Extract documentation from a function
202    ### Given source code of a function, return a list describing inline
203    ### documentation in that source code.
204    (src,
205    ### The source lines of the function to examine, as a character
206    ### vector.
207     name.fun="(unnamed function)",
208    ### The name of the function/chunk to use in warning messages.
209     ...
210    ### ignored.
211     ){
212      res <- list()
213    ##details<< For simple functions/arguments, the argument may also be    ##details<< For simple functions/arguments, the argument may also be
214    ## documented by appending \code{##<<} comments on the same line as the    ## documented by appending \code{##<<} comments on the same line as the
215    ## argument name. Mixing this mechanism with \code{###} comment lines for    ## argument name. Mixing this mechanism with \code{###} comment lines for
# Line 300  Line 282 
282          chunk.sep <- "\n\n"          chunk.sep <- "\n\n"
283        }        }
284        chunk.res <- NULL        chunk.res <- NULL
285        if ( 0 == length(grep("^\\s*$",payload,perl=TRUE)) )        if ( !grepl("^\\s*$",payload,perl=TRUE) )
286          chunk.res <-          chunk.res <-
287            if ( is.null(res[[field]]) ) payload            if ( is.null(res[[field]]) ) payload
288            else paste(res[[field]], payload, sep=chunk.sep)            else paste(res[[field]], payload, sep=chunk.sep)
289        invisible(chunk.res)        invisible(chunk.res)
290      }      }
291    while ( k <= length(code) ){    while ( k <= length(src) ){
292      line <- code[k]      line <- src[k]
293      if ( 0 < length(grep(extra.regexp,line,perl=TRUE) ) ){      if ( grepl(extra.regexp,line,perl=TRUE) ){
294        ## 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
295        new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)        new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)
296        new.contents <- gsub(extra.regexp,"\\2",line,perl=TRUE)        new.contents <- gsub(extra.regexp,"\\2",line,perl=TRUE)
# Line 383  Line 365 
365            first.describe <- TRUE;            first.describe <- TRUE;
366          }          }
367        }        }
368      } else if ( in.chunk && 0<length(grep(cont.re,line,perl=TRUE)) ){      } else if ( in.chunk && grepl(cont.re,line,perl=TRUE) ){
369        ## append this line to current chunk        ## append this line to current chunk
370        if ( 0 == length(grep(prefix,line,perl=TRUE)) ){        if ( !grepl(prefix,line,perl=TRUE) ){
371          ##describe<< Any lines with "\code{### }" at the left hand          ##describe<< Any lines with "\code{### }" at the left hand
372          ## margin within the included chunks are handled separately,          ## margin within the included chunks are handled separately,
373          ## so if they appear in the documentation they will appear          ## so if they appear in the documentation they will appear
# Line 398  Line 380 
380            payload <- stripped            payload <- stripped
381          }          }
382        }        }
383      } else if ( 0 < length(grep(arg.pat,line,perl=TRUE)) ){      } else if ( grepl(arg.pat,line,perl=TRUE) ){
384        not.describe <- (0==in.describe && !first.describe)        not.describe <- (0==in.describe && !first.describe)
385        if ( in.chunk && not.describe){        if ( in.chunk && not.describe){
386          res[[cur.field]] <- end.chunk(cur.field,payload)          res[[cur.field]] <- end.chunk(cur.field,payload)
# Line 407  Line 389 
389        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)
390        in.chunk <- TRUE        in.chunk <- TRUE
391        if ( not.describe ){        if ( not.describe ){
392            ## TDH 2010-06-18 For item{}s in the documentation list names,
393            ## we don't need to have a backslash before, so delete it.
394            arg <- gsub("^[\\]+","",arg)
395          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots
396          payload <- comment          payload <- comment
397        } else {        } else {
# Line 457  Line 442 
442  ### with the string in this list (implemented in modify.Rd.file).  ### with the string in this list (implemented in modify.Rd.file).
443  }  }
444    
445    ### Parsers for each function that are constructed automatically. This
446    ### is a named list, and each element is a parser function for an
447    ### individual object.
448    forfun.parsers <-
449      list(prefixed.lines=prefixed.lines,
450           examples.after.return=examples.after.return,
451           extract.xxx.chunks=extract.xxx.chunks,
452           ## title from first line of function def
453           title.from.firstline=function(src,...){
454             first <- src[1]
455             if(!is.character(first))return(list())
456             if(!grepl("#",first))return(list())
457             list(title=gsub("[^#]*#\\s*(.*)","\\1",first,perl=TRUE))
458           },
459           ## PhG: it is tests/FUN.R!!! I would like more flexibility here
460           ## please, let me choose which dir to use for examples!
461           ## Get examples for FUN from the file tests/FUN.R
462           examples.from.testfile=function(name,...){
463             tsubdir <- getOption("inlinedocs.exdir")
464             if (is.null(tsubdir)) tsubdir <- "tests"       # Default value
465             tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))
466             if(file.exists(tfile))
467               list(examples=readLines(tfile))
468             else list()
469           },
470           definition.from.source=function(doc,src,...){
471             def <- doc$definition
472             is.empty <- function(x)is.null(x)||x==""
473             if(is.empty(def) && !is.empty(src))
474               list(definition=src)
475             else list()
476           })
477    
478    ### List of Parser Functions that can be applied to any object.
479    forall.parsers <-
480      list(## Fill in author from DESCRIPTION and titles.
481           author.from.description=function(desc,...){
482             list(author=desc[,"Maintainer"])
483           },
484           ## The format section sometimes causes problems, so erase it.
485           erase.format=function(...){
486             list(format="")
487           },
488           ## Convert the function name to a title.
489           title.from.name=function(name,doc,...){
490             if("title"%in%names(doc))list() else
491             list(title=gsub("[._]"," ",name))
492           },
493           ## PhG: here is what I propose for examples code in the 'ex' attribute
494           examples.in.attr =  function (name, o, ...) {
495             ex <- attr(o, "ex")
496             if (!is.null(ex)) {
497               ## Special case for code contained in a function
498               if (inherits(ex, "function")) {
499                 ## If source is available, start from there
500                 src <- attr(ex, "source")
501                 if (!is.null(src)) {
502                   ex <- src
503                 } else { ## Use the body of the function
504                   ex <- deparse(body(ex))
505                 }
506                 ## Eliminate leading and trailing code
507                 ex <- ex[-c(1, length(ex))]
508                 ## all the prefixes
509                 ex <- kill.prefix.whitespace(ex)
510                 ## Add an empty line before and after example
511                 ex <- c("", ex, "")
512               }
513               list(examples = ex)
514             } else list()
515           },
516           collapse.docs=function(doc,...){
517             list(doc=lapply(doc,paste,collapse="\n"))
518           })
519    
520    ### List of parser functions that operate on single objects. This list
521    ### is useful for testing these functions.
522    lonely <- c(forall.parsers,forfun.parsers)
523    attr(lonely,"ex") <- function(){
524      f <- function # title
525    ### description
526      (x, ##<< arg x
527       y
528    ### arg y
529       ){
530        ##value<< a list with elements
531        list(x=x, ##<< original x value
532             y=y, ##<< original y value
533             sum=x+y) ##<< their sum
534        ##end<<
535      }
536      src <- attr(f,"source")
537      lonely$extract.xxx.chunks(src)
538      lonely$prefixed.lines(src)
539    }
540    
541    extra.code.docs <- function # Extract documentation from code chunks
542    ### Parse R code to extract inline documentation from comments around
543    ### each function. These are not able to be retreived simply by
544    ### looking at the "source" attribute. This is a Parser Function that
545    ### can be used in the parser list of package.skeleton.dx(). TODO:
546    ### Modularize this into separate Parsers Functions for S4 classes,
547    ### prefixes, ##<<blocks, etc. Right now it is not very clean!
548    (code,
549    ### Code lines in a character vector containing multiple R objects to
550    ### parse for documentation.
551     objs,
552    ### The objects defined in the code.
553     ...
554    ### ignored
555     ){
556      parsed <- extract.file.parse(code)
557      extract.docs.try <- function(o,on)
558        {
559          ## Note: we could use parsed information here too, but that
560          ## would produce different results for setMethodS3 etc.
561          doc <- list()
562          if ( !is.null(parsed[[on]]) ){
563            if ( !is.na(parsed[[on]]@code[1]) ){ # no code given for generics
564              doc$definition <- paste(parsed[[on]]@code)
565            }
566            if(!"description"%in%names(doc) && !is.na(parsed[[on]]@description) ){
567              doc$description <- parsed[[on]]@description
568            }
569            if ( "setMethodS3" == parsed[[on]]@created ){
570              # PhG: this may be wrong! It does not catch correctly how the method
571              # must be splitted in case of methods containing dots. for instance,
572              # as.data.frame.matrix must be split into: m1 = as.data.frame and
573              # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!
574                      pattern <- "^([^\\.]+)\\.(.*)$"
575              doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),
576                  m2 <- gsub(pattern,"\\2",on,perl=TRUE))
577              if ( grepl("\\W",m1,perl=TRUE) ){
578                              m1 <- paste("`",m1,"`",sep="")
579              }
580              cat("S3method(",m1,",",m2,")\n",sep="")
581            }
582          }
583          if("title" %in% names(doc) && !"description" %in% names(doc) ){
584            ## For short functions having both would duplicate, but a
585            ## description is required. Therefore automatically copy title
586            ## across to avoid errors at package build time.
587            doc$description <- doc$title
588          }
589          doc
590        }
591      extract.docs <- function(on){
592        res <- try({o <- objs[[on]]
593                    extract.docs.try(o, on)},FALSE)
594        if(class(res)=="try-error"){
595          cat("Failed to extract docs for: ",on,"\n\n")
596          list()
597        } else if(0 == length(res) && inherits(objs[[on]],"standardGeneric")){
598          NULL
599        } else if(0 == length(res) && "function" %in% class(o)
600                  && 1 == length(osource <- attr(o,"source"))
601                  && grepl(paste("UseMethod(",on,")",sep="\""),osource)
602                  ){
603          ## phew - this should only pick up R.oo S3 generic definitions like:
604          ## attr(*, "source")= chr "function(...) UseMethod(\"select\")"
605          NULL
606        } else res
607      }
608      doc.names <- names(objs)
609      res <- sapply(doc.names,extract.docs,simplify=FALSE)
610      ## Special processing for S4 classes as they do not appear in normal ls()
611      for ( nn in names(parsed) ){
612        if ( parsed[[nn]]@created == "setClass" ){
613          S4class.docs <- extract.docs.setClass(parsed[[nn]])
614          docname <- paste(nn,"class",sep="-")
615          if ( is.null(res[[docname]]) ){
616            res[[docname]] <- S4class.docs
617            doc.names <- c(doc.names,docname)
618          } else {
619            stop(nn," appears as both S4 class and some other definition")
620          }
621        }
622      }
623      inherit.docs <- function(on){
624        in.res <- res[[on]]
625        if ( !is.null(parsed[[on]]) ){
626          for ( parent in parsed[[on]]@parent ){
627            if ( !is.na(parent) ){
628              if ( is.null(in.res) ){
629                in.res <- res[[parent]]
630              } else if ( parent %in% names(res) ){
631                parent.docs <- res[[parent]]
632                for ( nn in names(parent.docs) ){
633                  if ( !nn %in% names(in.res) ){
634                    in.res[[nn]] <- parent.docs[[nn]]
635                  }
636                }
637              }
638            }
639          }
640        }
641        invisible(in.res)
642      }
643      all.done <- FALSE
644      while ( !all.done ){
645        res1 <- sapply(doc.names,inherit.docs,simplify=FALSE)
646        all.done <- identical(res1,res)
647        res <- res1
648      }
649      ## now strip out any generics (which have value NULL in res):
650      res.not.null <- sapply(res,function(x){!is.null(x)})
651      if ( 0 < length(res.not.null) && length(res.not.null) < length(res) ){
652        res <- res[res.not.null]
653      }
654      res
655    ### named list of lists, one for each object to document.
656    }
657    
658    ### List of parsers to use by default with package.skeleton.dx.
659    default.parsers <-
660      c(extra.code.docs=extra.code.docs, ## TODO: cleanup!
661        sapply(forfun.parsers,forfun),
662        edit.package.file=function(desc,...){
663          in.details <- setdiff(colnames(desc),"Description")
664          details <- sprintf("%s: \\tab %s\\cr",in.details,desc[,in.details])
665          L <-
666            list(list(title=desc[,"Title"],
667                      description=desc[,"Description"],
668                      `tabular{ll}`=details))
669          names(L) <- paste(desc[,"Package"],"-package",sep="")
670          L
671        },
672        sapply(forall.parsers,forall)
673        )
674    
675  setClass("DocLink", # Link documentation among related functions  setClass("DocLink", # Link documentation among related functions
676  ### The \code{.DocLink} class provides the basis for hooking together  ### The \code{.DocLink} class provides the basis for hooking together
677  ### documentation of related classes/functions/objects. The aim is that  ### documentation of related classes/functions/objects. The aim is that
# Line 495  Line 710 
710      ## "prefix" lines will be used instead.      ## "prefix" lines will be used instead.
711      default.description <- NULL      default.description <- NULL
712      while ( start > last.end+1      while ( start > last.end+1
713             && 1 == length(grep(prefix,code[start-1],perl=TRUE)) ){             && grepl(prefix,code[start-1],perl=TRUE) ){
714        start <- start-1        start <- start-1
715      }      }
716      if ( start < chunks[[k]][1] ){      if ( start < chunks[[k]][1] ){
# Line 522  Line 737 
737        ## If the function definition is not embedded within the call, then        ## If the function definition is not embedded within the call, then
738        ## the parent is that function. Test whether the the third value        ## the parent is that function. Test whether the the third value
739        ## looks like a name and add it to parents if so.        ## looks like a name and add it to parents if so.
740        if ( 1 == length(grep("^[\\._\\w]+$",chars[3],perl=TRUE)) ){        if ( grepl("^[\\._\\w]+$",chars[3],perl=TRUE) ){
741          parent <- chars[3]          parent <- chars[3]
742        }        }
743        res[[object.name]] <- new("DocLink",name=object.name,        res[[object.name]] <- new("DocLink",name=object.name,
# Line 554  Line 769 
769        ## If the function definition is not embedded within the call, then        ## If the function definition is not embedded within the call, then
770        ## the parent is that function. Test whether the the fourth value        ## the parent is that function. Test whether the the fourth value
771        ## looks like a name and add it to parents if so.        ## looks like a name and add it to parents if so.
772        if ( 1 == length(grep("^[\\._\\w]+$",chars[4],perl=TRUE)) ){        if ( grepl("^[\\._\\w]+$",chars[4],perl=TRUE) ){
773          parent <- c(chars[4],parent)          parent <- c(chars[4],parent)
774        }        }
775        res[[object.name]] <- new("DocLink",name=object.name,        res[[object.name]] <- new("DocLink",name=object.name,
# Line 574  Line 789 
789  ### Using the same conventions as for functions, definitions of S4 classes  ### Using the same conventions as for functions, definitions of S4 classes
790  ### in the form \code{setClass("classname",\dots)} are also located and  ### in the form \code{setClass("classname",\dots)} are also located and
791  ### scanned for inline comments.  ### scanned for inline comments.
792  (doc.link)  (doc.link
793  ### DocLink object as created by \code{\link{extract.file.parse}}.  ### DocLink object as created by \code{\link{extract.file.parse}}.
794  ### Note that \code{source} statements are \emph{ignored} when scanning for  ### Note that \code{source} statements are \emph{ignored} when scanning for
795  ### class definitions.  ### class definitions.
796  {   ){
797    chunk.source <- doc.link@code    chunk.source <- doc.link@code
798    ##details<<    ##details<<
799    ## Extraction of S4 class documentation is currently limited to expressions    ## Extraction of S4 class documentation is currently limited to expressions
# Line 595  Line 810 
810    ## the same line or \code{### } comments at the beginning of the    ## the same line or \code{### } comments at the beginning of the
811    ## following line.    ## following line.
812    f.n <- paste(class.name,"class",sep="-")    f.n <- paste(class.name,"class",sep="-")
813    docs <- extract.docs.fun(chunk.source,f.n)    docs <- extract.xxx.chunks(chunk.source,f.n)
814      ## also apply source parsing functions that I separated out into
815      ## separate functions
816      docs <- combine(docs,lonely$prefixed.lines(chunk.source))
817      docs$title <- lonely$title.from.firstline(chunk.source)
818    ##details<<    ##details<<
819    ## The class definition skeleton includes an \code{Objects from the Class}    ## The class definition skeleton includes an \code{Objects from the Class}
820    ## section, to which any \code{##details<<} documentation chunks are    ## section, to which any \code{##details<<} documentation chunks are
# Line 619  Line 838 
838    }    }
839    invisible(docs)    invisible(docs)
840  }  }
841    
842    apply.parsers <- function
843    ### Parse code to r objs, then run all the parsers and return the
844    ### documentation list.
845    (code,
846    ### Character vector of code lines.
847     parsers=default.parsers,
848    ### List of Parser Functions.
849     verbose=FALSE,
850    ### Echo names of Parser Functions?
851     ...
852    ### Additional arguments to pass to Parser Functions.
853     ){
854      e <- new.env()
855      old <- options(keep.source=TRUE)
856      on.exit(options(old))
857      exprs <- parse(text=code)
858      for (i in exprs){
859        tryCatch(eval(i, e),error=function(e){
860          ##print(e)
861        })
862      }
863      objs <- sapply(ls(e),get,e,simplify=FALSE)
864    
865      docs <- list()
866    
867      # PhG: Automatically determine who is S3 methods
868      for (name in names(objs)) {
869        parts <- strsplit(name, ".", fixed = TRUE)[[1]]
870            l <- length(parts)
871            if (l > 1) {
872            for (i in 1:(l - 1)) {
873                # Look for a generic function (known by the system or defined
874                # in the package) that matches that part of the function name
875                generic <- paste(parts[1:i], collapse = ".")
876                if (any(generic %in% utils:::getKnownS3generics()) ||
877                    utils:::findGeneric(generic, e) != "") {
878                    object <- paste(parts[(i + 1):l], collapse = ".")
879                    docs[[name]]$s3method <- c(generic, object)
880                    break
881                }
882            }
883        }
884      }
885      ## apply parsers in sequence to code and objs
886      for(i in seq_along(parsers)){
887        N <- names(parsers[i])
888        if(verbose){
889          if(is.character(N) && N!=""){
890            cat(N," ",sep="")
891          }else cat('. ')
892        }
893        p <- parsers[[i]]
894        ## This is the argument list that each parser receives:
895        L <- p(code=code,objs=objs,docs=docs,...)
896        docs <- combine(docs,L)
897      }
898      if(verbose)cat("\n")
899      docs
900    ### A list of extracted documentation from code.
901    }
902    
903    ### Names of Parser Functions that operate on the desc arg.
904    descfile.names <- c("author.from.description","edit.package.file")
905    
906    ### Names of Parser Functions that do NOT use the desc arg.
907    non.descfile.names <-
908      names(default.parsers)[!names(default.parsers)%in%descfile.names]
909    
910    ### Parsers that operate only on R code, independently of the
911    ### description file.
912    nondesc.parsers <- default.parsers[non.descfile.names]
913    
914    extract.docs.file <- function
915    ### Apply all parsers relevant to extract info from just 1 code file.
916    (f,
917    ### File name of R code to read and parse.
918     parsers=nondesc.parsers,
919    ### Parser Functions to use to parse the code and extract
920    ### documentation.
921     ...
922    ### Other arguments to pass to Parser Functions.
923     ){
924      apply.parsers(readLines(f),parsers,verbose=FALSE,...)
925    }

Legend:
Removed from v.87  
changed lines
  Added in v.195

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