(pattern: string)
| 831 | // `Pattern.compile` from moby/patternmatcher, with the generated regular |
| 832 | // expression replaced by tokens and the reference's string fast paths kept. |
| 833 | const compileDocker = (pattern: string): ((path: string) => boolean) | string => { |
| 834 | const chars = Array.from(pattern) |
| 835 | if (!isGoMatchSyntax(chars)) { |
| 836 | return "syntax error in pattern" |
| 837 | } |
| 838 | let kind: "exact" | "prefix" | "suffix" | "tokens" = "exact" |
| 839 | const tokens: Array<DockerToken> = [] |
| 840 | // `text/scanner` skips a leading BOM; the string tests keep it |
| 841 | for (let i = chars[0] === "\uFEFF" ? 1 : 0, iteration = 0; i < chars.length; iteration++) { |
| 842 | const char = chars[i++] |
| 843 | if (char === "*") { |
| 844 | if (chars[i] === "*") { |
| 845 | i++ |
| 846 | if (chars[i] === "/") { |
| 847 | i++ |
| 848 | } |
| 849 | if (i === chars.length) { |
| 850 | if (kind === "exact") { |
| 851 | kind = "prefix" |
| 852 | } else { |
| 853 | tokens.push({ _tag: "StarAll" }) |
| 854 | kind = "tokens" |
| 855 | } |
| 856 | } else { |
| 857 | tokens.push({ _tag: "GroupStart" }, { _tag: "GroupBody" }) |
| 858 | kind = "tokens" |
| 859 | } |
| 860 | if (iteration === 0) { |
| 861 | kind = "suffix" |
| 862 | } |
| 863 | } else { |
| 864 | tokens.push({ _tag: "Star" }) |
| 865 | kind = "tokens" |
| 866 | } |
| 867 | } else if (char === "?") { |
| 868 | tokens.push({ _tag: "Any" }) |
| 869 | kind = "tokens" |
| 870 | } else if (char === "\\") { |
| 871 | const next = chars[i++] |
| 872 | if (next === undefined) { |
| 873 | // the generated expression would end in a lone backslash |
| 874 | return `a trailing backslash is regular expression syntax in Docker; ` + |
| 875 | `escape it as "\\\\" to match a literal backslash` |
| 876 | } |
| 877 | const error = escapeError(next) |
| 878 | if (error !== undefined) { |
| 879 | return error |
| 880 | } |
| 881 | tokens.push({ _tag: "Char", code: codePoint(next) }) |
| 882 | kind = "tokens" |
| 883 | } else if (char === "[") { |
| 884 | const parsed = parseDockerClass(chars, i - 1) |
| 885 | if (typeof parsed === "string") { |
| 886 | return parsed |
| 887 | } |
| 888 | tokens.push(parsed.value) |
| 889 | i = parsed.end + 1 |
| 890 | kind = "tokens" |
no test coverage detected