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 103, Tue May 18 16:33:29 2010 UTC revision 142, Wed Oct 20 09:21:53 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             if(is.null(def)||def=="")
199               list(definition=paste(src,collapse="\n"))
200             else list()
201           })
202    
203    ### List of Parser Functions that can be applied to any object.
204    forall.parsers <-
205      list(## Fill in author from DESCRIPTION and titles.
206           author.from.description=function(desc,...){
207           list(author=desc[,"Maintainer"])           list(author=desc[,"Maintainer"])
208         }),         },
209         ## The format section sometimes causes problems, so erase it.         ## The format section sometimes causes problems, so erase it.
210         erase.format=list(forall,function(...){         erase.format=function(...){
211           list(format="")           list(format="")
212         }),         },
213         ## Convert the function name to a title.         ## Convert the function name to a title.
214         title.from.name=list(forall,function(name,doc,...){         title.from.name=function(name,doc,...){
215           if("title"%in%names(doc))list() else           if("title"%in%names(doc))list() else
216           list(title=gsub("[._]"," ",name))           list(title=gsub("[._]"," ",name))
217         }),         },
218         ## Get examples for FUN from the file test/FUN.R         ## PhG: here is what I propose for examples code in the 'ex' attribute
219         examples.from.testfile=list(forfun,function(name,...){         examples.in.attr =  function (name, o, ...) {
220           tfile <- file.path("..","tests",paste(name,".R",sep=""))           ex <- attr(o, "ex")
221           if(file.exists(tfile))           if (!is.null(ex)) {
222             list(examples=paste(readLines(tfile),collapse="\n"))             ## Special case for code contained in a function
223           else list()             if (inherits(ex, "function")) {
224         }),               ## If source is available, start from there
225         ## Get examples from inline definitions after return()               src <- attr(ex, "source")
226         examples.after.return=list(forfun,function(name,src,...){               if (!is.null(src)) {
227           coll <- paste(src,collapse="\n")                 ex <- src
228           thispre <- gsub("^[\\^]","",prefix)               } else { ## Use the body of the function
229           FIND <- paste("(return|UseMethod)[(][^\\n]*\\n",thispre,sep="")                 ex <- deparse(body(ex))
230           m <- regexpr(FIND,coll)               }
231           if(m[1]==-1)return(list())               ## Eliminate leading and trailing code
232           after <- substr(coll,m[1],nchar(coll))               ex <- ex[-c(1, length(ex))]
233           FIND <-               ## Eliminate leading tabulations or 2/4 spaces
234             paste("[^\\n]*",# rest of the return line               ex <- sub("^\t|    |  ", "", ex)
235                   "((?:\\n###[^\\n]*)+)",#comment value lines \\1               ## Add an empty line before and after example
236                   "([\\w\\W]*)[}]",#examples \\2               ex <- c("", ex, "")
237                   sep="")             }
238           SEP <- "----------"             list(examples = paste(ex, collapse = "\n"))
239           REP <- paste("\\1",SEP,"\\2",sep="")           } else list()
240           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]))  
        }))  
241    
242  ### List of parser functions that operate on single objects. This list  ### List of parser functions that operate on single objects. This list
243  ### is useful for testing these functions, ie  ### is useful for testing these functions.
244  ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")  lonely <- c(forall.parsers,forfun.parsers)
245  lonely <- sapply(forall.parsers,function(L)L[[2]])  attr(lonely,"ex") <- function(){
246      lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")
247    }
248    
249  extract.docs.file <- function # Extract documentation from a file  extra.code.docs <- function # Extract documentation from code chunks
250  ### Parse R code to extract inline documentation from comments around  ### Parse R code to extract inline documentation from comments around
251  ### each function. These are not able to be retreived simply by  ### each function. These are not able to be retreived simply by
252  ### looking at the "source" attribute. This is a Parser Function that  ### looking at the "source" attribute. This is a Parser Function that
253  ### can be used in the parser list of package.skeleton.dx().  ### can be used in the parser list of package.skeleton.dx(). TODO:
254    ### Modularize this into separate Parsers Functions for S4 classes,
255    ### prefixes, ##<<blocks, etc. Right now it is not very clean!
256  (code,  (code,
257  ### Code lines in a character vector containing multiple R objects to  ### Code lines in a character vector containing multiple R objects to
258  ### parse for documentation.  ### parse for documentation.
# Line 111  Line 275 
275            doc$description <- parsed[[on]]@description            doc$description <- parsed[[on]]@description
276          }          }
277          if ( "setMethodS3" == parsed[[on]]@created ){          if ( "setMethodS3" == parsed[[on]]@created ){
278              # PhG: this may be wrong! It does not catch correctly how the method
279              # must be splitted in case of methods containing dots. for instance,
280              # as.data.frame.matrix must be split into: m1 = as.data.frame and
281              # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!
282            pattern <- "^([^\\.]+)\\.(.*)$"            pattern <- "^([^\\.]+)\\.(.*)$"
283            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),
284                m2 <- gsub(pattern,"\\2",on,perl=TRUE))                m2 <- gsub(pattern,"\\2",on,perl=TRUE))
# Line 196  Line 364 
364  }  }
365    
366  ### List of parsers to use by default with package.skeleton.dx.  ### List of parsers to use by default with package.skeleton.dx.
367  default.parsers <- c(extract.docs.file=extract.docs.file,  default.parsers <-
368                       sapply(forall.parsers,function(L)L[[1]](L[[2]])))    c(extra.code.docs=extra.code.docs, ## TODO: cleanup!
369        sapply(forfun.parsers,forfun),
370        sapply(forall.parsers,forall),
371        edit.package.file=function(desc,...){
372          in.details <- setdiff(colnames(desc),"Description")
373          details <- paste(paste(in.details,": \\tab ",desc[,in.details],"\\cr",
374                                 sep=""),collapse="\n")
375          L <-
376            list(list(title=desc[,"Title"],
377                      description=desc[,"Description"],
378                      `tabular{ll}`=details,
379                      author=desc[,"Maintainer"]))
380          names(L) <- paste(desc[,"Package"],"-package",sep="")
381          L
382        })
383    
384  extract.docs.fun <- function # Extract documentation from a function  extract.docs.fun <- function # Extract documentation from a function
385  ### 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 390 
390  ### The name of the function/chunk to use in warning messages.  ### The name of the function/chunk to use in warning messages.
391   ){   ){
392    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])  
     }  
   }  
