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 105, Tue May 18 17:32:43 2010 UTC revision 152, Thu Oct 28 15:57:51 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(docs)){      for(N in names(docs)){
# Line 19  Line 50 
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    examples.after.return <- function
71    ### 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  ### Parsers for each object/function that are constructed
169  ### automatically. This is a named list, each element is a list of 2  ### automatically. This is a named list, each element is a list of 2
170  ### elements: forfun/forall, then a parser function for an individual  ### elements: forfun/forall, then a parser function for an individual
171  ### object.  ### object.
172  forall.parsers <-  forfun.parsers <-
173    list(## Extract lots of info from normal functions.    list(prefixed.lines=prefixed.lines,
174         parsefun=list(forfun,function(src,name,...){         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         ## Fill in author from DESCRIPTION and titles.         ## title from first line of function def
180         author.from.description=list(forall,function(desc,...){         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    ### List of Parser Functions that can be applied to any object.
205    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.         ## The format section sometimes causes problems, so erase it.
211         erase.format=list(forall,function(...){         erase.format=function(...){
212           list(format="")           list(format="")
213         }),         },
214         ## Convert the function name to a title.         ## Convert the function name to a title.
215         title.from.name=list(forall,function(name,doc,...){         title.from.name=function(name,doc,...){
216           if("title"%in%names(doc))list() else           if("title"%in%names(doc))list() else
217           list(title=gsub("[._]"," ",name))           list(title=gsub("[._]"," ",name))
218         }),         },
219         ## Get examples for FUN from the file test/FUN.R         ## PhG: here is what I propose for examples code in the 'ex' attribute
220         examples.from.testfile=list(forfun,function(name,...){         examples.in.attr =  function (name, o, ...) {
221           tfile <- file.path("..","tests",paste(name,".R",sep=""))           ex <- attr(o, "ex")
222           if(file.exists(tfile))           if (!is.null(ex)) {
223             list(examples=paste(readLines(tfile),collapse="\n"))             ## Special case for code contained in a function
224           else list()             if (inherits(ex, "function")) {
225         }),               ## If source is available, start from there
226         ## Get examples from inline definitions after return()               src <- attr(ex, "source")
227         examples.after.return=list(forfun,function(name,src,...){               if (!is.null(src)) {
228           coll <- paste(src,collapse="\n")                 ex <- src
229           thispre <- gsub("^[\\^]","",prefix)               } else { ## Use the body of the function
230           FIND <- paste("(return|UseMethod)[(][^\\n]*\\n",thispre,sep="")                 ex <- deparse(body(ex))
231           m <- regexpr(FIND,coll)               }
232           if(m[1]==-1)return(list())               ## Eliminate leading and trailing code
233           after <- substr(coll,m[1],nchar(coll))               ex <- ex[-c(1, length(ex))]
234           FIND <-               ## Eliminate leading tabulations or 2/4 spaces
235             paste("[^\\n]*",# rest of the return line               ex <- sub("^\t|    |  ", "", ex)
236                   "((?:\\n###[^\\n]*)+)",#comment value lines \\1               ## Add an empty line before and after example
237                   "([\\w\\W]*)[}]",#examples \\2               ex <- c("", ex, "")
238                   sep="")             }
239           SEP <- "-/-/-/-/-=====-----"             list(examples = paste(ex, collapse = "\n"))
240           REP <- paste("\\1",SEP,"\\2",sep="")           } else list()
241           r <- strsplit(gsub(FIND,REP,after,perl=TRUE),split=SEP)[[1]]         })
          l <- strsplit(r,split="\n")  
          excode <- c(l[[2]],"")  
          if(length(l)<2||paste(excode,collapse="")=="")return(list())  
          prefixes <- gsub("(\\s*).*","\\1",excode,perl=TRUE)[grep("\\w",excode)]  
          FIND <- prefixes[which.min(nchar(prefixes))]  
          list(examples=paste(sub(FIND,"",excode),collapse="\n"),  
               value=decomment(l[[1]][-1]))  
        }))  
242    
243  ### List of parser functions that operate on single objects. This list  ### List of parser functions that operate on single objects. This list
244  ### is useful for testing these functions, ie  ### is useful for testing these functions.
245  ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")  lonely <- c(forall.parsers,forfun.parsers)
246  lonely <- sapply(forall.parsers,function(L)L[[2]])  attr(lonely,"ex") <- function(){
247      lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")
248    }
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 111  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 196  Line 365 
365  }  }
366    
367  ### List of parsers to use by default with package.skeleton.dx.  ### List of parsers to use by default with package.skeleton.dx.
368  default.parsers <- c(extract.docs.file=extract.docs.file,  default.parsers <-
369                       sapply(forall.parsers,function(L)L[[1]](L[[2]])))    c(extra.code.docs=extra.code.docs, ## TODO: cleanup!
370        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 208  Line 391 
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 425  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 614  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 638  Line 790 
790    invisible(docs)    invisible(docs)
791  }  }
792    
793  extract.docs.code <- function  apply.parsers <- function
794  ### Write code to a file and parse it to r objs, then run all the  ### Parse code to r objs, then run all the parsers and return the
795  ### parsers and return the documentation list.  ### documentation list.
796  (code,  (code,
797  ### Character vector of code lines.  ### Character vector of code lines.
798   parsers,   parsers=default.parsers,
799  ### List of Parser Functions.  ### List of Parser Functions.
800   verbose=TRUE,   verbose=FALSE,
801  ### Echo names of Parser Functions?  ### Echo names of Parser Functions?
802   ...   ...
803  ### Additional arguments to pass to Parser Functions.  ### Additional arguments to pass to Parser Functions.
804   ){   ){
   code.file <- tempfile()  
   writeLines(code,code.file)  
805    e <- new.env()    e <- new.env()
806    old <- options(keep.source.pkgs=TRUE)    old <- options(keep.source=TRUE)
807    tryCatch(suppressWarnings(sys.source(code.file,e)),error=function(e){    on.exit(options(old))
808      stop("source ",code.file," failed with error:\n",e)    exprs <- parse(text=code)
809      for (i in exprs){
810        tryCatch(eval(i, e),error=function(e){
811          ##print(e)
812    })    })
813    options(old)    }
814    objs <- sapply(ls(e),get,e,simplify=FALSE)    objs <- sapply(ls(e),get,e,simplify=FALSE)
815    
   ## apply parsers in sequence to code and objs  
816    docs <- list()    docs <- list()
817    
818      # PhG: Automatically determine who is S3 methods
819      for (name in names(objs)) {
820        parts <- strsplit(name, ".", fixed = TRUE)[[1]]
821            l <- length(parts)
822            if (l > 1) {
823            for (i in 1:(l - 1)) {
824                # Look for a generic function (known by the system or defined
825                # in the package) that matches that part of the function name
826                generic <- paste(parts[1:i], collapse = ".")
827                if (any(generic %in% utils:::getKnownS3generics()) ||
828                    utils:::findGeneric(generic, e) != "") {
829                    object <- paste(parts[(i + 1):l], collapse = ".")
830                    docs[[name]]$s3method <- c(generic, object)
831                    break
832                }
833            }
834        }
835      }
836      ## apply parsers in sequence to code and objs
837    for(i in seq_along(parsers)){    for(i in seq_along(parsers)){
838      N <- names(parsers[i])      N <- names(parsers[i])
839      if(verbose){      if(verbose){
# Line 678  Line 850 
850    docs    docs
851  ### A list of extracted documentation from code.  ### A list of extracted documentation from code.
852  }  }
853    
854    ### Names of Parser Functions that operate on the desc arg.
855    descfile.names <- c("author.from.description","edit.package.file")
856    
857    ### Names of Parser Functions that do NOT use the desc arg.
858    non.descfile.names <-
859      names(default.parsers)[!names(default.parsers)%in%descfile.names]
860    
861    ### Parsers that operate only on R code, independently of the
862    ### description file.
863    nondesc.parsers <- default.parsers[non.descfile.names]
864    
865    extract.docs.file <- function
866    ### Apply all parsers relevant to extract info from just 1 code file.
867    (f,
868    ### File name of R code to read and parse.
869     parsers=nondesc.parsers,
870    ### Parser Functions to use to parse the code and extract
871    ### documentation.
872     ...
873    ### Other arguments to pass to Parser Functions.
874     ){
875      apply.parsers(readLines(f),parsers,verbose=FALSE,...)
876    }

Legend:
Removed from v.105  
changed lines
  Added in v.152

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