yaml.js 3.3 KB

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