github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/app/public/codemirror/mode/spreadsheet/spreadsheet.js (about)

     1  // CodeMirror, copyright (c) by Marijn Haverbeke and others
     2  // Distributed under an MIT license: http://codemirror.net/LICENSE
     3  
     4  (function(mod) {
     5    if (typeof exports == "object" && typeof module == "object") // CommonJS
     6      mod(require("../../lib/codemirror"));
     7    else if (typeof define == "function" && define.amd) // AMD
     8      define(["../../lib/codemirror"], mod);
     9    else // Plain browser env
    10      mod(CodeMirror);
    11  })(function(CodeMirror) {
    12    "use strict";
    13  
    14    CodeMirror.defineMode("spreadsheet", function () {
    15      return {
    16        startState: function () {
    17          return {
    18            stringType: null,
    19            stack: []
    20          };
    21        },
    22        token: function (stream, state) {
    23          if (!stream) return;
    24  
    25          //check for state changes
    26          if (state.stack.length === 0) {
    27            //strings
    28            if ((stream.peek() == '"') || (stream.peek() == "'")) {
    29              state.stringType = stream.peek();
    30              stream.next(); // Skip quote
    31              state.stack.unshift("string");
    32            }
    33          }
    34  
    35          //return state
    36          //stack has
    37          switch (state.stack[0]) {
    38          case "string":
    39            while (state.stack[0] === "string" && !stream.eol()) {
    40              if (stream.peek() === state.stringType) {
    41                stream.next(); // Skip quote
    42                state.stack.shift(); // Clear flag
    43              } else if (stream.peek() === "\\") {
    44                stream.next();
    45                stream.next();
    46              } else {
    47                stream.match(/^.[^\\\"\']*/);
    48              }
    49            }
    50            return "string";
    51  
    52          case "characterClass":
    53            while (state.stack[0] === "characterClass" && !stream.eol()) {
    54              if (!(stream.match(/^[^\]\\]+/) || stream.match(/^\\./)))
    55                state.stack.shift();
    56            }
    57            return "operator";
    58          }
    59  
    60          var peek = stream.peek();
    61  
    62          //no stack
    63          switch (peek) {
    64          case "[":
    65            stream.next();
    66            state.stack.unshift("characterClass");
    67            return "bracket";
    68          case ":":
    69            stream.next();
    70            return "operator";
    71          case "\\":
    72            if (stream.match(/\\[a-z]+/)) return "string-2";
    73            else return null;
    74          case ".":
    75          case ",":
    76          case ";":
    77          case "*":
    78          case "-":
    79          case "+":
    80          case "^":
    81          case "<":
    82          case "/":
    83          case "=":
    84            stream.next();
    85            return "atom";
    86          case "$":
    87            stream.next();
    88            return "builtin";
    89          }
    90  
    91          if (stream.match(/\d+/)) {
    92            if (stream.match(/^\w+/)) return "error";
    93            return "number";
    94          } else if (stream.match(/^[a-zA-Z_]\w*/)) {
    95            if (stream.match(/(?=[\(.])/, false)) return "keyword";
    96            return "variable-2";
    97          } else if (["[", "]", "(", ")", "{", "}"].indexOf(peek) != -1) {
    98            stream.next();
    99            return "bracket";
   100          } else if (!stream.eatSpace()) {
   101            stream.next();
   102          }
   103          return null;
   104        }
   105      };
   106    });
   107  
   108    CodeMirror.defineMIME("text/x-spreadsheet", "spreadsheet");
   109  });