File: VComments.R

package info (click to toggle)
r-cran-r.utils 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,936 kB
  • sloc: sh: 18; makefile: 6
file content (318 lines) | stat: -rwxr-xr-x 9,090 bytes parent folder | download
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
###########################################################################/**
# @RdocClass VComments
#
# @title "The VComments class"
#
# \description{
#  @classhierarchy
#
#  @get "title". 
# }
#
# @synopsis
#
# \arguments{
#   \item{letter}{The smart letter.}
#   \item{verboseName}{The name of the verbose object.}
#   \item{...}{Not used.}
# }
#
# \section{Fields and Methods}{
#  @allmethods  
# }
#
# \details{
#  The 'v' in VComments stands for 'verbose', because of its relationship
#  to the @see "Verbose" class.
#
#  Here is a list of VComments and the \R code that replaces each of them
#  by the compiler:
#
#  \bold{Constructors}\cr
#  \itemize{
#   \item{\#V0\#}{[<args>] - NullVerbose(<args>)}
#   \item{\#V1\#}{[<args>] - Verbose(<args>)}
#  }
#
#  \bold{Controls}\cr
#  \itemize{
#   \item{\#V=\#}{[<variable>] - Sets the name of the <verbose> object.
#                                Default is 'verbose'.}
#   \item{\#V\^\#}{<threshold> - setThreshold(<verbose>, <threshold>)}
#   \item{\#V?\#}{<expression> - if (isVisible(<verbose>)) \{ <expression> \}}
#   \item{\#V@\#}{<level> - setDefaultLevel(<verbose>, <level>)}
#   \item{\#Vm\#}{<method> <args> - <method>(<verbose>, <args>)}
#  }
#
#  \bold{Enters and exits}\cr
#  \itemize{
#   \item{\#V+\#}{[<message>] - enter(<verbose>, <message>)}
#   \item{\#V-\#}{[<message>] - exit(<verbose>, <message>)}
#   \item{\#V!\#}{[<message>] - pushState(<verbose>)\cr
#                             on.exit(popState(<verbose>))\cr
#                             If <message>, enter(<verbose>, <message>)}
#  }
#
#  \bold{Simple output}\cr
#  \itemize{
#   \item{\#Vn\#}{<ignored> - newline(<verbose>)}
#   \item{\#Vr\#}{<ignored> - ruler(<verbose>)}
#   \item{\#Vt\#}{<ignored> - timestamp(<verbose>)}
#   \item{\#Vw\#}{[<title>] - warnings(<verbose>, <title>)}
#  }
#
#  \bold{Output messages}\cr
#  \itemize{
#   \item{\#Vc\#}{[<message>] - cat(<verbose>, <message>)}
#   \item{\#Ve\#}{<expression> - eval(<verbose>, <expression>)}
#   \item{\#Vh\#}{<message> - header(<verbose>, <message>)}
#   \item{\#Vp\#}{<object> - print(<verbose>, <object>)}
#   \item{\#Vs\#}{<object> - summary(<verbose>, <object>)}
#   \item{\#Vz\#}{<object> - str(<verbose>, <object>)}
#  }
# }
#
# @examples "../incl/VComments.Rex"
#
# @author
#
# @keyword programming
# @keyword IO
#*/###########################################################################
setConstructorS3("VComments", function(letter="V", verboseName="verbose", ...) {
  verboseName <- as.character(verboseName);

  extend(SmartComments(letter=letter), "VComments",
    resetVerboseName = verboseName,
    verboseName = verboseName
  )
})



###########################################################################/**
# @RdocMethod reset
#
# @title "Resets a VComments compiler"
#
# \description{
#   @get "title".
# }
# 
# @synopsis
#
# \arguments{
#  \item{...}{Not used.}
# }
#
# \value{
#   Returns nothing.
# }
#
# @author
#
# \seealso{
#   @seeclass
# }
#
# @keyword programming
#*/###########################################################################
setMethodS3("reset", "VComments", function(this, ...) {
  NextMethod("reset");
  this$verboseName <- this$resetVerboseName;
})



