|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.r |
|
4 ~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for the R/S languages. |
|
7 |
|
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 import re |
|
13 |
|
14 from pygments.lexer import Lexer, RegexLexer, include, words, do_insertions |
|
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
|
16 Number, Punctuation, Generic |
|
17 |
|
18 __all__ = ['RConsoleLexer', 'SLexer', 'RdLexer'] |
|
19 |
|
20 |
|
21 line_re = re.compile('.*?\n') |
|
22 |
|
23 |
|
24 class RConsoleLexer(Lexer): |
|
25 """ |
|
26 For R console transcripts or R CMD BATCH output files. |
|
27 """ |
|
28 |
|
29 name = 'RConsole' |
|
30 aliases = ['rconsole', 'rout'] |
|
31 filenames = ['*.Rout'] |
|
32 |
|
33 def get_tokens_unprocessed(self, text): |
|
34 slexer = SLexer(**self.options) |
|
35 |
|
36 current_code_block = '' |
|
37 insertions = [] |
|
38 |
|
39 for match in line_re.finditer(text): |
|
40 line = match.group() |
|
41 if line.startswith('>') or line.startswith('+'): |
|
42 # Colorize the prompt as such, |
|
43 # then put rest of line into current_code_block |
|
44 insertions.append((len(current_code_block), |
|
45 [(0, Generic.Prompt, line[:2])])) |
|
46 current_code_block += line[2:] |
|
47 else: |
|
48 # We have reached a non-prompt line! |
|
49 # If we have stored prompt lines, need to process them first. |
|
50 if current_code_block: |
|
51 # Weave together the prompts and highlight code. |
|
52 for item in do_insertions( |
|
53 insertions, slexer.get_tokens_unprocessed(current_code_block)): |
|
54 yield item |
|
55 # Reset vars for next code block. |
|
56 current_code_block = '' |
|
57 insertions = [] |
|
58 # Now process the actual line itself, this is output from R. |
|
59 yield match.start(), Generic.Output, line |
|
60 |
|
61 # If we happen to end on a code block with nothing after it, need to |
|
62 # process the last code block. This is neither elegant nor DRY so |
|
63 # should be changed. |
|
64 if current_code_block: |
|
65 for item in do_insertions( |
|
66 insertions, slexer.get_tokens_unprocessed(current_code_block)): |
|
67 yield item |
|
68 |
|
69 |
|
70 class SLexer(RegexLexer): |
|
71 """ |
|
72 For S, S-plus, and R source code. |
|
73 |
|
74 .. versionadded:: 0.10 |
|
75 """ |
|
76 |
|
77 name = 'S' |
|
78 aliases = ['splus', 's', 'r'] |
|
79 filenames = ['*.S', '*.R', '.Rhistory', '.Rprofile', '.Renviron'] |
|
80 mimetypes = ['text/S-plus', 'text/S', 'text/x-r-source', 'text/x-r', |
|
81 'text/x-R', 'text/x-r-history', 'text/x-r-profile'] |
|
82 |
|
83 builtins_base = ( |
|
84 'Arg', 'Conj', 'Cstack_info', 'Encoding', 'FALSE', |
|
85 'Filter', 'Find', 'I', 'ISOdate', 'ISOdatetime', 'Im', 'Inf', |
|
86 'La.svd', 'Map', 'Math.Date', 'Math.POSIXt', 'Math.data.frame', |
|
87 'Math.difftime', 'Math.factor', 'Mod', 'NA_character_', |
|
88 'NA_complex_', 'NA_real_', 'NCOL', 'NROW', 'NULLNA_integer_', 'NaN', |
|
89 'Negate', 'NextMethod', 'Ops.Date', 'Ops.POSIXt', 'Ops.data.frame', |
|
90 'Ops.difftime', 'Ops.factor', 'Ops.numeric_version', 'Ops.ordered', |
|
91 'Position', 'R.Version', 'R.home', 'R.version', 'R.version.string', |
|
92 'RNGkind', 'RNGversion', 'R_system_version', 'Re', 'Recall', |
|
93 'Reduce', 'Summary.Date', 'Summary.POSIXct', 'Summary.POSIXlt', |
|
94 'Summary.data.frame', 'Summary.difftime', 'Summary.factor', |
|
95 'Summary.numeric_version', 'Summary.ordered', 'Sys.Date', |
|
96 'Sys.chmod', 'Sys.getenv', 'Sys.getlocale', 'Sys.getpid', |
|
97 'Sys.glob', 'Sys.info', 'Sys.localeconv', 'Sys.readlink', |
|
98 'Sys.setFileTime', 'Sys.setenv', 'Sys.setlocale', 'Sys.sleep', |
|
99 'Sys.time', 'Sys.timezone', 'Sys.umask', 'Sys.unsetenv', |
|
100 'Sys.which', 'TRUE', 'UseMethod', 'Vectorize', 'abbreviate', 'abs', |
|
101 'acos', 'acosh', 'addNA', 'addTaskCallback', 'agrep', 'alist', |
|
102 'all', 'all.equal', 'all.equal.POSIXct', 'all.equal.character', |
|
103 'all.equal.default', 'all.equal.factor', 'all.equal.formula', |
|
104 'all.equal.language', 'all.equal.list', 'all.equal.numeric', |
|
105 'all.equal.raw', 'all.names', 'all.vars', 'any', 'anyDuplicated', |
|
106 'anyDuplicated.array', 'anyDuplicated.data.frame', |
|
107 'anyDuplicated.default', 'anyDuplicated.matrix', 'aperm', |
|
108 'aperm.default', 'aperm.table', 'append', 'apply', 'args', |
|
109 'arrayInd', 'as.Date', 'as.Date.POSIXct', 'as.Date.POSIXlt', |
|
110 'as.Date.character', 'as.Date.date', 'as.Date.dates', |
|
111 'as.Date.default', 'as.Date.factor', 'as.Date.numeric', |
|
112 'as.POSIXct', 'as.POSIXct.Date', 'as.POSIXct.POSIXlt', |
|
113 'as.POSIXct.date', 'as.POSIXct.dates', 'as.POSIXct.default', |
|
114 'as.POSIXct.numeric', 'as.POSIXlt', 'as.POSIXlt.Date', |
|
115 'as.POSIXlt.POSIXct', 'as.POSIXlt.character', 'as.POSIXlt.date', |
|
116 'as.POSIXlt.dates', 'as.POSIXlt.default', 'as.POSIXlt.factor', |
|
117 'as.POSIXlt.numeric', 'as.array', 'as.array.default', 'as.call', |
|
118 'as.character', 'as.character.Date', 'as.character.POSIXt', |
|
119 'as.character.condition', 'as.character.default', |
|
120 'as.character.error', 'as.character.factor', 'as.character.hexmode', |
|
121 'as.character.numeric_version', 'as.character.octmode', |
|
122 'as.character.srcref', 'as.complex', 'as.data.frame', |
|
123 'as.data.frame.AsIs', 'as.data.frame.Date', 'as.data.frame.POSIXct', |
|
124 'as.data.frame.POSIXlt', 'as.data.frame.array', |
|
125 'as.data.frame.character', 'as.data.frame.complex', |
|
126 'as.data.frame.data.frame', 'as.data.frame.default', |
|
127 'as.data.frame.difftime', 'as.data.frame.factor', |
|
128 'as.data.frame.integer', 'as.data.frame.list', |
|
129 'as.data.frame.logical', 'as.data.frame.matrix', |
|
130 'as.data.frame.model.matrix', 'as.data.frame.numeric', |
|
131 'as.data.frame.numeric_version', 'as.data.frame.ordered', |
|
132 'as.data.frame.raw', 'as.data.frame.table', 'as.data.frame.ts', |
|
133 'as.data.frame.vector', 'as.difftime', 'as.double', |
|
134 'as.double.POSIXlt', 'as.double.difftime', 'as.environment', |
|
135 'as.expression', 'as.expression.default', 'as.factor', |
|
136 'as.function', 'as.function.default', 'as.hexmode', 'as.integer', |
|
137 'as.list', 'as.list.Date', 'as.list.POSIXct', 'as.list.data.frame', |
|
138 'as.list.default', 'as.list.environment', 'as.list.factor', |
|
139 'as.list.function', 'as.list.numeric_version', 'as.logical', |
|
140 'as.logical.factor', 'as.matrix', 'as.matrix.POSIXlt', |
|
141 'as.matrix.data.frame', 'as.matrix.default', 'as.matrix.noquote', |
|
142 'as.name', 'as.null', 'as.null.default', 'as.numeric', |
|
143 'as.numeric_version', 'as.octmode', 'as.ordered', |
|
144 'as.package_version', 'as.pairlist', 'as.qr', 'as.raw', 'as.single', |
|
145 'as.single.default', 'as.symbol', 'as.table', 'as.table.default', |
|
146 'as.vector', 'as.vector.factor', 'asNamespace', 'asS3', 'asS4', |
|
147 'asin', 'asinh', 'assign', 'atan', 'atan2', 'atanh', |
|
148 'attachNamespace', 'attr', 'attr.all.equal', 'attributes', |
|
149 'autoload', 'autoloader', 'backsolve', 'baseenv', 'basename', |
|
150 'besselI', 'besselJ', 'besselK', 'besselY', 'beta', |
|
151 'bindingIsActive', 'bindingIsLocked', 'bindtextdomain', 'bitwAnd', |
|
152 'bitwNot', 'bitwOr', 'bitwShiftL', 'bitwShiftR', 'bitwXor', 'body', |
|
153 'bquote', 'browser', 'browserCondition', 'browserSetDebug', |
|
154 'browserText', 'builtins', 'by', 'by.data.frame', 'by.default', |
|
155 'bzfile', 'c.Date', 'c.POSIXct', 'c.POSIXlt', 'c.noquote', |
|
156 'c.numeric_version', 'call', 'callCC', 'capabilities', 'casefold', |
|
157 'cat', 'category', 'cbind', 'cbind.data.frame', 'ceiling', |
|
158 'char.expand', 'charToRaw', 'charmatch', 'chartr', 'check_tzones', |
|
159 'chol', 'chol.default', 'chol2inv', 'choose', 'class', |
|
160 'clearPushBack', 'close', 'close.connection', 'close.srcfile', |
|
161 'close.srcfilealias', 'closeAllConnections', 'col', 'colMeans', |
|
162 'colSums', 'colnames', 'commandArgs', 'comment', 'computeRestarts', |
|
163 'conditionCall', 'conditionCall.condition', 'conditionMessage', |
|
164 'conditionMessage.condition', 'conflicts', 'contributors', 'cos', |
|
165 'cosh', 'crossprod', 'cummax', 'cummin', 'cumprod', 'cumsum', 'cut', |
|
166 'cut.Date', 'cut.POSIXt', 'cut.default', 'dQuote', 'data.class', |
|
167 'data.matrix', 'date', 'debug', 'debugonce', |
|
168 'default.stringsAsFactors', 'delayedAssign', 'deparse', 'det', |
|
169 'determinant', 'determinant.matrix', 'dget', 'diag', 'diff', |
|
170 'diff.Date', 'diff.POSIXt', 'diff.default', 'difftime', 'digamma', |
|
171 'dim', 'dim.data.frame', 'dimnames', 'dimnames.data.frame', 'dir', |
|
172 'dir.create', 'dirname', 'do.call', 'dput', 'drop', 'droplevels', |
|
173 'droplevels.data.frame', 'droplevels.factor', 'dump', 'duplicated', |
|
174 'duplicated.POSIXlt', 'duplicated.array', 'duplicated.data.frame', |
|
175 'duplicated.default', 'duplicated.matrix', |
|
176 'duplicated.numeric_version', 'dyn.load', 'dyn.unload', 'eapply', |
|
177 'eigen', 'else', 'emptyenv', 'enc2native', 'enc2utf8', |
|
178 'encodeString', 'enquote', 'env.profile', 'environment', |
|
179 'environmentIsLocked', 'environmentName', 'eval', 'eval.parent', |
|
180 'evalq', 'exists', 'exp', 'expand.grid', 'expm1', 'expression', |
|
181 'factor', 'factorial', 'fifo', 'file', 'file.access', 'file.append', |
|
182 'file.choose', 'file.copy', 'file.create', 'file.exists', |
|
183 'file.info', 'file.link', 'file.path', 'file.remove', 'file.rename', |
|
184 'file.show', 'file.symlink', 'find.package', 'findInterval', |
|
185 'findPackageEnv', 'findRestart', 'floor', 'flush', |
|
186 'flush.connection', 'force', 'formals', 'format', |
|
187 'format.AsIs', 'format.Date', 'format.POSIXct', 'format.POSIXlt', |
|
188 'format.data.frame', 'format.default', 'format.difftime', |
|
189 'format.factor', 'format.hexmode', 'format.info', |
|
190 'format.libraryIQR', 'format.numeric_version', 'format.octmode', |
|
191 'format.packageInfo', 'format.pval', 'format.summaryDefault', |
|
192 'formatC', 'formatDL', 'forwardsolve', 'gamma', 'gc', 'gc.time', |
|
193 'gcinfo', 'gctorture', 'gctorture2', 'get', 'getAllConnections', |
|
194 'getCallingDLL', 'getCallingDLLe', 'getConnection', |
|
195 'getDLLRegisteredRoutines', 'getDLLRegisteredRoutines.DLLInfo', |
|
196 'getDLLRegisteredRoutines.character', 'getElement', |
|
197 'getExportedValue', 'getHook', 'getLoadedDLLs', 'getNamespace', |
|
198 'getNamespaceExports', 'getNamespaceImports', 'getNamespaceInfo', |
|
199 'getNamespaceName', 'getNamespaceUsers', 'getNamespaceVersion', |
|
200 'getNativeSymbolInfo', 'getOption', 'getRversion', 'getSrcLines', |
|
201 'getTaskCallbackNames', 'geterrmessage', 'gettext', 'gettextf', |
|
202 'getwd', 'gl', 'globalenv', 'gregexpr', 'grep', 'grepRaw', 'grepl', |
|
203 'gsub', 'gzcon', 'gzfile', 'head', 'iconv', 'iconvlist', |
|
204 'icuSetCollate', 'identical', 'identity', 'ifelse', 'importIntoEnv', |
|
205 'in', 'inherits', 'intToBits', 'intToUtf8', 'interaction', 'interactive', |
|
206 'intersect', 'inverse.rle', 'invisible', 'invokeRestart', |
|
207 'invokeRestartInteractively', 'is.R', 'is.array', 'is.atomic', |
|
208 'is.call', 'is.character', 'is.complex', 'is.data.frame', |
|
209 'is.double', 'is.element', 'is.environment', 'is.expression', |
|
210 'is.factor', 'is.finite', 'is.function', 'is.infinite', |
|
211 'is.integer', 'is.language', 'is.list', 'is.loaded', 'is.logical', |
|
212 'is.matrix', 'is.na', 'is.na.POSIXlt', 'is.na.data.frame', |
|
213 'is.na.numeric_version', 'is.name', 'is.nan', 'is.null', |
|
214 'is.numeric', 'is.numeric.Date', 'is.numeric.POSIXt', |
|
215 'is.numeric.difftime', 'is.numeric_version', 'is.object', |
|
216 'is.ordered', 'is.package_version', 'is.pairlist', 'is.primitive', |
|
217 'is.qr', 'is.raw', 'is.recursive', 'is.single', 'is.symbol', |
|
218 'is.table', 'is.unsorted', 'is.vector', 'isBaseNamespace', |
|
219 'isIncomplete', 'isNamespace', 'isOpen', 'isRestart', 'isS4', |
|
220 'isSeekable', 'isSymmetric', 'isSymmetric.matrix', 'isTRUE', |
|
221 'isatty', 'isdebugged', 'jitter', 'julian', 'julian.Date', |
|
222 'julian.POSIXt', 'kappa', 'kappa.default', 'kappa.lm', 'kappa.qr', |
|
223 'kronecker', 'l10n_info', 'labels', 'labels.default', 'lapply', |
|
224 'lazyLoad', 'lazyLoadDBexec', 'lazyLoadDBfetch', 'lbeta', 'lchoose', |
|
225 'length', 'length.POSIXlt', 'letters', 'levels', 'levels.default', |
|
226 'lfactorial', 'lgamma', 'library.dynam', 'library.dynam.unload', |
|
227 'licence', 'license', 'list.dirs', 'list.files', 'list2env', 'load', |
|
228 'loadNamespace', 'loadedNamespaces', 'loadingNamespaceInfo', |
|
229 'local', 'lockBinding', 'lockEnvironment', 'log', 'log10', 'log1p', |
|
230 'log2', 'logb', 'lower.tri', 'ls', 'make.names', 'make.unique', |
|
231 'makeActiveBinding', 'mapply', 'margin.table', 'mat.or.vec', |
|
232 'match', 'match.arg', 'match.call', 'match.fun', 'max', 'max.col', |
|
233 'mean', 'mean.Date', 'mean.POSIXct', 'mean.POSIXlt', 'mean.default', |
|
234 'mean.difftime', 'mem.limits', 'memCompress', 'memDecompress', |
|
235 'memory.profile', 'merge', 'merge.data.frame', 'merge.default', |
|
236 'message', 'mget', 'min', 'missing', 'mode', 'month.abb', |
|
237 'month.name', 'months', 'months.Date', 'months.POSIXt', |
|
238 'months.abb', 'months.nameletters', 'names', 'names.POSIXlt', |
|
239 'namespaceExport', 'namespaceImport', 'namespaceImportClasses', |
|
240 'namespaceImportFrom', 'namespaceImportMethods', 'nargs', 'nchar', |
|
241 'ncol', 'new.env', 'ngettext', 'nlevels', 'noquote', 'norm', |
|
242 'normalizePath', 'nrow', 'numeric_version', 'nzchar', 'objects', |
|
243 'oldClass', 'on.exit', 'open', 'open.connection', 'open.srcfile', |
|
244 'open.srcfilealias', 'open.srcfilecopy', 'options', 'order', |
|
245 'ordered', 'outer', 'packBits', 'packageEvent', |
|
246 'packageHasNamespace', 'packageStartupMessage', 'package_version', |
|
247 'pairlist', 'parent.env', 'parent.frame', 'parse', |
|
248 'parseNamespaceFile', 'paste', 'paste0', 'path.expand', |
|
249 'path.package', 'pipe', 'pmatch', 'pmax', 'pmax.int', 'pmin', |
|
250 'pmin.int', 'polyroot', 'pos.to.env', 'pretty', 'pretty.default', |
|
251 'prettyNum', 'print', 'print.AsIs', 'print.DLLInfo', |
|
252 'print.DLLInfoList', 'print.DLLRegisteredRoutines', 'print.Date', |
|
253 'print.NativeRoutineList', 'print.POSIXct', 'print.POSIXlt', |
|
254 'print.by', 'print.condition', 'print.connection', |
|
255 'print.data.frame', 'print.default', 'print.difftime', |
|
256 'print.factor', 'print.function', 'print.hexmode', |
|
257 'print.libraryIQR', 'print.listof', 'print.noquote', |
|
258 'print.numeric_version', 'print.octmode', 'print.packageInfo', |
|
259 'print.proc_time', 'print.restart', 'print.rle', |
|
260 'print.simple.list', 'print.srcfile', 'print.srcref', |
|
261 'print.summary.table', 'print.summaryDefault', 'print.table', |
|
262 'print.warnings', 'prmatrix', 'proc.time', 'prod', 'prop.table', |
|
263 'provideDimnames', 'psigamma', 'pushBack', 'pushBackLength', 'q', |
|
264 'qr', 'qr.Q', 'qr.R', 'qr.X', 'qr.coef', 'qr.default', 'qr.fitted', |
|
265 'qr.qty', 'qr.qy', 'qr.resid', 'qr.solve', 'quarters', |
|
266 'quarters.Date', 'quarters.POSIXt', 'quit', 'quote', 'range', |
|
267 'range.default', 'rank', 'rapply', 'raw', 'rawConnection', |
|
268 'rawConnectionValue', 'rawShift', 'rawToBits', 'rawToChar', 'rbind', |
|
269 'rbind.data.frame', 'rcond', 'read.dcf', 'readBin', 'readChar', |
|
270 'readLines', 'readRDS', 'readRenviron', 'readline', 'reg.finalizer', |
|
271 'regexec', 'regexpr', 'registerS3method', 'registerS3methods', |
|
272 'regmatches', 'remove', 'removeTaskCallback', 'rep', 'rep.Date', |
|
273 'rep.POSIXct', 'rep.POSIXlt', 'rep.factor', 'rep.int', |
|
274 'rep.numeric_version', 'rep_len', 'replace', 'replicate', |
|
275 'requireNamespace', 'restartDescription', 'restartFormals', |
|
276 'retracemem', 'rev', 'rev.default', 'rle', 'rm', 'round', |
|
277 'round.Date', 'round.POSIXt', 'row', 'row.names', |
|
278 'row.names.data.frame', 'row.names.default', 'rowMeans', 'rowSums', |
|
279 'rownames', 'rowsum', 'rowsum.data.frame', 'rowsum.default', |
|
280 'sQuote', 'sample', 'sample.int', 'sapply', 'save', 'save.image', |
|
281 'saveRDS', 'scale', 'scale.default', 'scan', 'search', |
|
282 'searchpaths', 'seek', 'seek.connection', 'seq', 'seq.Date', |
|
283 'seq.POSIXt', 'seq.default', 'seq.int', 'seq_along', 'seq_len', |
|
284 'sequence', 'serialize', 'set.seed', 'setHook', 'setNamespaceInfo', |
|
285 'setSessionTimeLimit', 'setTimeLimit', 'setdiff', 'setequal', |
|
286 'setwd', 'shQuote', 'showConnections', 'sign', 'signalCondition', |
|
287 'signif', 'simpleCondition', 'simpleError', 'simpleMessage', |
|
288 'simpleWarning', 'simplify2array', 'sin', 'single', |
|
289 'sinh', 'sink', 'sink.number', 'slice.index', 'socketConnection', |
|
290 'socketSelect', 'solve', 'solve.default', 'solve.qr', 'sort', |
|
291 'sort.POSIXlt', 'sort.default', 'sort.int', 'sort.list', 'split', |
|
292 'split.Date', 'split.POSIXct', 'split.data.frame', 'split.default', |
|
293 'sprintf', 'sqrt', 'srcfile', 'srcfilealias', 'srcfilecopy', |
|
294 'srcref', 'standardGeneric', 'stderr', 'stdin', 'stdout', 'stop', |
|
295 'stopifnot', 'storage.mode', 'strftime', 'strptime', 'strsplit', |
|
296 'strtoi', 'strtrim', 'structure', 'strwrap', 'sub', 'subset', |
|
297 'subset.data.frame', 'subset.default', 'subset.matrix', |
|
298 'substitute', 'substr', 'substring', 'sum', 'summary', |
|
299 'summary.Date', 'summary.POSIXct', 'summary.POSIXlt', |
|
300 'summary.connection', 'summary.data.frame', 'summary.default', |
|
301 'summary.factor', 'summary.matrix', 'summary.proc_time', |
|
302 'summary.srcfile', 'summary.srcref', 'summary.table', |
|
303 'suppressMessages', 'suppressPackageStartupMessages', |
|
304 'suppressWarnings', 'svd', 'sweep', 'sys.call', 'sys.calls', |
|
305 'sys.frame', 'sys.frames', 'sys.function', 'sys.load.image', |
|
306 'sys.nframe', 'sys.on.exit', 'sys.parent', 'sys.parents', |
|
307 'sys.save.image', 'sys.source', 'sys.status', 'system', |
|
308 'system.file', 'system.time', 'system2', 't', 't.data.frame', |
|
309 't.default', 'table', 'tabulate', 'tail', 'tan', 'tanh', 'tapply', |
|
310 'taskCallbackManager', 'tcrossprod', 'tempdir', 'tempfile', |
|
311 'testPlatformEquivalence', 'textConnection', 'textConnectionValue', |
|
312 'toString', 'toString.default', 'tolower', 'topenv', 'toupper', |
|
313 'trace', 'traceback', 'tracemem', 'tracingState', 'transform', |
|
314 'transform.data.frame', 'transform.default', 'trigamma', 'trunc', |
|
315 'trunc.Date', 'trunc.POSIXt', 'truncate', 'truncate.connection', |
|
316 'try', 'tryCatch', 'typeof', 'unclass', 'undebug', 'union', |
|
317 'unique', 'unique.POSIXlt', 'unique.array', 'unique.data.frame', |
|
318 'unique.default', 'unique.matrix', 'unique.numeric_version', |
|
319 'units', 'units.difftime', 'unix.time', 'unlink', 'unlist', |
|
320 'unloadNamespace', 'unlockBinding', 'unname', 'unserialize', |
|
321 'unsplit', 'untrace', 'untracemem', 'unz', 'upper.tri', 'url', |
|
322 'utf8ToInt', 'vapply', 'version', 'warning', 'warnings', 'weekdays', |
|
323 'weekdays.Date', 'weekdays.POSIXt', 'which', 'which.max', |
|
324 'which.min', 'with', 'with.default', 'withCallingHandlers', |
|
325 'withRestarts', 'withVisible', 'within', 'within.data.frame', |
|
326 'within.list', 'write', 'write.dcf', 'writeBin', 'writeChar', |
|
327 'writeLines', 'xor', 'xor.hexmode', 'xor.octmode', |
|
328 'xpdrows.data.frame', 'xtfrm', 'xtfrm.AsIs', 'xtfrm.Date', |
|
329 'xtfrm.POSIXct', 'xtfrm.POSIXlt', 'xtfrm.Surv', 'xtfrm.default', |
|
330 'xtfrm.difftime', 'xtfrm.factor', 'xtfrm.numeric_version', 'xzfile', |
|
331 'zapsmall' |
|
332 ) |
|
333 |
|
334 tokens = { |
|
335 'comments': [ |
|
336 (r'#.*$', Comment.Single), |
|
337 ], |
|
338 'valid_name': [ |
|
339 (r'[a-zA-Z][\w.]*', Text), |
|
340 # can begin with ., but not if that is followed by a digit |
|
341 (r'\.[a-zA-Z_][\w.]*', Text), |
|
342 ], |
|
343 'punctuation': [ |
|
344 (r'\[{1,2}|\]{1,2}|\(|\)|;|,', Punctuation), |
|
345 ], |
|
346 'keywords': [ |
|
347 (words(builtins_base, suffix=r'(?![\w. =])'), |
|
348 Keyword.Pseudo), |
|
349 (r'(if|else|for|while|repeat|in|next|break|return|switch|function)' |
|
350 r'(?![\w.])', |
|
351 Keyword.Reserved), |
|
352 (r'(array|category|character|complex|double|function|integer|list|' |
|
353 r'logical|matrix|numeric|vector|data.frame|c)' |
|
354 r'(?![\w.])', |
|
355 Keyword.Type), |
|
356 (r'(library|require|attach|detach|source)' |
|
357 r'(?![\w.])', |
|
358 Keyword.Namespace) |
|
359 ], |
|
360 'operators': [ |
|
361 (r'<<?-|->>?|-|==|<=|>=|<|>|&&?|!=|\|\|?|\?', Operator), |
|
362 (r'\*|\+|\^|/|!|%[^%]*%|=|~|\$|@|:{1,3}', Operator) |
|
363 ], |
|
364 'builtin_symbols': [ |
|
365 (r'(NULL|NA(_(integer|real|complex|character)_)?|' |
|
366 r'letters|LETTERS|Inf|TRUE|FALSE|NaN|pi|\.\.(\.|[0-9]+))' |
|
367 r'(?![\w.])', |
|
368 Keyword.Constant), |
|
369 (r'(T|F)\b', Name.Builtin.Pseudo), |
|
370 ], |
|
371 'numbers': [ |
|
372 # hex number |
|
373 (r'0[xX][a-fA-F0-9]+([pP][0-9]+)?[Li]?', Number.Hex), |
|
374 # decimal number |
|
375 (r'[+-]?([0-9]+(\.[0-9]+)?|\.[0-9]+|\.)([eE][+-]?[0-9]+)?[Li]?', |
|
376 Number), |
|
377 ], |
|
378 'statements': [ |
|
379 include('comments'), |
|
380 # whitespaces |
|
381 (r'\s+', Text), |
|
382 (r'`.*?`', String.Backtick), |
|
383 (r'\'', String, 'string_squote'), |
|
384 (r'\"', String, 'string_dquote'), |
|
385 include('builtin_symbols'), |
|
386 include('numbers'), |
|
387 include('keywords'), |
|
388 include('punctuation'), |
|
389 include('operators'), |
|
390 include('valid_name'), |
|
391 ], |
|
392 'root': [ |
|
393 include('statements'), |
|
394 # blocks: |
|
395 (r'\{|\}', Punctuation), |
|
396 # (r'\{', Punctuation, 'block'), |
|
397 (r'.', Text), |
|
398 ], |
|
399 # 'block': [ |
|
400 # include('statements'), |
|
401 # ('\{', Punctuation, '#push'), |
|
402 # ('\}', Punctuation, '#pop') |
|
403 # ], |
|
404 'string_squote': [ |
|
405 (r'([^\'\\]|\\.)*\'', String, '#pop'), |
|
406 ], |
|
407 'string_dquote': [ |
|
408 (r'([^"\\]|\\.)*"', String, '#pop'), |
|
409 ], |
|
410 } |
|
411 |
|
412 def analyse_text(text): |
|
413 if re.search(r'[a-z0-9_\])\s]<-(?!-)', text): |
|
414 return 0.11 |
|
415 |
|
416 |
|
417 class RdLexer(RegexLexer): |
|
418 """ |
|
419 Pygments Lexer for R documentation (Rd) files |
|
420 |
|
421 This is a very minimal implementation, highlighting little more |
|
422 than the macros. A description of Rd syntax is found in `Writing R |
|
423 Extensions <http://cran.r-project.org/doc/manuals/R-exts.html>`_ |
|
424 and `Parsing Rd files <developer.r-project.org/parseRd.pdf>`_. |
|
425 |
|
426 .. versionadded:: 1.6 |
|
427 """ |
|
428 name = 'Rd' |
|
429 aliases = ['rd'] |
|
430 filenames = ['*.Rd'] |
|
431 mimetypes = ['text/x-r-doc'] |
|
432 |
|
433 # To account for verbatim / LaTeX-like / and R-like areas |
|
434 # would require parsing. |
|
435 tokens = { |
|
436 'root': [ |
|
437 # catch escaped brackets and percent sign |
|
438 (r'\\[\\{}%]', String.Escape), |
|
439 # comments |
|
440 (r'%.*$', Comment), |
|
441 # special macros with no arguments |
|
442 (r'\\(?:cr|l?dots|R|tab)\b', Keyword.Constant), |
|
443 # macros |
|
444 (r'\\[a-zA-Z]+\b', Keyword), |
|
445 # special preprocessor macros |
|
446 (r'^\s*#(?:ifn?def|endif).*\b', Comment.Preproc), |
|
447 # non-escaped brackets |
|
448 (r'[{}]', Name.Builtin), |
|
449 # everything else |
|
450 (r'[^\\%\n{}]+', Text), |
|
451 (r'.', Text), |
|
452 ] |
|
453 } |