[Roxygen-commits] r62 - in pkg: R sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jul 20 07:08:22 CEST 2008


Author: pcd
Date: 2008-07-20 07:08:22 +0200 (Sun, 20 Jul 2008)
New Revision: 62

Modified:
   pkg/R/Rd.R
   pkg/R/namespace.R
   pkg/R/roclet.R
   pkg/sandbox/Rd.R
Log:
Rd roclet in general style with closure


Modified: pkg/R/Rd.R
===================================================================
--- pkg/R/Rd.R	2008-07-20 05:08:11 UTC (rev 61)
+++ pkg/R/Rd.R	2008-07-20 05:08:22 UTC (rev 62)
@@ -1,68 +1,69 @@
 #' @include list.R
 #' @include string.R
-#' @include functional.R
-Rd <- function(partita) {
+make.Rd.roclet <- function() {
   Rd.expression <- function(key, ...)
     sprintf('\\%s%s\n',
             key,
             Reduce.paste(function(expression)
                          sprintf('{%s}', trim(expression)),
                          c(...),
-                         NIL.STRING))
+                         ''))
 
-  parse.default <- function(key, ...)
+  parse.expression <- function(key, ...)
     cat(Rd.expression(key, c(...)))
 
-  parse.name <- Curry(parse.default, key='name')
+  pre.parse <- function(partitum)
+    assign.parent('params', nil, environment())
 
-  parse.title <- Curry(parse.default, key='title')
+  post.parse <- function(partitum)
+    parse.arguments()
 
-  parse.usage <- Curry(parse.default, key='usage')
+  roclet <- make.roclet(parse.expression,
+                        pre.parse,
+                        post.parse)
 
-  parse.return <- Curry(parse.default, key='value')
+  roclet$register.default.parsers('name',
+                                  'title',
+                                  'usage',
+                                  'value',
+                                  'references',
+                                  'note',
+                                  'author',
+                                  'seealso',
+                                  'examples',
+                                  'concept')
 
-  parse.references <- Curry(parse.default, key='references')
-
-  parse.note <- Curry(parse.default, key='note')
-
-  parse.author <- Curry(parse.default, key='author')
-
-  parse.seealso <- Curry(parse.default, key='seealso')
-
-  parse.examples <- Curry(parse.default, key='examples')
-
-  parse.concept <- Curry(parse.default, key='concept')
-
   parse.split <- function(key, expressions) {
     expression <- strcar(expressions)
     rest <- strcdr(expressions)
-    parse.default(key, expression)
+    parse.expression(key, expression)
     if (!is.null.string(rest))
       parse.split(key, rest)
   }
 
-  parse.keywords <- Curry(parse.split, key='keyword')
+  roclet$register.parsers(parse.split,
+                          'keyword',
+                          'alias')
 
