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 104, Tue May 18 16:35:08 2010 UTC revision 128, Wed Sep 22 09:24:39 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         examples.after.return=list(forfun,function(name,src,...){         ## PhG: this does not work well! Think of these situations:
93           coll <- paste(src,collapse="\n")         ## 1) You have multiple return() in the code of your function,
94           thispre <- gsub("^[\\^]","",prefix)         ## 2) You have return() appearing is some example code, ...
95           FIND <- paste("(return|UseMethod)[(][^\\n]*\\n",thispre,sep="")         ## I can hardly propose a hack here. The whole code of the function
96           m <- regexpr(FIND,coll)         ## must be parsed, and one must determine which one is the last line
97           if(m[1]==-1)return(list())         ## of code that is actually executed.
98           after <- substr(coll,m[1],nchar(coll))         ##
99           FIND <-         ## I make two propositions here
100             paste("[^\\n]*",# rest of the return line         ## 1) to keep the same mechanism that has the advantage of simplicity
101                   "((?:\\n###[^\\n]*)+)",#comment value lines \\1         ##    but to use a special tag
102                   "([\\w\\W]*)[}]",#examples \\2         ##examples<< or #{{{examples to separate
103                   sep="")         ##    function code from examples explicitly, and
104           SEP <- "-/-/-/-/-=====-----"         ## 2) to place the example in an "ex" attribute
105           REP <- paste("\\1",SEP,"\\2",sep="")         ##    attached to the function
106           r <- strsplit(gsub(FIND,REP,after,perl=TRUE),split=SEP)[[1]]         ##    (see next parser). That solution will be also interesting for
107           l <- strsplit(r,split="\n")         ##    documenting datasets, something not done yet by inlinedocs!
108           excode <- c(l[[2]],"")         examples.after.return = list(forfun,function(src,name="",...) {
109           if(length(l)<2||paste(excode,collapse="")=="")return(list())           ## Look for the examples mark
110           prefixes <- gsub("(\\s*).*","\\1",excode,perl=TRUE)[grep("\\w",excode)]           m <- grep("##examples<<|#\\{\\{\\{examples", src)
111             if (!length(m)) return(list())
112             if (length(m) > 1)
113               warning("More than one examples tag for ", name,
114                       ". Taking the last one")
115             m <- m[length(m)]
116             ## Look for the lines containing return value comments just before
117             r <- grep("\\s*### ", src[1:(m-1)])
118               if (!length(r)) {
119                 value <- NULL
120               } else {
121                 ## Only take consecutive lines before the mark
122                 keep <- rev((m - rev(r)) == 1:length(r))
123                 if (!any(keep)) {
124                   value <- NULL
125                 } else {
126                   value <- decomment(src[r[keep]])
127                 }
128               }
129             ## Collect now the example code beneath the mark
130             ex <- src[(m + 1):(length(src) - 1)]
131             ## Possibly eliminate a #}}} tag
132             ex <- ex[!grepl("#}}}", ex)]
133             ## Eliminate leading tabulations or four spaces
134             prefixes <- gsub("(\\s*).*","\\1",ex,perl=TRUE)[grep("\\w",ex)]
135           FIND <- prefixes[which.min(nchar(prefixes))]           FIND <- prefixes[which.min(nchar(prefixes))]
136           list(examples=paste(sub(FIND,"",excode),collapse="\n"),           ex <- sub(FIND,"",ex)
137                value=decomment(l[[1]][-1]))           ## Add an empty line before and after example
138         }))           ex <- c("", ex, "")
139             ## Return examples and value
140             list(examples = paste(ex, collapse = "\n"), value = value)
141           }),
142           ## PhG: here is what I propose for examples code in the 'ex' attribute
143           examples.in.attr = list(forall, function (name, o, ...) {
144             ex <- attr(o, "ex")
145             if (!is.null(ex)) {
146               ## Special case for code contained in a function
147               if (inherits(ex, "function")) {
148                 ## If source is available, start from there
149                 src <- attr(ex, "source")
150                 if (!is.null(src)) {
151                   ex <- src
152                 } else { ## Use the body of the function
153                   ex <- deparse(body(ex))
154                 }
155                 ## Eliminate leading and trailing code
156                 ex <- ex[-c(1, length(ex))]
157                 ## Eliminate leading tabulations or four spaces
158                 ex <- sub("^\t|    ", "", ex)
159                 ## Add an empty line before and after example
160                 ex <- c("", ex, "")
161               }
162               list(examples = paste(ex, collapse = "\n"))
163             } else list()
164           })
165           )
166    
167  ### List of parser functions that operate on single objects. This list  ### List of parser functions that operate on single objects. This list
168  ### is useful for testing these functions, ie  ### is useful for testing these functions.
 ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")  