393    ##details<< For simple functions/arguments, the argument may also be    ##details<< For simple functions/arguments, the argument may also be
394    ## documented by appending \code{##<<} comments on the same line as the    ## documented by appending \code{##<<} comments on the same line as the
395    ## argument name. Mixing this mechanism with \code{###} comment lines for    ## argument name. Mixing this mechanism with \code{###} comment lines for
# Line 425  Line 569 
569        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)
570        in.chunk <- TRUE        in.chunk <- TRUE
571        if ( not.describe ){        if ( not.describe ){
572            ## TDH 2010-06-18 For item{}s in the documentation list names,
573            ## we don't need to have a backslash before, so delete it.
574            arg <- gsub("^[\\]+","",arg)
575          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots
576          payload <- comment          payload <- comment
577        } else {        } else {
# Line 614  Line 761 
761    ## following line.    ## following line.
762    f.n <- paste(class.name,"class",sep="-")    f.n <- paste(class.name,"class",sep="-")
763    docs <- extract.docs.fun(chunk.source,f.n)    docs <- extract.docs.fun(chunk.source,f.n)
764      ## also apply source parsing functions that I separated out into
765      ## separate functions
766      docs <- combine(docs,lonely$prefixed.lines(chunk.source))
767      docs$title <- lonely$title.from.firstline(chunk.source)
768    ##details<<    ##details<<
769    ## The class definition skeleton includes an \code{Objects from the Class}    ## The class definition skeleton includes an \code{Objects from the Class}
770    ## section, to which any \code{##details<<} documentation chunks are    ## section, to which any \code{##details<<} documentation chunks are
# Line 637  Line 788 
788    }    }
789    invisible(docs)    invisible(docs)
790  }  }
791    
792    apply.parsers <- function
793    ### Parse code to r objs, then run all the parsers and return the
794    ### documentation list.
795    (code,
796    ### Character vector of code lines.
797     parsers=default.parsers,
798    ### List of Parser Functions.
799     verbose=FALSE,
800    ### Echo names of Parser Functions?
801     ...
802    ### Additional arguments to pass to Parser Functions.
803     ){
804      e <- new.env()
805      old <- options(keep.source=TRUE)
806      on.exit(options(old))
807      exprs <- parse(text=code)
808      for (i in exprs) eval(i, e)
809      objs <- sapply(ls(e),get,e,simplify=FALSE)
810    
811      docs <- list()
812    
813      # PhG: Automatically determine who is S3 methods
814      for (name in names(objs)) {
815        parts <- strsplit(name, ".", fixed = TRUE)[[1]]
816            l <- length(parts)
817            if (l > 1) {
818            for (i in 1:(l - 1)) {
819                # Look for a generic function (known by the system or defined
820                # in the package) that matches that part of the function name
821                generic <- paste(parts[1:i], collapse = ".")
822                if (any(generic %in% utils:::getKnownS3generics()) ||
823                    utils:::findGeneric(generic, e) != "") {
824                    object <- paste(parts[(i + 1):l], collapse = ".")
825                    docs[[name]]$s3method <- c(generic, object)
826                    break
827                }
828            }
829        }
830      }
831      ## apply parsers in sequence to code and objs
832      for(i in seq_along(parsers)){
833        N <- names(parsers[i])
834        if(verbose){
835          if(is.character(N) && N!=""){
836            cat(N," ",sep="")
837          }else cat('. ')
838        }
839        p <- parsers[[i]]
840        ## This is the argument list that each parser receives:
841        L <- p(code=code,objs=objs,docs=docs,...)
842        docs <- combine(docs,L)
843      }
844      if(verbose)cat("\n")
845      docs
846    ### A list of extracted documentation from code.
847    }
848    
849    ### Names of Parser Functions that operate on the desc arg.
850    descfile.names <- c("author.from.description","edit.package.file")
851    
852    ### Names of Parser Functions that do NOT use the desc arg.
853    non.descfile.names <-
854      names(default.parsers)[!names(default.parsers)%in%descfile.names]
855    
856    ### Parsers that operate only on R code, independently of the
857    ### description file.
858    nondesc.parsers <- default.parsers[non.descfile.names]
859    
860    extract.docs.file <- function
861    ### Apply all parsers relevant to extract info from just 1 code file.
862    (f,
863    ### File name of R code to read and parse.
864     parsers=nondesc.parsers,
865    ### Parser Functions to use to parse the code and extract
866    ### documentation.
867     ...
868    ### Other arguments to pass to Parser Functions.
869     ){
870      apply.parsers(readLines(f),parsers,verbose=FALSE,...)
871    }

Legend:
Removed from v.103  
changed lines
  Added in v.142

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