-  parse.aliases <- Curry(parse.split, key='alias')
-
-  parse.description <- function(expressions) {
-    paragraphs <- car(strsplit(expressions, '\n\n', fixed=T))
+  parse.description <- function(key, expressions) {
+    paragraphs <- car(strsplit(car(expressions), '\n\n', fixed=T))
     description <- car(paragraphs)
     details <- do.call(paste, append(cdr(paragraphs), list(sep='\n\n')))
-    parse.default('description', description)
+    parse.expression('description', description)
     if (length(details) > 0 && !is.null.string(details))
-      parse.default('details', details)
+      parse.expression('details', details)
   }
 
+  roclet$register.parser('description', parse.description)
+
   params <- nil
 
-  parse.param <- function(expression)
-    ## Hack to access persistent `params'
-    assign('params',
-           append(params, list(expression)),
-           envir=parent.env(environment()))
+  parse.param <- function(key, expression)
+    assign.parent('params',
+                  append(params, expression),
+                  environment())
     
-  parse.params <- function(params)
+  parse.params <- function()
     Reduce.paste(function(name.param)
                  Rd.expression('item',
                      car(name.param),
@@ -70,35 +71,11 @@
                  params,
                  '')
 
-  parse.arguments <- function(params)
+  parse.arguments <- function()
     if (length(params) > 0)
-      parse.default('arguments', parse.params(params))
+      parse.expression('arguments', parse.params())
 
-  parse.noop <- function(expression) NULL
+  roclet$register.parser('param', parse.param)
 
-  parsers <- list(name=parse.name,
-                  title=parse.title,
-                  usage=parse.usage,
-                  return=parse.return,
-                  references=parse.references,
-                  note=parse.note,
-                  author=parse.author,
-                  seealso=parse.seealso,
-                  examples=parse.examples,
-                  concept=parse.concept,
-                  aliases=parse.aliases,
-                  keywords=parse.keywords,
-                  description=parse.description,
-                  param=parse.param)
-
-  parser <- function(key)
-    if (is.null(f <- parsers[[key]])) parse.noop else f
-
-  ## Parse the rest first for side-effects (i.e. parameter-gathering).
-  for (partitum in partita)
-    for (key.value in key.values(partitum))
-      do.call(parser(car(key.value)), cdr(key.value))
-
-  ## Then parse the arguments.
-  parse.arguments(params)
+  roclet
 }

Modified: pkg/R/namespace.R
===================================================================
--- pkg/R/namespace.R	2008-07-20 05:08:11 UTC (rev 61)
+++ pkg/R/namespace.R	2008-07-20 05:08:22 UTC (rev 62)
@@ -4,7 +4,7 @@
   parse.directive <- function(proc, parms)
     cat(sprintf('%s(%s)\n', proc, strmap(Identity, ', ', parms)))
   
-  roclet <- roclet(parse.directive)
+  roclet <- make.roclet(parse.directive)
 
   roclet$register.parser('exportClass',
                                    function(proc, parms)
@@ -13,13 +13,13 @@
                                    function(proc, parms)
                                    default.parse('exportMethods', parms))
 
-  roclet$register.default.parser('export')
-  roclet$register.default.parser('exportPattern')
-  roclet$register.default.parser('S3method')
-  roclet$register.default.parser('import')
-  roclet$register.default.parser('importFrom')
-  roclet$register.default.parser('importClassesFrom')
-  roclet$register.default.parser('importMethodsFrom')
+  roclet$register.default.parsers('export',
+                                  'exportPattern',
+                                  'S3method',
+                                  'import',
+                                  'importFrom',
+                                  'importClassesFrom',
+                                  'importMethodsFrom')
 
   roclet
 }

Modified: pkg/R/roclet.R
===================================================================
--- pkg/R/roclet.R	2008-07-20 05:08:11 UTC (rev 61)
+++ pkg/R/roclet.R	2008-07-20 05:08:22 UTC (rev 62)
@@ -1,5 +1,7 @@
 #' @include list.R
-roclet <- function(parse.default) {
+make.roclet <- function(parse.default,
+                        pre.parse=NULL,
+                        post.parse=NULL) {
   roclet <- new.env(parent=emptyenv())
 
   roclet$parsers <- list()
@@ -7,9 +9,17 @@
   roclet$register.parser <- function(key, parser)
     roclet$parsers[[key]] <- parser
 
+  roclet$register.parsers <- function(parser, ...)
+    for (key in c(...))
+      roclet$register.parser(key, parser)
+
   roclet$register.default.parser <- function(key)
     roclet$parsers[[key]] <- parse.default
 
+  roclet$register.default.parsers <- function(...)
+    for (parser in c(...))
+      roclet$register.default.parser(parser)
+
   roclet$parse <- function(...) {
     key.values <- function(partitum)
       zip.list(attributes(partitum)$names, partitum)
@@ -19,12 +29,22 @@
     parser <- function(key)
       if (is.null(f <- roclet$parsers[[key]])) parse.noop else f
 
-    for (partitum in parse.files(...))
+    maybe.call <- function(proc, ...)
+      if (!is.null(proc))
+        do.call(proc, list(...))
+
+    for (partitum in parse.files(...)) {
+      maybe.call(pre.parse, partitum)
       for (key.value in key.values(partitum)) {
         key <- car(key.value)
         do.call(parser(key), list(key, cdr(key.value)))
       }
+      maybe.call(post.parse, partitum)
+    }
   }
 
   roclet
 }
+
+assign.parent <- function(var, value, env)
+  assign(var, value, envir=parent.env(env))

Modified: pkg/sandbox/Rd.R
===================================================================
--- pkg/sandbox/Rd.R	2008-07-20 05:08:11 UTC (rev 61)
+++ pkg/sandbox/Rd.R	2008-07-20 05:08:22 UTC (rev 62)
@@ -11,4 +11,5 @@
 argc <- length(argv)
 files <- ifelse(argc > 0, as.list(argv), FILES)
 
-Rd(do.call(parse.file, files))
+roclet <- make.Rd.roclet()
+do.call(roclet$parse, files)



More information about the Roxygen-commits mailing list