ba6754a9177bc35a5a524715a7abc35b.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ace.define("ace/mode/csv_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/text_highlight_rules"], function(require, exports, module){"use strict";
  2. var oop = require("../lib/oop");
  3. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  4. var CsvHighlightRules = function () {
  5. TextHighlightRules.call(this);
  6. };
  7. oop.inherits(CsvHighlightRules, TextHighlightRules);
  8. exports.CsvHighlightRules = CsvHighlightRules;
  9. });
  10. ace.define("ace/mode/csv",["require","exports","module","ace/lib/oop","ace/mode/text","ace/lib/lang","ace/mode/csv_highlight_rules"], function(require, exports, module){"use strict";
  11. var oop = require("../lib/oop");
  12. var TextMode = require("./text").Mode;
  13. var escapeRegExp = require("../lib/lang").escapeRegExp;
  14. var CsvHighlightRules = require("./csv_highlight_rules").CsvHighlightRules;
  15. var Mode = function (options) {
  16. this.HighlightRules = CsvHighlightRules;
  17. if (!options)
  18. options = {};
  19. var separatorRegex = [options.splitter || ",", options.quote || '"']
  20. .map(escapeRegExp).join("|");
  21. this.$tokenizer = {
  22. getLineTokens: function (line, state, row) {
  23. return tokenizeCsv(line, state, this.options);
  24. },
  25. options: {
  26. quotes: options.quote || '"',
  27. separatorRegex: new RegExp("(" + separatorRegex + ")"),
  28. spliter: options.splitter || ","
  29. },
  30. states: {},
  31. };
  32. this.$highlightRules = new this.HighlightRules();
  33. };
  34. oop.inherits(Mode, TextMode);
  35. (function () {
  36. this.getTokenizer = function () {
  37. return this.$tokenizer;
  38. };
  39. this.$id = "ace/mode/csv";
  40. }).call(Mode.prototype);
  41. exports.Mode = Mode;
  42. var classNames = ["keyword", "text", "string", "string.regex", "variable", "constant.numeric"];
  43. function tokenizeCsv(line, state, options) {
  44. var result = [];
  45. var parts = line.split(options.separatorRegex);
  46. var spliter = options.spliter;
  47. var quote = options.quote || '"';
  48. var stateParts = (state || "start").split("-");
  49. var column = parseInt(stateParts[1]) || 0;
  50. var inString = stateParts[0] == 'string';
  51. var atColumnStart = !inString;
  52. for (var i = 0; i < parts.length; i++) {
  53. var value = parts[i];
  54. if (value) {
  55. var isSeparator = false;
  56. if (value == spliter && !inString) {
  57. column++;
  58. atColumnStart = true;
  59. isSeparator = true;
  60. }
  61. else if (value == quote) {
  62. if (atColumnStart) {
  63. inString = true;
  64. atColumnStart = false;
  65. }
  66. else if (inString) {
  67. if (parts[i + 1] == '' && parts[i + 2] == quote) {
  68. value = quote + quote;
  69. i += 2;
  70. }
  71. else {
  72. inString = false;
  73. }
  74. }
  75. }
  76. else {
  77. atColumnStart = false;
  78. }
  79. result.push({
  80. value: value,
  81. type: classNames[column % classNames.length] + ".csv_" + column + (isSeparator ? ".csv_separator" : "")
  82. });
  83. }
  84. }
  85. return { tokens: result, state: inString ? "string-" + column : "start" };
  86. }
  87. }); (function() {
  88. ace.require(["ace/mode/csv"], function(m) {
  89. if (typeof module == "object" && typeof exports == "object" && module) {
  90. module.exports = m;
  91. }
  92. });
  93. })();