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 115, Fri Jun 18 10:35:11 2010 UTC
# Line 1  Line 1 
1    ### combine lists or character strings
2    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  ### For each object in the package that satisfies the criterion
32  ### checked by subfun, parse source using FUN and return the resulting  ### checked by subfun, parse source using FUN and return the resulting
33  ### documentation list.  ### documentation list.
# Line 47  Line 77 
77           if("title"%in%names(doc))list() else           if("title"%in%names(doc))list() else
78           list(title=gsub("[._]"," ",name))           list(title=gsub("[._]"," ",name))
79         }),         }),
80         ## Get examples for FUN from the file test/FUN.R         # PhG: it is tests/FUN.R!!! I would like more flexibility here
81               # please, let me choose which dir to use for examples!
82               ## Get examples for FUN from the file tests/FUN.R
83         examples.from.testfile=list(forfun,function(name,...){         examples.from.testfile=list(forfun,function(name,...){
84           tfile <- file.path("..","tests",paste(name,".R",sep=""))           tsubdir <- getOption("inlinedocs.exdir")
85                     if (is.null(tsubdir)) tsubdir <- "tests"       # Default value
86                     tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))
87           if(file.exists(tfile))           if(file.exists(tfile))
88             list(examples=paste(readLines(tfile),collapse="\n"))             list(examples=paste(readLines(tfile),collapse="\n"))
89           else list()           else list()
90         }),         }),
91         ## Get examples from inline definitions after return()         ## Get examples from inline definitions after return()
92               # PhG: this does not work well! Think at these situations:
93               # 1) You have multiple return() in the code of your function,
94               # 2) You have return() appearing is some example code, ...
95               # I can hardly propose a hack here. The whole code of the function
96               # must be parsed, and one must determine which one is the last line
97               # of code that is actually executed.
98               #
99               # I make two propositions here
100               # 1) to keep the same mechanism that has the advantage of simplicity
101               #    but to use a special tag ##examples<< or #{{{examples to separate
102               #    function code from examples explicitly, and
103               # 2) to place the example in an "ex" attribute attached to the function
104               #    (see next parser). That solution will be also interesting for
105               #    documenting datasets, something not done yet by inlinedocs!
106         examples.after.return=list(forfun,function(name,src,...){         examples.after.return=list(forfun,function(name,src,...){
107           coll <- paste(src,collapse="\n")                          # Look for the examples mark
108           thispre <- gsub("^[\\^]","",prefix)                          m <- grep("##examples<<|#\\{\\{\\{examples", src)
109           FIND <- paste("(return|UseMethod)[(][^\\n]*\\n",thispre,sep="")                          if (!length(m)) return(list())
110           m <- regexpr(FIND,coll)                          if (length(m) > 1)
111           if(m[1]==-1)return(list())                                  warning("More than one examples tag for ", name, ". Taking the last one")
112           after <- substr(coll,m[1],nchar(coll))                          m <- m[length(m)]
113           FIND <-                          # Look for the lines containing return value comments just before
114             paste("[^\\n]*",# rest of the return line                          r <- grep("\\s*### ", src[1:(m-1)])
115                   "((?:\\n###[^\\n]*)+)",#comment value lines \\1                          if (!length(r)) {
116                   "([\\w\\W]*)[}]",#examples \\2                                  value <- NULL
117                   sep="")                          } else {
118           SEP <- "-/-/-/-/-=====-----"                                  # Only take consecutive lines before the mark
119           REP <- paste("\\1",SEP,"\\2",sep="")                                  keep <- rev((m - rev(r)) == 1:length(r))
120           r <- strsplit(gsub(FIND,REP,after,perl=TRUE),split=SEP)[[1]]                                  if (!any(keep)) {
121           l <- strsplit(r,split="\n")                                          value <- NULL
122           excode <- c(l[[2]],"")                                  } else {
123           if(length(l)<2||paste(excode,collapse="")=="")return(list())                                          value <- decomment(src[r[keep]])
124           prefixes <- gsub("(\\s*).*","\\1",excode,perl=TRUE)[grep("\\w",excode)]                                  }
125           FIND <- prefixes[which.min(nchar(prefixes))]                          }
126           list(examples=paste(sub(FIND,"",excode),collapse="\n"),                          # Collect now the example code beneath the mark
127                value=decomment(l[[1]][-1]))                          ex <- src[(m + 1):(length(src) - 1)]
128                            # Possibly eliminate a #}}} tag
129                            ex <- ex[!grepl("#}}}", ex)]
130                            # Eliminate leading tabulations or four spaces
131                            ex <- sub("^\t|    ", "", ex)
132                            # Add an empty line before and after example
133                            ex <- c("", ex, "")
134                            # Return examples and value
135                            list(examples = paste(ex, collapse = "\n"), value = value)
136               }),
137               # PhG: here is what I propose for examples code in the 'ex' attribute
138               examples.in.attr = list(forfun, function (name, o, ...) {
139                            ex <- attr(o, "ex")
140                            if (!is.null(ex)) {
141                                    # Special case for code contained in a function
142                                    if (inherits(ex, "function")) {
143                                            # If source is available, start from there
144                                            src <- attr(ex, "source")
145                                            if (!is.null(src)) {
146                                                    ex <- src
147                                            } else { # Use the body of the function
148                                                    ex <- deparse(body(ex))
149                                            }
150                                            # Eliminate leading and trailing code
151                                            ex <- ex[-c(1, length(ex))]
152                                            # Eliminate leading tabulations or four spaces
153                                            ex <- sub("^\t|    ", "", ex)
154                                            # Add an empty line before and after example
155                                            ex <- c("", ex, "")
156                                    }
157                                    list(examples = paste(ex, collapse = "\n"))
158                            } else list()
159         }))         }))
160    
161  ### List of parser functions that operate on single objects. This list  ### List of parser functions that operate on single objects. This list
# Line 84  Line 163 
163  ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")  ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")
164  lonely <- sapply(forall.parsers,function(L)L[[2]])  lonely <- sapply(forall.parsers,function(L)L[[2]])
165    
166  extract.docs.file <- function # Extract documentation from a file  extra.code.docs <- function # Extract documentation from code chunks
167  ### Parse R code to extract inline documentation from comments around  ### Parse R code to extract inline documentation from comments around
168  ### each function. These are not able to be retreived simply by  ### each function. These are not able to be retreived simply by
169  ### looking at the "source" attribute. This is a Parser Function that  ### looking at the "source" attribute. This is a Parser Function that
170  ### can be used in the parser list of package.skeleton.dx().  ### can be used in the parser list of package.skeleton.dx(). TODO:
171    ### Modularize this into separate Parsers Functions for S4 classes,
172    ### prefixes, ##<<blocks, etc. Right now it is not very clean!
173  (code,  (code,
174  ### Code lines in a character vector containing multiple R objects to  ### Code lines in a character vector containing multiple R objects to
175  ### parse for documentation.  ### parse for documentation.
# Line 111  Line 192 
192            doc$description <- parsed[[on]]@description            doc$description <- parsed[[on]]@description
193          }          }
194          if ( "setMethodS3" == parsed[[on]]@created ){          if ( "setMethodS3" == parsed[[on]]@created ){
195              # PhG: this may be wrong! It does not catch correctly how the method
196                      # must be splitted in case of methods containing dots. for instance,
197                      # as.data.frame.matrix must be split into: m1 = as.data.frame and
198                      # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!
199            pattern <- "^([^\\.]+)\\.(.*)$"            pattern <- "^([^\\.]+)\\.(.*)$"
200            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),
201                m2 <- gsub(pattern,"\\2",on,perl=TRUE))                m2 <- gsub(pattern,"\\2",on,perl=TRUE))
# Line 196  Line 281 
281  }  }
282    
283  ### List of parsers to use by default with package.skeleton.dx.  ### List of parsers to use by default with package.skeleton.dx.
284  default.parsers <- c(extract.docs.file=extract.docs.file,  default.parsers <-
285                       sapply(forall.parsers,function(L)L[[1]](L[[2]])))    c(extra.code.docs=extra.code.docs,
286        sapply(forall.parsers,function(L)L[[1]](L[[2]])),
287        edit.package.file=function(desc,...){
288          in.details <- setdiff(colnames(desc),"Description")
289          details <- paste(paste(in.details,": \\tab ",desc[,in.details],"\\cr",
290                                 sep=""),collapse="\n")
291          L <-
292            list(list(title=desc[,"Title"],
293                      description=desc[,"Description"],
294                      `tabular{ll}`=details,
295                      author=desc[,"Maintainer"]))
296          names(L) <- paste(desc[,"Package"],"-package",sep="")
297          L
298        })
299    
300  extract.docs.fun <- function # Extract documentation from a function  extract.docs.fun <- function # Extract documentation from a function
301  ### Given source code of a function, return a list describing inline  ### Given source code of a function, return a list describing inline
# Line 425  Line 523 
523        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)
524        in.chunk <- TRUE        in.chunk <- TRUE
525        if ( not.describe ){        if ( not.describe ){
526            ## TDH 2010-06-18 For item{}s in the documentation list names,
527            ## we don't need to have a backslash before, so delete it.
528            arg <- gsub("^[\\]+","",arg)
529          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots
530          payload <- comment          payload <- comment
531        } else {        } else {
# Line 638  Line 739 
739    invisible(docs)    invisible(docs)
740  }  }
741    
742  extract.docs.code <- function  apply.parsers <- function
743  ### 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
744  ### parsers and return the documentation list.  ### documentation list.
745  (code,  (code,
746  ### Character vector of code lines.  ### Character vector of code lines.
747   parsers,   parsers=default.parsers,
748  ### List of Parser Functions.  ### List of Parser Functions.
749   verbose=TRUE,   verbose=FALSE,
750  ### Echo names of Parser Functions?  ### Echo names of Parser Functions?
751   ...   ...
752  ### Additional arguments to pass to Parser Functions.  ### Additional arguments to pass to Parser Functions.
753   ){   ){
   code.file <- tempfile()  
   writeLines(code,code.file)  
754    e <- new.env()    e <- new.env()
755    old <- options(keep.source.pkgs=TRUE)    old <- options(keep.source=TRUE)
756    tryCatch(suppressWarnings(sys.source(code.file,e)),error=function(e){    on.exit(options(old))
757      stop("source ",code.file," failed with error:\n",e)    exprs <- parse(text=code)
758    })    for (i in exprs) eval(i, e)
   options(old)  
759    objs <- sapply(ls(e),get,e,simplify=FALSE)    objs <- sapply(ls(e),get,e,simplify=FALSE)
760    
   ## apply parsers in sequence to code and objs  
761    docs <- list()    docs <- list()
762    
763      # PhG: Automatically determine who is S3 methods
764      for (name in names(objs)) {
765        parts <- strsplit(name, ".", fixed = TRUE)[[1]]
766            l <- length(parts)
767            if (l > 1) {
768            for (i in 1:(l - 1)) {
769                # Look for a generic function (known by the system or defined
770                # in the package) that matches that part of the function name
771                generic <- paste(parts[1:i], collapse = ".")
772                if (any(generic %in% utils:::getKnownS3generics()) ||
773                    utils:::findGeneric(generic, e) != "") {
774                    object <- paste(parts[(i + 1):l], collapse = ".")
775                    docs[[name]]$s3method <- c(generic, object)
776                    break
777                }
778            }
779        }
780      }
781      ## apply parsers in sequence to code and objs
782    for(i in seq_along(parsers)){    for(i in seq_along(parsers)){
783      N <- names(parsers[i])      N <- names(parsers[i])
784      if(verbose){      if(verbose){
# Line 678  Line 795 
795    docs    docs
796  ### A list of extracted documentation from code.  ### A list of extracted documentation from code.
797  }  }
798    
799    extract.docs.file <- function
800    ### Apply all parsers relevant to extract info from just 1 code file.
801    (f,
802    ### File name of R code to read and parse.
803     parsers=list(extra.code.docs,default.parsers$parsefun),
804    ### Parser Functions to use to parse the code and extract
805    ### documentation.
806     ...
807    ### Other arguments to pass to Parser Functions.
808     ){
809      apply.parsers(readLines(f),parsers,verbose=FALSE,...)
810    }

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

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