File: RspParser.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 (631 lines) | stat: -rw-r--r-- 19,752 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
###########################################################################/**
# @RdocClass RspParser
#
# @title "The RspParser class"
#
# \description{
#  @classhierarchy
#
#  An RspParser is parser for the RSP language.
# }
#
# @synopsis
#
# \arguments{
#   \item{...}{Not used.}
# }
#
# \section{Fields and Methods}{
#  @allmethods
# }
#
# @author
#
# @keyword internal
#*/###########################################################################
setConstructorS3("RspParser", function(...) {
  extend(NA, "RspParser")
})


#########################################################################/**
# @RdocMethod parseRaw
#
# @title "Parses the string into blocks of text and RSP"
#
# \description{
#  @get "title".
# }
#
# @synopsis
#
# \arguments{
#   \item{object}{An @see RspString to be parsed.}
#   \item{what}{A @character string specifying what type of RSP construct
#     to parse for.}
#   \item{commentLength}{Specify the number of hyphens in RSP comments
#     to parse for.}
#   \item{...}{Not used.}
#   \item{verbose}{See @see "R.utils::Verbose".}
# }
#
# \value{
#  Returns a named @list with elements named "text" and "rsp".
# }
#
# @author
#
# \seealso{
#   @seeclass
# }
#*/#########################################################################
setMethodS3("parseRaw", "RspParser", function(parser, object, what=c("comment", "directive", "expression"), commentLength=-1L, ..., verbose=FALSE) {
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Local functions
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Escape '<%%' and '%%>'
  escapeP <- function(s) {
    s <- gsub(.rspBracketOpenEscape,  "---<<<---%%%---%%%---", s, fixed=TRUE)
    s <- gsub(.rspBracketCloseEscape, "---%%%---%%%--->>>---", s, fixed=TRUE)
    s
  } # escapeP()

  # Unescape '<%%' and '%%>'
  unescapeP <- function(s) {
    s <- gsub("---<<<---%%%---%%%---", .rspBracketOpenEscape,  s, fixed=TRUE)
    s <- gsub("---%%%---%%%--->>>---", .rspBracketCloseEscape, s, fixed=TRUE)
    s
  } # unescapeP()


  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Validate arguments
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Argument 'what':
  what <- match.arg(what)

  # Argument 'commentLength':
  commentLength <- as.integer(commentLength)
  stop_if_not(is.finite(commentLength))
  stop_if_not(commentLength == -1L || commentLength >= 2L)

  # Argument 'verbose':
  verbose <- Arguments$getVerbose(verbose)
  if (verbose) {
    pushState(verbose)
    on.exit(popState(verbose))
  }


  verbose && enter(verbose, "Raw parsing of RSP string")

  # Work with one large character string
  bfr <- paste(object, collapse="\n", sep="")
  verbose && cat(verbose, "Length of RSP string: ", nchar(bfr))


  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Setup
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Pattern for suffix specification
  patternS <- paste("([+]|[-]+(\\[[^]]*\\])?)", .rspBracketClose, sep="")

  # Setup the regular expressions for start and stop RSP constructs
  hasPatternLTail <- FALSE
  if (what == "comment") {
    if (commentLength == -1L) {
      # <%-%>, <%--%>, <%---%>, <%----%>, ...
      # <%-[suffix]%>, <%--[suffix]%>, <%---[suffix]%>, ...
      patternL <- sprintf("(%s([-]+(\\[[^]]*\\])?%s))", .rspBracketOpen, .rspBracketClose)
      patternR <- NULL
    } else {
      # <%-- --%>, <%--\n--%>, <%-- text --%>, ...
      # <%--- ---%>, <%--- text ---%>, ...
      patternL <- sprintf("(%s-{%d})([^-])", .rspBracketOpen, commentLength)
      hasPatternLTail <- TRUE
      patternR <- sprintf("(|[^-])(-{%d}(\\[[^]]*\\])?)%s", commentLength, .rspBracketClose)
    }
    bodyClass <- RspComment
  } else if (what == "directive") {
    patternL <- sprintf("(%s@)()", .rspBracketOpen)
    patternR <- sprintf("()(|[+]|-(\\[[^]]*\\])?)%s", .rspBracketClose)
    bodyClass <- RspUnparsedDirective
  } else if (what == "expression") {
    patternL <- sprintf("(%s)()", .rspBracketOpen)
    patternR <- sprintf("()(|[+]|-(\\[[^]]*\\])?)%s", .rspBracketClose)
    bodyClass <- RspUnparsedExpression
  }

  if (verbose) {
    cat(verbose, "Regular expression patterns to use:")
    str(verbose, list(patternL=patternL, patternR=patternR, patternS=patternS))
    cat(verbose, "Class to coerce to: ", class(bodyClass())[1L])
  }

  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Parse
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  verbose && cat(verbose, "What to parse for: ", what)

  # Hide '<%%' and '%%>' from parser
  n <- nchar(bfr)
  bfr <- escapeP(bfr)
  escapedP <- (nchar(bfr) != n)

  # Constants
  START <- 0L
  STOP <- 1L

  parts <- list()
  state <- START
  while(TRUE) {
    if (state == START) {
      # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      # (a) Scan for RSP start tag, i.e. <%, <%@, or <%--
      # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      # The start tag may exists *anywhere* in static code
      posL <- regexpr(patternL, bfr)
      if (posL == -1L)
        break
      nL <- attr(posL, "match.length")
      stop_if_not(is.integer(nL))

      # (i) Extract RSP construct, '<%...%>[extra]'
      tag <- substring(bfr, first=posL, last=posL+nL-1L)

      # Was it an escaped RSP start tag, i.e. '<%%'?
      if (what == "expression") {
        tagX <- substring(bfr, first=posL, last=posL+nL)
        if (tagX == .rspBracketOpenEscape)
          break
      }

      # If parsed too far, i.e. into the tailing text
      # (so that '[extra]' is non-empty), then adjust
      bfrExtra <- gsub(patternL, "\\4", tag)
      nExtra <- nchar(bfrExtra)
      nL <- nL - nExtra

      # (ii) Extract the preceeding text
      text <- substring(bfr, first=1L, last=posL-1L)

      # Record as RSP text, unless empty.
      if (nchar(text) > 0L) {
        # Update flag whether the RSP construct being parsed is
        # on the same output line as RSP text or not.  It is not
        # if the text ends with a line break.
        if (escapedP) text <- unescapeP(text)
        part <- list(text=RspText(text))
      } else {
        part <- NULL
      }


      # (iii) Special case: Locate RSP end tag immediately.
      if (is.null(patternR)) {
        body <- ""

        # Extract the '<%...%>' part
        if (nExtra > 0L) {
          tail <- substring(tag, first=1L, last=nL-1L)
        } else {
          tail <- tag
        }

        # Extract the '...%>' part
        # Currently only used for "empty" comments, e.g. <%---%>
        tail <- gsub(patternL, "\\2", tail)

        # Get optional suffix specifications, i.e. '+%>' or '-[{specs}]%>'
        if (regexpr(patternS, tail) != -1L) {
          suffixSpecs <- gsub(patternS, "\\1", tail)
          suffixSpecs <- gsub("--*", "-", suffixSpecs)
          verbose && printf(verbose, "Identified suffix specification: '%s'\n", suffixSpecs)
          attr(body, "suffixSpecs") <- suffixSpecs
        } else {
          verbose && cat(verbose, "Identified suffix specification: <none>")
        }

        if (what == "comment") {
          attr(body, "commentLength") <- commentLength
        }

        if (!is.null(bodyClass)) {
          body <- bodyClass(body)
        }

        part2 <- list(rsp=body)
        if (what != "expression") {
          names(part2)[1L] <- what
        }
        part <- c(part, part2)
        state <- START
      } else {
        # Push-back something to buffer?
        if (hasPatternLTail && nchar(gsub(patternL, "\\2", tag)) > 0L) {
          nL <- nL - 1L
        }
        state <- STOP
      } # if (is.null(patternR))

      # (iv) Finally, consume the read buffer
      bfr <- substring(bfr, first=posL+nL)
    } else if (state == STOP) {
      # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      # (b) Scan for RSP end tag, i.e. %>, %>, or --%>
      # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      # Not found?
      posR <- indexOfNonQuoted(bfr, patternR)
      if (posR == -1L)
        break

      # (i) Extract RSP body with RSP end tag, '[extra][pattern]%>'
      nR <- attr(posR, "match.length")
      tail <- substring(bfr, first=posR, last=posR+nR-1L)

      # Was it an escaped RSP end tag, i.e. '%%>'?
      if (what == "expression") {
        nT <- nchar(tail)
        if (nT >= nchar(.rspBracketCloseEscape) && substring(tail, first=nT-nchar(.rspBracketCloseEscape)+1L, last=nT) == .rspBracketCloseEscape)
          break
      }

      # If parsed too far, i.e. into the preceeding body
      # (so that '[extra]' is non-empty), then adjust
      bodyExtra <- gsub(patternR, "\\1", tail)
      nExtra <- nchar(bodyExtra)
      posR <- posR + nExtra
      nR <- nR - nExtra

      # Extract body of RSP construct (without RSP end tag)
      body <- substring(bfr, first=1L, last=posR-1L)
      if (escapedP) body <- unescapeP(body)

      # Get optional suffix specifications, i.e. '+%>' or '-[{specs}]%>'
      if (regexpr(patternS, tail) == 1L) {
        suffixSpecs <- gsub(patternS, "\\1", tail)
        verbose && printf(verbose, "Identified suffix specification: '%s'\n", suffixSpecs)
        attr(body, "suffixSpecs") <- suffixSpecs
      } else {
        verbose && cat(verbose, "Identified suffix specification: <none>")
      }

      if (what == "comment") {
        attr(body, "commentLength") <- commentLength
      }

      if (!is.null(bodyClass)) {
        body <- bodyClass(body)
      }

      part <- list(rsp=body)
      if (what != "expression") {
        names(part)[1L] <- what
      }

      # (iv) Finally, consume the read buffer
      bfr <- substring(bfr, first=posR+nR)

      state <- START
    } # if (state == ...)

    parts <- c(parts, part)

    if (verbose) {
      cat(verbose, "RSP construct(s) parsed:")
      print(verbose, part)
      cat(verbose, "Number of RSP constructs parsed this far: ", length(parts))
    }
  } # while(TRUE)


  # Add the rest of the buffer as text, unless empty.
  if (nchar(bfr) > 0L) {
    text <- bfr
    if (escapedP) text <- unescapeP(text)
    text <- RspText(text)
    parts <- c(parts, list(text=text))
  }
  verbose && cat(verbose, "Total number of RSP constructs parsed: ", length(parts))

  # Setup results
  doc <- RspDocument(parts, attrs=getAttributes(object))
  attr(doc, "what") <- what

  verbose && exit(verbose)

  doc
}, protected=TRUE) # parseRaw()



