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 163, Tue Nov 16 12:06:29 2010 UTC revision 191, Thu Feb 17 14:09:43 2011 UTC
# Line 8  Line 8 
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 <- !names(y)%in%names(x)
11    toup <- names(y)[names(y)%in%names(x)]    toup <- names(y)[!toadd]
12      if("doc"%in%names(x))return(x$doc)
13      if("doc"%in%names(y))return(y$doc)
14    x[names(y)[toadd]] <- y[toadd]    x[names(y)[toadd]] <- y[toadd]
15    for(up in toup)x[[up]] <- combine(x[[up]],y[[up]])    for(up in toup)x[[up]] <- combine(x[[up]],y[[up]])
16    return(x)    return(x)
# Line 24  Line 26 
26  (comments  (comments
27  ### Character vector of prefixed comment lines.  ### Character vector of prefixed comment lines.
28   ){   ){
29    paste(gsub(prefix,"",comments),collapse="\n")    gsub(prefix,"",comments)
30  ### String without prefixes or newlines.  ### String without prefixes or newlines.
31  }  }
32    
# Line 132  Line 134 
134    ## Add an empty line before and after example    ## Add an empty line before and after example
135    ex <- c("", ex, "")    ex <- c("", ex, "")
136    ## Return examples and value    ## Return examples and value
137    list(examples = paste(ex, collapse = "\n"), value = value)    list(examples = ex, value = value)
138  }  }
139    
140  prefixed.lines <- function(src,...){  prefixed.lines <- structure(function(src,...){
141  ### The primary mechanism of inline documentation is via consecutive  ### The primary mechanism of inline documentation is via consecutive
142  ### groups of lines matching the specified prefix regular expression  ### groups of lines matching the specified prefix regular expression
143  ### "\code{^### }" (i.e. lines beginning with "\code{### }") are  ### "\code{^### }" (i.e. lines beginning with "\code{### }") are
# Line 155  Line 157 
157    f <- function(ch)cumsum(nchar(gsub(sprintf("[^%s]",ch),"",code)))    f <- function(ch)cumsum(nchar(gsub(sprintf("[^%s]",ch),"",code)))
158    parens <- f("(")-f(")")    parens <- f("(")-f(")")
159    body.begin <- which(diff(parens)<0 & parens[-1]==0)+2    body.begin <- which(diff(parens)<0 & parens[-1]==0)+2
160      if(length(body.begin)==0)body.begin <- 1 ## rare cases
161    is.arg <- function(){    is.arg <- function(){
162      gres <- grep("^\\s*#",src[start-1],perl=TRUE)      gres <- grep("^\\s*#",src[start-1],perl=TRUE)
163      0 == length(gres) && start<=body.begin      0 == length(gres) && start<=body.begin
# Line 163  Line 166 
166    for(i in seq_along(starts)){    for(i in seq_along(starts)){
167      start <- clines[starts[i]]      start <- clines[starts[i]]
168      end <- clines[ends[i]]      end <- clines[ends[i]]
169      lab <- if(end+1==length(src))"value"      lab <- if(all(grepl("^\\s*#",src[end:(length(src)-1)])))"value"
170      else if(start==2)"description"      else if(start==2)"description"
171      else if(is.arg()){      else if(is.arg()){
172        ##twutz: strip leading white spaces and brackets and ,        ##twutz: strip leading white spaces and brackets and ,
# Line 179  Line 182 
182      res[[lab]] <- decomment(src[start:end])      res[[lab]] <- decomment(src[start:end])
183    }    }
184    res    res
185  }  },ex=function(){
186    test <- function
187    ### the description
188    (x,
189    ### the first argument
190     y ##<< another argument
191     ){
192      5
193    ### the return value
194    ##seealso<< foobar
195    }
196    src <- attr(test,"source")
197    prefixed.lines(src)
198    extract.xxx.chunks(src)
199    })
200    
201  extract.xxx.chunks <- function # Extract documentation from a function  extract.xxx.chunks <- function # Extract documentation from a function
202  ### Given source code of a function, return a list describing inline  ### Given source code of a function, return a list describing inline
# Line 265  Line 282 
282          chunk.sep <- "\n\n"          chunk.sep <- "\n\n"
283        }        }
284        chunk.res <- NULL        chunk.res <- NULL
285        if ( 0 == length(grep("^\\s*$",payload,perl=TRUE)) )        if ( !grepl("^\\s*$",payload,perl=TRUE) )
286          chunk.res <-          chunk.res <-
287            if ( is.null(res[[field]]) ) payload            if ( is.null(res[[field]]) ) payload
288            else paste(res[[field]], payload, sep=chunk.sep)            else paste(res[[field]], payload, sep=chunk.sep)
# Line 273  Line 290 
290      }      }
291    while ( k <= length(src) ){    while ( k <= length(src) ){
292      line <- src[k]      line <- src[k]
293      if ( 0 < length(grep(extra.regexp,line,perl=TRUE) ) ){      if ( grepl(extra.regexp,line,perl=TRUE) ){
294        ## 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
295        new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)        new.field <- gsub(extra.regexp,"\\1",line,perl=TRUE)
296        new.contents <- gsub(extra.regexp,"\\2",line,perl=TRUE)        new.contents <- gsub(extra.regexp,"\\2",line,perl=TRUE)
# Line 348  Line 365 
365            first.describe <- TRUE;            first.describe <- TRUE;
366          }          }
367        }        }
368      } else if ( in.chunk && 0<length(grep(cont.re,line,perl=TRUE)) ){      } else if ( in.chunk && grepl(cont.re,line,perl=TRUE) ){
369        ## append this line to current chunk        ## append this line to current chunk
370        if ( 0 == length(grep(prefix,line,perl=TRUE)) ){        if ( !grepl(prefix,line,perl=TRUE) ){
371          ##describe<< Any lines with "\code{### }" at the left hand          ##describe<< Any lines with "\code{### }" at the left hand
372          ## margin within the included chunks are handled separately,          ## margin within the included chunks are handled separately,
373          ## so if they appear in the documentation they will appear          ## so if they appear in the documentation they will appear
# Line 363  Line 380 
380            payload <- stripped            payload <- stripped
381          }          }
382        }        }
383      } else if ( 0 < length(grep(arg.pat,line,perl=TRUE)) ){      } else if ( grepl(arg.pat,line,perl=TRUE) ){
384        not.describe <- (0==in.describe && !first.describe)        not.describe <- (0==in.describe && !first.describe)
385        if ( in.chunk && not.describe){        if ( in.chunk && not.describe){
386          res[[cur.field]] <- end.chunk(cur.field,payload)          res[[cur.field]] <- end.chunk(cur.field,payload)
# Line 433  Line 450 
450         examples.after.return=examples.after.return,         examples.after.return=examples.after.return,
451         extract.xxx.chunks=extract.xxx.chunks,         extract.xxx.chunks=extract.xxx.chunks,
452         ## title from first line of function def         ## title from first line of function def
453         title.from.firstline=function(src,name,code,...){         title.from.firstline=function(src,...){
454           if(length(grep("#",src[1]))){           if(length(src) && grepl("#",src[1])){
455             list(title=gsub("[^#]*#\\s*(.*)","\\1",src[1],perl=TRUE))             list(title=gsub("[^#]*#\\s*(.*)","\\1",src[1],perl=TRUE))
456           } else list()           } else list()
457         },         },
# Line 446  Line 463 
463           if (is.null(tsubdir)) tsubdir <- "tests"       # Default value           if (is.null(tsubdir)) tsubdir <- "tests"       # Default value
464           tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))           tfile <- file.path("..",tsubdir,paste(name,".R",sep=""))
465           if(file.exists(tfile))           if(file.exists(tfile))
466             list(examples=paste(readLines(tfile),collapse="\n"))             list(examples=readLines(tfile))
467           else list()           else list()
468         },         },
469         definition.from.source=function(doc,src,...){         definition.from.source=function(doc,src,...){
470           def <- doc$definition           def <- doc$definition
471           is.empty <- function(x)is.null(x)||x==""           is.empty <- function(x)is.null(x)||x==""
472           if(is.empty(def) && !is.empty(src))           if(is.empty(def) && !is.empty(src))
473             list(definition=paste(src,collapse="\n"))             list(definition=src)
474           else list()           else list()
475         })         })
476    
# Line 492  Line 509 
509               ## Add an empty line before and after example               ## Add an empty line before and after example
510               ex <- c("", ex, "")               ex <- c("", ex, "")
511             }             }
512             list(examples = paste(ex, collapse = "\n"))             list(examples = ex)
513           } else list()           } else list()
514           },
515           collapse.docs=function(doc,...){
516             list(doc=lapply(doc,paste,collapse="\n"))
517         })         })
518    
519  ### List of parser functions that operate on single objects. This list  ### List of parser functions that operate on single objects. This list
# Line 540  Line 560 
560        doc <- list()        doc <- list()
561        if ( !is.null(parsed[[on]]) ){        if ( !is.null(parsed[[on]]) ){
562          if ( !is.na(parsed[[on]]@code[1]) ){ # no code given for generics          if ( !is.na(parsed[[on]]@code[1]) ){ # no code given for generics
563            doc$definition <- paste(parsed[[on]]@code,collapse="\n")            doc$definition <- paste(parsed[[on]]@code)
564          }          }
565          if(!"description"%in%names(doc) && !is.na(parsed[[on]]@description) ){          if(!"description"%in%names(doc) && !is.na(parsed[[on]]@description) ){
566            doc$description <- parsed[[on]]@description            doc$description <- parsed[[on]]@description
# Line 553  Line 573 
573                    pattern <- "^([^\\.]+)\\.(.*)$"                    pattern <- "^([^\\.]+)\\.(.*)$"
574            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),            doc$s3method=c(m1 <- gsub(pattern,"\\1",on,perl=TRUE),
575                m2 <- gsub(pattern,"\\2",on,perl=TRUE))                m2 <- gsub(pattern,"\\2",on,perl=TRUE))
576            if ( 0 < length(grep("\\W",m1,perl=TRUE)) ){            if ( grepl("\\W",m1,perl=TRUE) ){
577                            m1 <- paste("`",m1,"`",sep="")                            m1 <- paste("`",m1,"`",sep="")
578            }            }
579            cat("S3method(",m1,",",m2,")\n",sep="")            cat("S3method(",m1,",",m2,")\n",sep="")
# Line 577  Line 597 
597        NULL        NULL
598      } else if(0 == length(res) && "function" %in% class(o)      } else if(0 == length(res) && "function" %in% class(o)
599                && 1 == length(osource <- attr(o,"source"))                && 1 == length(osource <- attr(o,"source"))
600                && 1 == length(grep(paste("UseMethod(",on,")",sep="\""),osource))                && grepl(paste("UseMethod(",on,")",sep="\""),osource)
601                ){                ){
602        ## phew - this should only pick up R.oo S3 generic definitions like:        ## phew - this should only pick up R.oo S3 generic definitions like:
603        ## attr(*, "source")= chr "function(...) UseMethod(\"select\")"        ## attr(*, "source")= chr "function(...) UseMethod(\"select\")"
# Line 638  Line 658 
658  default.parsers <-  default.parsers <-
659    c(extra.code.docs=extra.code.docs, ## TODO: cleanup!    c(extra.code.docs=extra.code.docs, ## TODO: cleanup!
660      sapply(forfun.parsers,forfun),      sapply(forfun.parsers,forfun),
     sapply(forall.parsers,forall),  
661      edit.package.file=function(desc,...){      edit.package.file=function(desc,...){
662        in.details <- setdiff(colnames(desc),"Description")        in.details <- setdiff(colnames(desc),"Description")
663        details <- paste(paste(in.details,": \\tab ",desc[,in.details],"\\cr",        details <- sprintf("%s: \\tab %s\\cr",in.details,desc[,in.details])
                              sep=""),collapse="\n")  
664        L <-        L <-
665          list(list(title=desc[,"Title"],          list(list(title=desc[,"Title"],
666                    description=desc[,"Description"],                    description=desc[,"Description"],
# Line 650  Line 668 
668                    author=desc[,"Maintainer"]))                    author=desc[,"Maintainer"]))
669        names(L) <- paste(desc[,"Package"],"-package",sep="")        names(L) <- paste(desc[,"Package"],"-package",sep="")
670        L        L
671      })      },
672        sapply(forall.parsers,forall)
673        )
674    
675  setClass("DocLink", # Link documentation among related functions  setClass("DocLink", # Link documentation among related functions
676  ### The \code{.DocLink} class provides the basis for hooking together  ### The \code{.DocLink} class provides the basis for hooking together
# Line 690  Line 710 
710      ## "prefix" lines will be used instead.      ## "prefix" lines will be used instead.
711      default.description <- NULL      default.description <- NULL
712      while ( start > last.end+1      while ( start > last.end+1
713             && 1 == length(grep(prefix,code[start-1],perl=TRUE)) ){             && grepl(prefix,code[start-1],perl=TRUE) ){
714        start <- start-1        start <- start-1
715      }      }
716      if ( start < chunks[[k]][1] ){      if ( start < chunks[[k]][1] ){
# Line 717  Line 737 
737        ## If the function definition is not embedded within the call, then        ## If the function definition is not embedded within the call, then
738        ## the parent is that function. Test whether the the third value        ## the parent is that function. Test whether the the third value
739        ## looks like a name and add it to parents if so.        ## looks like a name and add it to parents if so.
740        if ( 1 == length(grep("^[\\._\\w]+$",chars[3],perl=TRUE)) ){        if ( grepl("^[\\._\\w]+$",chars[3],perl=TRUE) ){
741          parent <- chars[3]          parent <- chars[3]
742        }        }
743        res[[object.name]] <- new("DocLink",name=object.name,        res[[object.name]] <- new("DocLink",name=object.name,
# Line 749  Line 769 
769        ## If the function definition is not embedded within the call, then        ## If the function definition is not embedded within the call, then
770        ## the parent is that function. Test whether the the fourth value        ## the parent is that function. Test whether the the fourth value
771        ## looks like a name and add it to parents if so.        ## looks like a name and add it to parents if so.
772        if ( 1 == length(grep("^[\\._\\w]+$",chars[4],perl=TRUE)) ){        if ( grepl("^[\\._\\w]+$",chars[4],perl=TRUE) ){
773          parent <- c(chars[4],parent)          parent <- c(chars[4],parent)
774        }        }
775        res[[object.name]] <- new("DocLink",name=object.name,        res[[object.name]] <- new("DocLink",name=object.name,

Legend:
Removed from v.163  
changed lines
  Added in v.191

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