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 393, Tue Oct 1 07:15:26 2013 UTC revision 394, Mon Dec 23 15:36:20 2013 UTC
# Line 1  Line 1 
1    #
2    # vim:set ff=unix expandtab ts=2 sw=2:
3  do.not.generate <- structure(function  do.not.generate <- structure(function
4  ### Make a Parser Function used to indicate that certain Rd files  ### Make a Parser Function used to indicate that certain Rd files
5  ### should not be generated.  ### should not be generated.
# Line 166  Line 168 
168  ### For each function in the package, do something.  ### For each function in the package, do something.
169  forfun <- function(FUN)forall(FUN,is.function)  forfun <- function(FUN)forall(FUN,is.function)
170    
171    forGeneric<- function(
172      FUN  ### Function to apply to each method in the package (usually FUN is a parser)
173      ,
174      env
175      ,
176      gens
177      ){
178        #pe(quote(getwd()),environment())
179      force(FUN)
180            f <- function(objs,docs,...){
181        genericFuncNames=names(gens)
182              L <- list()
183        for(genName in genericFuncNames){
184          fg=gens[[genName]]
185          meths=findMethods(fg,where=env)
186          signatureStrings=names(meths)
187          pp("signatureStrings",environment())
188                on.exit(cat(sprintf("Parser Function failed on %s\n",N)))
189          for ( sig in signatureStrings){
190                  method <- meths[[sig]]
191            src=getSource(method)
192            N <- paste(genName,"-method-#",sig,sep="")
193            L[[N]]  <-  FUN(src=src,objs=meths,name=N,...)
194          }
195                on.exit()## remove warning message
196        }
197        L
198      }
199      f
200    }
201    
202  kill.prefix.whitespace <- function  kill.prefix.whitespace <- function
203  ### Figure out what the whitespace preceding the example code is, and  ### Figure out what the whitespace preceding the example code is, and
204  ### then delete that from every line.  ### then delete that from every line.
# Line 524  Line 557 
557    list()    list()
558  }  }
559    
560  ### Parsers for each function that are constructed automatically. This  definition.from.source=function(doc,src,...){
561  ### is a named list, and each element is a parser function for an    def <- doc$definition
562  ### individual object.    is.empty <- function(x)is.null(x)||x==""
563  forfun.parsers <-    if(is.empty(def) && !is.empty(src))
564    list(prefixed.lines=prefixed.lines,      list(definition=src)
565         extract.xxx.chunks=extract.xxx.chunks,    else list()
566    }
567         ## title from first line of function def         ## title from first line of function def
568         title.from.firstline=function(src,...){         title.from.firstline=function(src,...){
569           first <- src[1]           first <- src[1]
570           if(!is.character(first))return(list())           if(!is.character(first))return(list())
571           if(!grepl("#",first))return(list())           if(!grepl("#",first))return(list())
572           list(title=gsub("[^#]*#\\s*(.*)","\\1",first,perl=TRUE))           list(title=gsub("[^#]*#\\s*(.*)","\\1",first,perl=TRUE))
573         },  }
574    ############
575    mm.examples.from.testfile=function(name,...){
576      pp("name",environment())
577      tsubdir <- getOption("inlinedocs.exampleDir")
578      trunk<- getOption("inlinedocs.exampleRegExpression")
579      if (is.null(tsubdir)) return(list())# do nothing
580      #pe(quote(getwd()),environment())
581      #pp("tsubdir",environment())
582      p <- paste(trunk,name,"\\.R$",sep="")
583      #pp("p",environment())
584      allfiles=dir(tsubdir)
585      #pp("allfiles",environment())
586      L<- allfiles[grepl(pattern=p,allfiles)]
587      #pp("L",environment())
588      path=function(l){file.path(tsubdir,l)}
589      paths=lapply(L,path)
590      print(lapply(paths,file.exists))
591    
592      if(length(L)>0){
593        exampleTexts= lapply(paths,readLines)
594        #pp("exampleTexts",environment())
595        combinedText <- unlist(exampleTexts)
596    
597          return(list(examples=combinedText))
598          #pp("combinedTexts",environment())
599      }
600      else{
601        list()
602      }
603    }
604    ### Parsers for each function that are constructed automatically. This
605    ### is a named list, and each element is a parser function for an
606    ### individual object.
607    forfun.parsers <-
608      list(prefixed.lines=prefixed.lines,
609           extract.xxx.chunks=extract.xxx.chunks,
610           title.from.firstline=title.from.firstline,
611         ## PhG: it is tests/FUN.R!!! I would like more flexibility here         ## PhG: it is tests/FUN.R!!! I would like more flexibility here
612         ## please, let me choose which dir to use for examples!         ## please, let me choose which dir to use for examples!
613         ## Get examples for FUN from the file tests/FUN.R         ## Get examples for FUN from the file tests/FUN.R
# Line 544  Line 615 
615           tsubdir <- getOption("inlinedocs.exdir")           tsubdir <- getOption("inlinedocs.exdir")
616           if (is.null(tsubdir)) tsubdir <- "tests"       # Default value           if (is.null(tsubdir)) tsubdir <- "tests"       # Default value
617           tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))           tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))
618           if(file.exists(tfile))           print(file.exists(tfile))
619             if(file.exists(tfile)){
620             list(examples=readLines(tfile))             list(examples=readLines(tfile))
621             }
622           else list()           else list()
623         },         },
624         definition.from.source=function(doc,src,...){         mm.examples.from.testfile=mm.examples.from.testfile,
625           def <- doc$definition         definition.from.source=definition.from.source
          is.empty <- function(x)is.null(x)||x==""  
          if(is.empty(def) && !is.empty(src))  
            list(definition=src)  
          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[,"Author"])  
        },  
        ## 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 <- getSource(ex)  
              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))]  
              if( length(ex) ){  # avoid error on yet empty example  
                  if(ex[1]=="{")ex <- ex[-1]  
                  ## all the prefixes  
                  ex <- kill.prefix.whitespace(ex)  
              }  
              ## Add an empty line before and after example  
              ex <- c("", ex, "")  
            }  
            list(examples = ex)  
          } else list()  
        },collapse=function(doc,...){  
          L <- lapply(doc,paste,collapse="\n")  
          L$.overwrite <- TRUE  
          L  
        },tag.s3methods=leadingS3generic  
