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 196, Fri Feb 18 12:52:48 2011 UTC revision 209, Fri Feb 25 08:15:10 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             L <- lapply(doc,paste,collapse="\n")
520             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    
# Line 569  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 656  Line 679 
679  default.parsers <-  default.parsers <-
680    c(extra.code.docs=extra.code.docs, ## TODO: cleanup!    c(extra.code.docs=extra.code.docs, ## TODO: cleanup!
681      sapply(forfun.parsers,forfun),      sapply(forfun.parsers,forfun),
     sapply(forall.parsers,forall),  
682      edit.package.file=function(desc,...){      edit.package.file=function(desc,...){
683        in.details <- setdiff(colnames(desc),"Description")        in.details <- setdiff(colnames(desc),"Description")
684        details <- sprintf("%s: \\tab %s\\cr",in.details,desc[,in.details])        details <- sprintf("%s: \\tab %s\\cr",in.details,desc[,in.details])
# Line 666  Line 688 
688                    `tabular{ll}`=details))                    `tabular{ll}`=details))
689        names(L) <- paste(desc[,"Package"],"-package",sep="")        names(L) <- paste(desc[,"Package"],"-package",sep="")
690        L        L
691      }      },
692        sapply(forall.parsers,forall)
693      )      )
694    
695  setClass("DocLink", # Link documentation among related functions  setClass("DocLink", # Link documentation among related functions
# Line 861  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 889  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    ## post-process to collapse all character vectors
901    for(i in seq_along(docs)){    for(i in seq_along(docs)){
902      for(j in seq_along(docs[[i]])){      for(j in seq_along(docs[[i]])){
903          if(names(docs[[i]])[j]!=".s3method")
904        docs[[i]][[j]] <- paste(docs[[i]][[j]],collapse="\n")        docs[[i]][[j]] <- paste(docs[[i]][[j]],collapse="\n")
905      }      }
906    }    }
# Line 914  Line 920 
920  ### description file.  ### description file.
921  nondesc.parsers <- default.parsers[non.descfile.names]  nondesc.parsers <- default.parsers[non.descfile.names]
922    
923  extract.docs.file <- function  extract.docs.file <- structure(function
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  }  },ex=function(){
936      f <- system.file("silly","R","silly.R",package="inlinedocs")
937      extract.docs.file(f)
938    })

Legend:
Removed from v.196  
changed lines
  Added in v.209

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