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 204, Fri Feb 18 16:48:22 2011 UTC revision 246, Thu Apr 7 13:21:39 2011 UTC
# Line 445  Line 445 
445  ### with the string in this list (implemented in modify.Rd.file).  ### with the string in this list (implemented in modify.Rd.file).
446  }  }
447    
448    leadingS3generic <- function # check whether function name is an S3 generic
449    ### Determines whether a function name looks like an S3 generic function
450    (name,                     ##<< name of function
451     env,                      ##<< environment to search for additional generics
452     ...)                      ##<< ignored here
453    {
454      ##details<< This function is one of the default parsers, but exposed as
455      ## possibly of more general interest. Given a function name of the form
456      ## x.y.z it looks for the generic function x applying to objects of class
457      ## y.z and also for generic function x.y applying to objects of class z.
458      ##
459      parts <- strsplit(name, ".", fixed = TRUE)[[1]]
460      l <- length(parts)
461      if (l > 1) {
462        for (i in 1:(l - 1)) {
463          ## Look for a generic function (known by the system or defined
464          ## in the package) that matches that part of the function name
465          generic <- paste(parts[1:i], collapse = ".")
466          if (any(generic %in% utils:::getKnownS3generics()) ||
467              utils:::findGeneric(generic, env) != "") {
468            object <- paste(parts[(i + 1):l], collapse = ".")
469            ##details<< Assumes that the first name which matches any known
470            ## generics is the target generic function, so if both x and x.y
471            ## are generic functions, will assume generic x applying to objects
472            ## of class y.z
473            ##value<< If a matching generic found returns a list with a single component:
474            return(list(.s3method=c(generic, object))) ##<< a character vector containing generic name and object name.
475          }
476        }
477      }
478      ##value<< If no matching generic functions are found, returns an empty list.
479      list()
480    }
481    
482  ### Parsers for each function that are constructed automatically. This  ### Parsers for each function that are constructed automatically. This
483  ### is a named list, and each element is a parser function for an  ### is a named list, and each element is a parser function for an
484  ### individual object.  ### individual object.
# Line 519  Line 553 
553           L <- lapply(doc,paste,collapse="\n")           L <- lapply(doc,paste,collapse="\n")
554           L$.overwrite <- TRUE           L$.overwrite <- TRUE
555           L           L
556         },tag.s3methods=function(name,env,...){         },tag.s3methods=leadingS3generic
          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, env) != "") {  
                object <- paste(parts[(i + 1):l], collapse = ".")  
                return(list(.s3method=c(generic, object)))  
              }  
            }  
          }  
          list()  
        }  
557         )         )
558    
559  ### List of parser functions that operate on single objects. This list  ### List of parser functions that operate on single objects. This list
# Line 586  Line 604 
604          if(!"description"%in%names(doc) && !is.na(parsed[[on]]@description) ){          if(!"description"%in%names(doc) && !is.na(parsed[[on]]@description) ){
605            doc$description <- parsed[[on]]@description            doc$description <- parsed[[on]]@description
606          }          }
607          if ( "setMethodS3" == parsed[[on]]@created ){          ## if ( "setMethodS3" == parsed[[on]]@created ){
608            # PhG: this may be wrong! It does not catch correctly how the method          ##   gen <- leadingS3generic(on,topenv())
609            # must be splitted in case of methods containing dots. for instance,          ##   if ( 0 < length(gen) ){
610            # as.data.frame.matrix must be split into: m1 = as.data.frame and          ##     doc$.s3method <- gen$.s3method
611            # m2 = matrix... here you got m1 = as, and m2 = data.frame.matrix!!!          ##     cat("S3method(",gen$.s3method[1],",",gen$.s3method[2],")\n",sep="")
612                    pattern <- "^([^\\.]+)\\.(.*)$"          ##   }
613            doc$.s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),          ## }
               m2 <- gsub(pattern,"\\2",on,perl=TRUE))  
           if ( grepl("\\W",m1,perl=TRUE) ){  
                           m1 <- paste("`",m1,"`",sep="")  
           }  
           cat("S3method(",m1,",",m2,")\n",sep="")  
         }  
614        }        }
615        if("title" %in% names(doc) && !"description" %in% names(doc) ){        if("title" %in% names(doc) && !"description" %in% names(doc) ){
616          ## For short functions having both would duplicate, but a          ## For short functions having both would duplicate, but a
# Line 695  Line 707 
707  setClass("DocLink", # Link documentation among related functions  setClass("DocLink", # Link documentation among related functions
708  ### The \code{.DocLink} class provides the basis for hooking together  ### The \code{.DocLink} class provides the basis for hooking together
709  ### documentation of related classes/functions/objects. The aim is that  ### documentation of related classes/functions/objects. The aim is that
710  ### documentation sections missing from the child are  ### documentation sections missing from the child are inherited from
711    ### the parent class.
712           representation(name="character", ##<< name of object           representation(name="character", ##<< name of object
713                          created="character", ##<< how created                          created="character", ##<< how created
714                          parent="character", ##<< parent class or NA                          parent="character", ##<< parent class or NA
# Line 872  Line 885 
885  ### Additional arguments to pass to Parser Functions.  ### Additional arguments to pass to Parser Functions.
886   ){   ){
887    e <- new.env()    e <- new.env()
888    old <- options(keep.source=TRUE)    ## KMP 2011-03-09 fix problem with DocLink when inlinedocs ran on itself
889      ## Error in assignClassDef(Class, classDef, where) :
890      ##   Class "DocLink" has a locked definition in package "inlinedocs"
891      ## Traced to "where" argument in setClassDef which defaults to topenv()
892      ## which in turn is inlinedocs when processing inlinedocs package, hence
893      ## the clash. The following works (under R 2.12.2), so that the topenv()
894      ## now finds e before finding the inlinedocs environment.
895      old <- options(keep.source=TRUE,topLevelEnvironment=e)
896    on.exit(options(old))    on.exit(options(old))
897    exprs <- parse(text=code)    exprs <- parse(text=code)
898    for (i in exprs){    for (i in exprs){
# Line 920  Line 940 
940  ### description file.  ### description file.
941  nondesc.parsers <- default.parsers[non.descfile.names]  nondesc.parsers <- default.parsers[non.descfile.names]
942    
943  extract.docs.file <- function  extract.docs.file <- structure(function
944  ### Apply all parsers relevant to extract info from just 1 code file.  ### Apply all parsers relevant to extract info from just 1 code file.
945  (f,  (f,
946  ### File name of R code to read and parse.  ### File name of R code to read and parse.
# Line 932  Line 952 
952   ){   ){
953    if(is.null(parsers))parsers <- nondesc.parsers    if(is.null(parsers))parsers <- nondesc.parsers
954    apply.parsers(readLines(f),parsers,verbose=FALSE,...)    apply.parsers(readLines(f),parsers,verbose=FALSE,...)
955  }  },ex=function(){
956      f <- system.file("silly","R","silly.R",package="inlinedocs")
957      extract.docs.file(f) ##FIXME: warning
958    })

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

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