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 108, Wed May 26 12:00:17 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  extract.docs.file <- function # Extract documentation from a file
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 <- c(extract.docs.file=extract.docs.file,
255                          "examples.after.return","examples.from.testfile",                       sapply(forall.parsers,function(L)L[[1]](L[[2]])))
                         "author.from.description","erase.format","title.from.name")  
256    
257  extract.docs.fun <- function # Extract documentation from a function  extract.docs.fun <- function # Extract documentation from a function
258  ### 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 261 
261  ### The function to examine.  ### The function to examine.
262   name.fun   name.fun
263  ### The name of the function/chunk to use in warning messages.  ### The name of the function/chunk to use in warning messages.
264   )   ){
 {  
265    res <- list()    res <- list()
266    clines <- grep(prefix,code)    clines <- grep(prefix,code)
267    if(length(grep("#",code[1]))){    if(length(grep("#",code[1]))){
# Line 574  Line 647 
647  ### Using the same conventions as for functions, definitions of S4 classes  ### Using the same conventions as for functions, definitions of S4 classes
648  ### in the form \code{setClass("classname",\dots)} are also located and  ### in the form \code{setClass("classname",\dots)} are also located and
649  ### scanned for inline comments.  ### scanned for inline comments.
650  (doc.link)  (doc.link
651  ### DocLink object as created by \code{\link{extract.file.parse}}.  ### DocLink object as created by \code{\link{extract.file.parse}}.
652  ### Note that \code{source} statements are \emph{ignored} when scanning for  ### Note that \code{source} statements are \emph{ignored} when scanning for
653  ### class definitions.  ### class definitions.
654  {   ){
655    chunk.source <- doc.link@code    chunk.source <- doc.link@code
656    ##details<<    ##details<<
657    ## Extraction of S4 class documentation is currently limited to expressions    ## Extraction of S4 class documentation is currently limited to expressions
# Line 619  Line 692 
692    }    }
693    invisible(docs)    invisible(docs)
694  }  }
695    
696    extract.docs.code <- function
697    ### Write code to a file and parse it to r objs, then run all the
698    ### parsers and return the documentation list.
699    (code,
700    ### Character vector of code lines.
701     parsers,
702    ### List of Parser Functions.
703     verbose=TRUE,
704    ### Echo names of Parser Functions?
705     ...
706    ### Additional arguments to pass to Parser Functions.
707     ){
708      code.file <- tempfile()
709      writeLines(code,code.file)
710      e <- new.env()
711      old <- options(keep.source.pkgs=TRUE)
712      tryCatch(suppressWarnings(sys.source(code.file,e)),error=function(e){
713        stop("source ",code.file," failed with error:\n",e)
714      })
715      options(old)
716      objs <- sapply(ls(e),get,e,simplify=FALSE)
717    
718      ## apply parsers in sequence to code and objs
719      docs <- list()
720    
721      # PhG: Automatically determine who is S3 methods
722      for (name in names(objs)) {
723        parts <- strsplit(name, ".", fixed = TRUE)[[1]]
724            l <- length(parts)
725            if (l > 1) {
726            for (i in 1:(l - 1)) {
727                # Look for a generic function (known by the system or defined
728                # in the package) that matches that part of the function name
729                generic <- paste(parts[1:i], collapse = ".")
730                if (any(generic %in% utils:::getKnownS3generics()) ||
731                    utils:::findGeneric(generic, e) != "") {
732                    object <- paste(parts[(i + 1):l], collapse = ".")
733                    docs[[name]]$s3method <- c(generic, object)
734                    break
735                }
736            }
737        }
738      }
739      for(i in seq_along(parsers)){
740        N <- names(parsers[i])
741        if(verbose){
742          if(is.character(N) && N!=""){
743            cat(N," ",sep="")
744          }else cat('. ')
745        }
746        p <- parsers[[i]]
747        ## This is the argument list that each parser receives:
748        L <- p(code=code,objs=objs,docs=docs,...)
749        docs <- combine(docs,L)
750      }
751      if(verbose)cat("\n")
752      docs
753    ### A list of extracted documentation from code.
754    }

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

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