AmberRack 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. data/.gitignore +74 -0
  2. data/.rvmrc +4 -0
  3. data/Gemfile +2 -0
  4. data/Gemfile.lock +62 -0
  5. data/README.md +17 -0
  6. data/Rakefile +24 -0
  7. data/amber-rack.gemspec +33 -0
  8. data/app/amber_app.rb +35 -0
  9. data/app/javascripts/Benchfib.deploy.js +159 -0
  10. data/app/javascripts/Benchfib.js +159 -0
  11. data/app/javascripts/Canvas.deploy.js +1464 -0
  12. data/app/javascripts/Canvas.js +1464 -0
  13. data/app/javascripts/Compiler.deploy.js +1587 -0
  14. data/app/javascripts/Compiler.js +1587 -0
  15. data/app/javascripts/Examples.deploy.js +800 -0
  16. data/app/javascripts/Examples.js +800 -0
  17. data/app/javascripts/IDE.deploy.js +3457 -0
  18. data/app/javascripts/IDE.js +3457 -0
  19. data/app/javascripts/IDE.js.bak +3420 -0
  20. data/app/javascripts/JQuery.deploy.js +898 -0
  21. data/app/javascripts/JQuery.js +898 -0
  22. data/app/javascripts/Kernel.deploy.js +6761 -0
  23. data/app/javascripts/Kernel.js +6761 -0
  24. data/app/javascripts/Parser.deploy.js +1655 -0
  25. data/app/javascripts/Parser.js +1655 -0
  26. data/app/javascripts/Raphael-Core.js +0 -0
  27. data/app/javascripts/Raphael-Graph.js +0 -0
  28. data/app/javascripts/SUnit.deploy.js +1059 -0
  29. data/app/javascripts/SUnit.js +1059 -0
  30. data/app/javascripts/application.js +2 -0
  31. data/app/javascripts/boot.js +466 -0
  32. data/app/javascripts/init.js +2 -0
  33. data/app/javascripts/jquery-1.4.4.min.js +167 -0
  34. data/app/javascripts/jquery-ui-1.8.9.custom.min.js +781 -0
  35. data/app/javascripts/jquery.textarea.js +267 -0
  36. data/app/javascripts/jtalk.js +78 -0
  37. data/app/javascripts/lib/CodeMirror/LICENSE +19 -0
  38. data/app/javascripts/lib/CodeMirror/README.md +6 -0
  39. data/app/javascripts/lib/CodeMirror/compress.html +92 -0
  40. data/app/javascripts/lib/CodeMirror/css/baboon.png +0 -0
  41. data/app/javascripts/lib/CodeMirror/css/baboon_vector.svg +153 -0
  42. data/app/javascripts/lib/CodeMirror/css/docs.css +154 -0
  43. data/app/javascripts/lib/CodeMirror/demo/activeline.html +72 -0
  44. data/app/javascripts/lib/CodeMirror/demo/changemode.html +51 -0
  45. data/app/javascripts/lib/CodeMirror/demo/complete.html +79 -0
  46. data/app/javascripts/lib/CodeMirror/demo/complete.js +151 -0
  47. data/app/javascripts/lib/CodeMirror/demo/fullscreen.html +158 -0
  48. data/app/javascripts/lib/CodeMirror/demo/marker.html +53 -0
  49. data/app/javascripts/lib/CodeMirror/demo/mustache.html +57 -0
  50. data/app/javascripts/lib/CodeMirror/demo/preview.html +75 -0
  51. data/app/javascripts/lib/CodeMirror/demo/resize.html +44 -0
  52. data/app/javascripts/lib/CodeMirror/demo/runmode.html +50 -0
  53. data/app/javascripts/lib/CodeMirror/demo/search.html +106 -0
  54. data/app/javascripts/lib/CodeMirror/demo/theme.html +53 -0
  55. data/app/javascripts/lib/CodeMirror/index.html +239 -0
  56. data/app/javascripts/lib/CodeMirror/internals.html +389 -0
  57. data/app/javascripts/lib/CodeMirror/lib/codemirror.css +67 -0
  58. data/app/javascripts/lib/CodeMirror/lib/codemirror.js +2144 -0
  59. data/app/javascripts/lib/CodeMirror/lib/overlay.js +51 -0
  60. data/app/javascripts/lib/CodeMirror/lib/runmode.js +27 -0
  61. data/app/javascripts/lib/CodeMirror/manual.html +854 -0
  62. data/app/javascripts/lib/CodeMirror/mode/clike/clike.js +247 -0
  63. data/app/javascripts/lib/CodeMirror/mode/clike/index.html +102 -0
  64. data/app/javascripts/lib/CodeMirror/mode/css/css.js +124 -0
  65. data/app/javascripts/lib/CodeMirror/mode/css/index.html +56 -0
  66. data/app/javascripts/lib/CodeMirror/mode/diff/diff.css +3 -0
  67. data/app/javascripts/lib/CodeMirror/mode/diff/diff.js +13 -0
  68. data/app/javascripts/lib/CodeMirror/mode/diff/index.html +99 -0
  69. data/app/javascripts/lib/CodeMirror/mode/haskell/haskell.js +242 -0
  70. data/app/javascripts/lib/CodeMirror/mode/haskell/index.html +60 -0
  71. data/app/javascripts/lib/CodeMirror/mode/htmlmixed/htmlmixed.js +79 -0
  72. data/app/javascripts/lib/CodeMirror/mode/htmlmixed/index.html +52 -0
  73. data/app/javascripts/lib/CodeMirror/mode/javascript/index.html +78 -0
  74. data/app/javascripts/lib/CodeMirror/mode/javascript/javascript.js +348 -0
  75. data/app/javascripts/lib/CodeMirror/mode/lua/index.html +72 -0
  76. data/app/javascripts/lib/CodeMirror/mode/lua/lua.js +138 -0
  77. data/app/javascripts/lib/CodeMirror/mode/php/index.html +49 -0
  78. data/app/javascripts/lib/CodeMirror/mode/php/php.js +110 -0
  79. data/app/javascripts/lib/CodeMirror/mode/plsql/index.html +63 -0
  80. data/app/javascripts/lib/CodeMirror/mode/plsql/plsql.js +217 -0
  81. data/app/javascripts/lib/CodeMirror/mode/python/LICENSE.txt +21 -0
  82. data/app/javascripts/lib/CodeMirror/mode/python/index.html +123 -0
  83. data/app/javascripts/lib/CodeMirror/mode/python/python.js +321 -0
  84. data/app/javascripts/lib/CodeMirror/mode/rst/index.html +526 -0
  85. data/app/javascripts/lib/CodeMirror/mode/rst/rst.css +75 -0
  86. data/app/javascripts/lib/CodeMirror/mode/rst/rst.js +333 -0
  87. data/app/javascripts/lib/CodeMirror/mode/scheme/index.html +65 -0
  88. data/app/javascripts/lib/CodeMirror/mode/scheme/scheme.js +181 -0
  89. data/app/javascripts/lib/CodeMirror/mode/smalltalk/index.html +56 -0
  90. data/app/javascripts/lib/CodeMirror/mode/smalltalk/smalltalk.js +134 -0
  91. data/app/javascripts/lib/CodeMirror/mode/sparql/index.html +41 -0
  92. data/app/javascripts/lib/CodeMirror/mode/sparql/sparql.js +143 -0
  93. data/app/javascripts/lib/CodeMirror/mode/stex/index.html +96 -0
  94. data/app/javascripts/lib/CodeMirror/mode/stex/stex.js +167 -0
  95. data/app/javascripts/lib/CodeMirror/mode/xml/index.html +42 -0
  96. data/app/javascripts/lib/CodeMirror/mode/xml/xml.js +231 -0
  97. data/app/javascripts/lib/CodeMirror/mode/yaml/index.html +68 -0
  98. data/app/javascripts/lib/CodeMirror/mode/yaml/yaml.js +95 -0
  99. data/app/javascripts/lib/CodeMirror/oldrelease.html +178 -0
  100. data/app/javascripts/lib/CodeMirror/test/index.html +29 -0
  101. data/app/javascripts/lib/CodeMirror/test/test.js +249 -0
  102. data/app/javascripts/lib/CodeMirror/theme/default.css +18 -0
  103. data/app/javascripts/lib/CodeMirror/theme/elegant.css +9 -0
  104. data/app/javascripts/lib/CodeMirror/theme/jtalk.css +21 -0
  105. data/app/javascripts/lib/CodeMirror/theme/neat.css +8 -0
  106. data/app/javascripts/lib/CodeMirror/theme/night.css +20 -0
  107. data/app/javascripts/lib/jQuery/jquery-1.4.4.min.js +167 -0
  108. data/app/javascripts/lib/jQuery/jquery-ui-1.8.9.custom.min.js +781 -0
  109. data/app/javascripts/lib/jQuery/jquery.textarea.js +267 -0
  110. data/app/smalltalk/Canvas.st +481 -0
  111. data/app/smalltalk/IDE.st +1752 -0
  112. data/config.ru +2 -0
  113. data/lib/amber-rack.rb +4 -0
  114. data/lib/amber-rack/version.rb +3 -0
  115. data/public/CodeMirror/LICENSE +19 -0
  116. data/public/CodeMirror/README.md +6 -0
  117. data/public/CodeMirror/compress.html +92 -0
  118. data/public/CodeMirror/css/baboon.png +0 -0
  119. data/public/CodeMirror/css/baboon_vector.svg +153 -0
  120. data/public/CodeMirror/css/docs.css +154 -0
  121. data/public/CodeMirror/demo/activeline.html +72 -0
  122. data/public/CodeMirror/demo/changemode.html +51 -0
  123. data/public/CodeMirror/demo/complete.html +79 -0
  124. data/public/CodeMirror/demo/complete.js +151 -0
  125. data/public/CodeMirror/demo/fullscreen.html +158 -0
  126. data/public/CodeMirror/demo/marker.html +53 -0
  127. data/public/CodeMirror/demo/mustache.html +57 -0
  128. data/public/CodeMirror/demo/preview.html +75 -0
  129. data/public/CodeMirror/demo/resize.html +44 -0
  130. data/public/CodeMirror/demo/runmode.html +50 -0
  131. data/public/CodeMirror/demo/search.html +106 -0
  132. data/public/CodeMirror/demo/theme.html +53 -0
  133. data/public/CodeMirror/index.html +239 -0
  134. data/public/CodeMirror/internals.html +389 -0
  135. data/public/CodeMirror/lib/codemirror.css +67 -0
  136. data/public/CodeMirror/lib/codemirror.js +2144 -0
  137. data/public/CodeMirror/lib/overlay.js +51 -0
  138. data/public/CodeMirror/lib/runmode.js +27 -0
  139. data/public/CodeMirror/manual.html +854 -0
  140. data/public/CodeMirror/mode/clike/clike.js +247 -0
  141. data/public/CodeMirror/mode/clike/index.html +102 -0
  142. data/public/CodeMirror/mode/css/css.js +124 -0
  143. data/public/CodeMirror/mode/css/index.html +56 -0
  144. data/public/CodeMirror/mode/diff/diff.css +3 -0
  145. data/public/CodeMirror/mode/diff/diff.js +13 -0
  146. data/public/CodeMirror/mode/diff/index.html +99 -0
  147. data/public/CodeMirror/mode/haskell/haskell.js +242 -0
  148. data/public/CodeMirror/mode/haskell/index.html +60 -0
  149. data/public/CodeMirror/mode/htmlmixed/htmlmixed.js +79 -0
  150. data/public/CodeMirror/mode/htmlmixed/index.html +52 -0
  151. data/public/CodeMirror/mode/javascript/index.html +78 -0
  152. data/public/CodeMirror/mode/javascript/javascript.js +348 -0
  153. data/public/CodeMirror/mode/lua/index.html +72 -0
  154. data/public/CodeMirror/mode/lua/lua.js +138 -0
  155. data/public/CodeMirror/mode/php/index.html +49 -0
  156. data/public/CodeMirror/mode/php/php.js +110 -0
  157. data/public/CodeMirror/mode/plsql/index.html +63 -0
  158. data/public/CodeMirror/mode/plsql/plsql.js +217 -0
  159. data/public/CodeMirror/mode/python/LICENSE.txt +21 -0
  160. data/public/CodeMirror/mode/python/index.html +123 -0
  161. data/public/CodeMirror/mode/python/python.js +321 -0
  162. data/public/CodeMirror/mode/rst/index.html +526 -0
  163. data/public/CodeMirror/mode/rst/rst.css +75 -0
  164. data/public/CodeMirror/mode/rst/rst.js +333 -0
  165. data/public/CodeMirror/mode/scheme/index.html +65 -0
  166. data/public/CodeMirror/mode/scheme/scheme.js +181 -0
  167. data/public/CodeMirror/mode/smalltalk/index.html +56 -0
  168. data/public/CodeMirror/mode/smalltalk/smalltalk.js +134 -0
  169. data/public/CodeMirror/mode/sparql/index.html +41 -0
  170. data/public/CodeMirror/mode/sparql/sparql.js +143 -0
  171. data/public/CodeMirror/mode/stex/index.html +96 -0
  172. data/public/CodeMirror/mode/stex/stex.js +167 -0
  173. data/public/CodeMirror/mode/xml/index.html +42 -0
  174. data/public/CodeMirror/mode/xml/xml.js +231 -0
  175. data/public/CodeMirror/mode/yaml/index.html +68 -0
  176. data/public/CodeMirror/mode/yaml/yaml.js +95 -0
  177. data/public/CodeMirror/oldrelease.html +178 -0
  178. data/public/CodeMirror/test/index.html +29 -0
  179. data/public/CodeMirror/test/test.js +249 -0
  180. data/public/CodeMirror/theme/default.css +18 -0
  181. data/public/CodeMirror/theme/elegant.css +9 -0
  182. data/public/CodeMirror/theme/jtalk.css +21 -0
  183. data/public/CodeMirror/theme/neat.css +8 -0
  184. data/public/CodeMirror/theme/night.css +20 -0
  185. data/public/css/jtalk.css +362 -0
  186. data/public/css/style.css +456 -0
  187. data/public/css/sunit.css +66 -0
  188. data/public/ide/fork_me.png +0 -0
  189. data/public/ide/screen2.png +0 -0
  190. data/public/ide/style.css +456 -0
  191. data/public/ide/syntax.css +61 -0
  192. data/public/ide/text_header.png +0 -0
  193. data/public/ide/title_container1.png +0 -0
  194. data/public/images/background_box.png +0 -0
  195. data/public/images/background_header.png +0 -0
  196. data/public/images/balloon_header.png +0 -0
  197. data/views/index.haml +66 -0
  198. metadata +341 -0
