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 108, Wed May 26 12:00:17 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 88  Line 137 
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 637  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.104  
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 Powered By FusionForge