File: RspRSourceCodeFactory.R

package info (click to toggle)
r-cran-r.rsp 0.45.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,568 kB
  • sloc: javascript: 612; tcl: 304; sh: 18; makefile: 16
file content (339 lines) | stat: -rw-r--r-- 10,400 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
###########################################################################/**
# @RdocClass RspRSourceCodeFactory
#
# @title "The RspRSourceCodeFactory class"
#
# \description{
#  @classhierarchy
#
#  An RspRSourceCodeFactory is an @see "RspSourceCodeFactory" for
#  the R language.
# }
#
# @synopsis
#
# \arguments{
#   \item{...}{Not used.}
# }
#
# \section{Fields and Methods}{
#  @allmethods
# }
#
# @author
#
# @keyword internal
#*/###########################################################################
setConstructorS3("RspRSourceCodeFactory", function(...) {
  extend(RspSourceCodeFactory("R"), "RspRSourceCodeFactory")
})



setMethodS3("exprToCode", "RspRSourceCodeFactory", function(object, expr, ..., index=NA) {
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Local function
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  escapeRspText <- function(text) {
    stop_if_not(is.character(text), length(text) == 1L)
    
    ## NOTE: deparse() does not handle UTF-8 strings, e.g.
    ## deparse("g\u00e9nome") == "g<U+00E9>nome" :( /HB 2017-01-04
    # text <- deparse(text)
    # text <- substring(text, first=2L, last=nchar(text1)-1L)
    
    ## BETTER: encodeString() preserves the "\u00e9" format
    text <- encodeString(text)
    ## WORKAROUND: but we have to undo other escaped other characters
    text <- gsub('\"', '\\\"', text, fixed = TRUE)
    
    stop_if_not(is.character(text), length(text) == 1L)

    text
  } # escapeRspText()


  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Validate arguments
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Argument 'expr':
  reqClasses <- c("RspText", "RspExpression")
  if (!inherits(expr, reqClasses)) {
    throw("Argument 'expr' must be of class RspText or RspExpression: ", class(expr)[1L])
  }


  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # RspText => .rout("<text>")
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  if (inherits(expr, "RspText")) {
    text <- getContent(expr)

    code <- NULL
    while (nchar(text) > 0L) {
      textT <- substring(text, first=1L, last=1024L)
      textT <- escapeRspText(textT)
      codeT <- sprintf(".rout(\"%s\")", textT)
      code <- c(code, codeT)
      textT <- codeT <- NULL; # Not needed anymore
      text <- substring(text, first=1025L)
    }
    if (is.null(code)) {
      code <- ".rout(\"\")"
    }

    return(code)
  }

  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # RspCodeChunk => .rout({<expr>})
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  if (inherits(expr, "RspCodeChunk")) {
    code <- getCode(expr)
    code <- trim(code)

    # Parse and validate code chunk
    # (i) Try without { ... }
    codeT <- sprintf("(%s)", code)
    rexpr <- tryCatch({
      base::parse(text=codeT)
    }, error = function(ex) NULL)

    # (ii) Otherwise retry with { ... }
    if (is.null(rexpr)) {
      code <- sprintf("{%s}", code)
      codeT <- sprintf("(%s)", code)
      rexpr <- tryCatch({
        base::parse(text=codeT)
      }, error = function(ex) {
        codeTT <- unlist(strsplit(code, split="", fixed=TRUE))
        # Drop { ... } again
        codeTT <- codeTT[-1L]; codeTT <- codeTT[-length(codeTT)]
        codeTT <- hpaste(codeTT, collapse="", maxHead=100L, maxTail=30L)
        throw(sprintf("RSP code chunk (#%d):\n%s= %s %s\ndoes not contain a complete or valid R expression: %s", index, .rspBracketOpen, codeTT, .rspBracketClose, ex))
      })
    }

    rexpr <- NULL; # Not needed anymore

    echo <- getEcho(expr)
    ret <- getInclude(expr)

    # An <%= ... %> construct?
    if (ret && inherits(expr, "RspCodeChunk")) {
      rout <- ".rout0"
    } else {
      rout <- ".rout"
    }

    if (echo) {
      codeE <- sprintf("%s(\"%s\")", rout, escapeRspText(codeT))
    }

    if (echo && !ret) {
      code <- c(codeE, code)
    } else if (echo && ret) {
      codeT <- sprintf(".rtmp <- %s", code)
      code <- c(codeE, code, sprintf("%s(.rtmp)", "rm(list=\".rtmp\")", rout))
    } else if (!echo && ret) {
      code <- sprintf("%s(%s)", rout, code)
    } else {
    }

    return(code)
  }

  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # RspCode => <code>
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  if (inherits(expr, "RspCode")) {
    code <- getCode(expr)
    echo <- getEcho(expr)
    if (echo) {
      codeE <- sprintf(".rout(\"%s\")", escapeRspText(code))
      code <- c(codeE, code)
    }
    return(code)
  }

  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # RspComment => [void]
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  if (inherits(expr, "RspComment")) {
    return("")
  }

  throw(sprintf("Unknown class of RSP expression (#%d): %s", index, class(expr)[1L]))
}, protected=TRUE) # exprToCode()



