spreadsheet.js 3.0 KB

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