Theme Inspinia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

yaml.js 3.5KB

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