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 142, Wed Oct 20 09:21:53 2010 UTC revision 162, Wed Nov 10 09:18:20 2010 UTC
# Line 28  Line 28 
28  ### String without prefixes or newlines.  ### String without prefixes or newlines.
29  }  }
30    
 ### For each object in the package that satisfies the criterion#  
 ## checked by subfun, parse source using FUN and return the resulting  
 ### documentation list.  
31  forall <- function  forall <- function
32    ### For each object in the package that satisfies the criterion
33    ### checked by subfun, parse source using FUN and return the resulting
34    ### documentation list.
35  (FUN,  (FUN,
36  ### Function to apply to each element in the package.  ### Function to apply to each element in the package.
37   subfun=function(x)TRUE   subfun=function(x)TRUE
# Line 43  Line 43 
43    f <- function(objs,docs,...){    f <- function(objs,docs,...){
44      objs <- objs[sapply(objs,subfun)]      objs <- objs[sapply(objs,subfun)]
45      L <- list()      L <- list()
46        on.exit(cat(sprintf("Parser Function failed on %s\n",N)))
47      for(N in names(docs)){      for(N in names(docs)){
48        o <- objs[[N]]        o <- objs[[N]]
49        L[[N]] <- FUN(src=attr(o,"source"),        L[[N]] <- FUN(src=attr(o,"source"),
50                      name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)                      name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)
51      }      }
52        on.exit()## remove warning message
53      L      L
54    }    }
55    class(f) <- c("allfun","function")    class(f) <- c("allfun","function")
# Line 136  Line 138 
138    starts <- c(1,bounds+1)    starts <- c(1,bounds+1)
139    ends <- c(bounds,length(clines))    ends <- c(bounds,length(clines))
140    ## detect body of function using paren matching    ## detect body of function using paren matching
141    f <- function(ch)cumsum(nchar(gsub(sprintf("[^%s]",ch),"",src)))    code <- gsub("#.*","",src)
142      f <- function(ch)cumsum(nchar(gsub(sprintf("[^%s]",ch),"",code)))
143    parens <- f("(")-f(")")    parens <- f("(")-f(")")
144    body.begin <- which(diff(parens)<0 & parens[-1]==0)+2    body.begin <- which(diff(parens)<0 & parens[-1]==0)+2
145    is.arg <- function(){    is.arg <- function(){
146      0 == length(grep("^\\s*#",src[start-1],perl=TRUE)) &&      gres <- grep("^\\s*#",src[start-1],perl=TRUE)
147        start<=body.begin      0 == length(gres) && start<=body.begin
148      }      }
149    res <- list()    res <- list()
150    for(i in seq_along(starts)){    for(i in seq_along(starts)){
# Line 165  Line 168 
168    res    res
169  }  }
170    
171  ### Parsers for each object/function that are constructed  extract.xxx.chunks <- function # Extract documentation from a function
 ### automatically. This is a named list, each element is a list of 2  
 ### elements: forfun/forall, then a parser function for an individual  
 ### object.  
 forfun.parsers <-  
   list(prefixed.lines=prefixed.lines,  
        examples.after.return=examples.after.return,  
        ## Extract lots of info from normal functions.  
        parsefun=function(src,name,...){  
          extract.docs.fun(src,name)  
        },  
        ## title from first line of function def  
        title.from.firstline=function(src,name,code,...){  
          if(length(grep("#",src[1]))){  
            list(title=gsub("[^#]*#\\s*(.*)","\\1",src[1],perl=TRUE))  
          } else list()  
        },  
        ## 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=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()  
        },  
        definition.from.source=function(doc,src,...){  
          def <- doc$definition  
          if(is.null(def)||def=="")  
            list(definition=paste(src,collapse="\n"))  
          else list()  
        })  
   
 ### List of Parser Functions that can be applied to any object.  
 forall.parsers <-  
   list(## Fill in author from DESCRIPTION and titles.  
        author.from.description=function(desc,...){  
          list(author=desc[,"Maintainer"])  
        },  
        ## The format section sometimes causes problems, so erase it.  
        erase.format=function(...){  
          list(format="")  
        },  
        ## Convert the function name to a title.  
        title.from.name=function(name,doc,...){  
          if("title"%in%names(doc))list() else  
          list(title=gsub("[._]"," ",name))  
        },  
        ## PhG: here is what I propose for examples code in the 'ex' attribute  
        examples.in.attr =  function (name, o, ...) {  
          ex <- attr(o, "ex")  
          if (!is.null(ex)) {  
            ## Special case for code contained in a function  
            if (inherits(ex, "function")) {  
              ## If source is available, start from there  
              src <- attr(ex, "source")  
              if (!is.null(src)) {  
                ex <- src  
              } else { ## Use the body of the function  
                ex <- deparse(body(ex))  
              }  
              ## Eliminate leading and trailing code  
              ex <- ex[-c(1, length(ex))]  
              ## Eliminate leading tabulations or 2/4 spaces  
              ex <- sub("^\t|    |  ", "", ex)  
              ## Add an empty line before and after example  
              ex <- c("", ex, "")  
            }  
            list(examples = paste(ex, collapse = "\n"))  
          } else list()  
        })  
   
 ### List of parser functions that operate on single objects. This list  
 ### is useful for testing these functions.  
 lonely <- c(forall.parsers,forfun.parsers)  
 attr(lonely,"ex") <- function(){  
   lonely$parsefun(attr(extract.docs.file,"source"),"extract.docs.file")  
 }  
   
 extra.code.docs <- function # Extract documentation from code chunks  
 ### Parse R code to extract inline documentation from comments around  
 ### each function. These are not able to be retreived simply by  
 ### looking at the "source" attribute. This is a Parser Function that  
 ### can be used in the parser list of package.skeleton.dx(). TODO:  
 ### Modularize this into separate Parsers Functions for S4 classes,  
 ### prefixes, ##<<blocks, etc. Right now it is not very clean!  
 (code,  
 ### Code lines in a character vector containing multiple R objects to  
 ### parse for documentation.  
  objs,  
 ### The objects defined in the code.  
  ...  
 ### ignored  
  ){  
   parsed <- extract.file.parse(code)  
   extract.docs.try <- function(o,on)  
     {  
       ## Note: we could use parsed information here too, but that  
       ## would produce different results for setMethodS3 etc.  
       doc <- list()  
       if ( !is.null(parsed[[on]]) ){  
         if ( !is.na(parsed[[on]]@code[1]) ){ # no code given for generics  
           doc$definition <- paste(parsed[[on]]@code,collapse="\n")  
         }  
         if(!"description"%in%names(doc) && !is.na(parsed[[on]]@description) ){  
           doc$description <- parsed[[on]]@description  
         }  
         if ( "setMethodS3" == parsed[[on]]@created ){  
           # PhG: this may be wrong! It does not catch correctly how the method  
           # must be splitted in case of methods containing dots. for instance,  
           # as.data.frame.matrix must be split into: m1 = as.data.frame and  
           # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!  
                   pattern <- "^([^\\.]+)\\.(.*)$"  
           doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),  
               m2 <- gsub(pattern,"\\2",on,perl=TRUE))  
           if ( 0 < length(grep("\\W",m1,perl=TRUE)) ){  
                           m1 <- paste("`",m1,"`",sep="")  
           }  
           cat("S3method(",m1,",",m2,")\n",sep="")  
         }  
       }  
       if("title" %in% names(doc) && !"description" %in% names(doc) ){  
         ## For short functions having both would duplicate, but a  
         ## description is required. Therefore automatically copy title  
         ## across to avoid errors at package build time.  
         doc$description <- doc$title  
       }  
       doc  
     }  
   extract.docs <- function(on){  
     res <- try({o <- objs[[on]]  
                 extract.docs.try(o, on)},FALSE)  
     if(class(res)=="try-error"){  
       cat("Failed to extract docs for: ",on,"\n\n")  
       list()  
     } else if(0 == length(res) && inherits(objs[[on]],"standardGeneric")){  
       NULL  
     } else if(0 == length(res) && "function" %in% class(o)  
               && 1 == length(osource <- attr(o,"source"))  
               && 1 == length(grep(paste("UseMethod(",on,")",sep="\""),osource))  
               ){  
       ## phew - this should only pick up R.oo S3 generic definitions like:  
       ## attr(*, "source")= chr "function(...) UseMethod(\"select\")"  
       NULL  
     } else res  
   }  
   doc.names <- names(objs)  
   res <- sapply(doc.names,extract.docs,simplify=FALSE)  
   ## Special processing for S4 classes as they do not appear in normal ls()  
   for ( nn in names(parsed) ){  
     if ( parsed[[nn]]@created == "setClass" ){  
       S4class.docs <- extract.docs.setClass(parsed[[nn]])  
       docname <- paste(nn,"class",sep="-")  
       if ( is.null(res[[docname]]) ){  
         res[[docname]] <- S4class.docs  
         doc.names <- c(doc.names,docname)  
       } else {  
         stop(nn," appears as both S4 class and some other definition")  
       }  
     }  
   }  
   inherit.docs <- function(on){  
     in.res <- res[[on]]  
     if ( !is.null(parsed[[on]]) ){  
       for ( parent in parsed[[on]]@parent ){  
         if ( !is.na(parent) ){  
           if ( is.null(in.res) ){  
             in.res <- res[[parent]]  
           } else if ( parent %in% names(res) ){  
             parent.docs <- res[[parent]]  
             for ( nn in names(parent.docs) ){  
               if ( !nn %in% names(in.res) ){  
                 in.res[[nn]] <- parent.docs[[nn]]  
               }  
             }  
           }  
         }  
       }  
     }  
     invisible(in.res)  
   }  
   all.done <- FALSE  
   while ( !all.done ){  
     res1 <- sapply(doc.names,inherit.docs,simplify=FALSE)  
     all.done <- identical(res1,res)  
     res <- res1  
   }  
   ## now strip out any generics (which have value NULL in res):  
   res.not.null <- sapply(res,function(x){!is.null(x)})  
   if ( 0 < length(res.not.null) && length(res.not.null) < length(res) ){  
     res <- res[res.not.null]  
   }  
   res  
 ### named list of lists, one for each object to document.  
 }  
   
 ### List of parsers to use by default with package.skeleton.dx.  
 default.parsers <-  
   c(extra.code.docs=extra.code.docs, ## TODO: cleanup!  
     sapply(forfun.parsers,forfun),  
     sapply(forall.parsers,forall),  
     edit.package.file=function(desc,...){  
       in.details <- setdiff(colnames(desc),"Description")  
       details <- paste(paste(in.details,": \\tab ",desc[,in.details],"\\cr",  
                              sep=""),collapse="\n")  
       L <-  
         list(list(title=desc[,"Title"],  
                   description=desc[,"Description"],  
                   `tabular{ll}`=details,  
                   author=desc[,"Maintainer"]))  
       names(L) <- paste(desc[,"Package"],"-package",sep="")  
       L  
     })  
   
 extract.docs.fun <- function # Extract documentation from a function  