setMethodS3("getCompleteCode", "RspRSourceCodeFactory", function(this, object, ...) {
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Local functions
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  minIndent <- function(...) {
    s <- c(...)
    s <- gsub('"\n"', '"\r"', s)
    s <- unlist(strsplit(s, split="\n", fixed=TRUE), use.names=FALSE)
    s <- sapply(s, FUN=function(s) gsub('"\r"', '"\n"', s))
    names(s) <- NULL

    # Nothing todo?
    if (length(s) == 0L) return(s)

    # Clean all-blank lines
    s <- gsub("^[ ]*$", "", s)
    # Drop empty lines at the top and the end
    while (nchar(s[1L]) == 0L) {
      s <- s[-1L]
    }

    # Nothing todo?
    if (length(s) == 0L) return(s)

    while (nchar(s[length(s)]) == 0L) {
      s <- s[-length(s)]
    }

    # Drop duplicated empty lines
    idxs <- which(nchar(s) == 0L)
    if (length(idxs) > 0L) {
      idxs <- idxs[which(diff(idxs) == 1L)]
      if (length(idxs) > 0L) {
        s <- s[-idxs]
      }
    }

    # Find minimum indentation of non-blank lines
    idxs <- which(nchar(s) > 0L)

    # Nothing to do?
    if (length(idxs) == 0L) return(s)

    prefix <- gsub("^([ ]*).*", "\\1", s[idxs])
    min <- min(nchar(prefix))

    # Nothing to do?
    if (min == 0L) return(s)

    pattern <- sprintf("^%s", paste(rep(" ", times=min), collapse=""))
    s <- gsub(pattern, "", s)

    s
  } # minIndent()



  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Get the default code header, body and footer
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  res <- NextMethod()


  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Update the header
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  code <- NULL
##  code <- 'library("R.rsp")'

  # Add metadata
  metadata <- getMetadata(object, local=FALSE)
  hdr <- NULL
  for (key in names(metadata)) {
     value <- metadata[[key]]

     # Metadata assignments in source code
     value <- metadata[[key]]
     value <- gsub('"', '\\"', value, fixed=TRUE)
     value <- sprintf('  %s = "%s"', key, value)
     code <- c(code, value)

     # Metadata presentation in header comment
     value <- metadata[[key]]
     value <- gsub("\n", "\\n", value, fixed=TRUE)
     value <- gsub("\r", "\\r", value, fixed=TRUE)
     hdr <- c(hdr, sprintf("  '%s': '%s'", key, value))
  }

  # Metadata assignments in source code
  code <- unlist(strsplit(paste(code, collapse=",\n"), split="\n", fixed=TRUE), use.names=FALSE)
  code <- c('.rmeta <- list(', code, ')')
  header0 <- paste('    ', code, sep="")

  # Metadata presentation in header comment
  if (length(hdr) > 0L) {
    hdr <- sprintf("    ## %s", hdr)
    hdr <- c("    ##", "    ## Metadata:", hdr)
  }

  # Build R source code
  res$header <- minIndent('
    ## = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    ## This is a self-contained R script generated from an RSP document.
    ## It may be evaluated using source() as is.',
    hdr,
'    ## = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

    ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ## Local RSP utility functions
    ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ## RSP metadata function',
    header0,
    '
    rmeta <- function(...) {
      args <- list(...)
      if (length(args) == 0) return(.rmeta)
      names <- names(args)
      if (length(names) > 0) {
        for (name in names) .rmeta[[name]] <<- args[[name]]
      } else {
        names <- unlist(args, use.names=FALSE)
        if (length(names) == 1) .rmeta[[names]] else .rmeta[names]
      }
    }

    ## Look up \'base\' function once (faster)
    if (getRversion() < "2.15.0") {
      .base_paste <- base::paste
      .base_paste0 <- function(...) .base_paste(..., sep="")
    } else {
      .base_paste0 <- base::paste0
    }
    .base_cat <- base::cat

    ## RSP output function
    .rout <- function(x) .base_cat(.base_paste0(x))

    ## RSP output function for inline RSP constructs
    .rout0 <- function(x) .base_cat(rpaste(x))

    ## The output of inline RSP constructs is controlled by
    ## generic function rpaste().
    rpaste <- function(...) UseMethod("rpaste")

    setInlineRsp <- function(class, fun, envir=parent.frame()) {
      name <- sprintf("rpaste.%s", class)
      assign(name, fun, envir=envir)
      ## FIXME: How to register an S3 method at run-time? /HB 2018-04-06
      ## registerS3method("rpaste", class = class, method = name, envir = envir)
    }

    ## The default is to coerce to character and collapse without
    ## a separator.  It is possible to override the default in an
    ## RSP code expression.
    setInlineRsp("default", function(x, ...) .base_paste0(x, collapse=""))

    ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ## RSP source code script [BEGIN]
    ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  ')

  res$footer <- minIndent('
    ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ## RSP source code script [END]
    ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  ')

  res
}, protected=TRUE) # getCompleteCode()