626         )         )
627    
628  ### List of parser functions that operate on single objects. This list  extract.docs<-function(parsed,objs,on){
629  ### is useful for testing these functions.    #pp("on",environment())
 lonely <- structure(c(forall.parsers,forfun.parsers),ex=function(){  
   f <- function # title  
 ### description  
   (x, ##<< arg x  
    y  
 ### arg y  
    ){  
     ##value<< a list with elements  
     list(x=x, ##<< original x value  
          y=y, ##<< original y value  
          sum=x+y) ##<< their sum  
     ##end<<  
   }  
   src <- getSource(f)  
   lonely$extract.xxx.chunks(src)  
   lonely$prefixed.lines(src)  
 })  
   
 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)  
630    extract.docs.try <- function(o,on)    extract.docs.try <- function(o,on)
631      {      {
632        ## Note: we could use parsed information here too, but that        ## Note: we could use parsed information here too, but that
# Line 667  Line 655 
655        }        }
656        doc        doc
657      }      }
   extract.docs <- function(on){  
658      res <- try({o <- objs[[on]]      res <- try({o <- objs[[on]]
659                  extract.docs.try(o, on)},FALSE)                  extract.docs.try(o, on)},FALSE)
660      if(class(res)=="try-error"){      if(class(res)=="try-error"){
# Line 684  Line 671 
671        NULL        NULL
672      } else res      } else res
673    }    }
674    doc.names <- names(objs)  
675    res <- sapply(doc.names,extract.docs,simplify=FALSE)  inherit.docs <- function(
676    ## Special processing for S4 classes as they do not appear in normal ls()    parsed, ##<< a list of doc.link objects
677    for ( nn in names(parsed) ){    res,    ##<< the list of documentation to be extended
678      if ( parsed[[nn]]@created == "setClass" ){    childName      ##<< the name of the object who possibly inherits
679        S4class.docs <- extract.docs.setClass(parsed[[nn]])    ){
680        docname <- paste(nn,"class",sep="-")    in.res <- res[[childName]] #start with the present
681        if ( is.null(res[[docname]]) ){    #pp("in.res",environment())
682          res[[docname]] <- S4class.docs    childsDocLink <-parsed[[childName]]
683          doc.names <- c(doc.names,docname)    if ( !is.null(childsDocLink) ){
684        } else {      for ( parent in childsDocLink@parent ){
         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 ){  
685          if ( !is.na(parent) ){          if ( !is.na(parent) ){
686            #pp("parent",environment())
687            #pe(quote(names(res)),environment())
688            #pe(quote(parent %in% names(res)),environment())
689            if ( is.null(in.res) ){            if ( is.null(in.res) ){
690              in.res <- res[[parent]]              in.res <- res[[parent]]
691            } else if ( parent %in% names(res) ){            } else if ( parent %in% names(res) ){
# Line 718  Line 700 
700        }        }
701      }      }
702      invisible(in.res)      invisible(in.res)
703      ### the possibly extended list of documentation
704    }
705    extra.class.docs <- function # Extract documentation from code chunks
706    ### Parse R code to extract inline documentation from comments around
707    ### each class
708    ### looking at the "source" attribute. This is a Parser Function that
709    ### can be used in the parser list of package.skeleton.dx(). TODO:
710    (code,
711    ### Code lines in a character vector containing multiple R objects to
712    ### parse for documentation.
713    objs,
714    ### The objects defined in the code.
715    env,
716    ### The environment they inhibit (needed to pass on)
717    ...
718    ### ignored
719     ){
720      doc.names <- names(objs)
721      parsed <- extract.file.parse(code,env)
722      res=list()
723      for ( nn in names(parsed) ){
724        if ( parsed[[nn]]@created == "setClass" ){
725          S4class.docs <- extract.docs.setClass(parsed[[nn]])
726          docname <- paste(nn,"class",sep="-")
727          if ( is.null(res[[docname]]) ){
728            res[[docname]] <- S4class.docs
729            doc.names <- c(doc.names,docname)
730          } else {
731            stop(nn," appears as both S4 class and some other definition")
732          }
733        }
734      }
735      all.done <- FALSE
736      while ( !all.done ){
737        res1 <- sapply(doc.names,inherit.docs,parsed=parsed,res=res,simplify=FALSE)
738        all.done <- identical(res1,res)
739        res <- res1
740      }
741      res
742    ### named list of lists, one for each object to document.
743    }    }
744    extra.code.docs <- function # Extract documentation from code chunks
745    ### Parse R code to extract inline documentation from comments around
746    ### each function. These are not able to be retreived simply by
747    ### looking at the "source" attribute. This is a Parser Function that
748    ### can be used in the parser list of package.skeleton.dx(). TODO:
749    ### Modularize this into separate Parsers Functions for S4 classes,
750    ### prefixes, ##<<blocks, etc. Right now it is not very clean!
751    (code,
752    ### Code lines in a character vector containing multiple R objects to
753    ### parse for documentation.
754     objs,
755    ### The objects defined in the code.
756    env, # the environment
757     ...
758    ### ignored
759     ){
760      parsed <- extract.file.parse(code,env)
761      doc.names <- names(objs)
762      res <- sapply(doc.names,extract.docs,parsed=parsed,objs=objs,simplify=FALSE)
763    all.done <- FALSE    all.done <- FALSE
764    while ( !all.done ){    while ( !all.done ){
765      res1 <- sapply(doc.names,inherit.docs,simplify=FALSE)      res1 <- sapply(doc.names,inherit.docs,parsed=parsed,res=res,simplify=FALSE)
766      all.done <- identical(res1,res)      all.done <- identical(res1,res)
767      res <- res1      res <- res1
768    }    }
# Line 733  Line 774 
774    res    res
775  ### named list of lists, one for each object to document.  ### named list of lists, one for each object to document.
776  }  }
777    forMethod.parsers<-
778      list(
779        prefixed.lines=prefixed.lines,
780        extract.xxx.chunks=extract.xxx.chunks,
781        title.from.firstline=title.from.firstline,
782        mm.examples.from.testfile
783      )
784    ### List of Parser Functions that can be applied to any object.
785    forall.parsers <-
786      list(## Fill in author from DESCRIPTION and titles.
787           author.from.description=function(desc,...){
788             list(author=desc[,"Author"])
789           },
790           ## The format section sometimes causes problems, so erase it.
791           erase.format=function(...){
792             list(format="")
793           },
794           ## Convert the function name to a title.
795           title.from.name=function(name,doc,...){
796             if("title"%in%names(doc))list() else
797             list(title=gsub("[._]"," ",name))
798           },
799           ## PhG: here is what I propose for examples code in the 'ex' attribute
800           examples.in.attr =  function (name, o, ...) {
801             ex <- attr(o, "ex")
802             if (!is.null(ex)) {
803               ## Special case for code contained in a function
804               if (inherits(ex, "function")) {
805                 ## If source is available, start from there
806                 src <- getSource(ex)
807                 if (!is.null(src)) {
808                   ex <- src
809                 } else { ## Use the body of the function
810                   ex <- deparse(body(ex))
811                 }
812                 ## Eliminate leading and trailing code
813                 ex <- ex[-c(1, length(ex))]
814                 if( length(ex) ){  # avoid error on yet empty example
815                     if(ex[1]=="{")ex <- ex[-1]
816                     ## all the prefixes
817                     ex <- kill.prefix.whitespace(ex)
818                 }
819                 ## Add an empty line before and after example
820                 ex <- c("", ex, "")
821               }
822               list(examples = ex)
823             } else list()
824           },collapse=function(doc,...){
825             L <- lapply(doc,paste,collapse="\n")
826             L$.overwrite <- TRUE
827             L
828           },tag.s3methods=leadingS3generic
829           )
830    
831    ### List of parser functions that operate on single objects. This list
832    ### is useful for testing these functions.
833    lonely <- structure(c(forall.parsers,forfun.parsers),ex=function(){
834      f <- function # title
835    ### description
836      (x, ##<< arg x
837       y
838    ### arg y
839       ){
840        ##value<< a list with elements
841        list(x=x, ##<< original x value
842             y=y, ##<< original y value
843             sum=x+y) ##<< their sum
844        ##end<<
845      }
846      src <- getSource(f)
847      lonely$extract.xxx.chunks(src)
848      lonely$prefixed.lines(src)
849    })
850    
851    
852  ### List of parsers to use by default with package.skeleton.dx.  ### List of parsers to use by default with package.skeleton.dx.
853  default.parsers <-  default.parsers <-
854    c(extra.code.docs=extra.code.docs, ## TODO: cleanup!    c(
855        extra.code.docs=extra.code.docs, ## TODO: cleanup!
856        extra.class.docs=extra.class.docs, ## TODO: cleanup!
857      sapply(forfun.parsers,forfun),      sapply(forfun.parsers,forfun),
858      edit.package.file=function(desc,...){      edit.package.file=function(desc,...){
859        in.details <- setdiff(colnames(desc),"Description")        in.details <- setdiff(colnames(desc),"Description")
# Line 767  Line 884 
884  ### Using the base \code{parse} function, analyse the file to link  ### Using the base \code{parse} function, analyse the file to link
885  ### preceding "prefix" comments to each active chunk. Those comments form  ### preceding "prefix" comments to each active chunk. Those comments form
886  ### the default description for that chunk. The analysis also looks for  ### the default description for that chunk. The analysis also looks for
887  ### S4 class "setClass" calls and R.oo setConstructorS3 and R.methodsS3::setMethodS3  ### S4 class "setClass" ,R.oo setConstructorS3  R.methodsS3::setMethodS3
888  ### calls in order to link the documentation of those properly.  ### or S4 setMethod calls in order to link the documentation of those properly.
889  (code  (code,
890  ### Lines of R source code in a character vector - note that any  ### Lines of R source code in a character vector - note that any
891  ### nested \code{source} statements are \emph{ignored} when scanning  ### nested \code{source} statements are \emph{ignored} when scanning
892  ### for class definitions.  ### for class definitions.
893     env
894     ### the environment in which the code has been evaluated before.
895     ### This is e.g. iportant to make sure that we can evaluate expressions
896     ### like signature definitions for methods
897   ){   ){
898    res <- list()    res <- list()
899    old.opt <- options(keep.source=TRUE)    old.opt <- options(keep.source=TRUE)
# Line 802  Line 923 
923      ## determined by expression type: \describe{      ## determined by expression type: \describe{
924      ## \item{assignment (<-)}{Ordinary assignment of value/function;}      ## \item{assignment (<-)}{Ordinary assignment of value/function;}
925      ## \item{setClass}{Definition of S4 class;}      ## \item{setClass}{Definition of S4 class;}
926        ## \item{setMethod}{Definition of a method of a S4 generic;}
927      ## \item{setConstructorS3}{Definition of S3 class using R.oo package;}      ## \item{setConstructorS3}{Definition of S3 class using R.oo package;}
928      ## \item{R.methodsS3::setMethodS3}{Definition of method for S3 class using R.oo package.}}      ## \item{R.methodsS3::setMethodS3}{Definition of method for S3 class using R.oo package.}}
929      ## Additionally, the value may be a name of a function defined elsewhere,      ## Additionally, the value may be a name of a function defined elsewhere,
# Line 812  Line 934 
934      expr.type <- chars[1]      expr.type <- chars[1]
935      parent <- NA_character_      parent <- NA_character_
936    
937      if ( expr.type == "<-" || expr.type == "setConstructorS3" || expr.type == "setClass" ){      if ( expr.type == "<-" || expr.type == "setConstructorS3" ){
938        object.name <- chars[2]        object.name <- chars[2]
939        ## If the function definition is not embedded within the call, then        ## If the function definition is not embedded within the call, then
940        ## the parent is that function. Test whether the the third value        ## the parent is that function. Test whether the the third value
# Line 825  Line 947 
947                                  parent=parent,                                  parent=parent,
948                                  code=paste(chunks[[k]],sep=""),                                  code=paste(chunks[[k]],sep=""),
949                                  description=default.description)                                  description=default.description)
950      } else if ( expr.type == "R.methodsS3::setMethodS3" || expr.type ==  "R.methodsS3::R.methodsS3::setMethodS3"){      } else if ( expr.type == "setClass" ){
951          object.name <- chars[2]
952          res[[object.name]] <- new("DocLink",name=object.name,
953                                    created=expr.type,
954                                    parent=parent,
955                                    code=paste(chunks[[k]],sep=""),
956                                    description=default.description)
957    
958        }
959        else if ( expr.type == "R.methodsS3::setMethodS3" || expr.type ==  "R.methodsS3::R.methodsS3::setMethodS3"){
960        ##details<< The \code{R.methodsS3::setMethodS3} calls introduce additional        ##details<< The \code{R.methodsS3::setMethodS3} calls introduce additional
961        ## complexity: they will define an additional S3 generic (which        ## complexity: they will define an additional S3 generic (which
962        ## needs documentation to avoid warnings at package build time)        ## needs documentation to avoid warnings at package build time)
963        ## unless one already exists. This also is handled by "linking"        ## unless one already exists. This also is handled by "linking"
964        ## documentation. A previously unseen generic is linked to the        ## documentation. A previously unseen S3generic is linked to the
965        ## first defining instances, subsequent definitions of that generic        ## first defining instances, subsequent definitions of that S3generic
966        ## also link back to the first defining instance.        ## also link back to the first defining instance.
967        generic.name <- chars[2]        S3generic.name <- chars[2]
968        object.name <- paste(generic.name,chars[3],sep=".")        object.name <- paste(S3generic.name,chars[3],sep=".")
969        if ( is.null(res[[generic.name]]) ){        if ( is.null(res[[S3generic.name]]) ){
970          ## TDH 9 April 2012 Do NOT add \\link in generic.desc below,          ## TDH 9 April 2012 Do NOT add \\link in S3generic.desc below,
971          ## since it causes problems on R CMD check.          ## since it causes problems on R CMD check.
972          ##* checking Rd cross-references ... WARNING          ##* checking Rd cross-references ... WARNING
973          ##Error in find.package(package, lib.loc) :          ##Error in find.package(package, lib.loc) :
974          ##  there is no package called ‘MASS’          ##  there is no package called ‘MASS’
975          ##Calls: <Anonymous> -> lapply -> FUN -> find.package          ##Calls: <Anonymous> -> lapply -> FUN -> find.package
976    
977          generic.desc <-          S3generic.desc <-
978            paste("Generic method behind \\code{",object.name,"}",sep="")            paste("Generic method behind \\code{",object.name,"}",sep="")
979          res[[generic.name]] <- new("DocLink",          res[[S3generic.name]] <- new("DocLink",
980                                     name=generic.name,                                     name=S3generic.name,
981                                     created=expr.type,                                     created=expr.type,
982                                     parent=object.name,                                     parent=object.name,
983                                     code=NA_character_,                                     code=NA_character_,
984                                     description=generic.desc)                                     description=S3generic.desc)
985        } else {        } else {
986          parent <- res[[generic.name]]@parent          parent <- res[[S3generic.name]]@parent
987        }        }
988        ## If the function definition is not embedded within the call, then        ## If the function definition is not embedded within the call, then
989        ## the parent is that function. Test whether the the fourth value        ## the parent is that function. Test whether the the fourth value
# Line 865  Line 996 
996                                  parent=parent,                                  parent=parent,
997                                  code=paste(chunks[[k]],sep=""),                                  code=paste(chunks[[k]],sep=""),
998                                  description=default.description)                                  description=default.description)
999        } else if (expr.type == "setMethod" ) {
1000          pp("lang",environment())
1001          pp("chars",environment())
1002    
1003          ## Since we do not know if the arguments in the call to setMethod are given with
1004          ## keywords, partially matching keywords as an ordered list ore any
1005          ## combination of it, we use the same function as R  (match.arg )
1006          ## to rewrite our argumentlist to a (pair)list from which
1007          ## we can extract the information easily
1008          KeyWords=c("f","signature","definition","where")
1009          NamedArgs=list() # the new argument list
1010          args=lang[2:length(lang)]
1011          argNames=names(args)
1012          pp("args",environment())
1013          pp("argNames",environment())
1014          for (i in seq_along(lang[2:length(lang)])){
1015             argName=argNames[[i]]
1016             if(argNames[[i]]==""){ # no keyword=value given for this arg
1017               NamedArgs[[KeyWords[[i]]]] <- args[[i]] #determining the keyword  by position
1018      } else {      } else {
1019        ## Not sure what to do with these yet. Need to deal with setMethod, setAs etc.            newName=try(match.arg(argNames[[i]],KeyWords))
1020              if (class(newName)=="try-error") {
1021                stop(paste("could not match the argument with name : " ,argNames[[i]]," to a formal argument of setMethod",sep=""))
1022              }else{
1023               NamedArgs[[newName]] <- args[[i]]
1024             }
1025            }
1026          }
1027          pp("NamedArgs",environment())
1028          genName=NamedArgs[["f"]]
1029          sigexp=NamedArgs[["signature"]]
1030          pp("sigexp",environment())
1031          sig=eval(sigexp,env)
1032          pp("sig",environment())
1033          sigString <- paste(sig,collapse="#")
1034          N=paste(genName,"-method-#",sigString,sep="")
1035          object.name <- N
1036          pp("object.name",environment())
1037    
1038          ## If the function definition is not embedded within the call, then
1039          ## the parent is that function. Test whether the value for "definition"
1040          ## looks like a funktion name and add it to parents if so.
1041          def=paste(as.character(NamedArgs[["definition"]]),collapse="\n")
1042          if ( grepl("^[\\._\\w]+$",def,perl=TRUE) ){
1043            parent <- def
1044          }
1045          res[[object.name]] <- new("DocLink",name=object.name,
1046                                    created=expr.type,
1047                                    parent=parent,
1048                                    code=paste(chunks[[k]],sep=""),
1049                                    description=default.description)
1050        }else {
1051          ## Not sure what to do with these yet. Need to deal with setAs etc.
1052      }      }
1053    }    }
1054    invisible(res)    invisible(res)
# Line 932  Line 1114 
1114    }    }
1115    invisible(docs)    invisible(docs)
1116  }  }
1117    createObjects <- function(code){
1118  apply.parsers <- function    # this is factored out to make writing tests easier
1119  ### Parse code to r objs, then run all the parsers and return the    # since we often need the objects and the environment
1120  ### documentation list.    # they inhabit
 (code,  
 ### Character vector of code lines.  
  parsers=default.parsers,  
 ### List of Parser Functions.  
  verbose=FALSE,  
 ### Echo names of Parser Functions?  
  ...  
 ### Additional arguments to pass to Parser Functions.  
  ){  
1121    e <- new.env()    e <- new.env()
1122    ## KMP 2011-03-09 fix problem with DocLink when inlinedocs ran on itself    ## KMP 2011-03-09 fix problem with DocLink when inlinedocs ran on itself
1123    ## Error in assignClassDef(Class, classDef, where) :    ## Error in assignClassDef(Class, classDef, where) :
# Line 962  Line 1135 
1135    for (i in exprs){    for (i in exprs){
1136        eval(i, e)        eval(i, e)
1137    }    }
1138    objs <- sapply(ls(e),get,e,simplify=FALSE)    objs <- sapply(ls(e),get,e,simplify=FALSE) # note that ls will not find S4 classes nor methods for generic functions
1139      list(objs=objs,env=e,exprs=exprs)
1140    }
1141    
1142    
1143    apply.parsers<- function
1144    ### Parse code to r objs, then run all the parsers and return the
1145    ### documentation list.
1146    (code,
1147    ### Character vector of code lines.
1148     parsers=default.parsers,
1149    ### List of Parser Functions.
1150     verbose=FALSE,
1151    ### Echo names of Parser Functions?
1152     ...
1153    ### Additional arguments to pass to Parser Functions.
1154     ){
1155    #  #####################################
1156    #  e <- new.env()
1157    #  ## KMP 2011-03-09 fix problem with DocLink when inlinedocs ran on itself
1158    #  ## Error in assignClassDef(Class, classDef, where) :
1159    #  ##   Class "DocLink" has a locked definition in package "inlinedocs"
1160    #  ## Traced to "where" argument in setClassDef which defaults to topenv()
1161    #  ## which in turn is inlinedocs when processing inlinedocs package, hence
1162    #  ## the clash. The following works (under R 2.12.2), so that the topenv()
1163    #  ## now finds e before finding the inlinedocs environment.
1164    #  old <- options(keep.source=TRUE,topLevelEnvironment=e)
1165    #  on.exit(options(old))
1166    #  exprs <- parse(text=code)
1167    #  ## TDH 2011-04-07 set this so that no warnings about creating a fake
1168    #  ## package when we try to process S4 classes defined in code
1169    #  e$.packageName <- "inlinedocs.processor"
1170    #  for (i in exprs){
1171    #      eval(i, e)
1172    #  }
1173    #  objs <- sapply(ls(e),get,e,simplify=FALSE) # note that ls will not find S4 classes nor methods for generic functions
1174      l=createObjects(code)
1175      objs=l[["objs"]]
1176      e=l[["env"]]
1177      exprs=l[["exprs"]]
1178      # since th method definitions do not appear in ls() so are not represented in obs
1179      # so we have to find them in the parsed code
1180      glo=list()
1181      for ( k in 1:length(exprs)){
1182        lang <- exprs[[k]]
1183        chars <- as.character(lang)
1184        #pp("chars",environment())
1185        expr.type <- chars[[1]]
1186        object.name <- chars[[2]]
1187        if (expr.type == "setMethod"){glo=c(glo,object.name)}
1188      }
1189      gloFuncs=unlist(sapply(glo,getGeneric,where=e))
1190      #####################################
1191    docs <- list()    docs <- list()
1192      # now find generic Functions that are defined in the code
1193      # since those lead to entries in objs we can find them
1194      pe(quote(length(names(objs))),environment())
1195      if (length(names(objs))!=0){
1196        definedGenerics=objs[sapply(names(objs),isGeneric,e)]
1197      }else{
1198        definedGenerics=list()
1199      }
1200      #gens=unique(c(definedGenerics,gloFuncs))
1201      gens=definedGenerics
1202      #gens=gloFuncs
1203      # gens=glo
1204      pp("gens",environment())
1205    
1206    
1207    ## apply parsers in sequence to code and objs    ## apply parsers in sequence to code and objs
1208    if(verbose)cat("Applying parsers:\n")    if(verbose)cat("Applying parsers:\n")
1209    for(i in seq_along(parsers)){    for(i in seq_along(parsers)){
1210      N <- names(parsers[i])      N <- names(parsers[i])
1211      #mm if(verbose){      if(verbose){
1212        if(is.character(N) && N!=""){        if(is.character(N) && N!=""){
1213          cat(" this is parser:",N,"\n",sep="")          cat(" this is parser:",N,"\n",sep="")
1214        }else cat('.\n')        }else cat('.\n')
1215      #mm }      }
1216      p <- parsers[[i]]      p <- parsers[[i]]
1217      ## This is the argument list that each parser receives:      ## This is the argument list that each parser receives:
1218      L <- p(code=code,objs=objs,docs=docs,env=e,...)      L <- p(code=code,objs=objs,docs=docs,env=e,...)
1219      # print("mm point1")      docs <- combine(docs,L)
     #save(docs,L,file="/home/mm/SoilR/scripts/docs_L.RData")  
     #print(paste(L,"\n"))  
     #if(N=="exclude")browser()  
     docs <- combine(docs,L) #mm  
1220    }    }
1221    ## post-process to collapse all character vectors    ## post-process to collapse all character vectors
1222    for(i in seq_along(docs)){    for(i in seq_along(docs)){
# Line 992  Line 1226 
1226      }      }
1227   }   }
1228    if(verbose)cat("\n")    if(verbose)cat("\n")
1229    return(docs)    ## mm I added a second parser loop here for my method parsers
1230      ## It would perhaps be possible to integrate the new parsers in the
1231      ## main loop above
1232      docs2 <- list()
1233      parsersForMethods=sapply(forMethod.parsers,forGeneric,env=e,gens=gens)
1234      for(i in seq_along(parsersForMethods)){
1235        N <- names(parsersForMethods[[i]])
1236        p <- parsersForMethods[[i]]
1237        cat(" this is parser:",N,"\n",sep="")
1238        L <- p(code=code,objs=objs,docs=docs2,env=e,...)
1239        #pp("L",environment())
1240        docs2 <- combine(docs2,L)
1241      }
1242    
1243      return(list(docs=combine(docs,docs2),env=e,objs=objs,gens=gens))
1244  ### A list of extracted documentation from code.  ### A list of extracted documentation from code.
1245  }  }
1246    
# Line 1018  Line 1266 
1266  ### Other arguments to pass to Parser Functions.  ### Other arguments to pass to Parser Functions.
1267   ){   ){
1268    if(is.null(parsers))parsers <- nondesc.parsers    if(is.null(parsers))parsers <- nondesc.parsers
1269    apply.parsers(readLines(f),parsers,verbose=FALSE,...)    apply.parsers(readLines(f),parsers,verbose=FALSE,...)[["docs"]]
1270  },ex=function(){  },ex=function(){
1271    f <- system.file("silly","R","silly.R",package="inlinedocs")    f <- system.file("silly","R","silly.R",package="inlinedocs")
1272    extract.docs.file(f)    extract.docs.file(f)

Legend:
Removed from v.393  
changed lines
  Added in v.394

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