172  ### Given source code of a function, return a list describing inline  ### Given source code of a function, return a list describing inline
173  ### documentation in that source code.  ### documentation in that source code.
174  (code,  (src,
175  ### The function to examine.  ### The source lines of the function to examine, as a character
176   name.fun  ### vector.
177     name.fun="(unnamed function)",
178  ### The name of the function/chunk to use in warning messages.  ### The name of the function/chunk to use in warning messages.
179     ...
180    ### ignored.
181   ){   ){
182    res <- list()    res <- list()
183    ##details<< For simple functions/arguments, the argument may also be    ##details<< For simple functions/arguments, the argument may also be
# Line 468  Line 258 
258            else paste(res[[field]], payload, sep=chunk.sep)            else paste(res[[field]], payload, sep=chunk.sep)
259        invisible(chunk.res)        invisible(chunk.res)
260      }      }
261    while ( k <= length(code) ){    while ( k <= length(src) ){
262      line <- code[k]      line <- src[k]
263      if ( 0 < length(grep(extra.regexp,line,perl=TRUE) ) ){      if ( 0 < length(grep(extra.regexp,line,perl=TRUE) ) ){
264        ## we have a new extra chunk - first get field name and any payload        ## we have a new extra chunk - first get field name and any payload
265        new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)        new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)
# Line 622  Line 412 
412  ### with the string in this list (implemented in modify.Rd.file).  ### with the string in this list (implemented in modify.Rd.file).
413  }  }
414    
415    ### Parsers for each function that are constructed automatically. This
416    ### is a named list, and each element is a parser function for an
417    ### individual object.
418    forfun.parsers <-
419      list(prefixed.lines=prefixed.lines,
420           examples.after.return=examples.after.return,
421           extract.xxx.chunks=extract.xxx.chunks,
422           ## title from first line of function def
423           title.from.firstline=function(src,name,code,...){
424             if(length(grep("#",src[1]))){
425               list(title=gsub("[^#]*#\\s*(.*)","\\1",src[1],perl=TRUE))
426             } else list()
427           },
428           ## PhG: it is tests/FUN.R!!! I would like more flexibility here
429           ## please, let me choose which dir to use for examples!
430           ## Get examples for FUN from the file tests/FUN.R
431           examples.from.testfile=function(name,...){
432             tsubdir <- getOption("inlinedocs.exdir")
433             if (is.null(tsubdir)) tsubdir <- "tests"       # Default value
434             tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))
435             if(file.exists(tfile))
436               list(examples=paste(readLines(tfile),collapse="\n"))
437             else list()
438           },
439           definition.from.source=function(doc,src,...){
440             def <- doc$definition
441             is.empty <- function(x)is.null(x)||x==""
442             if(is.empty(def) && !is.empty(src))
443               list(definition=paste(src,collapse="\n"))
444             else list()
445           })
446    
447    ### List of Parser Functions that can be applied to any object.
448    forall.parsers <-
449      list(## Fill in author from DESCRIPTION and titles.
450           author.from.description=function(desc,...){
451             list(author=desc[,"Maintainer"])
452           },
453           ## The format section sometimes causes problems, so erase it.
454           erase.format=function(...){
455             list(format="")
456           },
457           ## Convert the function name to a title.
458           title.from.name=function(name,doc,...){
459             if("title"%in%names(doc))list() else
460             list(title=gsub("[._]"," ",name))
461           },
462           ## PhG: here is what I propose for examples code in the 'ex' attribute
463           examples.in.attr =  function (name, o, ...) {
464             ex <- attr(o, "ex")
465             if (!is.null(ex)) {
466               ## Special case for code contained in a function
467               if (inherits(ex, "function")) {
468                 ## If source is available, start from there
469                 src <- attr(ex, "source")
470                 if (!is.null(src)) {
471                   ex <- src
472                 } else { ## Use the body of the function
473                   ex <- deparse(body(ex))
474                 }
475                 ## Eliminate leading and trailing code
476                 ex <- ex[-c(1, length(ex))]
477                 ## Eliminate leading tabulations or 2/4 spaces
478                 ex <- sub("^\t|    |  ", "", ex)
479                 ## Add an empty line before and after example
480                 ex <- c("", ex, "")
481               }
482               list(examples = paste(ex, collapse = "\n"))
483             } else list()
484           })
485    
486    ### List of parser functions that operate on single objects. This list
487    ### is useful for testing these functions.
488    lonely <- c(forall.parsers,forfun.parsers)
489    attr(lonely,"ex") <- function(){
490      f <- function # title
491    ### description
492      (x, ##<< arg x
493       y
494    ### arg y
495       ){
496        ##value<< a list with elements
497        list(x=x, ##<< original x value
498             y=y, ##<< original y value
499             sum=x+y) ##<< their sum
500        ##end<<
501      }
502      src <- attr(f,"source")
503      lonely$extract.xxx.chunks(src)
504      lonely$prefixed.lines(src)
505    }
506    
507    extra.code.docs <- function # Extract documentation from code chunks
508    ### Parse R code to extract inline documentation from comments around
509    ### each function. These are not able to be retreived simply by
510    ### looking at the "source" attribute. This is a Parser Function that
511    ### can be used in the parser list of package.skeleton.dx(). TODO:
512    ### Modularize this into separate Parsers Functions for S4 classes,
513    ### prefixes, ##<<blocks, etc. Right now it is not very clean!
514    (code,
515    ### Code lines in a character vector containing multiple R objects to
516    ### parse for documentation.
517     objs,
518    ### The objects defined in the code.
519     ...
520    ### ignored
521     ){
522      parsed <- extract.file.parse(code)
523      extract.docs.try <- function(o,on)
524        {
525          ## Note: we could use parsed information here too, but that
526          ## would produce different results for setMethodS3 etc.
527          doc <- list()
528          if ( !is.null(parsed[[on]]) ){
529            if ( !is.na(parsed[[on]]@code[1]) ){ # no code given for generics
530              doc$definition <- paste(parsed[[on]]@code,collapse="\n")
531            }
532            if(!"description"%in%names(doc) && !is.na(parsed[[on]]@description) ){
533              doc$description <- parsed[[on]]@description
534            }
535            if ( "setMethodS3" == parsed[[on]]@created ){
536              # PhG: this may be wrong! It does not catch correctly how the method
537              # must be splitted in case of methods containing dots. for instance,
538              # as.data.frame.matrix must be split into: m1 = as.data.frame and
539              # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!
540                      pattern <- "^([^\\.]+)\\.(.*)$"
541              doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),
542                  m2 <- gsub(pattern,"\\2",on,perl=TRUE))
543              if ( 0 < length(grep("\\W",m1,perl=TRUE)) ){
544                              m1 <- paste("`",m1,"`",sep="")
545              }
546              cat("S3method(",m1,",",m2,")\n",sep="")
547            }
548          }
549          if("title" %in% names(doc) && !"description" %in% names(doc) ){
550            ## For short functions having both would duplicate, but a
551            ## description is required. Therefore automatically copy title
552            ## across to avoid errors at package build time.
553            doc$description <- doc$title
554          }
555          doc
556        }
557      extract.docs <- function(on){
558        res <- try({o <- objs[[on]]
559                    extract.docs.try(o, on)},FALSE)
560        if(class(res)=="try-error"){
561          cat("Failed to extract docs for: ",on,"\n\n")
562          list()
563        } else if(0 == length(res) && inherits(objs[[on]],"standardGeneric")){
564          NULL
565        } else if(0 == length(res) && "function" %in% class(o)
566                  && 1 == length(osource <- attr(o,"source"))
567                  && 1 == length(grep(paste("UseMethod(",on,")",sep="\""),osource))
568                  ){
569          ## phew - this should only pick up R.oo S3 generic definitions like:
570          ## attr(*, "source")= chr "function(...) UseMethod(\"select\")"
571          NULL
572        } else res
573      }
574      doc.names <- names(objs)
575      res <- sapply(doc.names,extract.docs,simplify=FALSE)
576      ## Special processing for S4 classes as they do not appear in normal ls()
577      for ( nn in names(parsed) ){
578        if ( parsed[[nn]]@created == "setClass" ){
579          S4class.docs <- extract.docs.setClass(parsed[[nn]])
580          docname <- paste(nn,"class",sep="-")
581          if ( is.null(res[[docname]]) ){
582            res[[docname]] <- S4class.docs
583            doc.names <- c(doc.names,docname)
584          } else {
585            stop(nn," appears as both S4 class and some other definition")
586          }
587        }
588      }
589      inherit.docs <- function(on){
590        in.res <- res[[on]]
591        if ( !is.null(parsed[[on]]) ){
592          for ( parent in parsed[[on]]@parent ){
593            if ( !is.na(parent) ){
594              if ( is.null(in.res) ){
595                in.res <- res[[parent]]
596              } else if ( parent %in% names(res) ){
597                parent.docs <- res[[parent]]
598                for ( nn in names(parent.docs) ){
599                  if ( !nn %in% names(in.res) ){
600                    in.res[[nn]] <- parent.docs[[nn]]
601                  }
602                }
603              }
604            }
605          }
606        }
607        invisible(in.res)
608      }
609      all.done <- FALSE
610      while ( !all.done ){
611        res1 <- sapply(doc.names,inherit.docs,simplify=FALSE)
612        all.done <- identical(res1,res)
613        res <- res1
614      }
615      ## now strip out any generics (which have value NULL in res):
616      res.not.null <- sapply(res,function(x){!is.null(x)})
617      if ( 0 < length(res.not.null) && length(res.not.null) < length(res) ){
618        res <- res[res.not.null]
619      }
620      res
621    ### named list of lists, one for each object to document.
622    }
623    
624    ### List of parsers to use by default with package.skeleton.dx.
625    default.parsers <-
626      c(extra.code.docs=extra.code.docs, ## TODO: cleanup!
627        sapply(forfun.parsers,forfun),
628        sapply(forall.parsers,forall),
629        edit.package.file=function(desc,...){
630          in.details <- setdiff(colnames(desc),"Description")
631          details <- paste(paste(in.details,": \\tab ",desc[,in.details],"\\cr",
632                                 sep=""),collapse="\n")
633          L <-
634            list(list(title=desc[,"Title"],
635                      description=desc[,"Description"],
636                      `tabular{ll}`=details,
637                      author=desc[,"Maintainer"]))
638          names(L) <- paste(desc[,"Package"],"-package",sep="")
639          L
640        })
641    
642  setClass("DocLink", # Link documentation among related functions  setClass("DocLink", # Link documentation among related functions
643  ### The \code{.DocLink} class provides the basis for hooking together  ### The \code{.DocLink} class provides the basis for hooking together
644  ### documentation of related classes/functions/objects. The aim is that  ### documentation of related classes/functions/objects. The aim is that
# Line 760  Line 777 
777    ## the same line or \code{### } comments at the beginning of the    ## the same line or \code{### } comments at the beginning of the
778    ## following line.    ## following line.
779    f.n <- paste(class.name,"class",sep="-")    f.n <- paste(class.name,"class",sep="-")
780    docs <- extract.docs.fun(chunk.source,f.n)    docs <- extract.xxx.chunks(chunk.source,f.n)
781    ## also apply source parsing functions that I separated out into    ## also apply source parsing functions that I separated out into
782    ## separate functions    ## separate functions
783    docs <- combine(docs,lonely$prefixed.lines(chunk.source))    docs <- combine(docs,lonely$prefixed.lines(chunk.source))
# Line 805  Line 822 
822    old <- options(keep.source=TRUE)    old <- options(keep.source=TRUE)
823    on.exit(options(old))    on.exit(options(old))
824    exprs <- parse(text=code)    exprs <- parse(text=code)
825    for (i in exprs) eval(i, e)    for (i in exprs){
826        tryCatch(eval(i, e),error=function(e){
827          ##print(e)
828        })
829      }
830    objs <- sapply(ls(e),get,e,simplify=FALSE)    objs <- sapply(ls(e),get,e,simplify=FALSE)
831    
832    docs <- list()    docs <- list()

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

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