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 109, Mon Jun 7 08:42:15 2010 UTC
# Line 47  Line 47 
47           if("title"%in%names(doc))list() else           if("title"%in%names(doc))list() else
48           list(title=gsub("[._]"," ",name))           list(title=gsub("[._]"," ",name))
49         }),         }),
50         ## Get examples for FUN from the file test/FUN.R         # PhG: it is tests/FUN.R!!! I would like more flexibility here
51               # please, let me choose which dir to use for examples!
52               ## Get examples for FUN from the file tests/FUN.R
53         examples.from.testfile=list(forfun,function(name,...){         examples.from.testfile=list(forfun,function(name,...){
54           tfile <- file.path("..","tests",paste(name,".R",sep=""))           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()         ## Get examples from inline definitions after return()
62               # PhG: this does not work well! Think at these situations:
63               # 1) You have multiple return() in the code of your function,
64               # 2) You have return() appearing is some example code, ...
65               # I can hardly propose a hack here. The whole code of the function
66               # must be parsed, and one must determine which one is the last line
67               # of code that is actually executed.
68               #
69               # I make two propositions here
70               # 1) to keep the same mechanism that has the advantage of simplicity
71               #    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,...){         examples.after.return=list(forfun,function(name,src,...){
77           coll <- paste(src,collapse="\n")                          # Look for the examples mark
78           thispre <- gsub("^[\\^]","",prefix)                          m <- grep("##examples<<|#\\{\\{\\{examples", src)
79           FIND <- paste("(return|UseMethod)[(][^\\n]*\\n",thispre,sep="")                          if (!length(m)) return(list())
80           m <- regexpr(FIND,coll)                          if (length(m) > 1)
81           if(m[1]==-1)return(list())                                  warning("More than one examples tag for ", name, ". Taking the last one")
82           after <- substr(coll,m[1],nchar(coll))                          m <- m[length(m)]
83           FIND <-                          # Look for the lines containing return value comments just before
84             paste("[^\\n]*",# rest of the return line                          r <- grep("\\s*### ", src[1:(m-1)])
85                   "((?:\\n###[^\\n]*)+)",#comment value lines \\1                          if (!length(r)) {
86                   "([\\w\\W]*)[}]",#examples \\2                                  value <- NULL
87                   sep="")                          } else {
88           SEP <- "-/-/-/-/-=====-----"                                  # Only take consecutive lines before the mark
89           REP <- paste("\\1",SEP,"\\2",sep="")                                  keep <- rev((m - rev(r)) == 1:length(r))
90           r <- strsplit(gsub(FIND,REP,after,perl=TRUE),split=SEP)[[1]]                                  if (!any(keep)) {
91           l <- strsplit(r,split="\n")                                          value <- NULL
92           excode <- c(l[[2]],"")                                  } else {
93           if(length(l)<2||paste(excode,collapse="")=="")return(list())                                          value <- decomment(src[r[keep]])
94           prefixes <- gsub("(\\s*).*","\\1",excode,perl=TRUE)[grep("\\w",excode)]                                  }
95           FIND <- prefixes[which.min(nchar(prefixes))]                          }
96           list(examples=paste(sub(FIND,"",excode),collapse="\n"),                          # Collect now the example code beneath the mark
97                value=decomment(l[[1]][-1]))                          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  ### List of parser functions that operate on single objects. This list
# Line 84  Line 133 
133  ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")  ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")
134  lonely <- sapply(forall.parsers,function(L)L[[2]])  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 111  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 196  Line 251 
251  }  }
252    
253  ### List of parsers to use by default with package.skeleton.dx.  ### List of parsers to use by default with package.skeleton.dx.
254  default.parsers <- c(extract.docs.file=extract.docs.file,  default.parsers <-
255                       sapply(forall.parsers,function(L)L[[1]](L[[2]])))    c(extra.code.docs=extra.code.docs,
256        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 637  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.104  
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 University of Wisconsin - Madison Powered By FusionForge