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.

120 lines
3.6 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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();
  23. return "comment";
  24. }
  25. if (stream.match(/^('([^']|\\.)*'?|"([^"]|\\.)*"?)/))
  26. return "string";
  27. if (state.literal && stream.indentation() > state.keyCol) {
  28. stream.skipToEnd(); return "string";
  29. } else if (state.literal) { state.literal = false; }
  30. if (stream.sol()) {
  31. state.keyCol = 0;
  32. state.pair = false;
  33. state.pairStart = false;
  34. /* document start */
  35. if(stream.match('---')) { return "def"; }
  36. /* document end */
  37. if (stream.match('...')) { return "def"; }
  38. /* array list item */
  39. if (stream.match(/\s*-\s+/)) { return 'meta'; }
  40. }
  41. /* inline pairs/lists */
  42. if (stream.match(/^(\{|\}|\[|\])/)) {
  43. if (ch == '{')
  44. state.inlinePairs++;
  45. else if (ch == '}')
  46. state.inlinePairs--;
  47. else if (ch == '[')
  48. state.inlineList++;
  49. else
  50. state.inlineList--;
  51. return 'meta';
  52. }
  53. /* list separator */
  54. if (state.inlineList > 0 && !esc && ch == ',') {
  55. stream.next();
  56. return 'meta';
  57. }
  58. /* pairs separator */
  59. if (state.inlinePairs > 0 && !esc && ch == ',') {
  60. state.keyCol = 0;
  61. state.pair = false;
  62. state.pairStart = false;
  63. stream.next();
  64. return 'meta';
  65. }
  66. /* start of value of a pair */
  67. if (state.pairStart) {
  68. /* block literals */
  69. if (stream.match(/^\s*(\||\>)\s*/)) { state.literal = true; return 'meta'; };
  70. /* references */
  71. if (stream.match(/^\s*(\&|\*)[a-z0-9\._-]+\b/i)) { return 'variable-2'; }
  72. /* numbers */
  73. if (state.inlinePairs == 0 && stream.match(/^\s*-?[0-9\.\,]+\s?$/)) { return 'number'; }
  74. if (state.inlinePairs > 0 && stream.match(/^\s*-?[0-9\.\,]+\s?(?=(,|}))/)) { return 'number'; }
  75. /* keywords */
  76. if (stream.match(keywordRegex)) { return 'keyword'; }
  77. }
  78. /* pairs (associative arrays) -> key */
  79. if (!state.pair && stream.match(/^\s*(?:[,\[\]{}&*!|>'"%@`][^\s'":]|[^\s,\[\]{}#&*!|>'"%@`])[^#:]*(?=:($|\s))/)) {
  80. state.pair = true;
  81. state.keyCol = stream.indentation();
  82. return "atom";
  83. }
  84. if (state.pair && stream.match(/^:\s*/)) { state.pairStart = true; return 'meta'; }
  85. /* nothing found, continue */
  86. state.pairStart = false;
  87. state.escaped = (ch == '\\');
  88. stream.next();
  89. return null;
  90. },
  91. startState: function() {
  92. return {
  93. pair: false,
  94. pairStart: false,
  95. keyCol: 0,
  96. inlinePairs: 0,
  97. inlineList: 0,
  98. literal: false,
  99. escaped: false
  100. };
  101. },
  102. lineComment: "#",
  103. fold: "indent"
  104. };
  105. });
  106. CodeMirror.defineMIME("text/x-yaml", "yaml");
  107. CodeMirror.defineMIME("text/yaml", "yaml");
  108. });