dockerfile.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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'), require('../../addon/mode/simple'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror', '../../addon/mode/simple'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. var from = 'from'
  15. var fromRegex = new RegExp('^(\\s*)\\b(' + from + ')\\b', 'i')
  16. var shells = ['run', 'cmd', 'entrypoint', 'shell']
  17. var shellsAsArrayRegex = new RegExp('^(\\s*)(' + shells.join('|') + ')(\\s+\\[)', 'i')
  18. var expose = 'expose'
  19. var exposeRegex = new RegExp('^(\\s*)(' + expose + ')(\\s+)', 'i')
  20. var others = ['arg', 'from', 'maintainer', 'label', 'env', 'add', 'copy', 'volume', 'user', 'workdir', 'onbuild', 'stopsignal', 'healthcheck', 'shell']
  21. // Collect all Dockerfile directives
  22. var instructions = [from, expose].concat(shells).concat(others),
  23. instructionRegex = '(' + instructions.join('|') + ')',
  24. instructionOnlyLine = new RegExp('^(\\s*)' + instructionRegex + '(\\s*)(#.*)?$', 'i'),
  25. instructionWithArguments = new RegExp('^(\\s*)' + instructionRegex + '(\\s+)', 'i')
  26. CodeMirror.defineSimpleMode('dockerfile', {
  27. start: [
  28. // Block comment: This is a line starting with a comment
  29. {
  30. regex: /^\s*#.*$/,
  31. sol: true,
  32. token: 'comment',
  33. },
  34. {
  35. regex: fromRegex,
  36. token: [null, 'keyword'],
  37. sol: true,
  38. next: 'from',
  39. },
  40. // Highlight an instruction without any arguments (for convenience)
  41. {
  42. regex: instructionOnlyLine,
  43. token: [null, 'keyword', null, 'error'],
  44. sol: true,
  45. },
  46. {
  47. regex: shellsAsArrayRegex,
  48. token: [null, 'keyword', null],
  49. sol: true,
  50. next: 'array',
  51. },
  52. {
  53. regex: exposeRegex,
  54. token: [null, 'keyword', null],
  55. sol: true,
  56. next: 'expose',
  57. },
  58. // Highlight an instruction followed by arguments
  59. {
  60. regex: instructionWithArguments,
  61. token: [null, 'keyword', null],
  62. sol: true,
  63. next: 'arguments',
  64. },
  65. {
  66. regex: /./,
  67. token: null,
  68. },
  69. ],
  70. from: [
  71. {
  72. regex: /\s*$/,
  73. token: null,
  74. next: 'start',
  75. },
  76. {
  77. // Line comment without instruction arguments is an error
  78. regex: /(\s*)(#.*)$/,
  79. token: [null, 'error'],
  80. next: 'start',
  81. },
  82. {
  83. regex: /(\s*\S+\s+)(as)/i,
  84. token: [null, 'keyword'],
  85. next: 'start',
  86. },
  87. // Fail safe return to start
  88. {
  89. token: null,
  90. next: 'start',
  91. },
  92. ],
  93. single: [
  94. {
  95. regex: /(?:[^\\']|\\.)/,
  96. token: 'string',
  97. },
  98. {
  99. regex: /'/,
  100. token: 'string',
  101. pop: true,
  102. },
  103. ],
  104. double: [
  105. {
  106. regex: /(?:[^\\"]|\\.)/,
  107. token: 'string',
  108. },
  109. {
  110. regex: /"/,
  111. token: 'string',
  112. pop: true,
  113. },
  114. ],
  115. array: [
  116. {
  117. regex: /\]/,
  118. token: null,
  119. next: 'start',
  120. },
  121. {
  122. regex: /"(?:[^\\"]|\\.)*"?/,
  123. token: 'string',
  124. },
  125. ],
  126. expose: [
  127. {
  128. regex: /\d+$/,
  129. token: 'number',
  130. next: 'start',
  131. },
  132. {
  133. regex: /[^\d]+$/,
  134. token: null,
  135. next: 'start',
  136. },
  137. {
  138. regex: /\d+/,
  139. token: 'number',
  140. },
  141. {
  142. regex: /[^\d]+/,
  143. token: null,
  144. },
  145. // Fail safe return to start
  146. {
  147. token: null,
  148. next: 'start',
  149. },
  150. ],
  151. arguments: [
  152. {
  153. regex: /^\s*#.*$/,
  154. sol: true,
  155. token: 'comment',
  156. },
  157. {
  158. regex: /"(?:[^\\"]|\\.)*"?$/,
  159. token: 'string',
  160. next: 'start',
  161. },
  162. {
  163. regex: /"/,
  164. token: 'string',
  165. push: 'double',
  166. },
  167. {
  168. regex: /'(?:[^\\']|\\.)*'?$/,
  169. token: 'string',
  170. next: 'start',
  171. },
  172. {
  173. regex: /'/,
  174. token: 'string',
  175. push: 'single',
  176. },
  177. {
  178. regex: /[^#"']+[\\`]$/,
  179. token: null,
  180. },
  181. {
  182. regex: /[^#"']+$/,
  183. token: null,
  184. next: 'start',
  185. },
  186. {
  187. regex: /[^#"']+/,
  188. token: null,
  189. },
  190. // Fail safe return to start
  191. {
  192. token: null,
  193. next: 'start',
  194. },
  195. ],
  196. meta: {
  197. lineComment: '#',
  198. },
  199. })
  200. CodeMirror.defineMIME('text/x-dockerfile', 'dockerfile')
  201. })