toml.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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("toml", function () {
  13. return {
  14. startState: function () {
  15. return {
  16. inString: false,
  17. stringType: "",
  18. lhs: true,
  19. inArray: 0
  20. };
  21. },
  22. token: function (stream, state) {
  23. //check for state changes
  24. if (!state.inString && ((stream.peek() == '"') || (stream.peek() == "'"))) {
  25. state.stringType = stream.peek();
  26. stream.next(); // Skip quote
  27. state.inString = true; // Update state
  28. }
  29. if (stream.sol() && state.inArray === 0) {
  30. state.lhs = true;
  31. }
  32. //return state
  33. if (state.inString) {
  34. while (state.inString && !stream.eol()) {
  35. if (stream.peek() === state.stringType) {
  36. stream.next(); // Skip quote
  37. state.inString = false; // Clear flag
  38. } else if (stream.peek() === '\\') {
  39. stream.next();
  40. stream.next();
  41. } else {
  42. stream.match(/^.[^\\\"\']*/);
  43. }
  44. }
  45. return state.lhs ? "property string" : "string"; // Token style
  46. } else if (state.inArray && stream.peek() === ']') {
  47. stream.next();
  48. state.inArray--;
  49. return 'bracket';
  50. } else if (state.lhs && stream.peek() === '[' && stream.skipTo(']')) {
  51. stream.next();//skip closing ]
  52. // array of objects has an extra open & close []
  53. if (stream.peek() === ']') stream.next();
  54. return "atom";
  55. } else if (stream.peek() === "#") {
  56. stream.skipToEnd();
  57. return "comment";
  58. } else if (stream.eatSpace()) {
  59. return null;
  60. } else if (state.lhs && stream.eatWhile(function (c) { return c != '=' && c != ' '; })) {
  61. return "property";
  62. } else if (state.lhs && stream.peek() === "=") {
  63. stream.next();
  64. state.lhs = false;
  65. return null;
  66. } else if (!state.lhs && stream.match(/^\d\d\d\d[\d\-\:\.T]*Z/)) {
  67. return 'atom'; //date
  68. } else if (!state.lhs && (stream.match('true') || stream.match('false'))) {
  69. return 'atom';
  70. } else if (!state.lhs && stream.peek() === '[') {
  71. state.inArray++;
  72. stream.next();
  73. return 'bracket';
  74. } else if (!state.lhs && stream.match(/^\-?\d+(?:\.\d+)?/)) {
  75. return 'number';
  76. } else if (!stream.eatSpace()) {
  77. stream.next();
  78. }
  79. return null;
  80. }
  81. };
  82. });
  83. CodeMirror.defineMIME('text/x-toml', 'toml');
  84. });