1
0

yaml.js 2.6 KB

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