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.

211 lines
4.6 KiB

5 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"), 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. });