###########################################################################/**
# @RdocMethod convertComment
#
# @title "Converts a verbose comment to R code"
#
# \description{
#   @get "title".
# }
# 
# @synopsis
#
# \arguments{
#  \item{vcomment}{A vcomment @list structure.}
#  \item{.currLine, .line}{A line number and the line currently processed. 
#      Used for error message and warnings.}
#  \item{...}{Not used.}
# }
#
# \value{
#   Returns one @character string of \R code.
# }
#
# @author
#
# \seealso{
#   @seeclass
# }
#
# @keyword programming
#*/###########################################################################
setMethodS3("convertComment", "VComments", function(this, vcomment, .currLine=NA, .line=NA, ...) {
  cmd <- vcomment$cmd;
  args <- vcomment$args;

  if (is.null(args)) {
    argsStr <- "";
  } else {
    argsStr <- sprintf(", \"%s\"", gsub("\"", "\\\"", args, fixed=TRUE));
  }

  vcmd <- NULL;
  if (cmd == "+") {
    vcmd <- sprintf("enter(<verbose>%s)", argsStr);
  } else if (cmd == "-") {
    vcmd <- sprintf("exit(<verbose>%s)", argsStr);
  } else if (cmd %in% c("0", "1")) {
    clazz <- ifelse(cmd == "1", "Verbose", "NullVerbose");
    if (is.null(args)) {
      vcmd <- sprintf("<verbose> <- %s()", clazz);
    } else {
      vcmd <- sprintf("<verbose> <- %s(%s)", clazz, args);
    }
  } else if (cmd == "=") {
    if (is.null(args)) {
      reset(this);
    } else {
      this$verboseName <- args;
    }
  } else if (cmd == "^") {
    threshold <- as.integer(args);
    if (!is.na(threshold)) {
      vcmd <- sprintf("setThreshold(<verbose>, threshold=%d)", threshold);
    } else {
      throw("Invalid threshold value: ", threshold);
    }
  } else if (cmd == "?") {
    vcmd <- sprintf("if (isVisible(<verbose>)) { capture(<verbose>, %s) }", args);
  } else if (cmd == "@") {
    if (is.na(as.numeric(args))) {
      throw("VComment error: Invalid verbose level on line ", .currLine, ": ", .line);
    }
    vcmd <- sprintf("setDefaultVerboseLevel(<verbose>, %s)", as.double(args));
  } else if (cmd == "!") {
    vcmd <- "pushState(<verbose>); on.exit(popState(<verbose>), add=TRUE)";
    if (!is.null(args))
      vcmd <- sprintf("%s; enter(<verbose>, \"%s\")", vcmd, args);
  } else if (cmd == "c") {
    vcmd <- sprintf("cat(<verbose>%s)", argsStr);
  } else if (cmd == "e") {
    vcmd <- sprintf("evaluate(<verbose>, %s)", args);
  } else if (cmd == "m") {
    method <- gsub("^([^ ]*)([ ]*)(.*)", "\\1", args);
    args <- gsub("^([^ ]*)([ ]*)(.*)", "\\3", args);
    args <- trim(args);
    if (nchar(args) == 0) {
      vcmd <- sprintf("%s(<verbose>)", method);
    } else {
      vcmd <- sprintf("%s(<verbose>, %s)", method, args);
    }
  } else if (cmd == "n") {
    vcmd <- sprintf("newline(<verbose>)");
  } else if (cmd == "p") {
    vcmd <- sprintf("print(<verbose>, %s)", args);
  } else if (cmd == "s") {
    vcmd <- sprintf("summary(<verbose>, %s)", args);
  } else if (cmd == "t") {
    vcmd <- "timestamp(<verbose>)";
  } else if (cmd == "w") {
    if (nchar(args) == 0) {
      vcmd <- "warnings(<verbose>)";
    } else {
      vcmd <- sprintf("warnings(<verbose>, %s)", args);
    }
  } else if (cmd == "z") {
    vcmd <- sprintf("str(<verbose>, %s)", args);
  } else if (cmd == "r") {
    vcmd <- sprintf("ruler(<verbose>)");
  } else if (cmd == "h") {
    vcmd <- sprintf("header(<verbose>, \"%s\")", args);
  } else {
    vcmd <- NA;
  }

  if (!is.null(vcmd)) {
    if (is.na(vcmd)) {
      newLine <- paste("# <?>", .line, "</?>", sep="");
      warning("Unknown VComment on line ", .currLine, ": ", .line);
    } else if (cmd %in% c("0", "1")) {
      newLine <- vcmd;
    } else {
      newLine <- paste("if (<verbose>) { ", vcmd, " }", sep="");
    }
  
    newLine <- gsub("<verbose>", this$verboseName, newLine, fixed=TRUE);
    paste(vcomment$indent, newLine, sep="");
  } else {
    NA;
  }
}, protected=TRUE)




###########################################################################/**
# @RdocMethod validate
#
# @title "Validates the compiled lines"
#
# \description{
#   @get "title"
# }
# 
# @synopsis
#
# \arguments{
#  \item{lines}{A @character @vector of lines of code to validated.}
#  \item{...}{Not used.}
# }
#
# \value{
#   Returns a @character @vector.
# }
#
# @author
#
# \seealso{
#   @seeclass
# }
#
# @keyword programming
#*/###########################################################################
setMethodS3("validate", "VComments", function(this, lines, ...) {
  # Check number of enters and exits.
  pattern <- paste("enter(", this$verboseName, sep="");
  nbrOfEnters <- sum(regexpr(pattern, lines, fixed=TRUE) != -1);
  pattern <- paste("exit(", this$verboseName, sep="");
  nbrOfExits <- sum(regexpr(pattern, lines, fixed=TRUE) != -1);
  if (nbrOfEnters != nbrOfExits) {
    warning("Number of verbose enters and exits do not match: ", 
                                             nbrOfEnters, " != ", nbrOfExits);
  }

  lines;
}, protected=TRUE)


############################################################################
# HISTORY: 
# 2011-02-01
# o ROBUSTNESS: Now using argument 'fixed' (not 'fix') in regexpr() calls.
# 2005-07-07
# o Added output of warnings.
# 2005-06-26
# o Made class and methods non-static, but they can still be used by the
#   static class instance, which then works as a default object.
# 2005-06-23
# o Added reset().
# o Now it is possible to rename the 'verbose' object.
# o Added Rdoc comments.
# 2005-06-22
# o Created.
############################################################################