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 109, Mon Jun 7 08:42:15 2010 UTC
# Line 12  Line 12 
12    function(objs,docs,...){    function(objs,docs,...){
13      objs <- objs[sapply(objs,subfun)]      objs <- objs[sapply(objs,subfun)]
14      L <- list()      L <- list()
15      for(N in names(objs)){      for(N in names(docs)){
16        o <- objs[[N]]        o <- objs[[N]]
17        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"),
18                        name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)
19      }      }
20      L      L
21    }    }
# Line 24  Line 25 
25  ### For each function in the package, do something.  ### For each function in the package, do something.
26  forfun <- function(FUN)forall(FUN,is.function)  forfun <- function(FUN)forall(FUN,is.function)
27    
28  ### Get last line (test)  ### Parsers for each object/function that are constructed
29  parsefuns <- forfun(function(src,name,...){  ### automatically. This is a named list, each element is a list of 2
30    ### elements: forfun/forall, then a parser function for an individual
31    ### object.
32    forall.parsers <-
33      list(## Extract lots of info from normal functions.
34           parsefun=list(forfun,function(src,name,...){
35    extract.docs.fun(src,name)    extract.docs.fun(src,name)
36  })         }),
37           ## Fill in author from DESCRIPTION and titles.
38  ### Fill in author from DESCRIPTION and titles.         author.from.description=list(forall,function(desc,...){
 author.from.description <- forall(function(desc,...){  
39    list(author=desc[,"Maintainer"])    list(author=desc[,"Maintainer"])
40  })         }),
41           ## The format section sometimes causes problems, so erase it.
42  ### The format section sometimes causes problems, so erase it.         erase.format=list(forall,function(...){
 erase.format <- forall(function(...){  
43    list(format="")    list(format="")
44  })         }),
45           ## Convert the function name to a title.
46  ### Convert the function name to a title.         title.from.name=list(forall,function(name,doc,...){
47  title.from.name <- forall(function(name,...){           if("title"%in%names(doc))list() else
48    list(title=gsub("[._]"," ",name))    list(title=gsub("[._]"," ",name))
49  })         }),
50           # PhG: it is tests/FUN.R!!! I would like more flexibility here
51  ### Get examples for FUN from the file test/FUN.R             # please, let me choose which dir to use for examples!
52  examples.from.testfile <- forfun(function(name,...){             ## Get examples for FUN from the file tests/FUN.R
53    tfile <- file.path("..","tests",paste(name,".R",sep=""))         examples.from.testfile=list(forfun,function(name,...){
54             tsubdir <- getOption("inlinedocs.exdir")
55                     if (is.null(tsubdir)) tsubdir <- "tests"       # Default value
56                     tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))
57    if(file.exists(tfile))    if(file.exists(tfile))
58      list(examples=paste(readLines(tfile),collapse="\n"))      list(examples=paste(readLines(tfile),collapse="\n"))
59    else list()    else list()
60  })         }),
61           ## Get examples from inline definitions after return()
62  ### Get examples from inline definitions after return()             # PhG: this does not work well! Think at these situations:
63  examples.after.return <- forfun(function(src,...){             # 1) You have multiple return() in the code of your function,
64    rline <- grep("return([^)]*)",src)             # 2) You have return() appearing is some example code, ...
65    if(length(rline)==0)return(list())             # I can hardly propose a hack here. The whole code of the function
66    rline <- rline[length(rline)]             # must be parsed, and one must determine which one is the last line
67    comment.line.nums <- grep(prefix,src)             # of code that is actually executed.
68    if(!(comment.end <- rline+1)%in%comment.line.nums)return(list())             #
69    while(comment.end%in%comment.line.nums)comment.end <- comment.end+1             # I make two propositions here
70    excode <- src[comment.end:(length(src)-1)]             # 1) to keep the same mechanism that has the advantage of simplicity
71    list(examples=paste(gsub("^\\W*","",excode),collapse="\n"))             #    but to use a special tag ##examples<< or #{{{examples to separate
72  })             #    function code from examples explicitly, and
73               # 2) to place the example in an "ex" attribute attached to the function
74               #    (see next parser). That solution will be also interesting for
75               #    documenting datasets, something not done yet by inlinedocs!
76               examples.after.return = list(forfun, function(name, src, ...) {
77                            # Look for the examples mark
78                            m <- grep("##examples<<|#\\{\\{\\{examples", src)
79                            if (!length(m)) return(list())
80                            if (length(m) > 1)
81                                    warning("More than one examples tag for ", name, ". Taking the last one")
82                            m <- m[length(m)]
83                            # Look for the lines containing return value comments just before
84                            r <- grep("\\s*### ", src[1:(m-1)])
85                            if (!length(r)) {
86                                    value <- NULL
87                            } else {
88                                    # Only take consecutive lines before the mark
89                                    keep <- rev((m - rev(r)) == 1:length(r))
90                                    if (!any(keep)) {
91                                            value <- NULL
92                                    } else {
93                                            value <- decomment(src[r[keep]])
94                                    }
95                            }
96                            # Collect now the example code beneath the mark
97                            ex <- src[(m + 1):(length(src) - 1)]
98                            # Possibly eliminate a #}}} tag
99                            ex <- ex[!grepl("#}}}", ex)]
100                            # Eliminate leading tabulations or four spaces
101                            ex <- sub("^\t|    ", "", ex)
102                            # Add an empty line before and after example
103                            ex <- c("", ex, "")
104                            # Return examples and value
105                            list(examples = paste(ex, collapse = "\n"), value = value)
106               }),
107               # PhG: here is what I propose for examples code in the 'ex' attribute
108               examples.in.attr = list(forfun, function (name, o, ...) {
109                            ex <- attr(o, "ex")
110                            if (!is.null(ex)) {
111                                    # Special case for code contained in a function
112                                    if (inherits(ex, "function")) {
113                                            # If source is available, start from there
114                                            src <- attr(ex, "source")
115                                            if (!is.null(src)) {
116                                                    ex <- src
117                                            } else { # Use the body of the function
118                                                    ex <- deparse(body(ex))
119                                            }
120                                            # Eliminate leading and trailing code
121                                            ex <- ex[-c(1, length(ex))]
122                                            # Eliminate leading tabulations or four spaces
123                                            ex <- sub("^\t|    ", "", ex)
124                                            # Add an empty line before and after example
125                                            ex <- c("", ex, "")
126                                    }
127                                    list(examples = paste(ex, collapse = "\n"))
128                            } else list()
129               }))
130    
131    ### List of parser functions that operate on single objects. This list
132    ### is useful for testing these functions, ie
133    ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")
134    lonely <- sapply(forall.parsers,function(L)L[[2]])
135    
136  extract.docs.file <- function # Extract documentation from a file  extra.code.docs <- function # Extract documentation from code chunks
137  ### Parse R code to extract inline documentation from comments around  ### Parse R code to extract inline documentation from comments around
138  ### each function. These are not able to be retreived simply by  ### each function. These are not able to be retreived simply by
139  ### looking at the "source" attribute. This is a Parser Function that  ### looking at the "source" attribute. This is a Parser Function that
140  ### can be used in the parser list of package.skeleton.dx().  ### can be used in the parser list of package.skeleton.dx(). TODO:
141    ### Modularize this into separate Parsers Functions for S4 classes,
142    ### prefixes, ##<<blocks, etc. Right now it is not very clean!
143  (code,  (code,
144  ### Code lines in a character vector containing multiple R objects to  ### Code lines in a character vector containing multiple R objects to
145  ### parse for documentation.  ### parse for documentation.
# Line 91  Line 162 
162            doc$description <- parsed[[on]]@description            doc$description <- parsed[[on]]@description
163          }          }
164          if ( "setMethodS3" == parsed[[on]]@created ){          if ( "setMethodS3" == parsed[[on]]@created ){
165              # PhG: this may be wrong! It does not catch correctly how the method
166                      # must be splitted in case of methods containing dots. for instance,
167                      # as.data.frame.matrix must be split into: m1 = as.data.frame and
168                      # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!
169            pattern <- "^([^\\.]+)\\.(.*)$"            pattern <- "^([^\\.]+)\\.(.*)$"
170            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),
171                m2 <- gsub(pattern,"\\2",on,perl=TRUE))                m2 <- gsub(pattern,"\\2",on,perl=TRUE))
# Line 175  Line 250 
250  ### named list of lists, one for each object to document.  ### named list of lists, one for each object to document.
251  }  }
252    
253  ### Default parsers to use with package.skeleton.dx  ### List of parsers to use by default with package.skeleton.dx.
254  default.parsers <- list("parsefuns","extract.docs.file",  default.parsers <-
255                          "examples.after.return","examples.from.testfile",    c(extra.code.docs=extra.code.docs,
256                          "author.from.description","erase.format","title.from.name")      sapply(forall.parsers,function(L)L[[1]](L[[2]])),
257        edit.package.file=function(desc,...){
258          in.details <- setdiff(colnames(desc),"Description")
259          details <- paste(paste(in.details,": \\tab ",desc[,in.details],"\\cr",
260                                 sep=""),collapse="\n")
261          L <-
262            list(list(title=desc[,"Title"],
263                      description=desc[,"Description"],
264                      `tabular{ll}`=details,
265                      author=desc[,"Maintainer"]))
266          names(L) <- paste(desc[,"Package"],"-package",sep="")
267          L
268        })
269    
270  extract.docs.fun <- function # Extract documentation from a function  extract.docs.fun <- function # Extract documentation from a function
271  ### 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 274 
274  ### The function to examine.  ### The function to examine.
275   name.fun   name.fun
276  ### The name of the function/chunk to use in warning messages.  ### The name of the function/chunk to use in warning messages.
277   )   ){
 {  
278    res <- list()    res <- list()
279    clines <- grep(prefix,code)    clines <- grep(prefix,code)
280    if(length(grep("#",code[1]))){    if(length(grep("#",code[1]))){
# Line 574  Line 660 
660  ### Using the same conventions as for functions, definitions of S4 classes  ### Using the same conventions as for functions, definitions of S4 classes
661  ### in the form \code{setClass("classname",\dots)} are also located and  ### in the form \code{setClass("classname",\dots)} are also located and
662  ### scanned for inline comments.  ### scanned for inline comments.
663  (doc.link)  (doc.link
664  ### DocLink object as created by \code{\link{extract.file.parse}}.  ### DocLink object as created by \code{\link{extract.file.parse}}.
665  ### Note that \code{source} statements are \emph{ignored} when scanning for  ### Note that \code{source} statements are \emph{ignored} when scanning for
666  ### class definitions.  ### class definitions.
667  {   ){
668    chunk.source <- doc.link@code    chunk.source <- doc.link@code
669    ##details<<    ##details<<
670    ## Extraction of S4 class documentation is currently limited to expressions    ## Extraction of S4 class documentation is currently limited to expressions
# Line 619  Line 705 
705    }    }
706    invisible(docs)    invisible(docs)
707  }  }
708    
709    apply.parsers <- function
710    ### Parse code to r objs, then run all the parsers and return the
711    ### documentation list.
712    (code,
713    ### Character vector of code lines.
714     parsers=default.parsers,
715    ### List of Parser Functions.
716     verbose=FALSE,
717    ### Echo names of Parser Functions?
718     ...
719    ### Additional arguments to pass to Parser Functions.
720     ){
721      e <- new.env()
722      old <- options(keep.source.pkgs=TRUE)
723      on.exit(options(old))
724      ##tryCatch({
725        exprs <- parse(text=code)
726        for (i in exprs) eval(i, e)
727    ##   },error=function(err){
728    ##     print(i)
729    ##     stop("eval or parse failed with error:\n",err)
730    ##   })
731      objs <- sapply(ls(e),get,e,simplify=FALSE)
732    
733      docs <- list()
734    
735      # PhG: Automatically determine who is S3 methods
736      for (name in names(objs)) {
737        parts <- strsplit(name, ".", fixed = TRUE)[[1]]
738            l <- length(parts)
739            if (l > 1) {
740            for (i in 1:(l - 1)) {
741                # Look for a generic function (known by the system or defined
742                # in the package) that matches that part of the function name
743                generic <- paste(parts[1:i], collapse = ".")
744                if (any(generic %in% utils:::getKnownS3generics()) ||
745                    utils:::findGeneric(generic, e) != "") {
746                    object <- paste(parts[(i + 1):l], collapse = ".")
747                    docs[[name]]$s3method <- c(generic, object)
748                    break
749                }
750            }
751        }
752      }
753      ## apply parsers in sequence to code and objs
754      for(i in seq_along(parsers)){
755        N <- names(parsers[i])
756        if(verbose){
757          if(is.character(N) && N!=""){
758            cat(N," ",sep="")
759          }else cat('. ')
760        }
761        p <- parsers[[i]]
762        ## This is the argument list that each parser receives:
763        L <- p(code=code,objs=objs,docs=docs,...)
764        docs <- combine(docs,L)
765      }
766      if(verbose)cat("\n")
767      docs
768    ### A list of extracted documentation from code.
769    }
770    
771    extract.docs.file <- function
772    ### Apply all parsers relevant to extract info from just 1 code file.
773    (f,
774    ### File name of R code to read and parse.
775     parsers=list(extra.code.docs,default.parsers$parsefun),
776    ### Parser Functions to use to parse the code and extract
777    ### documentation.
778     ...
779    ### Other arguments to pass to Parser Functions.
780     ){
781      apply.parsers(readLines(f),parsers,verbose=FALSE,...)
782    }

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

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