diff options
author | Nivesh Rajbhandari | 2012-02-20 11:14:44 -0800 |
---|---|---|
committer | Nivesh Rajbhandari | 2012-02-20 11:14:44 -0800 |
commit | abf78e2d7a97d295ce5a1c425fd359d47379137e (patch) | |
tree | d08c91bd2aef31e6325e0b499b2ffc390018bec6 /imports/codemirror/mode/ruby | |
parent | e80a79bff57fecf3aa9b869d8ed2de5fd815287c (diff) | |
parent | e23708721a71ca4c71365f5f8e8ac7d6113926db (diff) | |
download | ninja-abf78e2d7a97d295ce5a1c425fd359d47379137e.tar.gz |
Merge branch 'refs/heads/ninja-internal' into ToolFixes
Diffstat (limited to 'imports/codemirror/mode/ruby')
-rwxr-xr-x | imports/codemirror/mode/ruby/LICENSE | 24 | ||||
-rwxr-xr-x | imports/codemirror/mode/ruby/index.html | 171 | ||||
-rwxr-xr-x | imports/codemirror/mode/ruby/ruby.js | 195 |
3 files changed, 390 insertions, 0 deletions
diff --git a/imports/codemirror/mode/ruby/LICENSE b/imports/codemirror/mode/ruby/LICENSE new file mode 100755 index 00000000..ac09fc40 --- /dev/null +++ b/imports/codemirror/mode/ruby/LICENSE | |||
@@ -0,0 +1,24 @@ | |||
1 | Copyright (c) 2011, Ubalo, Inc. | ||
2 | All rights reserved. | ||
3 | |||
4 | Redistribution and use in source and binary forms, with or without | ||
5 | modification, 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 | |||
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
18 | DISCLAIMED. IN NO EVENT SHALL UBALO, INC BE LIABLE FOR ANY | ||
19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
22 | ON 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 | ||
24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
diff --git a/imports/codemirror/mode/ruby/index.html b/imports/codemirror/mode/ruby/index.html new file mode 100755 index 00000000..6d33db19 --- /dev/null +++ b/imports/codemirror/mode/ruby/index.html | |||
@@ -0,0 +1,171 @@ | |||
1 | <!doctype html> | ||
2 | <html> | ||
3 | <head> | ||
4 | <title>CodeMirror: Ruby mode</title> | ||
5 | <link rel="stylesheet" href="../../lib/codemirror.css"> | ||
6 | <script src="../../lib/codemirror.js"></script> | ||
7 | <script src="ruby.js"></script> | ||
8 | <style> | ||
9 | .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;} | ||
10 | .cm-s-default span.cm-arrow { color: red; } | ||
11 | </style> | ||
12 | <link rel="stylesheet" href="../../doc/docs.css"> | ||
13 | </head> | ||
14 | <body> | ||
15 | <h1>CodeMirror: Ruby mode</h1> | ||
16 | <form><textarea id="code" name="code"> | ||
17 | # Code from http://sandbox.mc.edu/~bennet/ruby/code/poly_rb.html | ||
18 | # | ||
19 | # This program evaluates polynomials. It first asks for the coefficients | ||
20 | # of a polynomial, which must be entered on one line, highest-order first. | ||
21 | # It then requests values of x and will compute the value of the poly for | ||
22 | # each x. It will repeatly ask for x values, unless you the user enters | ||
23 | # a blank line. It that case, it will ask for another polynomial. If the | ||
24 | # user types quit for either input, the program immediately exits. | ||
25 | # | ||
26 | |||
27 | # | ||
28 | # Function to evaluate a polynomial at x. The polynomial is given | ||
29 | # as a list of coefficients, from the greatest to the least. | ||
30 | def polyval(x, coef) | ||
31 | sum = 0 | ||
32 | coef = coef.clone # Don't want to destroy the original | ||
33 | while true | ||
34 | sum += coef.shift # Add and remove the next coef | ||
35 | break if coef.empty? # If no more, done entirely. | ||
36 | sum *= x # This happens the right number of times. | ||
37 | end | ||
38 | return sum | ||
39 | end | ||
40 | |||
41 | # | ||
42 | # Function to read a line containing a list of integers and return | ||
43 | # them as an array of integers. If the string conversion fails, it | ||
44 | # throws TypeError. If the input line is the word 'quit', then it | ||
45 | # converts it to an end-of-file exception | ||
46 | def readints(prompt) | ||
47 | # Read a line | ||
48 | print prompt | ||
49 | line = readline.chomp | ||
50 | raise EOFError.new if line == 'quit' # You can also use a real EOF. | ||
51 | |||
52 | # Go through each item on the line, converting each one and adding it | ||
53 | # to retval. | ||
54 | retval = [ ] | ||
55 | for str in line.split(/\s+/) | ||
56 | if str =~ /^\-?\d+$/ | ||
57 | retval.push(str.to_i) | ||
58 | else | ||
59 | raise TypeError.new | ||
60 | end | ||
61 | end | ||
62 | |||
63 | return retval | ||
64 | end | ||
65 | |||
66 | # | ||
67 | # Take a coeff and an exponent and return the string representation, ignoring | ||
68 | # the sign of the coefficient. | ||
69 | def term_to_str(coef, exp) | ||
70 | ret = "" | ||
71 | |||
72 | # Show coeff, unless it's 1 or at the right | ||
73 | coef = coef.abs | ||
74 | ret = coef.to_s unless coef == 1 && exp > 0 | ||
75 | ret += "x" if exp > 0 # x if exponent not 0 | ||
76 | ret += "^" + exp.to_s if exp > 1 # ^exponent, if > 1. | ||
77 | |||
78 | return ret | ||
79 | end | ||
80 | |||
81 | # | ||
82 | # Create a string of the polynomial in sort-of-readable form. | ||
83 | def polystr(p) | ||
84 | # Get the exponent of first coefficient, plus 1. | ||
85 | exp = p.length | ||
86 | |||
87 | # Assign exponents to each term, making pairs of coeff and exponent, | ||
88 | # Then get rid of the zero terms. | ||
89 | p = (p.map { |c| exp -= 1; [ c, exp ] }).select { |p| p[0] != 0 } | ||
90 | |||
91 | # If there's nothing left, it's a zero | ||
92 | return "0" if p.empty? | ||
93 | |||
94 | # *** Now p is a non-empty list of [ coef, exponent ] pairs. *** | ||
95 | |||
96 | # Convert the first term, preceded by a "-" if it's negative. | ||
97 | result = (if p[0][0] < 0 then "-" else "" end) + term_to_str(*p[0]) | ||
98 | |||
99 | # Convert the rest of the terms, in each case adding the appropriate | ||
100 | # + or - separating them. | ||
101 | for term in p[1...p.length] | ||
102 | # Add the separator then the rep. of the term. | ||
103 | result += (if term[0] < 0 then " - " else " + " end) + | ||
104 | term_to_str(*term) | ||
105 | end | ||
106 | |||
107 | return result | ||
108 | end | ||
109 | |||
110 | # | ||
111 | # Run until some kind of endfile. | ||
112 | begin | ||
113 | # Repeat until an exception or quit gets us out. | ||
114 | while true | ||
115 | # Read a poly until it works. An EOF will except out of the | ||
116 | # program. | ||
117 | print "\n" | ||
118 | begin | ||
119 | poly = readints("Enter a polynomial coefficients: ") | ||
120 | rescue TypeError | ||
121 | print "Try again.\n" | ||
122 | retry | ||
123 | end | ||
124 | break if poly.empty? | ||
125 | |||
126 | # Read and evaluate x values until the user types a blank line. | ||
127 | # Again, an EOF will except out of the pgm. | ||
128 | while true | ||
129 | # Request an integer. | ||
130 | print "Enter x value or blank line: " | ||
131 | x = readline.chomp | ||
132 | break if x == '' | ||
133 | raise EOFError.new if x == 'quit' | ||
134 | |||
135 | # If it looks bad, let's try again. | ||
136 | if x !~ /^\-?\d+$/ | ||
137 | print "That doesn't look like an integer. Please try again.\n" | ||
138 | next | ||
139 | end | ||
140 | |||
141 | # Convert to an integer and print the result. | ||
142 | x = x.to_i | ||
143 | print "p(x) = ", polystr(poly), "\n" | ||
144 | print "p(", x, ") = ", polyval(x, poly), "\n" | ||
145 | end | ||
146 | end | ||
147 | rescue EOFError | ||
148 | print "\n=== EOF ===\n" | ||
149 | rescue Interrupt, SignalException | ||
150 | print "\n=== Interrupted ===\n" | ||
151 | else | ||
152 | print "--- Bye ---\n" | ||
153 | end | ||
154 | </textarea></form> | ||
155 | <script> | ||
156 | var editor = CodeMirror.fromTextArea(document.getElementById("code"), { | ||
157 | mode: "text/x-ruby", | ||
158 | tabMode: "indent", | ||
159 | matchBrackets: true, | ||
160 | indentUnit: 4 | ||
161 | }); | ||
162 | </script> | ||
163 | |||
164 | <p><strong>MIME types defined:</strong> <code>text/x-ruby</code>.</p> | ||
165 | |||
166 | <p>Development of the CodeMirror Ruby mode was kindly sponsored | ||
167 | by <a href="http://ubalo.com/">Ubalo</a>, who hold | ||
168 | the <a href="LICENSE">license</a>.</p> | ||
169 | |||
170 | </body> | ||
171 | </html> | ||
diff --git a/imports/codemirror/mode/ruby/ruby.js b/imports/codemirror/mode/ruby/ruby.js new file mode 100755 index 00000000..ddc1a654 --- /dev/null +++ b/imports/codemirror/mode/ruby/ruby.js | |||
@@ -0,0 +1,195 @@ | |||
1 | CodeMirror.defineMode("ruby", function(config, parserConfig) { | ||
2 | function wordObj(words) { | ||
3 | var o = {}; | ||
4 | for (var i = 0, e = words.length; i < e; ++i) o[words[i]] = true; | ||
5 | return o; | ||
6 | } | ||
7 | var keywords = wordObj([ | ||
8 | "alias", "and", "BEGIN", "begin", "break", "case", "class", "def", "defined?", "do", "else", | ||
9 | "elsif", "END", "end", "ensure", "false", "for", "if", "in", "module", "next", "not", "or", | ||
10 | "redo", "rescue", "retry", "return", "self", "super", "then", "true", "undef", "unless", | ||
11 | "until", "when", "while", "yield", "nil", "raise", "throw", "catch", "fail", "loop", "callcc", | ||
12 | "caller", "lambda", "proc", "public", "protected", "private", "require", "load", | ||
13 | "require_relative", "extend", "autoload" | ||
14 | ]); | ||
15 | var indentWords = wordObj(["def", "class", "case", "for", "while", "do", "module", "then", | ||
16 | "unless", "catch", "loop", "proc"]); | ||
17 | var dedentWords = wordObj(["end", "until"]); | ||
18 | var matching = {"[": "]", "{": "}", "(": ")"}; | ||
19 | var curPunc; | ||
20 | |||
21 | function chain(newtok, stream, state) { | ||