169  lonely <- sapply(forall.parsers,function(L)L[[2]])  lonely <- sapply(forall.parsers,function(L)L[[2]])
170    attr(lonely,"ex") <- function(){
171      lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")
172    }
173    
174  extract.docs.file <- function # Extract documentation from a file  extra.code.docs <- function # Extract documentation from code chunks
175  ### Parse R code to extract inline documentation from comments around  ### Parse R code to extract inline documentation from comments around
176  ### each function. These are not able to be retreived simply by  ### each function. These are not able to be retreived simply by
177  ### looking at the "source" attribute. This is a Parser Function that  ### looking at the "source" attribute. This is a Parser Function that
178  ### can be used in the parser list of package.skeleton.dx().  ### can be used in the parser list of package.skeleton.dx(). TODO:
179    ### Modularize this into separate Parsers Functions for S4 classes,
180    ### prefixes, ##<<blocks, etc. Right now it is not very clean!
181  (code,  (code,
182  ### Code lines in a character vector containing multiple R objects to  ### Code lines in a character vector containing multiple R objects to
183  ### parse for documentation.  ### parse for documentation.
# Line 111  Line 200 
200            doc$description <- parsed[[on]]@description            doc$description <- parsed[[on]]@description
201          }          }
202          if ( "setMethodS3" == parsed[[on]]@created ){          if ( "setMethodS3" == parsed[[on]]@created ){
203              # PhG: this may be wrong! It does not catch correctly how the method
204                      # must be splitted in case of methods containing dots. for instance,
205                      # as.data.frame.matrix must be split into: m1 = as.data.frame and
206                      # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!
207            pattern <- "^([^\\.]+)\\.(.*)$"            pattern <- "^([^\\.]+)\\.(.*)$"
208            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),
209                m2 <- gsub(pattern,"\\2",on,perl=TRUE))                m2 <- gsub(pattern,"\\2",on,perl=TRUE))
# Line 196  Line 289 
289  }  }
290    
291  ### List of parsers to use by default with package.skeleton.dx.  ### List of parsers to use by default with package.skeleton.dx.
292  default.parsers <- c(extract.docs.file=extract.docs.file,  default.parsers <-
293                       sapply(forall.parsers,function(L)L[[1]](L[[2]])))    c(extra.code.docs=extra.code.docs,
294        sapply(forall.parsers,function(L)L[[1]](L[[2]])),
295        edit.package.file=function(desc,...){
296          in.details <- setdiff(colnames(desc),"Description")
297          details <- paste(paste(in.details,": \\tab ",desc[,in.details],"\\cr",
298                                 sep=""),collapse="\n")
299          L <-
300            list(list(title=desc[,"Title"],
301                      description=desc[,"Description"],
302                      `tabular{ll}`=details,
303                      author=desc[,"Maintainer"]))
304          names(L) <- paste(desc[,"Package"],"-package",sep="")
305          L
306        })
307    
308  extract.docs.fun <- function # Extract documentation from a function  extract.docs.fun <- function # Extract documentation from a function
309  ### 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 531 
531        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)
532        in.chunk <- TRUE        in.chunk <- TRUE
533        if ( not.describe ){        if ( not.describe ){
534            ## TDH 2010-06-18 For item{}s in the documentation list names,
535            ## we don't need to have a backslash before, so delete it.
536            arg <- gsub("^[\\]+","",arg)
537          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots
538          payload <- comment          payload <- comment
539        } else {        } else {
# Line 637  Line 746 
746    }    }
747    invisible(docs)    invisible(docs)
748  }  }
749    
750    apply.parsers <- function
751    ### Parse code to r objs, then run all the parsers and return the
752    ### documentation list.
753    (code,
754    ### Character vector of code lines.
755     parsers=default.parsers,
756    ### List of Parser Functions.
757     verbose=FALSE,
758    ### Echo names of Parser Functions?
759     ...
760    ### Additional arguments to pass to Parser Functions.
761     ){
762      e <- new.env()
763      old <- options(keep.source=TRUE)
764      on.exit(options(old))
765      exprs <- parse(text=code)
766      for (i in exprs) eval(i, e)
767      objs <- sapply(ls(e),get,e,simplify=FALSE)
768    
769      docs <- list()
770    
771      # PhG: Automatically determine who is S3 methods
772      for (name in names(objs)) {
773        parts <- strsplit(name, ".", fixed = TRUE)[[1]]
774            l <- length(parts)
775            if (l > 1) {
776            for (i in 1:(l - 1)) {
777                # Look for a generic function (known by the system or defined
778                # in the package) that matches that part of the function name
779                generic <- paste(parts[1:i], collapse = ".")
780                if (any(generic %in% utils:::getKnownS3generics()) ||
781                    utils:::findGeneric(generic, e) != "") {
782                    object <- paste(parts[(i + 1):l], collapse = ".")
783                    docs[[name]]$s3method <- c(generic, object)
784                    break
785                }
786            }
787        }
788      }
789      ## apply parsers in sequence to code and objs
790      for(i in seq_along(parsers)){
791        N <- names(parsers[i])
792        if(verbose){
793          if(is.character(N) && N!=""){
794            cat(N," ",sep="")
795          }else cat('. ')
796        }
797        p <- parsers[[i]]
798        ## This is the argument list that each parser receives:
799        L <- p(code=code,objs=objs,docs=docs,...)
800        docs <- combine(docs,L)
801      }
802      if(verbose)cat("\n")
803      docs
804    ### A list of extracted documentation from code.
805    }
806    
807    ### Parsers that operate only on R code, independently of the
808    ### description file.
809    nondesc.parsers <- c(extra.code.docs=list(extra.code.docs),
810      default.parsers[c("parsefun","examples.after.return","examples.in.attr")])
811    
812    extract.docs.file <- function
813    ### Apply all parsers relevant to extract info from just 1 code file.
814    (f,
815    ### File name of R code to read and parse.
816     parsers=nondesc.parsers,
817    ### Parser Functions to use to parse the code and extract
818    ### documentation.
819     ...
820    ### Other arguments to pass to Parser Functions.
821     ){
822      apply.parsers(readLines(f),parsers,verbose=FALSE,...)
823    }

Legend:
Removed from v.104  
changed lines
  Added in v.128

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