@@ -0,0 +1,79 @@
1
+ CodeMirror.defineMode("htmlmixed", function(config, parserConfig) {
2
+ var htmlMode = CodeMirror.getMode(config, {name: "xml", htmlMode: true});
3
+ var jsMode = CodeMirror.getMode(config, "javascript");
4
+ var cssMode = CodeMirror.getMode(config, "css");
5
+
6
+ function html(stream, state) {
7
+ var style = htmlMode.token(stream, state.htmlState);
8
+ if (style == "tag" && stream.current() == ">" && state.htmlState.context) {
9
+ if (/^script$/i.test(state.htmlState.context.tagName)) {
10
+ state.token = javascript;
11
+ state.localState = jsMode.startState(htmlMode.indent(state.htmlState, ""));
12
+ state.mode = "javascript";
13
+ }
14
+ else if (/^style$/i.test(state.htmlState.context.tagName)) {
15
+ state.token = css;
16
+ state.localState = cssMode.startState(htmlMode.indent(state.htmlState, ""));
17
+ state.mode = "css";
18
+ }
19
+ }
20
+ return style;
21
+ }
22
+ function maybeBackup(stream, pat, style) {
23
+ var cur = stream.current();
24
+ var close = cur.search(pat);
25
+ if (close > -1) stream.backUp(cur.length - close);
26
+ return style;
27
+ }
28
+ function javascript(stream, state) {
29
+ if (stream.match(/^<\/\s*script\s*>/i, false)) {
30
+ state.token = html;
31
+ state.curState = null;
32
+ state.mode = "html";
33
+ return html(stream, state);
34
+ }
35
+ return maybeBackup(stream, /<\/\s*script\s*>/,
36
+ jsMode.token(stream, state.localState));
37
+ }
38
+ function css(stream, state) {
39
+ if (stream.match(/^<\/\s*style\s*>/i, false)) {
40
+ state.token = html;
41
+ state.localState = null;
42
+ state.mode = "html";
43
+ return html(stream, state);
44
+ }
45
+ return maybeBackup(stream, /<\/\s*style\s*>/,
46
+ cssMode.token(stream, state.localState));
47
+ }
48
+
49
+ return {
50
+ startState: function() {
51
+ var state = htmlMode.startState();
52
+ return {token: html, localState: null, mode: "html", htmlState: state};
53
+ },
54
+
55
+ copyState: function(state) {
56
+ if (state.localState)
57
+ var local = CodeMirror.copyState(state.token == css ? cssMode : jsMode, state.localState);
58
+ return {token: state.token, localState: local, mode: state.mode,
59
+ htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};
60
+ },
61
+
62
+ token: function(stream, state) {
63
+ return state.token(stream, state);
64
+ },
65
+
66
+ indent: function(state, textAfter) {
67
+ if (state.token == html || /^\s*<\//.test(textAfter))
68
+ return htmlMode.indent(state.htmlState, textAfter);
69
+ else if (state.token == javascript)
70
+ return jsMode.indent(state.localState, textAfter);
71
+ else
72
+ return cssMode.indent(state.localState, textAfter);
73
+ },
74
+
75
+ electricChars: "/{}:"
76
+ }
77
+ });
78
+
79
+ CodeMirror.defineMIME("text/html", "htmlmixed");
@@ -0,0 +1,52 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <title>CodeMirror 2: HTML mixed mode</title>
5
+ <link rel="stylesheet" href="../../lib/codemirror.css">
6
+ <script src="../../lib/codemirror.js"></script>
7
+ <script src="../xml/xml.js"></script>
8
+ <script src="../javascript/javascript.js"></script>
9
+ <script src="../css/css.js"></script>
10
+ <link rel="stylesheet" href="../../theme/default.css">
11
+ <script src="htmlmixed.js"></script>
12
+ <link rel="stylesheet" href="../../css/docs.css">
13
+ <style>.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
14
+ </head>
15
+ <body>
16
+ <h1>CodeMirror 2: HTML mixed mode</h1>
17
+ <form><textarea id="code" name="code">
18
+ <html style="color: green">
19
+ <!-- this is a comment -->
20
+ <head>
21
+ <title>Mixed HTML Example</title>
22
+ <style type="text/css">
23
+ h1 {font-family: comic sans; color: #f0f;}
24
+ div {background: yellow !important;}
25
+ body {
26
+ max-width: 50em;
27
+ margin: 1em 2em 1em 5em;
28
+ }
29
+ </style>
30
+ </head>
31
+ <body>
32
+ <h1>Mixed HTML Example</h1>
33
+ <script>
34
+ function jsFunc(arg1, arg2) {
35
+ if (arg1 && arg2) document.body.innerHTML = "achoo";
36
+ }
37
+ </script>
38
+ </body>
39
+ </html>
40
+ </textarea></form>
41
+ <script>
42
+ var editor = CodeMirror.fromTextArea(document.getElementById("code"), {mode: "text/html", tabMode: "indent"});
43
+ </script>
44
+
45
+ <p>The HTML mixed mode depends on the XML, JavaScript, and CSS modes.</p>
46
+
47
+ <p><strong>MIME types defined:</strong> <code>text/html</code>
48
+ (redefined, only takes effect if you load this parser after the
49
+ XML parser).</p>
50
+
51
+ </body>
52
+ </html>
@@ -0,0 +1,78 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <title>CodeMirror 2: JavaScript mode</title>
5
+ <link rel="stylesheet" href="../../lib/codemirror.css">
6
+ <script src="../../lib/codemirror.js"></script>
7
+ <script src="javascript.js"></script>
8
+ <link rel="stylesheet" href="../../theme/default.css">
9
+ <link rel="stylesheet" href="../../css/docs.css">
10
+ <style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
11
+ </head>
12
+ <body>
13
+ <h1>CodeMirror 2: JavaScript mode</h1>
14
+
15
+ <div><textarea id="code" name="code">
16
+ // Demo code (the actual new parser character stream implementation)
17
+
18
+ function StringStream(string) {
19
+ this.pos = 0;
20
+ this.string = string;
21
+ }
22
+
23
+ StringStream.prototype = {
24
+ done: function() {return this.pos >= this.string.length;},
25
+ peek: function() {return this.string.charAt(this.pos);},
26
+ next: function() {
27
+ if (this.pos &lt; this.string.length)
28
+ return this.string.charAt(this.pos++);
29
+ },
30
+ eat: function(match) {
31
+ var ch = this.string.charAt(this.pos);
32
+ if (typeof match == "string") var ok = ch == match;
33
+ else var ok = ch &amp;&amp; match.test ? match.test(ch) : match(ch);
34
+ if (ok) {this.pos++; return ch;}
35
+ },
36
+ eatWhile: function(match) {
37
+ var start = this.pos;
38
+ while (this.eat(match));
39
+ if (this.pos > start) return this.string.slice(start, this.pos);
40
+ },
41
+ backUp: function(n) {this.pos -= n;},
42
+ column: function() {return this.pos;},
43
+ eatSpace: function() {
44
+ var start = this.pos;
45
+ while (/\s/.test(this.string.charAt(this.pos))) this.pos++;
46
+ return this.pos - start;
47
+ },
48
+ match: function(pattern, consume, caseInsensitive) {
49
+ if (typeof pattern == "string") {
50
+ function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}
51
+ if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
52
+ if (consume !== false) this.pos += str.length;
53
+ return true;
54
+ }
55
+ }
56
+ else {
57
+ var match = this.string.slice(this.pos).match(pattern);
58
+ if (match &amp;&amp; consume !== false) this.pos += match[0].length;
59
+ return match;
60
+ }
61
+ }
62
+ };
63
+ </textarea></div>
64
+
65
+ <script>
66
+ var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
67
+ lineNumbers: true,
68
+ matchBrackets: true
69
+ });
70
+ </script>
71
+
72
+ <p>JavaScript mode supports a single configuration
73
+ option, <code>json</code>, which will set the mode to expect JSON
74
+ data rather than a JavaScript program.</p>
75
+
76
+ <p><strong>MIME types defined:</strong> <code>text/javascript</code>, <code>application/json</code>.</p>
77
+ </body>
78
+ </html>
@@ -0,0 +1,348 @@
1
+ CodeMirror.defineMode("javascript", function(config, parserConfig) {
2
+ var indentUnit = config.indentUnit;
3
+ var jsonMode = parserConfig.json;
4
+
5
+ // Tokenizer
6
+
7
+ var keywords = function(){
8
+ function kw(type) {return {type: type, style: "keyword"};}
9
+ var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
10
+ var operator = kw("operator"), atom = {type: "atom", style: "atom"};
11
+ return {
12
+ "if": A, "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
13
+ "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C,
14
+ "var": kw("var"), "function": kw("function"), "catch": kw("catch"),
15
+ "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
16
+ "in": operator, "typeof": operator, "instanceof": operator,
17
+ "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom
18
+ };
19
+ }();
20
+
21
+ var isOperatorChar = /[+\-*&%=<>!?|]/;
22
+
23
+ function chain(stream, state, f) {
24
+ state.tokenize = f;
25
+ return f(stream, state);
26
+ }
27
+
28
+ function nextUntilUnescaped(stream, end) {
29
+ var escaped = false, next;
30
+ while ((next = stream.next()) != null) {
31
+ if (next == end && !escaped)
32
+ return false;
33
+ escaped = !escaped && next == "\\";
34
+ }
35
+ return escaped;
36
+ }
37
+
38
+ // Used as scratch variables to communicate multiple values without
39
+ // consing up tons of objects.
40
+ var type, content;
41
+ function ret(tp, style, cont) {
42
+ type = tp; content = cont;
43
+ return style;
44
+ }
45
+
46
+ function jsTokenBase(stream, state) {
47
+ var ch = stream.next();
48
+ if (ch == '"' || ch == "'")
49
+ return chain(stream, state, jsTokenString(ch));
50
+ else if (/[\[\]{}\(\),;\:\.]/.test(ch))
51
+ return ret(ch);
52
+ else if (ch == "0" && stream.eat(/x/i)) {
53
+ stream.eatWhile(/[\da-f]/i);
54
+ return ret("number", "number");
55
+ }
56
+ else if (/\d/.test(ch)) {
57
+ stream.match(/^\d*(?:\.\d*)?(?:e[+\-]?\d+)?/);
58
+ return ret("number", "number");
59
+ }
60
+ else if (ch == "/") {
61
+ if (stream.eat("*")) {
62
+ return chain(stream, state, jsTokenComment);
63
+ }
64
+ else if (stream.eat("/")) {
65
+ stream.skipToEnd();
66
+ return ret("comment", "comment");
67
+ }
68
+ else if (state.reAllowed) {
69
+ nextUntilUnescaped(stream, "/");
70
+ stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla
71
+ return ret("regexp", "string");
72
+ }
73
+ else {
74
+ stream.eatWhile(isOperatorChar);
75
+ return ret("operator", null, stream.current());
76
+ }
77
+ }
78
+ else if (isOperatorChar.test(ch)) {
79
+ stream.eatWhile(isOperatorChar);
80
+ return ret("operator", null, stream.current());
81
+ }
82
+ else {
83
+ stream.eatWhile(/[\w\$_]/);
84
+ var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
85
+ return known ? ret(known.type, known.style, word) :
86
+ ret("variable", "variable", word);
87
+ }
88
+ }
89
+
90
+ function jsTokenString(quote) {
91
+ return function(stream, state) {
92
+ if (!nextUntilUnescaped(stream, quote))
93
+ state.tokenize = jsTokenBase;
94
+ return ret("string", "string");
95
+ };
96
+ }
97
+
98
+ function jsTokenComment(stream, state) {
99
+ var maybeEnd = false, ch;
100
+ while (ch = stream.next()) {
101
+ if (ch == "/" && maybeEnd) {
102
+ state.tokenize = jsTokenBase;
103
+ break;
104
+ }
105
+ maybeEnd = (ch == "*");
106
+ }
107
+ return ret("comment", "comment");
108
+ }
109
+
110
+ // Parser
111
+
112
+ var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true};
113
+
114
+ function JSLexical(indented, column, type, align, prev, info) {
115
+ this.indented = indented;
116
+ this.column = column;
117
+ this.type = type;
118
+ this.prev = prev;
119
+ this.info = info;
120
+ if (align != null) this.align = align;
121
+ }
122
+
123
+ function inScope(state, varname) {
124
+ for (var v = state.localVars; v; v = v.next)
125
+ if (v.name == varname) return true;
126
+ }
127
+
128
+ function parseJS(state, style, type, content, stream) {
129
+ var cc = state.cc;
130
+ // Communicate our context to the combinators.
131
+ // (Less wasteful than consing up a hundred closures on every call.)
132
+ cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
133
+
134
+ if (!state.lexical.hasOwnProperty("align"))
135
+ state.lexical.align = true;
136
+
137
+ while(true) {
138
+ var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
139
+ if (combinator(type, content)) {
140
+ while(cc.length && cc[cc.length - 1].lex)
141
+ cc.pop()();
142
+ if (cx.marked) return cx.marked;
143
+ if (type == "variable" && inScope(state, content)) return "variable-2";
144
+ return style;
145
+ }
146
+ }
147
+ }
148
+
149
+ // Combinator utils
150
+
151
+ var cx = {state: null, column: null, marked: null, cc: null};
152
+ function pass() {
153
+ for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
154
+ }
155
+ function cont() {
156
+ pass.apply(null, arguments);
157
+ return true;
158
+ }
159
+ function register(varname) {
160
+ var state = cx.state;
161
+ if (state.context) {
162
+ cx.marked = "def";
163
+ for (var v = state.localVars; v; v = v.next)
164
+ if (v.name == varname) return;
165
+ state.localVars = {name: varname, next: state.localVars};
166
+ }
167
+ }
168
+
169
+ // Combinators
170
+
171
+ var defaultVars = {name: "this", next: {name: "arguments"}};
172
+ function pushcontext() {
173
+ if (!cx.state.context) cx.state.localVars = defaultVars;
174
+ cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
175
+ }
176
+ function popcontext() {
177
+ cx.state.localVars = cx.state.context.vars;
178
+ cx.state.context = cx.state.context.prev;
179
+ }
180
+ function pushlex(type, info) {
181
+ var result = function() {
182
+ var state = cx.state;
183
+ state.lexical = new JSLexical(state.indented, cx.stream.column(), type, null, state.lexical, info)
184
+ };
185
+ result.lex = true;
186
+ return result;
187
+ }
188
+ function poplex() {
189
+ var state = cx.state;
190
+ if (state.lexical.prev) {
191
+ if (state.lexical.type == ")")
192
+ state.indented = state.lexical.indented;
193
+ state.lexical = state.lexical.prev;
194
+ }
195
+ }
196
+ poplex.lex = true;
197
+
198
+ function expect(wanted) {
199
+ return function expecting(type) {
200
+ if (type == wanted) return cont();
201
+ else if (wanted == ";") return pass();
202
+ else return cont(arguments.callee);
203
+ };
204
+ }
205
+
206
+ function statement(type) {
207
+ if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex);
208
+ if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
209
+ if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
210
+ if (type == "{") return cont(pushlex("}"), block, poplex);
211
+ if (type == ";") return cont();
212
+ if (type == "function") return cont(functiondef);
213
+ if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),
214
+ poplex, statement, poplex);
215
+ if (type == "variable") return cont(pushlex("stat"), maybelabel);
216
+ if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
217
+ block, poplex, poplex);
218
+ if (type == "case") return cont(expression, expect(":"));
219
+ if (type == "default") return cont(expect(":"));
220
+ if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
221
+ statement, poplex, popcontext);
222
+ return pass(pushlex("stat"), expression, expect(";"), poplex);
223
+ }
224
+ function expression(type) {
225
+ if (atomicTypes.hasOwnProperty(type)) return cont(maybeoperator);
226
+ if (type == "function") return cont(functiondef);
227
+ if (type == "keyword c") return cont(expression);
228
+ if (type == "(") return cont(pushlex(")"), expression, expect(")"), poplex, maybeoperator);
229
+ if (type == "operator") return cont(expression);
230
+ if (type == "[") return cont(pushlex("]"), commasep(expression, "]"), poplex, maybeoperator);
231
+ if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator);
232
+ return cont();
233
+ }
234
+ function maybeoperator(type, value) {
235
+ if (type == "operator" && /\+\+|--/.test(value)) return cont(maybeoperator);
236
+ if (type == "operator") return cont(expression);
237
+ if (type == ";") return;
238
+ if (type == "(") return cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator);
239
+ if (type == ".") return cont(property, maybeoperator);
240
+ if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator);
241
+ }
242
+ function maybelabel(type) {
243
+ if (type == ":") return cont(poplex, statement);
244
+ return pass(maybeoperator, expect(";"), poplex);
245
+ }
246
+ function property(type) {
247
+ if (type == "variable") {cx.marked = "property"; return cont();}
248
+ }
249
+ function objprop(type) {
250
+ if (type == "variable") cx.marked = "property";
251
+ if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expression);
252
+ }
253
+ function commasep(what, end) {
254
+ function proceed(type) {
255
+ if (type == ",") return cont(what, proceed);
256
+ if (type == end) return cont();
257
+ return cont(expect(end));
258
+ }
259
+ return function commaSeparated(type) {
260
+ if (type == end) return cont();
261
+ else return pass(what, proceed);
262
+ };
263
+ }
264
+ function block(type) {
265
+ if (type == "}") return cont();
266
+ return pass(statement, block);
267
+ }
268
+ function vardef1(type, value) {
269
+ if (type == "variable"){register(value); return cont(vardef2);}
270
+ return cont();
271
+ }
272
+ function vardef2(type, value) {
273
+ if (value == "=") return cont(expression, vardef2);
274
+ if (type == ",") return cont(vardef1);
275
+ }
276
+ function forspec1(type) {
277
+ if (type == "var") return cont(vardef1, forspec2);
278
+ if (type == ";") return pass(forspec2);
279
+ if (type == "variable") return cont(formaybein);
280
+ return pass(forspec2);
281
+ }
282
+ function formaybein(type, value) {
283
+ if (value == "in") return cont(expression);
284
+ return cont(maybeoperator, forspec2);
285
+ }
286
+ function forspec2(type, value) {
287
+ if (type == ";") return cont(forspec3);
288
+ if (value == "in") return cont(expression);
289
+ return cont(expression, expect(";"), forspec3);
290
+ }
291
+ function forspec3(type) {
292
+ if (type != ")") cont(expression);
293
+ }
294
+ function functiondef(type, value) {
295
+ if (type == "variable") {register(value); return cont(functiondef);}
296
+ if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, statement, popcontext);
297
+ }
298
+ function funarg(type, value) {
299
+ if (type == "variable") {register(value); return cont();}
300
+ }
301
+
302
+ // Interface
303
+
304
+ return {
305
+ startState: function(basecolumn) {
306
+ return {
307
+ tokenize: jsTokenBase,
308
+ reAllowed: true,
309
+ cc: [],
310
+ lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
311
+ localVars: null,
312
+ context: null,
313
+ indented: 0
314
+ };
315
+ },
316
+
317
+ token: function(stream, state) {
318
+ if (stream.sol()) {
319
+ if (!state.lexical.hasOwnProperty("align"))
320
+ state.lexical.align = false;
321
+ state.indented = stream.indentation();
322
+ }
323
+ if (stream.eatSpace()) return null;
324
+ var style = state.tokenize(stream, state);
325
+ if (type == "comment") return style;
326
+ state.reAllowed = type == "operator" || type == "keyword c" || type.match(/^[\[{}\(,;:]$/);
327
+ return parseJS(state, style, type, content, stream);
328
+ },
329
+
330
+ indent: function(state, textAfter) {
331
+ if (state.tokenize != jsTokenBase) return 0;
332
+ var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical,
333
+ type = lexical.type, closing = firstChar == type;
334
+ if (type == "vardef") return lexical.indented + 4;
335
+ else if (type == "form" && firstChar == "{") return lexical.indented;
336
+ else if (type == "stat" || type == "form") return lexical.indented + indentUnit;
337
+ else if (lexical.info == "switch" && !closing)
338
+ return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
339
+ else if (lexical.align) return lexical.column + (closing ? 0 : 1);
340
+ else return lexical.indented + (closing ? 0 : indentUnit);
341
+ },
342
+
343
+ electricChars: ":{}"
344
+ };
345
+ });
346
+
347
+ CodeMirror.defineMIME("text/javascript", "javascript");
348
+ CodeMirror.defineMIME("application/json", {name: "javascript", json: true});