spreadsheet.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 {
  67. stream.next();
  68. return "atom";
  69. }
  70. case ".":
  71. case ",":
  72. case ";":
  73. case "*":
  74. case "-":
  75. case "+":
  76. case "^":
  77. case "<":
  78. case "/":
  79. case "=":
  80. stream.next();
  81. return "atom";
  82. case "$":
  83. stream.next();
  84. return "builtin";
  85. }
  86. if (stream.match(/\d+/)) {
  87. if (stream.match(/^\w+/)) return "error";
  88. return "number";
  89. } else if (stream.match(/^[a-zA-Z_]\w*/)) {
  90. if (stream.match(/(?=[\(.])/, false)) return "keyword";
  91. return "variable-2";
  92. } else if (["[", "]", "(", ")", "{", "}"].indexOf(peek) != -1) {
  93. stream.next();
  94. return "bracket";
  95. } else if (!stream.eatSpace()) {
  96. stream.next();
  97. }
  98. return null;
  99. }
  100. };
  101. });
  102. CodeMirror.defineMIME("text/x-spreadsheet", "spreadsheet");
  103. });