GPQAPP
211 строк · 4.6 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"), require("../../addon/mode/simple"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror", "../../addon/mode/simple"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14var from = "from";15var fromRegex = new RegExp("^(\\s*)\\b(" + from + ")\\b", "i");16
17var shells = ["run", "cmd", "entrypoint", "shell"];18var shellsAsArrayRegex = new RegExp("^(\\s*)(" + shells.join('|') + ")(\\s+\\[)", "i");19
20var expose = "expose";21var exposeRegex = new RegExp("^(\\s*)(" + expose + ")(\\s+)", "i");22
23var others = [24"arg", "from", "maintainer", "label", "env",25"add", "copy", "volume", "user",26"workdir", "onbuild", "stopsignal", "healthcheck", "shell"27];28
29// Collect all Dockerfile directives30var instructions = [from, expose].concat(shells).concat(others),31instructionRegex = "(" + instructions.join('|') + ")",32instructionOnlyLine = new RegExp("^(\\s*)" + instructionRegex + "(\\s*)(#.*)?$", "i"),33instructionWithArguments = new RegExp("^(\\s*)" + instructionRegex + "(\\s+)", "i");34
35CodeMirror.defineSimpleMode("dockerfile", {36start: [37// Block comment: This is a line starting with a comment38{39regex: /^\s*#.*$/,40sol: true,41token: "comment"42},43{44regex: fromRegex,45token: [null, "keyword"],46sol: true,47next: "from"48},49// Highlight an instruction without any arguments (for convenience)50{51regex: instructionOnlyLine,52token: [null, "keyword", null, "error"],53sol: true54},55{56regex: shellsAsArrayRegex,57token: [null, "keyword", null],58sol: true,59next: "array"60},61{62regex: exposeRegex,63token: [null, "keyword", null],64sol: true,65next: "expose"66},67// Highlight an instruction followed by arguments68{69regex: instructionWithArguments,70token: [null, "keyword", null],71sol: true,72next: "arguments"73},74{75regex: /./,76token: null77}78],79from: [80{81regex: /\s*$/,82token: null,83next: "start"84},85{86// Line comment without instruction arguments is an error87regex: /(\s*)(#.*)$/,88token: [null, "error"],89next: "start"90},91{92regex: /(\s*\S+\s+)(as)/i,93token: [null, "keyword"],94next: "start"95},96// Fail safe return to start97{98token: null,99next: "start"100}101],102single: [103{104regex: /(?:[^\\']|\\.)/,105token: "string"106},107{108regex: /'/,109token: "string",110pop: true111}112],113double: [114{115regex: /(?:[^\\"]|\\.)/,116token: "string"117},118{119regex: /"/,120token: "string",121pop: true122}123],124array: [125{126regex: /\]/,127token: null,128next: "start"129},130{131regex: /"(?:[^\\"]|\\.)*"?/,132token: "string"133}134],135expose: [136{137regex: /\d+$/,138token: "number",139next: "start"140},141{142regex: /[^\d]+$/,143token: null,144next: "start"145},146{147regex: /\d+/,148token: "number"149},150{151regex: /[^\d]+/,152token: null153},154// Fail safe return to start155{156token: null,157next: "start"158}159],160arguments: [161{162regex: /^\s*#.*$/,163sol: true,164token: "comment"165},166{167regex: /"(?:[^\\"]|\\.)*"?$/,168token: "string",169next: "start"170},171{172regex: /"/,173token: "string",174push: "double"175},176{177regex: /'(?:[^\\']|\\.)*'?$/,178token: "string",179next: "start"180},181{182regex: /'/,183token: "string",184push: "single"185},186{187regex: /[^#"']+[\\`]$/,188token: null189},190{191regex: /[^#"']+$/,192token: null,193next: "start"194},195{196regex: /[^#"']+/,197token: null198},199// Fail safe return to start200{201token: null,202next: "start"203}204],205meta: {206lineComment: "#"207}208});209
210CodeMirror.defineMIME("text/x-dockerfile", "dockerfile");211});212