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 128, Wed Sep 22 09:24:39 2010 UTC revision 142, Wed Oct 20 09:21:53 2010 UTC
# Line 28  Line 28 
28  ### String without prefixes or newlines.  ### 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.
34  forall <- function  forall <- function
35  (FUN,  (FUN,
# Line 39  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 49  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="",...) {
          if("title"%in%names(doc))list() else  
          list(title=gsub("[._]"," ",name))  
        }),  
        ## 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 of 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(src,name="",...) {  
89           ## Look for the examples mark           ## Look for the examples mark
90           m <- grep("##examples<<|#\\{\\{\\{examples", src)    m <- grep("^\\s*(##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,             warning("More than one examples tag for ", name,
# Line 138  Line 118 
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    
123    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         ## PhG: here is what I propose for examples code in the 'ex' attribute
219         examples.in.attr = list(forall, function (name, o, ...) {         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
# Line 154  Line 230 
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.  ### is useful for testing these functions.
244  lonely <- sapply(forall.parsers,function(L)L[[2]])  lonely <- c(forall.parsers,forfun.parsers)
245  attr(lonely,"ex") <- function(){  attr(lonely,"ex") <- function(){
246    lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")    lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")
247  }  }
# Line 290  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 314  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 723  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 804  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  ### Parsers that operate only on R code, independently of the
857  ### description file.  ### description file.
858  nondesc.parsers <- c(extra.code.docs=list(extra.code.docs),  nondesc.parsers <- default.parsers[non.descfile.names]
   default.parsers[c("parsefun","examples.after.return","examples.in.attr")])  
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.

Legend:
Removed from v.128  
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 Powered By FusionForge