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 89, Mon May 3 13:01:06 2010 UTC revision 105, Tue May 18 17:32:43 2010 UTC
# Line 12  Line 12 
12    function(objs,docs,...){    function(objs,docs,...){
13      objs <- objs[sapply(objs,subfun)]      objs <- objs[sapply(objs,subfun)]
14      L <- list()      L <- list()
15      for(N in names(objs)){      for(N in names(docs)){
16        o <- objs[[N]]        o <- objs[[N]]
17        L[[N]] <- FUN(src=attr(o,"source"),        L[[N]] <- FUN(src=attr(o,"source"),
18                      name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)                      name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)
# Line 25  Line 25 
25  ### For each function in the package, do something.  ### For each function in the package, do something.
26  forfun <- function(FUN)forall(FUN,is.function)  forfun <- function(FUN)forall(FUN,is.function)
27    
28  ### Default parsers to use with package.skeleton.dx  ### Parsers for each object/function that are constructed
29    ### automatically. This is a named list, each element is a list of 2
30    ### elements: forfun/forall, then a parser function for an individual
31    ### object.
32  forall.parsers <-  forall.parsers <-
33    list(## Extract lots of info from normal functions.    list(## Extract lots of info from normal functions.
34         parsefun=list(forfun,function(src,name,...){         parsefun=list(forfun,function(src,name,...){
# Line 52  Line 55 
55           else list()           else list()
56         }),         }),
57         ## Get examples from inline definitions after return()         ## Get examples from inline definitions after return()
58         examples.after.return=list(forfun,function(src,...){         examples.after.return=list(forfun,function(name,src,...){
59           rline <- grep("^\\W*return[(]",src)           coll <- paste(src,collapse="\n")
60           if(length(rline)==0)return(list())           thispre <- gsub("^[\\^]","",prefix)
61           rline <- rline[length(rline)]           FIND <- paste("(return|UseMethod)[(][^\\n]*\\n",thispre,sep="")
62           comment.line.nums <- grep(prefix,src)           m <- regexpr(FIND,coll)
63           if(!(comment.end <- rline+1)%in%comment.line.nums)return(list())           if(m[1]==-1)return(list())
64           while(comment.end%in%comment.line.nums)comment.end <- comment.end+1           after <- substr(coll,m[1],nchar(coll))
65           excode <- src[comment.end:(length(src)-1)]           FIND <-
66           list(examples=paste(gsub("^\\W*","",excode),collapse="\n"))             paste("[^\\n]*",# rest of the return line
67                     "((?:\\n###[^\\n]*)+)",#comment value lines \\1
68                     "([\\w\\W]*)[}]",#examples \\2
69                     sep="")
70             SEP <- "-/-/-/-/-=====-----"
71             REP <- paste("\\1",SEP,"\\2",sep="")
72             r <- strsplit(gsub(FIND,REP,after,perl=TRUE),split=SEP)[[1]]
73             l <- strsplit(r,split="\n")
74             excode <- c(l[[2]],"")
75             if(length(l)<2||paste(excode,collapse="")=="")return(list())
76             prefixes <- gsub("(\\s*).*","\\1",excode,perl=TRUE)[grep("\\w",excode)]
77             FIND <- prefixes[which.min(nchar(prefixes))]
78             list(examples=paste(sub(FIND,"",excode),collapse="\n"),
79                  value=decomment(l[[1]][-1]))
80         }))         }))
81    
82  ### List of parser functions that operate on single objects. This list  ### List of parser functions that operate on single objects. This list
83  ### is useful for testing these functions, ie  ### is useful for testing these functions, ie
84  ### lonely$parsefuns(attr(extract.docs.file,"src"),"extract.docs.file")  ### lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")
85  lonely <- sapply(forall.parsers,function(L)L[[2]])  lonely <- sapply(forall.parsers,function(L)L[[2]])
86    
87  extract.docs.file <- function # Extract documentation from a file  extract.docs.file <- function # Extract documentation from a file
# Line 180  Line 196 
196  }  }
197    
198  ### List of parsers to use by default with package.skeleton.dx.  ### List of parsers to use by default with package.skeleton.dx.
199  default.parsers <- c(sapply(forall.parsers,function(L)L[[1]](L[[2]])),  default.parsers <- c(extract.docs.file=extract.docs.file,
200                       extract.docs.file=extract.docs.file)                       sapply(forall.parsers,function(L)L[[1]](L[[2]])))
201    
202  extract.docs.fun <- function # Extract documentation from a function  extract.docs.fun <- function # Extract documentation from a function
203  ### Given source code of a function, return a list describing inline  ### Given source code of a function, return a list describing inline
# Line 190  Line 206 
206  ### The function to examine.  ### The function to examine.
207   name.fun   name.fun
208  ### The name of the function/chunk to use in warning messages.  ### The name of the function/chunk to use in warning messages.
209   )   ){
 {  
210    res <- list()    res <- list()
211    clines <- grep(prefix,code)    clines <- grep(prefix,code)
212    if(length(grep("#",code[1]))){    if(length(grep("#",code[1]))){
# Line 577  Line 592 
592  ### Using the same conventions as for functions, definitions of S4 classes  ### Using the same conventions as for functions, definitions of S4 classes
593  ### in the form \code{setClass("classname",\dots)} are also located and  ### in the form \code{setClass("classname",\dots)} are also located and
594  ### scanned for inline comments.  ### scanned for inline comments.
595  (doc.link)  (doc.link
596  ### DocLink object as created by \code{\link{extract.file.parse}}.  ### DocLink object as created by \code{\link{extract.file.parse}}.
597  ### Note that \code{source} statements are \emph{ignored} when scanning for  ### Note that \code{source} statements are \emph{ignored} when scanning for
598  ### class definitions.  ### class definitions.
599  {   ){
600    chunk.source <- doc.link@code    chunk.source <- doc.link@code
601    ##details<<    ##details<<
602    ## Extraction of S4 class documentation is currently limited to expressions    ## Extraction of S4 class documentation is currently limited to expressions
# Line 622  Line 637 
637    }    }
638    invisible(docs)    invisible(docs)
639  }  }
640    
641    extract.docs.code <- function
642    ### Write code to a file and parse it to r objs, then run all the
643    ### parsers and return the documentation list.
644    (code,
645    ### Character vector of code lines.
646     parsers,
647    ### List of Parser Functions.
648     verbose=TRUE,
649    ### Echo names of Parser Functions?
650     ...
651    ### Additional arguments to pass to Parser Functions.
652     ){
653      code.file <- tempfile()
654      writeLines(code,code.file)
655      e <- new.env()
656      old <- options(keep.source.pkgs=TRUE)
657      tryCatch(suppressWarnings(sys.source(code.file,e)),error=function(e){
658        stop("source ",code.file," failed with error:\n",e)
659      })
660      options(old)
661      objs <- sapply(ls(e),get,e,simplify=FALSE)
662    
663      ## apply parsers in sequence to code and objs
664      docs <- list()
665      for(i in seq_along(parsers)){
666        N <- names(parsers[i])
667        if(verbose){
668          if(is.character(N) && N!=""){
669            cat(N," ",sep="")
670          }else cat('. ')
671        }
672        p <- parsers[[i]]
673        ## This is the argument list that each parser receives:
674        L <- p(code=code,objs=objs,docs=docs,...)
675        docs <- combine(docs,L)
676      }
677      if(verbose)cat("\n")
678      docs
679    ### A list of extracted documentation from code.
680    }

Legend:
Removed from v.89  
changed lines
  Added in v.105

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