aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/mode/r
diff options
context:
space:
mode:
Diffstat (limited to 'imports/codemirror/mode/r')
-rwxr-xr-ximports/codemirror/mode/r/LICENSE24
-rwxr-xr-ximports/codemirror/mode/r/index.html73
-rwxr-xr-ximports/codemirror/mode/r/r.js141
3 files changed, 238 insertions, 0 deletions
diff --git a/imports/codemirror/mode/r/LICENSE b/imports/codemirror/mode/r/LICENSE
new file mode 100755
index 00000000..2510ae16
--- /dev/null
+++ b/imports/codemirror/mode/r/LICENSE
@@ -0,0 +1,24 @@
1Copyright (c) 2011, Ubalo, Inc.
2All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are met:
6 * Redistributions of source code must retain the above copyright
7 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 * Neither the name of the Ubalo, Inc nor the names of its
12 contributors may be used to endorse or promote products derived
13 from this software without specific prior written permission.
14
15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18DISCLAIMED. IN NO EVENT SHALL UBALO, INC BE LIABLE FOR ANY
19DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/imports/codemirror/mode/r/index.html b/imports/codemirror/mode/r/index.html
new file mode 100755
index 00000000..69775055
--- /dev/null
+++ b/imports/codemirror/mode/r/index.html
@@ -0,0 +1,73 @@
1<!doctype html>
2<html>
3 <head>
4 <title>CodeMirror: R mode</title>
5 <link rel="stylesheet" href="../../lib/codemirror.css">
6 <script src="../../lib/codemirror.js"></script>
7 <script src="r.js"></script>
8 <style>
9 .CodeMirror { border-top: 1px solid silver; border-bottom: 1px solid silver; }
10 .cm-s-default span.cm-semi { color: blue; font-weight: bold; }
11 .cm-s-default span.cm-dollar { color: orange; font-weight: bold; }
12 .cm-s-default span.cm-arrow { color: brown; }
13 .cm-s-default span.cm-arg-is { color: brown; }
14 </style>
15 <link rel="stylesheet" href="../../doc/docs.css">
16 </head>
17 <body>
18 <h1>CodeMirror: R mode</h1>
19 <form><textarea id="code" name="code">
20# Code from http://www.mayin.org/ajayshah/KB/R/
21
22# FIRST LEARN ABOUT LISTS --
23X = list(height=5.4, weight=54)
24print("Use default printing --")
25print(X)
26print("Accessing individual elements --")
27cat("Your height is ", X$height, " and your weight is ", X$weight, "\n")
28
29# FUNCTIONS --
30square <- function(x) {
31 return(x*x)
32}
33cat("The square of 3 is ", square(3), "\n")
34
35 # default value of the arg is set to 5.
36cube <- function(x=5) {
37 return(x*x*x);
38}
39cat("Calling cube with 2 : ", cube(2), "\n") # will give 2^3
40cat("Calling cube : ", cube(), "\n") # will default to 5^3.
41
42# LEARN ABOUT FUNCTIONS THAT RETURN MULTIPLE OBJECTS --
43powers <- function(x) {
44 parcel = list(x2=x*x, x3=x*x*x, x4=x*x*x*x);
45 return(parcel);
46}
47
48X = powers(3);
49print("Showing powers of 3 --"); print(X);
50
51# WRITING THIS COMPACTLY (4 lines instead of 7)
52
53powerful <- function(x) {
54 return(list(x2=x*x, x3=x*x*x, x4=x*x*x*x));
55}
56print("Showing powers of 3 --"); print(powerful(3));
57
58# In R, the last expression in a function is, by default, what is
59# returned. So you could equally just say:
60powerful <- function(x) {list(x2=x*x, x3=x*x*x, x4=x*x*x*x)}
61</textarea></form>
62 <script>
63 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
64 </script>
65
66 <p><strong>MIME types defined:</strong> <code>text/x-rsrc</code>.</p>
67
68 <p>Development of the CodeMirror R mode was kindly sponsored
69 by <a href="http://ubalo.com/">Ubalo</a>, who hold
70 the <a href="LICENSE">license</a>.</p>
71
72 </body>
73</html>
diff --git a/imports/codemirror/mode/r/r.js b/imports/codemirror/mode/r/r.js
new file mode 100755
index 00000000..53647f23
--- /dev/null
+++ b/imports/codemirror/mode/r/r.js
@@ -0,0 +1,141 @@
1CodeMirror.defineMode("r", function(config) {
2 function wordObj(str) {
3 var words = str.split(" "), res = {};
4 for (var i = 0; i < words.length; ++i) res[words[i]] = true;
5 return res;
6 }
7 var atoms = wordObj("NULL NA Inf NaN NA_integer_ NA_real_ NA_complex_ NA_character_");
8 var builtins = wordObj("list quote bquote eval return call parse deparse");
9 var keywords = wordObj("if else repeat while function for in next break");
10 var blockkeywords = wordObj("if else repeat while function for");
11 var opChars = /[+\-*\/^<>=!&|~$:]/;
12 var curPunc;
13
14 function tokenBase(stream, state) {
15 curPunc = null;
16 var ch = stream.next();
17 if (ch == "#") {
18 stream.skipToEnd();
19 return "comment";
20 } else if (ch == "0" && stream.eat("x")) {
21 stream.eatWhile(/[\da-f]/i);
22 return "number";
23 } else if (ch == "." && stream.eat(/\d/)) {
24 stream.match(/\d*(?:e[+\-]?\d+)?/);
25 return "number";
26 } else if (/\d/.test(ch)) {
27 stream.match(/\d*(?:\.\d+)?(?:e[+\-]\d+)?L?/);
28 return "number";
29 } else if (ch == "'" || ch == '"') {
30 state.tokenize = tokenString(ch);
31 return "string";
32 } else if (ch == "." && stream.match(/.[.\d]+/)) {
33 return "keyword";
34 } else if (/[\w\.]/.test(ch) && ch != "_") {
35 stream.eatWhile(/[\w\.]/);
36 var word = stream.current();
37 if (atoms.propertyIsEnumerable(word)) return "atom";
38 if (keywords.propertyIsEnumerable(word)) {
39 if (blockkeywords.propertyIsEnumerable(word)) curPunc = "block";
40 return "keyword";
41 }
42 if (builtins.propertyIsEnumerable(word)) return "builtin";
43 return "variable";
44 } else if (ch == "%") {
45 if (stream.skipTo("%")) stream.next();
46 return "variable-2";
47 } else if (ch == "<" && stream.eat("-")) {
48 return "arrow";
49 } else if (ch == "=" && state.ctx.argList) {
50 return "arg-is";
51 } else if (opChars.test(ch)) {
52 if (ch == "$") return "dollar";
53 stream.eatWhile(opChars);
54 return "operator";
55 } else if (/[\(\){}\[\];]/.test(ch)) {
56 curPunc = ch;
57 if (ch == ";") return "semi";
58 return null;
59 } else {
60 return null;
61 }
62 }
63
64 function tokenString(quote) {
65 return function(stream, state) {
66 if (stream.eat("\\")) {
67 var ch = stream.next();
68 if (ch == "x") stream.match(/^[a-f0-9]{2}/i);
69 else if ((ch == "u" || ch == "U") && stream.eat("{") && stream.skipTo("}")) stream.next();
70 else if (ch == "u") stream.match(/^[a-f0-9]{4}/i);
71 else if (ch == "U") stream.match(/^[a-f0-9]{8}/i);
72 else if (/[0-7]/.test(ch)) stream.match(/^[0-7]{1,2}/);
73 return "string-2";
74 } else {
75 var next;
76 while ((next = stream.next()) != null) {
77 if (next == quote) { state.tokenize = tokenBase; break; }
78 if (next == "\\") { stream.backUp(1); break; }
79 }
80 return "string";
81 }
82 };
83 }
84
85 function push(state, type, stream) {
86 state.ctx = {type: type,
87 indent: state.indent,
88 align: null,
89 column: stream.column(),
90 prev: state.ctx};
91 }
92 function pop(state) {
93 state.indent = state.ctx.indent;
94 state.ctx = state.ctx.prev;
95 }
96
97 return {
98 startState: function(base) {
99 return {tokenize: tokenBase,
100 ctx: {type: "top",
101 indent: -config.indentUnit,
102 align: false},
103 indent: 0,
104 afterIdent: false};
105 },
106
107 token: function(stream, state) {
108 if (stream.sol()) {
109 if (state.ctx.align == null) state.ctx.align = false;
110 state.indent = stream.indentation();
111 }
112 if (stream.eatSpace()) return null;
113 var style = state.tokenize(stream, state);
114 if (style != "comment" && state.ctx.align == null) state.ctx.align = true;
115
116 var ctype = state.ctx.type;
117 if ((curPunc == ";" || curPunc == "{" || curPunc == "}") && ctype == "block") pop(state);
118 if (curPunc == "{") push(state, "}", stream);
119 else if (curPunc == "(") {
120 push(state, ")", stream);
121 if (state.afterIdent) state.ctx.argList = true;
122 }
123 else if (curPunc == "[") push(state, "]", stream);
124 else if (curPunc == "block") push(state, "block", stream);
125 else if (curPunc == ctype) pop(state);
126 state.afterIdent = style == "variable" || style == "keyword";
127 return style;
128 },
129
130 indent: function(state, textAfter) {
131 if (state.tokenize != tokenBase) return 0;
132 var firstChar = textAfter && te