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

Legend:
Removed from v.152  
changed lines
  Added in v.154

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