yaml.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. CodeMirror.defineMode("yaml", function() {
  2. var cons = ['true', 'false', 'on', 'off', 'yes', 'no'];
  3. var keywordRegex = new RegExp("\\b(("+cons.join(")|(")+"))$", 'i');
  4. return {
  5. token: function(stream, state) {
  6. var ch = stream.peek();
  7. var esc = state.escaped;
  8. state.escaped = false;
  9. /* comments */
  10. if (ch == "#" && (stream.pos == 0 || /\s/.test(stream.string.charAt(stream.pos - 1)))) {
  11. stream.skipToEnd(); return "comment";
  12. }
  13. if (state.literal && stream.indentation() > state.keyCol) {
  14. stream.skipToEnd(); return "string";
  15. } else if (state.literal) { state.literal = false; }
  16. if (stream.sol()) {
  17. state.keyCol = 0;
  18. state.pair = false;
  19. state.pairStart = false;
  20. /* document start */
  21. if(stream.match(/---/)) { return "def"; }
  22. /* document end */
  23. if (stream.match(/\.\.\./)) { return "def"; }
  24. /* array list item */
  25. if (stream.match(/\s*-\s+/)) { return 'meta'; }
  26. }
  27. /* inline pairs/lists */
  28. if (stream.match(/^(\{|\}|\[|\])/)) {
  29. if (ch == '{')
  30. state.inlinePairs++;
  31. else if (ch == '}')
  32. state.inlinePairs--;
  33. else if (ch == '[')
  34. state.inlineList++;
  35. else
  36. state.inlineList--;
  37. return 'meta';
  38. }
  39. /* list seperator */
  40. if (state.inlineList > 0 && !esc && ch == ',') {
  41. stream.next();
  42. return 'meta';
  43. }
  44. /* pairs seperator */
  45. if (state.inlinePairs > 0 && !esc && ch == ',') {
  46. state.keyCol = 0;
  47. state.pair = false;
  48. state.pairStart = false;
  49. stream.next();
  50. return 'meta';
  51. }
  52. /* start of value of a pair */
  53. if (state.pairStart) {
  54. /* block literals */
  55. if (stream.match(/^\s*(\||\>)\s*/)) { state.literal = true; return 'meta'; };
  56. /* references */
  57. if (stream.match(/^\s*(\&|\*)[a-z0-9\._-]+\b/i)) { return 'variable-2'; }
  58. /* numbers */
  59. if (state.inlinePairs == 0 && stream.match(/^\s*-?[0-9\.\,]+\s?$/)) { return 'number'; }
  60. if (state.inlinePairs > 0 && stream.match(/^\s*-?[0-9\.\,]+\s?(?=(,|}))/)) { return 'number'; }
  61. /* keywords */
  62. if (stream.match(keywordRegex)) { return 'keyword'; }
  63. }
  64. /* pairs (associative arrays) -> key */
  65. if (!state.pair && stream.match(/^\s*\S+(?=\s*:($|\s))/i)) {
  66. state.pair = true;
  67. state.keyCol = stream.indentation();
  68. return "atom";
  69. }
  70. if (state.pair && stream.match(/^:\s*/)) { state.pairStart = true; return 'meta'; }
  71. /* nothing found, continue */
  72. state.pairStart = false;
  73. state.escaped = (ch == '\\');
  74. stream.next();
  75. return null;
  76. },
  77. startState: function() {
  78. return {
  79. pair: false,
  80. pairStart: false,
  81. keyCol: 0,
  82. inlinePairs: 0,
  83. inlineList: 0,
  84. literal: false,
  85. escaped: false
  86. };
  87. }
  88. };
  89. });
  90. CodeMirror.defineMIME("text/x-yaml", "yaml");