LaravelTest
284 строки · 14.9 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4/**
5* Author: Koh Zi Han, based on implementation by Koh Zi Chun
6* Improved by: Jakub T. Jankiewicz
7*/
8
9(function(mod) {10if (typeof exports == "object" && typeof module == "object") // CommonJS11mod(require("../../lib/codemirror"));12else if (typeof define == "function" && define.amd) // AMD13define(["../../lib/codemirror"], mod);14else // Plain browser env15mod(CodeMirror);16})(function(CodeMirror) {17"use strict";18
19CodeMirror.defineMode("scheme", function () {20var BUILTIN = "builtin", COMMENT = "comment", STRING = "string",21SYMBOL = "symbol", ATOM = "atom", NUMBER = "number", BRACKET = "bracket";22var INDENT_WORD_SKIP = 2;23
24function makeKeywords(str) {25var obj = {}, words = str.split(" ");26for (var i = 0; i < words.length; ++i) obj[words[i]] = true;27return obj;28}29
30var keywords = makeKeywords("λ case-lambda call/cc class cond-expand define-class define-values exit-handler field import inherit init-field interface let*-values let-values let/ec mixin opt-lambda override protect provide public rename require require-for-syntax syntax syntax-case syntax-error unit/sig unless when with-syntax and begin call-with-current-continuation call-with-input-file call-with-output-file case cond define define-syntax define-macro defmacro delay do dynamic-wind else for-each if lambda let let* let-syntax letrec letrec-syntax map or syntax-rules abs acos angle append apply asin assoc assq assv atan boolean? caar cadr call-with-input-file call-with-output-file call-with-values car cdddar cddddr cdr ceiling char->integer char-alphabetic? char-ci<=? char-ci<? char-ci=? char-ci>=? char-ci>? char-downcase char-lower-case? char-numeric? char-ready? char-upcase char-upper-case? char-whitespace? char<=? char<? char=? char>=? char>? char? close-input-port close-output-port complex? cons cos current-input-port current-output-port denominator display eof-object? eq? equal? eqv? eval even? exact->inexact exact? exp expt #f floor force gcd imag-part inexact->exact inexact? input-port? integer->char integer? interaction-environment lcm length list list->string list->vector list-ref list-tail list? load log magnitude make-polar make-rectangular make-string make-vector max member memq memv min modulo negative? newline not null-environment null? number->string number? numerator odd? open-input-file open-output-file output-port? pair? peek-char port? positive? procedure? quasiquote quote quotient rational? rationalize read read-char real-part real? remainder reverse round scheme-report-environment set! set-car! set-cdr! sin sqrt string string->list string->number string->symbol string-append string-ci<=? string-ci<? string-ci=? string-ci>=? string-ci>? string-copy string-fill! string-length string-ref string-set! string<=? string<? string=? string>=? string>? string? substring symbol->string symbol? #t tan transcript-off transcript-on truncate values vector vector->list vector-fill! vector-length vector-ref vector-set! with-input-from-file with-output-to-file write write-char zero?");31var indentKeys = makeKeywords("define let letrec let* lambda define-macro defmacro let-syntax letrec-syntax let-values let*-values define-syntax syntax-rules define-values when unless");32
33function stateStack(indent, type, prev) { // represents a state stack object34this.indent = indent;35this.type = type;36this.prev = prev;37}38
39function pushStack(state, indent, type) {40state.indentStack = new stateStack(indent, type, state.indentStack);41}42
43function popStack(state) {44state.indentStack = state.indentStack.prev;45}46
47var binaryMatcher = new RegExp(/^(?:[-+]i|[-+][01]+#*(?:\/[01]+#*)?i|[-+]?[01]+#*(?:\/[01]+#*)?@[-+]?[01]+#*(?:\/[01]+#*)?|[-+]?[01]+#*(?:\/[01]+#*)?[-+](?:[01]+#*(?:\/[01]+#*)?)?i|[-+]?[01]+#*(?:\/[01]+#*)?)(?=[()\s;"]|$)/i);48var octalMatcher = new RegExp(/^(?:[-+]i|[-+][0-7]+#*(?:\/[0-7]+#*)?i|[-+]?[0-7]+#*(?:\/[0-7]+#*)?@[-+]?[0-7]+#*(?:\/[0-7]+#*)?|[-+]?[0-7]+#*(?:\/[0-7]+#*)?[-+](?:[0-7]+#*(?:\/[0-7]+#*)?)?i|[-+]?[0-7]+#*(?:\/[0-7]+#*)?)(?=[()\s;"]|$)/i);49var hexMatcher = new RegExp(/^(?:[-+]i|[-+][\da-f]+#*(?:\/[\da-f]+#*)?i|[-+]?[\da-f]+#*(?:\/[\da-f]+#*)?@[-+]?[\da-f]+#*(?:\/[\da-f]+#*)?|[-+]?[\da-f]+#*(?:\/[\da-f]+#*)?[-+](?:[\da-f]+#*(?:\/[\da-f]+#*)?)?i|[-+]?[\da-f]+#*(?:\/[\da-f]+#*)?)(?=[()\s;"]|$)/i);50var decimalMatcher = new RegExp(/^(?:[-+]i|[-+](?:(?:(?:\d+#+\.?#*|\d+\.\d*#*|\.\d+#*|\d+)(?:[esfdl][-+]?\d+)?)|\d+#*\/\d+#*)i|[-+]?(?:(?:(?:\d+#+\.?#*|\d+\.\d*#*|\.\d+#*|\d+)(?:[esfdl][-+]?\d+)?)|\d+#*\/\d+#*)@[-+]?(?:(?:(?:\d+#+\.?#*|\d+\.\d*#*|\.\d+#*|\d+)(?:[esfdl][-+]?\d+)?)|\d+#*\/\d+#*)|[-+]?(?:(?:(?:\d+#+\.?#*|\d+\.\d*#*|\.\d+#*|\d+)(?:[esfdl][-+]?\d+)?)|\d+#*\/\d+#*)[-+](?:(?:(?:\d+#+\.?#*|\d+\.\d*#*|\.\d+#*|\d+)(?:[esfdl][-+]?\d+)?)|\d+#*\/\d+#*)?i|(?:(?:(?:\d+#+\.?#*|\d+\.\d*#*|\.\d+#*|\d+)(?:[esfdl][-+]?\d+)?)|\d+#*\/\d+#*))(?=[()\s;"]|$)/i);51
52function isBinaryNumber (stream) {53return stream.match(binaryMatcher);54}55
56function isOctalNumber (stream) {57return stream.match(octalMatcher);58}59
60function isDecimalNumber (stream, backup) {61if (backup === true) {62stream.backUp(1);63}64return stream.match(decimalMatcher);65}66
67function isHexNumber (stream) {68return stream.match(hexMatcher);69}70
71function processEscapedSequence(stream, options) {72var next, escaped = false;73while ((next = stream.next()) != null) {74if (next == options.token && !escaped) {75
76options.state.mode = false;77break;78}79escaped = !escaped && next == "\\";80}81}82
83return {84startState: function () {85return {86indentStack: null,87indentation: 0,88mode: false,89sExprComment: false,90sExprQuote: false91};92},93
94token: function (stream, state) {95if (state.indentStack == null && stream.sol()) {96// update indentation, but only if indentStack is empty97state.indentation = stream.indentation();98}99
100// skip spaces101if (stream.eatSpace()) {102return null;103}104var returnType = null;105
106switch(state.mode){107case "string": // multi-line string parsing mode108processEscapedSequence(stream, {109token: "\"",110state: state111});112returnType = STRING; // continue on in scheme-string mode113break;114case "symbol": // escape symbol115processEscapedSequence(stream, {116token: "|",117state: state118});119returnType = SYMBOL; // continue on in scheme-symbol mode120break;121case "comment": // comment parsing mode122var next, maybeEnd = false;123while ((next = stream.next()) != null) {124if (next == "#" && maybeEnd) {125
126state.mode = false;127break;128}129maybeEnd = (next == "|");130}131returnType = COMMENT;132break;133case "s-expr-comment": // s-expr commenting mode134state.mode = false;135if(stream.peek() == "(" || stream.peek() == "["){136// actually start scheme s-expr commenting mode137state.sExprComment = 0;138}else{139// if not we just comment the entire of the next token140stream.eatWhile(/[^\s\(\)\[\]]/); // eat symbol atom141returnType = COMMENT;142break;143}144default: // default parsing mode145var ch = stream.next();146
147if (ch == "\"") {148state.mode = "string";149returnType = STRING;150
151} else if (ch == "'") {152if (stream.peek() == "(" || stream.peek() == "["){153if (typeof state.sExprQuote != "number") {154state.sExprQuote = 0;155} // else already in a quoted expression156returnType = ATOM;157} else {158stream.eatWhile(/[\w_\-!$%&*+\.\/:<=>?@\^~]/);159returnType = ATOM;160}161} else if (ch == '|') {162state.mode = "symbol";163returnType = SYMBOL;164} else if (ch == '#') {165if (stream.eat("|")) { // Multi-line comment166state.mode = "comment"; // toggle to comment mode167returnType = COMMENT;168} else if (stream.eat(/[tf]/i)) { // #t/#f (atom)169returnType = ATOM;170} else if (stream.eat(';')) { // S-Expr comment171state.mode = "s-expr-comment";172returnType = COMMENT;173} else {174var numTest = null, hasExactness = false, hasRadix = true;175if (stream.eat(/[ei]/i)) {176hasExactness = true;177} else {178stream.backUp(1); // must be radix specifier179}180if (stream.match(/^#b/i)) {181numTest = isBinaryNumber;182} else if (stream.match(/^#o/i)) {183numTest = isOctalNumber;184} else if (stream.match(/^#x/i)) {185numTest = isHexNumber;186} else if (stream.match(/^#d/i)) {187numTest = isDecimalNumber;188} else if (stream.match(/^[-+0-9.]/, false)) {189hasRadix = false;190numTest = isDecimalNumber;191// re-consume the initial # if all matches failed192} else if (!hasExactness) {193stream.eat('#');194}195if (numTest != null) {196if (hasRadix && !hasExactness) {197// consume optional exactness after radix198stream.match(/^#[ei]/i);199}200if (numTest(stream))201returnType = NUMBER;202}203}204} else if (/^[-+0-9.]/.test(ch) && isDecimalNumber(stream, true)) { // match non-prefixed number, must be decimal205returnType = NUMBER;206} else if (ch == ";") { // comment207stream.skipToEnd(); // rest of the line is a comment208returnType = COMMENT;209} else if (ch == "(" || ch == "[") {210var keyWord = ''; var indentTemp = stream.column(), letter;211/**212Either
213(indent-word ..
214(non-indent-word ..
215(;something else, bracket, etc.
216*/
217
218while ((letter = stream.eat(/[^\s\(\[\;\)\]]/)) != null) {219keyWord += letter;220}221
222if (keyWord.length > 0 && indentKeys.propertyIsEnumerable(keyWord)) { // indent-word223
224pushStack(state, indentTemp + INDENT_WORD_SKIP, ch);225} else { // non-indent word226// we continue eating the spaces227stream.eatSpace();228if (stream.eol() || stream.peek() == ";") {229// nothing significant after230// we restart indentation 1 space after231pushStack(state, indentTemp + 1, ch);232} else {233pushStack(state, indentTemp + stream.current().length, ch); // else we match234}235}236stream.backUp(stream.current().length - 1); // undo all the eating237
238if(typeof state.sExprComment == "number") state.sExprComment++;239if(typeof state.sExprQuote == "number") state.sExprQuote++;240
241returnType = BRACKET;242} else if (ch == ")" || ch == "]") {243returnType = BRACKET;244if (state.indentStack != null && state.indentStack.type == (ch == ")" ? "(" : "[")) {245popStack(state);246
247if(typeof state.sExprComment == "number"){248if(--state.sExprComment == 0){249returnType = COMMENT; // final closing bracket250state.sExprComment = false; // turn off s-expr commenting mode251}252}253if(typeof state.sExprQuote == "number"){254if(--state.sExprQuote == 0){255returnType = ATOM; // final closing bracket256state.sExprQuote = false; // turn off s-expr quote mode257}258}259}260} else {261stream.eatWhile(/[\w_\-!$%&*+\.\/:<=>?@\^~]/);262
263if (keywords && keywords.propertyIsEnumerable(stream.current())) {264returnType = BUILTIN;265} else returnType = "variable";266}267}268return (typeof state.sExprComment == "number") ? COMMENT : ((typeof state.sExprQuote == "number") ? ATOM : returnType);269},270
271indent: function (state) {272if (state.indentStack == null) return state.indentation;273return state.indentStack.indent;274},275
276fold: "brace-paren",277closeBrackets: {pairs: "()[]{}\"\""},278lineComment: ";;"279};280});281
282CodeMirror.defineMIME("text/x-scheme", "scheme");283
284});285