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 144, Wed Oct 20 09:33:04 2010 UTC
# Line 1  Line 1 
1  ### For each object in the package that satisfies the criterion  ### combine lists or character strings
2  ### checked by subfun, parse source using FUN and return the resulting  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    ### For each object in the package that satisfies the criterion#
32    ## checked by subfun, parse source using FUN and return the resulting
33  ### documentation list.  ### documentation list.
34  forall <- function  forall <- function
35  (FUN,  (FUN,
# 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      for(N in names(objs)){      for(N in names(docs)){
47        o <- objs[[N]]        o <- objs[[N]]
48        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"),
49                        name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)
50      }      }
51      L      L
52    }    }
53      class(f) <- c("allfun","function")
54      f
55  ### A Parser Function.  ### A Parser Function.
56  }  }
57    
58    ### Print method for functions constructed using forall.
59    print.allfun <- function(x,...){
60      e <- environment(x)
61      cat("Function to apply to every element.\nselector:")
62      print(e$subfun)
63      cat("processor:")
64      print(e$FUN)
65    }
66    
67  ### For each function in the package, do something.  ### For each function in the package, do something.
68  forfun <- function(FUN)forall(FUN,is.function)  forfun <- function(FUN)forall(FUN,is.function)
69    
70  ### Get last line (test)  examples.after.return <- function
71  parsefuns <- forfun(function(src,name,...){  ### Get examples from inline definitions after return()
72    ### PhG: this does not work well! Think of these situations:
73    ### 1) You have multiple return() in the code of your function,
74    ### 2) You have return() appearing is some example code, ...
75    ### I can hardly propose a hack here. The whole code of the function
76    ### must be parsed, and one must determine which one is the last line
77    ### of code that is actually executed.
78    ###
79    ### I make two propositions here
80    ### 1) to keep the same mechanism that has the advantage of simplicity
81    ###    but to use a special tag
82    ###    ##examples<< or #{{{examples to separate
83    ###    function code from examples explicitly, and
84    ### 2) to place the example in an "ex" attribute
85    ###    attached to the function
86    ###    (see next parser). That solution will be also interesting for
87    ###    documenting datasets, something not done yet by inlinedocs!
88    (src,name="",...) {
89      ## Look for the examples mark
90      m <- grep("^\\s*(##examples<<|#\\{\\{\\{examples)", src)
91      if (!length(m)) return(list())
92      if (length(m) > 1)
93        warning("More than one examples tag for ", name,
94                ". Taking the last one")
95      m <- m[length(m)]
96      ## Look for the lines containing return value comments just before
97      r <- grep("\\s*### ", src[1:(m-1)])
98        if (!length(r)) {
99          value <- NULL
100        } else {
101          ## Only take consecutive lines before the mark
102          keep <- rev((m - rev(r)) == 1:length(r))
103          if (!any(keep)) {
104            value <- NULL
105          } else {
106            value <- decomment(src[r[keep]])
107          }
108        }
109      ## Collect now the example code beneath the mark
110      ex <- src[(m + 1):(length(src) - 1)]
111      ## Possibly eliminate a #}}} tag
112      ex <- ex[!grepl("#}}}", ex)]
113      ## Eliminate leading tabulations or four spaces
114      prefixes <- gsub("(\\s*).*","\\1",ex,perl=TRUE)[grep("\\w",ex)]
115      FIND <- prefixes[which.min(nchar(prefixes))]
116      ex <- sub(FIND,"",ex)
117      ## Add an empty line before and after example
118      ex <- c("", ex, "")
119      ## Return examples and value
120      list(examples = paste(ex, collapse = "\n"), value = value)
121    }
122    
123    prefixed.lines <- function(src,...){
124    ### The primary mechanism of inline documentation is via consecutive
125    ### groups of lines matching the specified prefix regular expression
126    ### "\code{^### }" (i.e. lines beginning with "\code{### }") are
127    ### collected as follows into documentation sections:\describe{
128    ### \item{description}{group starting at line 2 in the code}
129    ### \item{arguments}{group following each function argument}
130    ### \item{value}{group ending at the penultimate line of the code}}
131    ### These may be added to by use of the \code{##<<} constructs
132    ### described below.
133      clines <- grep(prefix,src)
134      if(length(clines)==0)return(list())
135      bounds <- which(diff(clines)!=1)
136      starts <- c(1,bounds+1)
137      ends <- c(bounds,length(clines))
138      ## detect body of function using paren matching
139      f <- function(ch)cumsum(nchar(gsub(sprintf("[^%s]",ch),"",src)))
140      parens <- f("(")-f(")")
141      body.begin <- which(diff(parens)<0 & parens[-1]==0)+2
142      is.arg <- function(){
143        0 == length(grep("^\\s*#",src[start-1],perl=TRUE)) &&
144          start<=body.begin
145        }
146      res <- list()
147      for(i in seq_along(starts)){
148        start <- clines[starts[i]]
149        end <- clines[ends[i]]
150        lab <- if(end+1==length(src))"value"
151        else if(start==2)"description"
152        else if(is.arg()){
153          ##twutz: strip leading white spaces and brackets and ,
154          arg <- gsub("^[ \t(,]*", "", src[start - 1])
155          arg <- gsub("^([^=,]*)[=,].*", "\\1", arg)
156          ##twutz: remove trailing whitespaces
157          arg <- gsub("^([^ \t]*)([ \t]+)$","\\1",arg)
158          arg <- gsub("...", "\\dots", arg, fix = TRUE)
159          paste("item{",arg,"}",sep="")
160        } else {
161          next;
162        }
163        res[[lab]] <- decomment(src[start:end])
164      }
165      res
166    }
167    
168    ### Parsers for each object/function that are constructed
169    ### automatically. This is a named list, each element is a list of 2
170    ### elements: forfun/forall, then a parser function for an individual
171    ### object.
172    forfun.parsers <-
173      list(prefixed.lines=prefixed.lines,
174           examples.after.return=examples.after.return,
175           ## Extract lots of info from normal functions.
176           parsefun=function(src,name,...){
177    extract.docs.fun(src,name)    extract.docs.fun(src,name)
178           },
179           ## title from first line of function def
180           title.from.firstline=function(src,name,code,...){
181             if(length(grep("#",src[1]))){
182               list(title=gsub("[^#]*#\\s*(.*)","\\1",src[1],perl=TRUE))
183             } else list()
184           },
185           ## PhG: it is tests/FUN.R!!! I would like more flexibility here
186           ## please, let me choose which dir to use for examples!
187           ## Get examples for FUN from the file tests/FUN.R
188           examples.from.testfile=function(name,...){
189             tsubdir <- getOption("inlinedocs.exdir")
190             if (is.null(tsubdir)) tsubdir <- "tests"       # Default value
191             tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))
192             if(file.exists(tfile))
193               list(examples=paste(readLines(tfile),collapse="\n"))
194             else list()
195           },
196           definition.from.source=function(doc,src,...){
197             def <- doc$definition
198             is.empty <- function(x)is.null(x)||x==""
199             if(is.empty(def) && !is.empty(src))
200               list(definition=paste(src,collapse="\n"))
201             else list()
202  })  })
203    
204  ### Fill in author from DESCRIPTION and titles.  ### List of Parser Functions that can be applied to any object.
205  author.from.description <- forall(function(desc,...){  forall.parsers <-
206      list(## Fill in author from DESCRIPTION and titles.
207           author.from.description=function(desc,...){
208    list(author=desc[,"Maintainer"])    list(author=desc[,"Maintainer"])
209  })         },
210           ## The format section sometimes causes problems, so erase it.
211  ### The format section sometimes causes problems, so erase it.         erase.format=function(...){
 erase.format <- forall(function(...){  
212    list(format="")    list(format="")
213  })         },
214           ## Convert the function name to a title.
215  ### Convert the function name to a title.         title.from.name=function(name,doc,...){
216  title.from.name <- forall(function(name,...){           if("title"%in%names(doc))list() else
217    list(title=gsub("[._]"," ",name))    list(title=gsub("[._]"," ",name))
218           },
219           ## PhG: here is what I propose for examples code in the 'ex' attribute
220           examples.in.attr =  function (name, o, ...) {
221             ex <- attr(o, "ex")
222             if (!is.null(ex)) {
223               ## Special case for code contained in a function
224               if (inherits(ex, "function")) {
225                 ## If source is available, start from there
226                 src <- attr(ex, "source")
227                 if (!is.null(src)) {
228                   ex <- src
229                 } else { ## Use the body of the function
230                   ex <- deparse(body(ex))
231                 }
232                 ## Eliminate leading and trailing code
233                 ex <- ex[-c(1, length(ex))]
234                 ## Eliminate leading tabulations or 2/4 spaces
235                 ex <- sub("^\t|    |  ", "", ex)
236                 ## Add an empty line before and after example
237                 ex <- c("", ex, "")
238               }
239               list(examples = paste(ex, collapse = "\n"))
240             } else list()
241  })  })
242    
243  ### Get examples for FUN from the file test/FUN.R  ### List of parser functions that operate on single objects. This list
244  examples.from.testfile <- forfun(function(name,...){  ### is useful for testing these functions.
245    tfile <- file.path("..","tests",paste(name,".R",sep=""))  lonely <- c(forall.parsers,forfun.parsers)
246    if(file.exists(tfile))  attr(lonely,"ex") <- function(){
247      list(examples=paste(readLines(tfile),collapse="\n"))    lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")
248    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"))  
 })  
249    
250  extract.docs.file <- function # Extract documentation from a file  extra.code.docs <- function # Extract documentation from code chunks
251  ### Parse R code to extract inline documentation from comments around  ### Parse R code to extract inline documentation from comments around
252  ### each function. These are not able to be retreived simply by  ### each function. These are not able to be retreived simply by
253  ### looking at the "source" attribute. This is a Parser Function that  ### looking at the "source" attribute. This is a Parser Function that
254  ### can be used in the parser list of package.skeleton.dx().  ### can be used in the parser list of package.skeleton.dx(). TODO:
255    ### Modularize this into separate Parsers Functions for S4 classes,
256    ### prefixes, ##<<blocks, etc. Right now it is not very clean!
257  (code,  (code,
258  ### Code lines in a character vector containing multiple R objects to  ### Code lines in a character vector containing multiple R objects to
259  ### parse for documentation.  ### parse for documentation.
# Line 91  Line 276 
276            doc$description <- parsed[[on]]@description            doc$description <- parsed[[on]]@description
277          }          }
278          if ( "setMethodS3" == parsed[[on]]@created ){          if ( "setMethodS3" == parsed[[on]]@created ){
279              # PhG: this may be wrong! It does not catch correctly how the method
280              # must be splitted in case of methods containing dots. for instance,
281              # as.data.frame.matrix must be split into: m1 = as.data.frame and
282              # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!
283            pattern <- "^([^\\.]+)\\.(.*)$"            pattern <- "^([^\\.]+)\\.(.*)$"
284            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),
285                m2 <- gsub(pattern,"\\2",on,perl=TRUE))                m2 <- gsub(pattern,"\\2",on,perl=TRUE))
# Line 175  Line 364 
364  ### named list of lists, one for each object to document.  ### named list of lists, one for each object to document.
365  }  }
366    
367  ### Default parsers to use with package.skeleton.dx  ### List of parsers to use by default with package.skeleton.dx.
368  default.parsers <- list("parsefuns","extract.docs.file",  default.parsers <-
369                          "examples.after.return","examples.from.testfile",    c(extra.code.docs=extra.code.docs, ## TODO: cleanup!
370                          "author.from.description","erase.format","title.from.name")      sapply(forfun.parsers,forfun),
371        sapply(forall.parsers,forall),
372        edit.package.file=function(desc,...){
373          in.details <- setdiff(colnames(desc),"Description")
374          details <- paste(paste(in.details,": \\tab ",desc[,in.details],"\\cr",
375                                 sep=""),collapse="\n")
376          L <-
377            list(list(title=desc[,"Title"],
378                      description=desc[,"Description"],
379                      `tabular{ll}`=details,
380                      author=desc[,"Maintainer"]))
381          names(L) <- paste(desc[,"Package"],"-package",sep="")
382          L
383        })
384    
385  extract.docs.fun <- function # Extract documentation from a function  extract.docs.fun <- function # Extract documentation from a function
386  ### Given source code of a function, return a list describing inline  ### Given source code of a function, return a list describing inline
# Line 187  Line 389 
389  ### The function to examine.  ### The function to examine.
390   name.fun   name.fun
391  ### The name of the function/chunk to use in warning messages.  ### The name of the function/chunk to use in warning messages.
392   )   ){
 {  
393    res <- list()    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.  
     bounds <- which(diff(clines)!=1)  
     starts <- c(1,bounds+1)  
     ends <- c(bounds,length(clines))  
     for(i in seq_along(starts)){  
       start <- clines[starts[i]]  
       end <- clines[ends[i]]  
       lab <- if(end+1==length(code))"value"  
       else if(start==2)"description"  
       else if ( 0 == length(grep("^\\s*#",code[start-1],perl=TRUE)) ){  
          #arg <- gsub("^[ (]*","",code[start-1])  
          #arg <- gsub("^([^=,]*)[=,].*","\\1",arg)  
          #arg <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots  
                  arg <- gsub("^[ \t(,]*", "", code[start - 1])  #twutz: strip leading white spaces and brackets and ,  
                  arg <- gsub("^([^=,]*)[=,].*", "\\1", arg)  
                  arg <- gsub("^([^ \t]*)([ \t]+)$","\\1",arg)   #twutz: remove trailing whitespaces  
                  arg <- gsub("...", "\\dots", arg, fix = TRUE)  
          paste("item{",arg,"}",sep="")  
        } else {  
          next;  
        }  
       res[[lab]] <- decomment(code[start:end])  
     }  
   }  
394    ##details<< For simple functions/arguments, the argument may also be    ##details<< For simple functions/arguments, the argument may also be
395    ## documented by appending \code{##<<} comments on the same line as the    ## documented by appending \code{##<<} comments on the same line as the
396    ## argument name. Mixing this mechanism with \code{###} comment lines for    ## argument name. Mixing this mechanism with \code{###} comment lines for
# Line 407  Line 570 
570        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)
571        in.chunk <- TRUE        in.chunk <- TRUE
572        if ( not.describe ){        if ( not.describe ){
573            ## TDH 2010-06-18 For item{}s in the documentation list names,
574            ## we don't need to have a backslash before, so delete it.
575            arg <- gsub("^[\\]+","",arg)
576          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots
577          payload <- comment          payload <- comment
578        } else {        } else {
# Line 574  Line 740 
740  ### Using the same conventions as for functions, definitions of S4 classes  ### Using the same conventions as for functions, definitions of S4 classes
741  ### in the form \code{setClass("classname",\dots)} are also located and  ### in the form \code{setClass("classname",\dots)} are also located and
742  ### scanned for inline comments.  ### scanned for inline comments.
743  (doc.link)  (doc.link
744  ### DocLink object as created by \code{\link{extract.file.parse}}.  ### DocLink object as created by \code{\link{extract.file.parse}}.
745  ### Note that \code{source} statements are \emph{ignored} when scanning for  ### Note that \code{source} statements are \emph{ignored} when scanning for
746  ### class definitions.  ### class definitions.
747  {   ){
748    chunk.source <- doc.link@code    chunk.source <- doc.link@code
749    ##details<<    ##details<<
750    ## Extraction of S4 class documentation is currently limited to expressions    ## Extraction of S4 class documentation is currently limited to expressions
# Line 596  Line 762 
762    ## following line.    ## following line.
763    f.n <- paste(class.name,"class",sep="-")    f.n <- paste(class.name,"class",sep="-")
764    docs <- extract.docs.fun(chunk.source,f.n)    docs <- extract.docs.fun(chunk.source,f.n)
765      ## also apply source parsing functions that I separated out into
766      ## separate functions
767      docs <- combine(docs,lonely$prefixed.lines(chunk.source))
768      docs$title <- lonely$title.from.firstline(chunk.source)
769    ##details<<    ##details<<
770    ## The class definition skeleton includes an \code{Objects from the Class}    ## The class definition skeleton includes an \code{Objects from the Class}
771    ## section, to which any \code{##details<<} documentation chunks are    ## section, to which any \code{##details<<} documentation chunks are
# Line 619  Line 789 
789    }    }
790    invisible(docs)    invisible(docs)
791  }  }
792    
793    apply.parsers <- function
794    ### Parse code to r objs, then run all the parsers and return the
795    ### documentation list.
796    (code,
797    ### Character vector of code lines.
798     parsers=default.parsers,
799    ### List of Parser Functions.
800     verbose=FALSE,
801    ### Echo names of Parser Functions?
802     ...
803    ### Additional arguments to pass to Parser Functions.
804     ){
805      e <- new.env()
806      old <- options(keep.source=TRUE)
807      on.exit(options(old))
808      exprs <- parse(text=code)
809      for (i in exprs) eval(i, e)
810      objs <- sapply(ls(e),get,e,simplify=FALSE)
811    
812      docs <- list()
813    
814      # PhG: Automatically determine who is S3 methods
815      for (name in names(objs)) {
816        parts <- strsplit(name, ".", fixed = TRUE)[[1]]
817            l <- length(parts)
818            if (l > 1) {
819            for (i in 1:(l - 1)) {
820                # Look for a generic function (known by the system or defined
821                # in the package) that matches that part of the function name
822                generic <- paste(parts[1:i], collapse = ".")
823                if (any(generic %in% utils:::getKnownS3generics()) ||
824                    utils:::findGeneric(generic, e) != "") {
825                    object <- paste(parts[(i + 1):l], collapse = ".")
826                    docs[[name]]$s3method <- c(generic, object)
827                    break
828                }
829            }
830        }
831      }
832      ## apply parsers in sequence to code and objs
833      for(i in seq_along(parsers)){
834        N <- names(parsers[i])
835        if(verbose){
836          if(is.character(N) && N!=""){
837            cat(N," ",sep="")
838          }else cat('. ')
839        }
840        p <- parsers[[i]]
841        ## This is the argument list that each parser receives:
842        L <- p(code=code,objs=objs,docs=docs,...)
843        docs <- combine(docs,L)
844      }
845      if(verbose)cat("\n")
846      docs
847    ### A list of extracted documentation from code.
848    }
849    
850    ### Names of Parser Functions that operate on the desc arg.
851    descfile.names <- c("author.from.description","edit.package.file")
852    
853    ### Names of Parser Functions that do NOT use the desc arg.
854    non.descfile.names <-
855      names(default.parsers)[!names(default.parsers)%in%descfile.names]
856    
857    ### Parsers that operate only on R code, independently of the
858    ### description file.
859    nondesc.parsers <- default.parsers[non.descfile.names]
860    
861    extract.docs.file <- function
862    ### Apply all parsers relevant to extract info from just 1 code file.
863    (f,
864    ### File name of R code to read and parse.
865     parsers=nondesc.parsers,
866    ### Parser Functions to use to parse the code and extract
867    ### documentation.
868     ...
869    ### Other arguments to pass to Parser Functions.
870     ){
871      apply.parsers(readLines(f),parsers,verbose=FALSE,...)
872    }

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

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