yaml.js 5.0 KB

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