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 195, Fri Feb 18 12:30:30 2011 UTC revision 204, Fri Feb 18 16:48:22 2011 UTC
# Line 7  Line 7 
7    
8  ### combine lists by adding elements or adding to existing elements  ### combine lists by adding elements or adding to existing elements
9  combine.list <- function(x,y){  combine.list <- function(x,y){
10    toadd <- !names(y)%in%names(x)    toadd <- if(".overwrite"%in%names(y)){
11        y <- y[names(y)!=".overwrite"]
12        rep(TRUE,length(y))
13      }else{
14        !names(y)%in%names(x)
15      }
16    toup <- names(y)[!toadd]    toup <- names(y)[!toadd]
   if("doc"%in%names(x))return(x$doc)  
   if("doc"%in%names(y))return(y$doc)  
17    x[names(y)[toadd]] <- y[toadd]    x[names(y)[toadd]] <- y[toadd]
18    for(up in toup)x[[up]] <- combine(x[[up]],y[[up]])    for(up in toup)x[[up]] <- combine(x[[up]],y[[up]])
19    return(x)    x
20  ### A list, same type as x, but with added elements from y.  ### A list, same type as x, but with added elements from y.
21  }  }
22    
# Line 46  Line 49 
49      objs <- objs[sapply(objs,subfun)]      objs <- objs[sapply(objs,subfun)]
50      L <- list()      L <- list()
51      on.exit(cat(sprintf("Parser Function failed on %s\n",N)))      on.exit(cat(sprintf("Parser Function failed on %s\n",N)))
52      for(N in names(docs)){      for(N in union(names(docs),names(objs))){
53        o <- objs[[N]]        o <- objs[[N]]
54        L[[N]] <- FUN(src=attr(o,"source"),        L[[N]] <- FUN(src=attr(o,"source"),
55                      name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)                      name=N,objs=objs,o=o,docs=docs,doc=docs[[N]],...)
# Line 512  Line 515 
515             }             }
516             list(examples = ex)             list(examples = ex)
517           } else list()           } else list()
518         },         },collapse=function(doc,...){
519         collapse.docs=function(doc,...){           L <- lapply(doc,paste,collapse="\n")
520           list(doc=lapply(doc,paste,collapse="\n"))           L$.overwrite <- TRUE
521         })           L
522           },tag.s3methods=function(name,env,...){
523             parts <- strsplit(name, ".", fixed = TRUE)[[1]]
524             l <- length(parts)
525             if (l > 1) {
526               for (i in 1:(l - 1)) {
527                 ## Look for a generic function (known by the system or defined
528                 ## in the package) that matches that part of the function name
529                 generic <- paste(parts[1:i], collapse = ".")
530                 if (any(generic %in% utils:::getKnownS3generics()) ||
531                     utils:::findGeneric(generic, env) != "") {
532                   object <- paste(parts[(i + 1):l], collapse = ".")
533                   return(list(.s3method=c(generic, object)))
534                 }
535               }
536             }
537             list()
538           }
539           )
540    
541  ### List of parser functions that operate on single objects. This list  ### List of parser functions that operate on single objects. This list
542  ### is useful for testing these functions.  ### is useful for testing these functions.
543  lonely <- c(forall.parsers,forfun.parsers)  lonely <- structure(c(forall.parsers,forfun.parsers),ex=function(){
 attr(lonely,"ex") <- function(){  
544    f <- function # title    f <- function # title
545  ### description  ### description
546    (x, ##<< arg x    (x, ##<< arg x
# Line 536  Line 556 
556    src <- attr(f,"source")    src <- attr(f,"source")
557    lonely$extract.xxx.chunks(src)    lonely$extract.xxx.chunks(src)
558    lonely$prefixed.lines(src)    lonely$prefixed.lines(src)
559  }  })
560    
561  extra.code.docs <- function # Extract documentation from code chunks  extra.code.docs <- function # Extract documentation from code chunks
562  ### Parse R code to extract inline documentation from comments around  ### Parse R code to extract inline documentation from comments around
# Line 572  Line 592 
592            # as.data.frame.matrix must be split into: m1 = as.data.frame and            # as.data.frame.matrix must be split into: m1 = as.data.frame and
593            # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!            # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!
594                    pattern <- "^([^\\.]+)\\.(.*)$"                    pattern <- "^([^\\.]+)\\.(.*)$"
595            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),            doc$.s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),
596                m2 <- gsub(pattern,"\\2",on,perl=TRUE))                m2 <- gsub(pattern,"\\2",on,perl=TRUE))
597            if ( grepl("\\W",m1,perl=TRUE) ){            if ( grepl("\\W",m1,perl=TRUE) ){
598                            m1 <- paste("`",m1,"`",sep="")                            m1 <- paste("`",m1,"`",sep="")
# Line 864  Line 884 
884    
885    docs <- list()    docs <- list()
886    
   # PhG: Automatically determine who is S3 methods  
   for (name in names(objs)) {  
     parts <- strsplit(name, ".", fixed = TRUE)[[1]]  
         l <- length(parts)  
         if (l > 1) {  
         for (i in 1:(l - 1)) {  
             # Look for a generic function (known by the system or defined  
             # in the package) that matches that part of the function name  
             generic <- paste(parts[1:i], collapse = ".")  
             if (any(generic %in% utils:::getKnownS3generics()) ||  
                 utils:::findGeneric(generic, e) != "") {  
                 object <- paste(parts[(i + 1):l], collapse = ".")  
                 docs[[name]]$s3method <- c(generic, object)  
                 break  
             }  
         }  
     }  
   }  
887    ## apply parsers in sequence to code and objs    ## apply parsers in sequence to code and objs
888    for(i in seq_along(parsers)){    for(i in seq_along(parsers)){
889      N <- names(parsers[i])      N <- names(parsers[i])
# Line 892  Line 894 
894      }      }
895      p <- parsers[[i]]      p <- parsers[[i]]
896      ## This is the argument list that each parser receives:      ## This is the argument list that each parser receives:
897      L <- p(code=code,objs=objs,docs=docs,...)      L <- p(code=code,objs=objs,docs=docs,env=e,...)
898      docs <- combine(docs,L)      docs <- combine(docs,L)
899    }    }
900      ## post-process to collapse all character vectors
901      for(i in seq_along(docs)){
902        for(j in seq_along(docs[[i]])){
903          if(names(docs[[i]])[j]!=".s3method")
904          docs[[i]][[j]] <- paste(docs[[i]][[j]],collapse="\n")
905        }
906      }
907    if(verbose)cat("\n")    if(verbose)cat("\n")
908    docs    docs
909  ### A list of extracted documentation from code.  ### A list of extracted documentation from code.
# Line 915  Line 924 
924  ### Apply all parsers relevant to extract info from just 1 code file.  ### Apply all parsers relevant to extract info from just 1 code file.
925  (f,  (f,
926  ### File name of R code to read and parse.  ### File name of R code to read and parse.
927   parsers=nondesc.parsers,   parsers=NULL,
928  ### Parser Functions to use to parse the code and extract  ### Parser Functions to use to parse the code and extract
929  ### documentation.  ### documentation.
930   ...   ...
931  ### Other arguments to pass to Parser Functions.  ### Other arguments to pass to Parser Functions.
932   ){   ){
933      if(is.null(parsers))parsers <- nondesc.parsers
934    apply.parsers(readLines(f),parsers,verbose=FALSE,...)    apply.parsers(readLines(f),parsers,verbose=FALSE,...)
935  }  }

Legend:
Removed from v.195  
changed lines
  Added in v.204

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