File: RspShSourceCodeFactory.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 (140 lines) | stat: -rw-r--r-- 4,115 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
###########################################################################/**
# @RdocClass RspShSourceCodeFactory
#
# @title "The RspShSourceCodeFactory class"
#
# \description{
#  @classhierarchy
#
#  An RspShSourceCodeFactory is an @see "RspSourceCodeFactory" for
#  the shell ('sh') script language.
# }
#
# @synopsis
#
# \arguments{
#   \item{...}{Not used.}
# }
#
# \section{Fields and Methods}{
#  @allmethods
# }
#
# @author
#
# @keyword internal
#*/###########################################################################
setConstructorS3("RspShSourceCodeFactory", function(...) {
  extend(RspSourceCodeFactory("sh"), "RspShSourceCodeFactory")
})



setMethodS3("exprToCode", "RspShSourceCodeFactory", function(object, expr, ..., index=NA) {
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Local function
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  escapeRspText <- function(text) {
    text <- deparse(text)
    text <- substring(text, first=2L, last=nchar(text)-1L)
    ## SHELL: Escape backticks
    text <- sapply(text, FUN=function(s) {
      gsub("`", "\\`", s, fixed = TRUE)
    })
    ## SHELL: Escape dollar signs
    text <- sapply(text, FUN=function(s) {
      gsub("$", "\\$", s, fixed = TRUE)
    })
    text
  } # escapeRspText()

  makeCode <- function(code, echo=FALSE, include=FALSE, ...) {
    code <- unlist(strsplit(code, split="\n", fixed=TRUE), use.names=FALSE)
    codeT <- trim(code)

    n <- length(code)
    codeE <- sapply(code, FUN=escapeRspText)
    codeE <- sprintf("printf \"%s\"", codeE)
    suffixR <- rep(" 2> /dev/null", times=n)
    codeR <- sprintf("%s%s", codeT, suffixR)
    if (include) {
      # Output the last out
      codeR[n] <- sprintf("printf \"%s\"", code[n])
    }

    codeS <- matrix(c(codeE, codeR), nrow=2L, byrow=TRUE)
    rownames(codeS) <- c("echo", "include")

    if (echo && !include) {
      code <- codeS[1L,,drop=TRUE]
    } else if (echo && include) {
      code <- codeS
    } else if (!echo && include) {
      code <- codeS[2L,,drop=TRUE]
    } else if (!echo && !include) {
      code <- codeS[2L,,drop=TRUE]
    }

    code
  } # makeCode()


  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # 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 => echo "<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("printf \"%s\"", textT)
      code <- c(code, codeT)
      text <- substring(text, first=1025L)
    }
    if (is.null(code)) {
      code <- "printf \"\\n\""
    }

    return(code)
  }


  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # RspCodeChunk => ...
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  if (inherits(expr, "RspCodeChunk")) {
    code <- makeCode(getCode(expr), echo=getEcho(expr), include=getInclude(expr))
    return(code)
  }

  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # RspCode => <code>
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  if (inherits(expr, "RspCode")) {
    code <- makeCode(getCode(expr), echo=getEcho(expr), include=FALSE)
    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()