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. /* pairs (associative arrays) -> key */
  28. if (!state.pair && stream.match(/^\s*([a-z0-9\._-])+(?=\s*:)/i)) {
  29. state.pair = true;
  30. state.keyCol = stream.indentation();
  31. return "atom";
  32. }
  33. if (state.pair && stream.match(/^:\s*/)) { state.pairStart = true; return 'meta'; }
  34. /* inline pairs/lists */
  35. if (stream.match(/^(\{|\}|\[|\])/)) {
  36. if (ch == '{')
  37. state.inlinePairs++;
  38. else if (ch == '}')
  39. state.inlinePairs--;
  40. else if (ch == '[')
  41. state.inlineList++;
  42. else
  43. state.inlineList--;
  44. return 'meta';
  45. }
  46. /* list seperator */
  47. if (state.inlineList > 0 && !esc && ch == ',') {
  48. stream.next();
  49. return 'meta';
  50. }
  51. /* pairs seperator */
  52. if (state.inlinePairs > 0 && !esc && ch == ',') {
  53. state.keyCol = 0;
  54. state.pair = false;
  55. state.pairStart = false;
  56. stream.next();
  57. return 'meta';
  58. }
  59. /* start of value of a pair */
  60. if (state.pairStart) {
  61. /* block literals */
  62. if (stream.match(/^\s*(\||\>)\s*/)) { state.literal = true; return 'meta'; };
  63. /* references */
  64. if (stream.match(/^\s*(\&|\*)[a-z0-9\._-]+\b/i)) { return 'variable-2'; }
  65. /* numbers */
  66. if (state.inlinePairs == 0 && stream.match(/^\s*-?[0-9\.\,]+\s?$/)) { return 'number'; }
  67. if (state.inlinePairs > 0 && stream.match(/^\s*-?[0-9\.\,]+\s?(?=(,|}))/)) { return 'number'; }
  68. /* keywords */
  69. if (stream.match(keywordRegex)) { return 'keyword'; }
  70. }
  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");