dockerfile.js 4.6 KB

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