UNPKG

3.14 kBJavaScriptView Raw
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://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 {
74 stream.next();
75 return "atom";
76 }
77 case ".":
78 case ",":
79 case ";":
80 case "*":
81 case "-":
82 case "+":
83 case "^":
84 case "<":
85 case "/":
86 case "=":
87 stream.next();
88 return "atom";
89 case "$":
90 stream.next();
91 return "builtin";
92 }
93
94 if (stream.match(/\d+/)) {
95 if (stream.match(/^\w+/)) return "error";
96 return "number";
97 } else if (stream.match(/^[a-zA-Z_]\w*/)) {
98 if (stream.match(/(?=[\(.])/, false)) return "keyword";
99 return "variable-2";
100 } else if (["[", "]", "(", ")", "{", "}"].indexOf(peek) != -1) {
101 stream.next();
102 return "bracket";
103 } else if (!stream.eatSpace()) {
104 stream.next();
105 }
106 return null;
107 }
108 };
109 });
110
111 CodeMirror.defineMIME("text/x-spreadsheet", "spreadsheet");
112});