#########################################################################/**
# @RdocMethod parseDocument
#
# @title "Parse an RSP string into and RSP document"
#
# \description{
#  @get "title" with RSP comments dropped.
# }
#
# @synopsis
#
# \arguments{
#   \item{object}{An @see RspString to be parsed.}
#   \item{envir}{The @environment where the RSP document is preprocessed.}
#   \item{...}{Passed to the processor in each step.}
#   \item{until}{Specifies how far the parse should proceed, which is useful
#      for troubleshooting and debugging.}
#   \item{as}{Specifies in what format the parsed RSP document
#      should be returned.}
#   \item{verbose}{See @see "R.utils::Verbose".}
# }
#
# \value{
#  Returns a @see "RspDocument" (when \code{as = "RspDocument"}; default)
#  or @see "RspString" (when \code{as = "RspString"}).
# }
#
# @author
#
# \seealso{
#   @seeclass
# }
#*/#########################################################################
setMethodS3("parseDocument", "RspParser", function(parser, object, envir=parent.frame(), ..., until=c("*", "end", "expressions", "directives", "comments"), as=c("RspDocument", "RspString"), verbose=FALSE) {
  ## WORKAROUND: For unknown reasons, the R.oo package needs to be
  ## attached in order for 'R CMD build' to build the R.rsp package.
  ## If not, the generated RSP-to-R script becomes corrupt and contains
  ## invalid symbols, at least for '<%= ... %>' RSP constructs.
  ## Below use("R.oo", quietly=TRUE) is used to attach 'R.oo',
  ## but we do it as late as possible, in order narrow down the cause.
  ## It appears to be related to garbage collection and finalizers of
  ## Object, which will try to attach 'R.oo' temporarily before running
  ## finalize() on the object.  If so, it's a bug in R.oo.
  ## /HB 2013-09-17
  use("R.oo", quietly=TRUE)


  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Local functions
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  metadata <- getMetadata(object, local=TRUE)

  returnAs <- function(doc, as=c("RspDocument", "RspString")) {
    as <- match.arg(as)

    # Make sure to always output something
    if (length(doc) == 0L) {
      expr <- RspText("")
      doc[[1]] <- expr
    }

    if (length(metadata) > 0L) doc <- setMetadata(doc, metadata)

    if (as == "RspDocument") {
      return(doc)
    } else if (as == "RspString") {
      object <- asRspString(doc)
      return(object)
    }
  } # returnAs()


  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Validate arguments
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Argument 'until':
  until <- match.arg(until)

  # Argument 'as':
  as <- match.arg(as)

  # Argument 'verbose':
  verbose <- Arguments$getVerbose(verbose)
  if (verbose) {
    pushState(verbose)
    on.exit(popState(verbose))
  }


  verbose && enter(verbose, "Parsing RSP string")
  verbose && cat(verbose, "Compile until: ", sQuote(until))
  verbose && cat(verbose, "Return as: ", sQuote(as))

  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # (1a) Parse and drop "empty" RSP comments
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  verbose && enter(verbose, "Dropping 'empty' RSP comments")
  verbose && cat(verbose, "Length of RSP string before: ", nchar(object))

  # This is only for comments such as <%-%>, <%--%>, <%---%>, ...
  doc <- parseRaw(parser, object, what="comment", commentLength=-1L, verbose=less(verbose, 50))

  # Nothing todo?
  if (length(doc) == 0L) {
    return(returnAs(doc, as=as))
  }

  idxs <- which(sapply(doc, FUN=inherits, "RspComment"))
  count <- length(idxs)

  # Empty comments found?
  if (count > 0L) {
    verbose && print(verbose, doc)

    # Preprocess, drop RspComments and adjust for empty lines
    doc <- preprocess(doc, verbose=less(verbose, 10))
    verbose && print(verbose, doc)

    verbose && cat(verbose, "Number of 'empty' RSP comments dropped: ", count)

    # Coerce to RspString
    object <- asRspString(doc)
    verbose && cat(verbose, "Length of RSP string after: ", nchar(object))
  } else {
    verbose && cat(verbose, "No 'empty' RSP comments found.")
  }

  verbose && exit(verbose)

  if (until == "comments") {
    verbose && exit(verbose)
    return(returnAs(doc, as=as))
  }


  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # (1b) Parse and drop RSP comments
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  verbose && enter(verbose, "Dropping 'paired' RSP comments")
  verbose && cat(verbose, "Length of RSP string before: ", nchar(object))

  # Nothing todo?
  if (length(doc) == 0L) {
    return(returnAs(doc, as=as))
  }

  count <- 0L
  posL <- -1L
  while ((pos <- regexpr(sprintf("%s[-]+", .rspBracketOpen), object)) != -1L) {
    # Nothing changed? (e.g. if there is an unclosed comment)
    if (identical(pos, posL)) {
      break
    }

    # Identify the comment length of the first comment found
    n <- attr(pos, "match.length") - 2L
    if (n < 2L) {
       tag <- substring(object, first=pos)
       start <- substring(tag, first=1L, n+2L)
       throw(RspParseException(sprintf("Detected an RSP comment start tag (%s) but no matching end tag: %s", sQuote(start), sQuote(tag))))
    }

    verbose && printf(verbose, "Number of hypens of first comment found: %d\n", n)

    # Find all comments of this same length
    doc <- parseRaw(parser, object, what="comment", commentLength=n, verbose=less(verbose, 50))

    idxs <- which(sapply(doc, FUN=inherits, "RspComment"))
    count <- count + length(idxs)

    # Trim non-text RSP constructs
    doc <- trimNonText(doc, verbose=less(verbose, 10))

    # Preprocess (=drop RspComments and adjust for empty lines)
    doc <- preprocess(doc, verbose=less(verbose, 10))

    # Coerce to RspString
    object <- asRspString(doc)

    posL <- pos
  }

  if (count > 0L) {
    verbose && cat(verbose, "Number of 'paired' RSP comments dropped: ", count)
    verbose && cat(verbose, "Length of RSP string after: ", nchar(object))
  } else {
    verbose && cat(verbose, "No 'paired' RSP comments found.")
  }

  verbose && exit(verbose)


  if (until == "directives") {
    verbose && exit(verbose)
    docP <- parseRaw(parser, object, what="directive", verbose=less(verbose, 50))
    return(returnAs(docP, as=as))
  }

  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # (2a) Parse RSP preprocessing directive
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  verbose && enter(verbose, "Processing RSP preprocessing directives")
  verbose && cat(verbose, "Length of RSP string before: ", nchar(object))

  doc <- parseRaw(parser, object, what="directive", verbose=less(verbose, 50))

  # Nothing todo?
  if (length(doc) == 0L) {
    return(returnAs(doc, as=as))
  }

  idxs <- which(sapply(doc, FUN=inherits, "RspUnparsedDirective"))
  if (length(idxs) > 0L) {
    verbose && cat(verbose, "Number of (unparsed) RSP preprocessing directives found: ", length(idxs))

    # Parse each of them
    for (idx in idxs) {
      doc[[idx]] <- parseDirective(doc[[idx]])
    }

    # Trim non-text RSP constructs
    doc <- trimNonText(doc, verbose=less(verbose, 10))

    # Process all RSP preprocessing directives, i.e. <%@...%>
    doc <- preprocess(doc, envir=envir, ..., verbose=less(verbose, 10))

    # Coerce to RspString
    object <- asRspString(doc)
    verbose && cat(verbose, "Length of RSP string after: ", nchar(object))
  } else {
    verbose && cat(verbose, "No RSP preprocessing directives found.")
  }
  idxs <- NULL; # Not needed anymore

  verbose && exit(verbose)

  if (until == "expressions") {
    verbose && exit(verbose)
    return(returnAs(doc, as=as))
  }


  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # (3) Parse RSP expressions
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  verbose && enter(verbose, "Processing RSP expressions")
  verbose && cat(verbose, "Length of RSP string before: ", nchar(object))

  doc <- parseRaw(parser, object, what="expression", verbose=less(verbose, 50))

  # Nothing todo?
  if (length(doc) == 0L) {
    return(returnAs(doc, as=as))
  }

  idxs <- which(sapply(doc, FUN=inherits, "RspUnparsedExpression"))

  if (length(idxs) > 0L) {
    verbose && cat(verbose, "Number of (unparsed) RSP expressions found: ", length(idxs))

    # Parse them
    for (idx in idxs) {
      doc[[idx]] <- parseExpression(doc[[idx]])
    }

    # Trim non-text RSP constructs
    doc <- trimNonText(doc, verbose=less(verbose, 10))

    # Preprocess (=trim all empty lines)
    doc <- preprocess(doc, envir=envir, ..., verbose=less(verbose, 10))

    if (verbose && isVisible(verbose)) {
      object <- asRspString(doc)
      verbose && cat(verbose, "Length of RSP string after: ", nchar(object))
    }
  } else {
    verbose && cat(verbose, "No RSP expressions found.")
  }

  verbose && exit(verbose)

  if (until == "end") {
    verbose && exit(verbose)
    return(returnAs(doc, as=as))
  }

  verbose && exit(verbose)

  returnAs(doc, as=as)
}, protected=TRUE)