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 109, Mon Jun 7 08:42:15 2010 UTC revision 142, Wed Oct 20 09:21:53 2010 UTC
# Line 1  Line 1 
1  ### For each object in the package that satisfies the criterion  ### combine lists or character strings
2  ### checked by subfun, parse source using FUN and return the resulting  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#
32    ## checked by subfun, parse source using FUN and return the resulting
33  ### documentation list.  ### documentation list.
34  forall <- function  forall <- function
35  (FUN,  (FUN,
# Line 9  Line 39 
39  ### is.function. subfun(x)==TRUE means FUN will be applied to x and  ### is.function. subfun(x)==TRUE means FUN will be applied to x and
40  ### the result will be returned.  ### the result will be returned.
41   ){   ){
42    function(objs,docs,...){    FUN <- FUN
43      f <- function(objs,docs,...){
44      objs <- objs[sapply(objs,subfun)]      objs <- objs[sapply(objs,subfun)]
45      L <- list()      L <- list()
46      for(N in names(docs)){      for(N in names(docs)){
# Line 19  Line 50 
50      }      }
51      L      L
52    }    }
53      class(f) <- c("allfun","function")
54      f
55  ### A Parser Function.  ### A Parser Function.
56  }  }
57    
58    ### Print method for functions constructed using forall.
59    print.allfun <- function(x,...){
60      e <- environment(x)
61      cat("Function to apply to every element.\nselector:")
62      print(e$subfun)
63      cat("processor:")
64      print(e$FUN)
65    }
66    
67  ### For each function in the package, do something.  ### For each function in the package, do something.
68  forfun <- function(FUN)forall(FUN,is.function)  forfun <- function(FUN)forall(FUN,is.function)
69    
70  ### Parsers for each object/function that are constructed  examples.after.return <- function
71  ### automatically. This is a named list, each element is a list of 2  ### Get examples from inline definitions after return()
72  ### elements: forfun/forall, then a parser function for an individual  ### PhG: this does not work well! Think of these situations:
73  ### object.  ### 1) You have multiple return() in the code of your function,
74  forall.parsers <-  ### 2) You have return() appearing is some example code, ...
75    list(## Extract lots of info from normal functions.  ### I can hardly propose a hack here. The whole code of the function
76         parsefun=list(forfun,function(src,name,...){  ### must be parsed, and one must determine which one is the last line
77           extract.docs.fun(src,name)  ### of code that is actually executed.
78         }),  ###
79         ## Fill in author from DESCRIPTION and titles.  ### I make two propositions here
80         author.from.description=list(forall,function(desc,...){  ### 1) to keep the same mechanism that has the advantage of simplicity
81           list(author=desc[,"Maintainer"])  ###    but to use a special tag
82         }),  ###    ##examples<< or #{{{examples to separate
83         ## The format section sometimes causes problems, so erase it.  ###    function code from examples explicitly, and
84         erase.format=list(forall,function(...){  ### 2) to place the example in an "ex" attribute
85           list(format="")  ###    attached to the function
86         }),  ###    (see next parser). That solution will be also interesting for
87         ## Convert the function name to a title.  ###    documenting datasets, something not done yet by inlinedocs!
88         title.from.name=list(forall,function(name,doc,...){  (src,name="",...) {
89           if("title"%in%names(doc))list() else    ## Look for the examples mark
90           list(title=gsub("[._]"," ",name))    m <- grep("^\\s*(##examples<<|#\\{\\{\\{examples)", src)
        }),  
        # PhG: it is tests/FUN.R!!! I would like more flexibility here  
            # please, let me choose which dir to use for examples!  
            ## Get examples for FUN from the file tests/FUN.R  
        examples.from.testfile=list(forfun,function(name,...){  
          tsubdir <- getOption("inlinedocs.exdir")  
                  if (is.null(tsubdir)) tsubdir <- "tests"       # Default value  
                  tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))  
          if(file.exists(tfile))  
            list(examples=paste(readLines(tfile),collapse="\n"))  
          else list()  
        }),  
        ## Get examples from inline definitions after return()  
            # PhG: this does not work well! Think at these situations:  
            # 1) You have multiple return() in the code of your function,  
            # 2) You have return() appearing is some example code, ...  
            # I can hardly propose a hack here. The whole code of the function  
            # must be parsed, and one must determine which one is the last line  
            # of code that is actually executed.  
            #  
            # I make two propositions here  
            # 1) to keep the same mechanism that has the advantage of simplicity  
            #    but to use a special tag ##examples<< or #{{{examples to separate  
            #    function code from examples explicitly, and  
            # 2) to place the example in an "ex" attribute attached to the function  
            #    (see next parser). That solution will be also interesting for  
            #    documenting datasets, something not done yet by inlinedocs!  
            examples.after.return = list(forfun, function(name, src, ...) {  
                         # Look for the examples mark  
                         m <- grep("##examples<<|#\\{\\{\\{examples", src)  
91                          if (!length(m)) return(list())                          if (!length(m)) return(list())
92                          if (length(m) > 1)                          if (length(m) > 1)
93                                  warning("More than one examples tag for ", name, ". Taking the last one")      warning("More than one examples tag for ", name,
94                ". Taking the last one")
95                          m <- m[length(m)]                          m <- m[length(m)]
96                          # Look for the lines containing return value comments just before    ## Look for the lines containing return value comments just before
97                          r <- grep("\\s*### ", src[1:(m-1)])                          r <- grep("\\s*### ", src[1:(m-1)])
98                          if (!length(r)) {                          if (!length(r)) {
99                                  value <- NULL                                  value <- NULL
100                          } else {                          } else {
101                                  # Only take consecutive lines before the mark        ## Only take consecutive lines before the mark
102                                  keep <- rev((m - rev(r)) == 1:length(r))                                  keep <- rev((m - rev(r)) == 1:length(r))
103                                  if (!any(keep)) {                                  if (!any(keep)) {
104                                          value <- NULL                                          value <- NULL
# Line 93  Line 106 
106                                          value <- decomment(src[r[keep]])                                          value <- decomment(src[r[keep]])
107                                  }                                  }
108                          }                          }
109                          # Collect now the example code beneath the mark    ## Collect now the example code beneath the mark
110                          ex <- src[(m + 1):(length(src) - 1)]                          ex <- src[(m + 1):(length(src) - 1)]
111                          # Possibly eliminate a #}}} tag    ## Possibly eliminate a #}}} tag
112                          ex <- ex[!grepl("#}}}", ex)]                          ex <- ex[!grepl("#}}}", ex)]
113                          # Eliminate leading tabulations or four spaces    ## Eliminate leading tabulations or four spaces
114                          ex <- sub("^\t|    ", "", ex)    prefixes <- gsub("(\\s*).*","\\1",ex,perl=TRUE)[grep("\\w",ex)]
115                          # Add an empty line before and after example    FIND <- prefixes[which.min(nchar(prefixes))]
116      ex <- sub(FIND,"",ex)
117      ## Add an empty line before and after example
118                          ex <- c("", ex, "")                          ex <- c("", ex, "")
119                          # Return examples and value    ## Return examples and value
120                          list(examples = paste(ex, collapse = "\n"), value = value)                          list(examples = paste(ex, collapse = "\n"), value = value)
121             }),  }
122             # PhG: here is what I propose for examples code in the 'ex' attribute  
123             examples.in.attr = list(forfun, function (name, o, ...) {  prefixed.lines <- function(src,...){
124    ### The primary mechanism of inline documentation is via consecutive
125    ### groups of lines matching the specified prefix regular expression
126    ### "\code{^### }" (i.e. lines beginning with "\code{### }") are
127    ### collected as follows into documentation sections:\describe{
128    ### \item{description}{group starting at line 2 in the code}
129    ### \item{arguments}{group following each function argument}
130    ### \item{value}{group ending at the penultimate line of the code}}
131    ### These may be added to by use of the \code{##<<} constructs
132    ### described below.
133      clines <- grep(prefix,src)
134      if(length(clines)==0)return(list())
135      bounds <- which(diff(clines)!=1)
136      starts <- c(1,bounds+1)
137      ends <- c(bounds,length(clines))
138      ## detect body of function using paren matching
139      f <- function(ch)cumsum(nchar(gsub(sprintf("[^%s]",ch),"",src)))
140      parens <- f("(")-f(")")
141      body.begin <- which(diff(parens)<0 & parens[-1]==0)+2
142      is.arg <- function(){
143        0 == length(grep("^\\s*#",src[start-1],perl=TRUE)) &&
144          start<=body.begin
145        }
146      res <- list()
147      for(i in seq_along(starts)){
148        start <- clines[starts[i]]
149        end <- clines[ends[i]]
150        lab <- if(end+1==length(src))"value"
151        else if(start==2)"description"
152        else if(is.arg()){
153          ##twutz: strip leading white spaces and brackets and ,
154          arg <- gsub("^[ \t(,]*", "", src[start - 1])
155          arg <- gsub("^([^=,]*)[=,].*", "\\1", arg)
156          ##twutz: remove trailing whitespaces
157          arg <- gsub("^([^ \t]*)([ \t]+)$","\\1",arg)
158          arg <- gsub("...", "\\dots", arg, fix = TRUE)
159          paste("item{",arg,"}",sep="")
160        } else {
161          next;
162        }
163        res[[lab]] <- decomment(src[start:end])
164      }
165      res
166    }
167    
168    ### Parsers for each object/function that are constructed
169    ### automatically. This is a named list, each element is a list of 2
170    ### elements: forfun/forall, then a parser function for an individual
171    ### object.
172    forfun.parsers <-
173      list(prefixed.lines=prefixed.lines,
174           examples.after.return=examples.after.return,
175           ## Extract lots of info from normal functions.
176           parsefun=function(src,name,...){
177             extract.docs.fun(src,name)
178           },
179           ## title from first line of function def
180           title.from.firstline=function(src,name,code,...){
181             if(length(grep("#",src[1]))){
182               list(title=gsub("[^#]*#\\s*(.*)","\\1",src[1],perl=TRUE))
183             } else list()
184           },
185           ## PhG: it is tests/FUN.R!!! I would like more flexibility here
186           ## please, let me choose which dir to use for examples!
187           ## Get examples for FUN from the file tests/FUN.R
188           examples.from.testfile=function(name,...){
189             tsubdir <- getOption("inlinedocs.exdir")
190             if (is.null(tsubdir)) tsubdir <- "tests"       # Default value
191             tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))
192             if(file.exists(tfile))
193               list(examples=paste(readLines(tfile),collapse="\n"))
194             else list()
195           },
196           definition.from.source=function(doc,src,...){
197             def <- doc$definition
198             if(is.null(def)||def=="")
199               list(definition=paste(src,collapse="\n"))
200             else list()
201           })
202    
203    ### List of Parser Functions that can be applied to any object.
204    forall.parsers <-
205      list(## Fill in author from DESCRIPTION and titles.
206           author.from.description=function(desc,...){
207             list(author=desc[,"Maintainer"])
208           },
209           ## The format section sometimes causes problems, so erase it.
210           erase.format=function(...){
211             list(format="")
212           },
213           ## Convert the function name to a title.
214           title.from.name=function(name,doc,...){
215             if("title"%in%names(doc))list() else
216             list(title=gsub("[._]"," ",name))
217           },
218           ## PhG: here is what I propose for examples code in the 'ex' attribute
219           examples.in.attr =  function (name, o, ...) {
220                          ex <- attr(o, "ex")                          ex <- attr(o, "ex")
221                          if (!is.null(ex)) {                          if (!is.null(ex)) {
222                                  # Special case for code contained in a function             ## Special case for code contained in a function
223                                  if (inherits(ex, "function")) {                                  if (inherits(ex, "function")) {
224                                          # If source is available, start from there               ## If source is available, start from there
225                                          src <- attr(ex, "source")                                          src <- attr(ex, "source")
226                                          if (!is.null(src)) {                                          if (!is.null(src)) {
227                                                  ex <- src                                                  ex <- src
228                                          } else { # Use the body of the function               } else { ## Use the body of the function
229                                                  ex <- deparse(body(ex))                                                  ex <- deparse(body(ex))
230                                          }                                          }
231                                          # Eliminate leading and trailing code               ## Eliminate leading and trailing code
232                                          ex <- ex[-c(1, length(ex))]                                          ex <- ex[-c(1, length(ex))]
233                                          # Eliminate leading tabulations or four spaces               ## Eliminate leading tabulations or 2/4 spaces
234                                          ex <- sub("^\t|    ", "", ex)               ex <- sub("^\t|    |  ", "", ex)
235                                          # Add an empty line before and after example               ## Add an empty line before and after example
236                                          ex <- c("", ex, "")                                          ex <- c("", ex, "")
237                                  }                                  }
238                                  list(examples = paste(ex, collapse = "\n"))                                  list(examples = paste(ex, collapse = "\n"))
239                          } else list()                          } else list()
240             }))         })
241    
242  ### List of parser functions that operate on single objects. This list  ### List of parser functions that operate on single objects. This list
243  ### is useful for testing these functions, ie  ### is useful for testing these functions.
244  ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")  lonely <- c(forall.parsers,forfun.parsers)
245  lonely <- sapply(forall.parsers,function(L)L[[2]])  attr(lonely,"ex") <- function(){
246      lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")
247    }
248    
249  extra.code.docs <- function # Extract documentation from code chunks  extra.code.docs <- function # Extract documentation from code chunks
250  ### Parse R code to extract inline documentation from comments around  ### Parse R code to extract inline documentation from comments around
# Line 252  Line 365 
365    
366  ### List of parsers to use by default with package.skeleton.dx.  ### List of parsers to use by default with package.skeleton.dx.
367  default.parsers <-  default.parsers <-
368    c(extra.code.docs=extra.code.docs,    c(extra.code.docs=extra.code.docs, ## TODO: cleanup!
369      sapply(forall.parsers,function(L)L[[1]](L[[2]])),      sapply(forfun.parsers,forfun),
370        sapply(forall.parsers,forall),
371      edit.package.file=function(desc,...){      edit.package.file=function(desc,...){
372        in.details <- setdiff(colnames(desc),"Description")        in.details <- setdiff(colnames(desc),"Description")
373        details <- paste(paste(in.details,": \\tab ",desc[,in.details],"\\cr",        details <- paste(paste(in.details,": \\tab ",desc[,in.details],"\\cr",
# Line 276  Line 390 
390  ### The name of the function/chunk to use in warning messages.  ### The name of the function/chunk to use in warning messages.
391   ){   ){
392    res <- list()    res <- list()
   clines <- grep(prefix,code)  
   if(length(grep("#",code[1]))){  
     res$title <- gsub("[^#]*#\\s*(.*)","\\1",code[1],perl=TRUE)  
   }  
   if(length(clines) > 0){  
     ##details<<  
     ## The primary mechanism is that consecutive groups of lines matching  
     ## the specified prefix regular expression "\code{^### }" (i.e. lines  
     ## beginning with "\code{### }") are collected  
     ## as follows into documentation sections:\describe{  
     ## \item{description}{group starting at line 2 in the code}  
     ## \item{arguments}{group following each function argument}  
     ## \item{value}{group ending at the penultimate line of the code}}  
     ## These may be added to by use of the \code{##<<} constructs described  
     ## below.  
     bounds <- which(diff(clines)!=1)  
     starts <- c(1,bounds+1)  
     ends <- c(bounds,length(clines))  
     for(i in seq_along(starts)){  
       start <- clines[starts[i]]  
       end <- clines[ends[i]]  
       lab <- if(end+1==length(code))"value"  
       else if(start==2)"description"  
       else if ( 0 == length(grep("^\\s*#",code[start-1],perl=TRUE)) ){  
          #arg <- gsub("^[ (]*","",code[start-1])  
          #arg <- gsub("^([^=,]*)[=,].*","\\1",arg)  
          #arg <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots  
                  arg <- gsub("^[ \t(,]*", "", code[start - 1])  #twutz: strip leading white spaces and brackets and ,  
                  arg <- gsub("^([^=,]*)[=,].*", "\\1", arg)  
                  arg <- gsub("^([^ \t]*)([ \t]+)$","\\1",arg)   #twutz: remove trailing whitespaces  
                  arg <- gsub("...", "\\dots", arg, fix = TRUE)  
          paste("item{",arg,"}",sep="")  
        } else {  
          next;  
        }  
       res[[lab]] <- decomment(code[start:end])  
     }  
   }  
393    ##details<< For simple functions/arguments, the argument may also be    ##details<< For simple functions/arguments, the argument may also be
394    ## documented by appending \code{##<<} comments on the same line as the    ## documented by appending \code{##<<} comments on the same line as the
395    ## argument name. Mixing this mechanism with \code{###} comment lines for    ## argument name. Mixing this mechanism with \code{###} comment lines for
# Line 493  Line 569 
569        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)        arg <- gsub(arg.pat,"\\\\item\\{\\1\\}",line,perl=TRUE)
570        in.chunk <- TRUE        in.chunk <- TRUE
571        if ( not.describe ){        if ( not.describe ){
572            ## TDH 2010-06-18 For item{}s in the documentation list names,
573            ## we don't need to have a backslash before, so delete it.
574            arg <- gsub("^[\\]+","",arg)
575          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots          cur.field <- gsub("...","\\dots",arg,fix=TRUE) ##special case for dots
576          payload <- comment          payload <- comment
577        } else {        } else {
# Line 682  Line 761 
761    ## following line.    ## following line.
762    f.n <- paste(class.name,"class",sep="-")    f.n <- paste(class.name,"class",sep="-")
763    docs <- extract.docs.fun(chunk.source,f.n)    docs <- extract.docs.fun(chunk.source,f.n)
764      ## also apply source parsing functions that I separated out into
765      ## separate functions
766      docs <- combine(docs,lonely$prefixed.lines(chunk.source))
767      docs$title <- lonely$title.from.firstline(chunk.source)
768    ##details<<    ##details<<
769    ## The class definition skeleton includes an \code{Objects from the Class}    ## The class definition skeleton includes an \code{Objects from the Class}
770    ## section, to which any \code{##details<<} documentation chunks are    ## section, to which any \code{##details<<} documentation chunks are
# Line 719  Line 802 
802  ### Additional arguments to pass to Parser Functions.  ### Additional arguments to pass to Parser Functions.
803   ){   ){
804    e <- new.env()    e <- new.env()
805    old <- options(keep.source.pkgs=TRUE)    old <- options(keep.source=TRUE)
806    on.exit(options(old))    on.exit(options(old))
   ##tryCatch({  
807      exprs <- parse(text=code)      exprs <- parse(text=code)
808      for (i in exprs) eval(i, e)      for (i in exprs) eval(i, e)
 ##   },error=function(err){  
 ##     print(i)  
 ##     stop("eval or parse failed with error:\n",err)  
 ##   })  
809    objs <- sapply(ls(e),get,e,simplify=FALSE)    objs <- sapply(ls(e),get,e,simplify=FALSE)
810    
811    docs <- list()    docs <- list()
# Line 768  Line 846 
846  ### A list of extracted documentation from code.  ### A list of extracted documentation from code.
847  }  }
848    
849    ### Names of Parser Functions that operate on the desc arg.
850    descfile.names <- c("author.from.description","edit.package.file")
851    
852    ### Names of Parser Functions that do NOT use the desc arg.
853    non.descfile.names <-
854      names(default.parsers)[!names(default.parsers)%in%descfile.names]
855    
856    ### Parsers that operate only on R code, independently of the
857    ### description file.
858    nondesc.parsers <- default.parsers[non.descfile.names]
859    
860  extract.docs.file <- function  extract.docs.file <- function
861  ### Apply all parsers relevant to extract info from just 1 code file.  ### Apply all parsers relevant to extract info from just 1 code file.
862  (f,  (f,
863  ### File name of R code to read and parse.  ### File name of R code to read and parse.
864   parsers=list(extra.code.docs,default.parsers$parsefun),   parsers=nondesc.parsers,
865  ### Parser Functions to use to parse the code and extract  ### Parser Functions to use to parse the code and extract
866  ### documentation.  ### documentation.
867   ...   ...

Legend:
Removed from v.109  
changed lines
  Added in v.142

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