diff options
author | Wu Cheng-Han | 2015-05-04 15:53:29 +0800 |
---|---|---|
committer | Wu Cheng-Han | 2015-05-04 15:53:29 +0800 |
commit | 4b0ca55eb79e963523eb6c8197825e9e8ae904e2 (patch) | |
tree | 574f3923af77b37b41dbf1b00bcd7827ef724a28 /public/vendor/codemirror/mode/ruby | |
parent | 61eb11d23c65c9e5c493c67d055f785cbec139e2 (diff) |
First commit, version 0.2.7
Diffstat (limited to 'public/vendor/codemirror/mode/ruby')
-rwxr-xr-x | public/vendor/codemirror/mode/ruby/index.html | 183 | ||||
-rwxr-xr-x | public/vendor/codemirror/mode/ruby/ruby.js | 285 | ||||
-rwxr-xr-x | public/vendor/codemirror/mode/ruby/test.js | 14 |
3 files changed, 482 insertions, 0 deletions
diff --git a/public/vendor/codemirror/mode/ruby/index.html b/public/vendor/codemirror/mode/ruby/index.html new file mode 100755 index 00000000..97544bab --- /dev/null +++ b/public/vendor/codemirror/mode/ruby/index.html @@ -0,0 +1,183 @@ +<!doctype html> + +<title>CodeMirror: Ruby mode</title> +<meta charset="utf-8"/> +<link rel=stylesheet href="../../doc/docs.css"> + +<link rel="stylesheet" href="../../lib/codemirror.css"> +<script src="../../lib/codemirror.js"></script> +<script src="../../addon/edit/matchbrackets.js"></script> +<script src="ruby.js"></script> +<style> + .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;} + .cm-s-default span.cm-arrow { color: red; } + </style> +<div id=nav> + <a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a> + + <ul> + <li><a href="../../index.html">Home</a> + <li><a href="../../doc/manual.html">Manual</a> + <li><a href="https://github.com/codemirror/codemirror">Code</a> + </ul> + <ul> + <li><a href="../index.html">Language modes</a> + <li><a class=active href="#">Ruby</a> + </ul> +</div> + +<article> +<h2>Ruby mode</h2> +<form><textarea id="code" name="code"> +# Code from http://sandbox.mc.edu/~bennet/ruby/code/poly_rb.html +# +# This program evaluates polynomials. It first asks for the coefficients +# of a polynomial, which must be entered on one line, highest-order first. +# It then requests values of x and will compute the value of the poly for +# each x. It will repeatly ask for x values, unless you the user enters +# a blank line. It that case, it will ask for another polynomial. If the +# user types quit for either input, the program immediately exits. +# + +# +# Function to evaluate a polynomial at x. The polynomial is given +# as a list of coefficients, from the greatest to the least. +def polyval(x, coef) + sum = 0 + coef = coef.clone # Don't want to destroy the original + while true + sum += coef.shift # Add and remove the next coef + break if coef.empty? # If no more, done entirely. + sum *= x # This happens the right number of times. + end + return sum +end + +# +# Function to read a line containing a list of integers and return +# them as an array of integers. If the string conversion fails, it +# throws TypeError. If the input line is the word 'quit', then it +# converts it to an end-of-file exception +def readints(prompt) + # Read a line + print prompt + line = readline.chomp + raise EOFError.new if line == 'quit' # You can also use a real EOF. + + # Go through each item on the line, converting each one and adding it + # to retval. + retval = [ ] + for str in line.split(/\s+/) + if str =~ /^\-?\d+$/ + retval.push(str.to_i) + else + raise TypeError.new + end + end + + return retval +end + +# +# Take a coeff and an exponent and return the string representation, ignoring +# the sign of the coefficient. +def term_to_str(coef, exp) + ret = "" + + # Show coeff, unless it's 1 or at the right + coef = coef.abs + ret = coef.to_s unless coef == 1 && exp > 0 + ret += "x" if exp > 0 # x if exponent not 0 + ret += "^" + exp.to_s if exp > 1 # ^exponent, if > 1. + + return ret +end + +# +# Create a string of the polynomial in sort-of-readable form. +def polystr(p) + # Get the exponent of first coefficient, plus 1. + exp = p.length + + # Assign exponents to each term, making pairs of coeff and exponent, + # Then get rid of the zero terms. + p = (p.map { |c| exp -= 1; [ c, exp ] }).select { |p| p[0] != 0 } + + # If there's nothing left, it's a zero + return "0" if p.empty? + + # *** Now p is a non-empty list of [ coef, exponent ] pairs. *** + + # Convert the first term, preceded by a "-" if it's negative. + result = (if p[0][0] < 0 then "-" else "" end) + term_to_str(*p[0]) + + # Convert the rest of the terms, in each case adding the appropriate + # + or - separating them. + for term in p[1...p.length] + # Add the separator then the rep. of the term. + result += (if term[0] < 0 then " - " else " + " end) + + term_to_str(*term) + end + + return result +end + +# +# Run until some kind of endfile. +begin + # Repeat until an exception or quit gets us out. + while true + # Read a poly until it works. An EOF will except out of the + # program. + print "\n" + begin + poly = readints("Enter a polynomial coefficients: ") + rescue TypeError + print "Try again.\n" + retry + end + break if poly.empty? + + # Read and evaluate x values until the user types a blank line. + # Again, an EOF will except out of the pgm. + while true + # Request an integer. + print "Enter x value or blank line: " + x = readline.chomp + break if x == '' + raise EOFError.new if x == 'quit' + + # If it looks bad, let's try again. + if x !~ /^\-?\d+$/ + print "That doesn't look like an integer. Please try again.\n" + next + end + + # Convert to an integer and print the result. + x = x.to_i + print "p(x) = ", polystr(poly), "\n" + print "p(", x, ") = ", polyval(x, poly), "\n" + end + end +rescue EOFError + print "\n=== EOF ===\n" +rescue Interrupt, SignalException + print "\n=== Interrupted ===\n" +else + print "--- Bye ---\n" +end +</textarea></form> + <script> + var editor = CodeMirror.fromTextArea(document.getElementById("code"), { + mode: "text/x-ruby", + matchBrackets: true, + indentUnit: 4 + }); + </script> + + <p><strong>MIME types defined:</strong> <code>text/x-ruby</code>.</p> + + <p>Development of the CodeMirror Ruby mode was kindly sponsored + by <a href="http://ubalo.com/">Ubalo</a>.</p> + + </article> diff --git a/public/vendor/codemirror/mode/ruby/ruby.js b/public/vendor/codemirror/mode/ruby/ruby.js new file mode 100755 index 00000000..eab9d9da --- /dev/null +++ b/public/vendor/codemirror/mode/ruby/ruby.js @@ -0,0 +1,285 @@ +// CodeMirror, copyright (c) by Marijn Haverbeke and others +// Distributed under an MIT license: http://codemirror.net/LICENSE + +(function(mod) { + if (typeof exports == "object" && typeof module == "object") // CommonJS + mod(require("../../lib/codemirror")); + else if (typeof define == "function" && define.amd) // AMD + define(["../../lib/codemirror"], mod); + else // Plain browser env + mod(CodeMirror); +})(function(CodeMirror) { +"use strict"; + +CodeMirror.defineMode("ruby", function(config) { + function wordObj(words) { + var o = {}; + for (var i = 0, e = words.length; i < e; ++i) o[words[i]] = true; + return o; + } + var keywords = wordObj([ + "alias", "and", "BEGIN", "begin", "break", "case", "class", "def", "defined?", "do", "else", + "elsif", "END", "end", "ensure", "false", "for", "if", "in", "module", "next", "not", "or", + "redo", "rescue", "retry", "return", "self", "super", "then", "true", "undef", "unless", + "until", "when", "while", "yield", "nil", "raise", "throw", "catch", "fail", "loop", "callcc", + "caller", "lambda", "proc", "public", "protected", "private", "require", "load", + "require_relative", "extend", "autoload", "__END__", "__FILE__", "__LINE__", "__dir__" + ]); + var indentWords = wordObj(["def", "class", "case", "for", "while", "module", "then", + "catch", "loop", "proc", "begin"]); + var dedentWords = wordObj(["end", "until"]); + var matching = {"[": "]", "{": "}", "(": ")"}; + var curPunc; + + function chain(newtok, stream, state) { + state.tokenize.push(newtok); + return newtok(stream, state); + } + + function tokenBase(stream, state) { + curPunc = null; + if (stream.sol() && stream.match("=begin") && stream.eol()) { + state.tokenize.push(readBlockComment); + return "comment"; + } + if (stream.eatSpace()) return null; + var ch = stream.next(), m; + if (ch == "`" || ch == "'" || ch == '"') { + return chain(readQuoted(ch, "string", ch == '"' || ch == "`"), stream, state); + } else if (ch == "/") { + var currentIndex = stream.current().length; + if (stream.skipTo("/")) { + var search_till = stream.current().length; + stream.backUp(stream.current().length - currentIndex); + var balance = 0; // balance brackets + while (stream.current().length < search_till) { + var chchr = stream.next(); + if (chchr == "(") balance += 1; + else if (chchr == ")") balance -= 1; + if (balance < 0) break; + } + stream.backUp(stream.current().length - currentIndex); + if (balance == 0) + return chain(readQuoted(ch, "string-2", true), stream, state); + } + return "operator"; + } else if (ch == "%") { + var style = "string", embed = true; + if (stream.eat("s")) style = "atom"; + else if (stream.eat(/[WQ]/)) style = "string"; + else if (stream.eat(/[r]/)) style = "string-2"; + else if (stream.eat(/[wxq]/)) { style = "string"; embed = false; } + var delim = stream.eat(/[^\w\s=]/); + if (!delim) return "operator"; + if (matching.propertyIsEnumerable(delim)) delim = matching[delim]; + return chain(readQuoted(delim, style, embed, true), stream, state); + } else if (ch == "#") { + stream.skipToEnd(); + return "comment"; + } else if (ch == "<" && (m = stream.match(/^<-?[\`\"\']?([a-zA-Z_?]\w*)[\`\"\']?(?:;|$)/))) { + return chain(readHereDoc(m[1]), stream, state); + } else if (ch == "0") { + if (stream.eat("x")) stream.eatWhile(/[\da-fA-F]/); + else if (stream.eat("b")) stream.eatWhile(/[01]/); + else stream.eatWhile(/[0-7]/); + return "number"; + } else if (/\d/.test(ch)) { + stream.match(/^[\d_]*(?:\.[\d_]+)?(?:[eE][+\-]?[\d_]+)?/); + return "number"; + } else if (ch == "?") { + while (stream.match(/^\\[CM]-/)) {} + if (stream.eat("\\")) stream.eatWhile(/\w/); + else stream.next(); + return "string"; + } else if (ch == ":") { + if (stream.eat("'")) return chain(readQuoted("'", "atom", false), stream, state); + if (stream.eat('"')) return chain(readQuoted('"', "atom", true), stream, state); + + // :> :>> :< :<< are valid symbols + if (stream.eat(/[\<\>]/)) { + stream.eat(/[\<\>]/); + return "atom"; + } + + // :+ :- :/ :* :| :& :! are valid symbols + if (stream.eat(/[\+\-\*\/\&\|\:\!]/)) { + return "atom"; + } + + // Symbols can't start by a digit + if (stream.eat(/[a-zA-Z$@_\xa1-\uffff]/)) { + stream.eatWhile(/[\w$\xa1-\uffff]/); + // Only one ? ! = is allowed and only as the last character + stream.eat(/[\?\!\=]/); + return "atom"; + } + return "operator"; + } else if (ch == "@" && stream.match(/^@?[a-zA-Z_\xa1-\uffff]/)) { + stream.eat("@"); + stream.eatWhile(/[\w\xa1-\uffff]/); + return "variable-2"; + } else if (ch == "$") { + if (stream.eat(/[a-zA-Z_]/)) { + stream.eatWhile(/[\w]/); + } else if (stream.eat(/\d/)) { + stream.eat(/\d/); + } else { + stream.next(); // Must be a special global like $: or $! + } + return "variable-3"; + } else if (/[a-zA-Z_\xa1-\uffff]/.test(ch)) { + stream.eatWhile(/[\w\xa1-\uffff]/); + stream.eat(/[\?\!]/); + if (stream.eat(":")) return "atom"; + return "ident"; + } else if (ch == "|" && (state.varList || state.lastTok == "{" || state.lastTok == "do")) { + curPunc = "|"; + return null; + } else if (/[\(\)\[\]{}\\;]/.test(ch)) { + curPunc = ch; + return null; + } else if (ch == "-" && stream.eat(">")) { + return "arrow"; + } else if (/[=+\-\/*:\.^%<>~|]/.test(ch)) { + var more = stream.eatWhile(/[=+\-\/*:\.^%<>~|]/); + if (ch == "." && !more) curPunc = "."; + return "operator"; + } else { + return null; + } + } + + function tokenBaseUntilBrace(depth) { + if (!depth) depth = 1; + return function(stream, state) { + if (stream.peek() == "}") { + if (depth == 1) { + state.tokenize.pop(); + return state.tokenize[state.tokenize.length-1](stream, state); + } else { + state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth - 1); + } + } else if (stream.peek() == "{") { + state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth + 1); + } + return tokenBase(stream, state); + }; + } + function tokenBaseOnce() { + var alreadyCalled = false; + return function(stream, state) { + if (alreadyCalled) { + state.tokenize.pop(); + return state.tokenize[state.tokenize.length-1](stream, state); + } + alreadyCalled = true; + return tokenBase(stream, state); + }; + } + function readQuoted(quote, style, embed, unescaped) { + return function(stream, state) { + var escaped = false, ch; + + if (state.context.type === 'read-quoted-paused') { + state.context = state.context.prev; + stream.eat("}"); + } + + while ((ch = stream.next()) != null) { + if (ch == quote && (unescaped || !escaped)) { + state.tokenize.pop(); + break; + } + if (embed && ch == "#" && !escaped) { + if (stream.eat("{")) { + if (quote == "}") { + state.context = {prev: state.context, type: 'read-quoted-paused'}; + } + state.tokenize.push(tokenBaseUntilBrace()); + break; + } else if (/[@\$]/.test(stream.peek())) { + state.tokenize.push(tokenBaseOnce()); + break; + } + } + escaped = !escaped && ch == "\\"; + } + return style; + }; + } + function readHereDoc(phrase) { + return function(stream, state) { + if (stream.match(phrase)) state.tokenize.pop(); + else stream.skipToEnd(); + return "string"; + }; + } + function readBlockComment(stream, state) { + if (stream.sol() && stream.match("=end") && stream.eol()) + state.tokenize.pop(); + stream.skipToEnd(); + return "comment"; + } + + return { + startState: function() { + return {tokenize: [tokenBase], + indented: 0, + context: {type: "top", indented: -config.indentUnit}, + continuedLine: false, + lastTok: null, + varList: false}; + }, + + token: function(stream, state) { + if (stream.sol()) state.indented = stream.indentation(); + var style = state.tokenize[state.tokenize.length-1](stream, state), kwtype; + var thisTok = curPunc; + if (style == "ident") { + var word = stream.current(); + style = state.lastTok == "." ? "property" + : keywords.propertyIsEnumerable(stream.current()) ? "keyword" + : /^[A-Z]/.test(word) ? "tag" + : (state.lastTok == "def" || state.lastTok == "class" || state.varList) ? "def" + : "variable"; + if (style == "keyword") { + thisTok = word; + if (indentWords.propertyIsEnumerable(word)) kwtype = "indent"; + else if (dedentWords.propertyIsEnumerable(word)) kwtype = "dedent"; + else if ((word == "if" || word == "unless") && stream.column() == stream.indentation()) + kwtype = "indent"; + else if (word == "do" && state.context.indented < state.indented) + kwtype = "indent"; + } + } + if (curPunc || (style && style != "comment")) state.lastTok = thisTok; + if (curPunc == "|") state.varList = !state.varList; + + if (kwtype == "indent" || /[\(\[\{]/.test(curPunc)) + state.context = {prev: state.context, type: curPunc || style, indented: state.indented}; + else if ((kwtype == "dedent" || /[\)\]\}]/.test(curPunc)) && state.context.prev) + state.context = state.context.prev; + + if (stream.eol()) + state.continuedLine = (curPunc == "\\" || style == "operator"); + return style; + }, + + indent: function(state, textAfter) { + if (state.tokenize[state.tokenize.length-1] != tokenBase) return 0; + var firstChar = textAfter && textAfter.charAt(0); + var ct = state.context; + var closing = ct.type == matching[firstChar] || + ct.type == "keyword" && /^(?:end|until|else|elsif|when|rescue)\b/.test(textAfter); + return ct.indented + (closing ? 0 : config.indentUnit) + + (state.continuedLine ? config.indentUnit : 0); + }, + + electricChars: "}de", // enD and rescuE + lineComment: "#" + }; +}); + +CodeMirror.defineMIME("text/x-ruby", "ruby"); + +}); diff --git a/public/vendor/codemirror/mode/ruby/test.js b/public/vendor/codemirror/mode/ruby/test.js new file mode 100755 index 00000000..cade864f --- /dev/null +++ b/public/vendor/codemirror/mode/ruby/test.js @@ -0,0 +1,14 @@ +// CodeMirror, copyright (c) by Marijn Haverbeke and others +// Distributed under an MIT license: http://codemirror.net/LICENSE + +(function() { + var mode = CodeMirror.getMode({indentUnit: 2}, "ruby"); + function MT(name) { test.mode(name, mode, Array.prototype.slice.call(arguments, 1)); } + + MT("divide_equal_operator", + "[variable bar] [operator /=] [variable foo]"); + + MT("divide_equal_operator_no_spacing", + "[variable foo][operator /=][number 42]"); + +})(); |