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