|
14 | 14 | */ |
15 | 15 | package com.github.jimschubert.rewrite.docker.internal; |
16 | 16 |
|
17 | | -import com.github.jimschubert.rewrite.docker.tree.Docker; |
18 | | -import com.github.jimschubert.rewrite.docker.tree.Quoting; |
| 17 | +import com.github.jimschubert.rewrite.docker.tree.*; |
| 18 | +import org.openrewrite.internal.StringUtils; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.List; |
| 23 | +import java.util.function.Function; |
19 | 24 |
|
20 | 25 | import static com.github.jimschubert.rewrite.docker.internal.ParserConstants.*; |
21 | 26 |
|
@@ -62,4 +67,215 @@ public static Docker.KeyArgs stringToKeyArgs(String s) { |
62 | 67 | } |
63 | 68 | return new Docker.KeyArgs(stringWithPadding.prefix(), Docker.Literal.build(key), Docker.Literal.build(value), EQUAL.equals(delim), q); |
64 | 69 | } |
| 70 | + |
| 71 | + public static <T> List<DockerRightPadded<T>> parseElements(String input, String delims, boolean appendRightPadding, ParserState state, Function<String, T> elementCreator) { |
| 72 | + List<DockerRightPadded<T>> elements = new ArrayList<>(); |
| 73 | + StringBuilder currentElement = new StringBuilder(); |
| 74 | + StringBuilder afterBuilder = new StringBuilder(); // queue up escaped newlines and whitespace as 'after' for previous element |
| 75 | + |
| 76 | + // inCollectible is used to accumulate elements within surrounding characters like single/double quotes, parentheses, etc. |
| 77 | + boolean inCollectible = false; |
| 78 | + char doubleQuote = DOUBLE_QUOTE.charAt(0); |
| 79 | + char singleQuote = SINGLE_QUOTE.charAt(0); |
| 80 | + char bracketOpen = '('; |
| 81 | + char bracketClose = ')'; |
| 82 | + char braceOpen = '{'; |
| 83 | + char braceClose = '}'; |
| 84 | + char squareBracketOpen = '['; |
| 85 | + char squareBracketClose = ']'; |
| 86 | + char escape = state.escapeChar(); |
| 87 | + char quote = 0; |
| 88 | + char lastChar = 0; |
| 89 | + char comma = ','; |
| 90 | + |
| 91 | + // create a lookup of chars from delims |
| 92 | + HashSet<Character> delimSet = new HashSet<>(); |
| 93 | + for (char c : delims.toCharArray()) { |
| 94 | + delimSet.add(c); |
| 95 | + } |
| 96 | + |
| 97 | + boolean inHeredoc = false; |
| 98 | + |
| 99 | + for (int i = 0; i < input.length(); i++) { |
| 100 | + char c = input.charAt(i); |
| 101 | + if (inCollectible) { |
| 102 | + if ((c == quote || c == bracketClose || c == braceClose || c == squareBracketClose) && lastChar != escape) { |
| 103 | + inCollectible = false; |
| 104 | + } |
| 105 | + currentElement.append(c); |
| 106 | + } else { |
| 107 | + if (delimSet.contains(c) && (lastChar != escape || (inHeredoc && c == '\n'))) { |
| 108 | + if (!StringUtils.isBlank(currentElement.toString())) { |
| 109 | + elements.add(DockerRightPadded.build(elementCreator.apply(currentElement.toString())) |
| 110 | + .withAfter(Space.EMPTY)); |
| 111 | + currentElement.setLength(0); |
| 112 | + } |
| 113 | + // drop comma, assuming we are creating a list of elements |
| 114 | + if (c != comma) { |
| 115 | + currentElement.append(c); |
| 116 | + } |
| 117 | + } else { |
| 118 | + if (c == doubleQuote || c == singleQuote || c == bracketOpen || c == braceOpen || c == squareBracketOpen) { |
| 119 | + inCollectible = true; |
| 120 | + quote = c; |
| 121 | + } |
| 122 | + |
| 123 | + Heredoc heredoc = state.heredoc(); |
| 124 | + if (inHeredoc && !elements.isEmpty() && elements.get(elements.size() - 1).getElement() instanceof Docker.Literal) { |
| 125 | + Docker.Literal literal = (Docker.Literal) elements.get(elements.size() - 1).getElement(); |
| 126 | + // allows commands to come after a heredoc. Does not support heredoc within a heredoc or multiple heredocs |
| 127 | + |
| 128 | + if (literal.getText() != null && heredoc != null && literal.getText().equals(heredoc.name())) { |
| 129 | + inHeredoc = false; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Check if within a heredoc and set escape character to '\n' |
| 134 | + if (heredoc != null && c == '\n' && !inHeredoc) { |
| 135 | + inHeredoc = true; |
| 136 | + afterBuilder.append(c); |
| 137 | + if (currentElement.length() > 0 && ( |
| 138 | + currentElement.toString().endsWith(heredoc.indicator()) || (heredoc.redirectionTo() != null && currentElement.toString().endsWith(heredoc.redirectionTo())))) { |
| 139 | + elements.add(DockerRightPadded.build(elementCreator.apply(currentElement.toString())) |
| 140 | + .withAfter(Space.build(afterBuilder.toString()))); |
| 141 | + currentElement.setLength(0); |
| 142 | + afterBuilder.setLength(0); |
| 143 | + } |
| 144 | + |
| 145 | + lastChar = c; |
| 146 | + continue; |
| 147 | + } else //noinspection ConstantValue |
| 148 | + if (heredoc != null && c == '\n' && inHeredoc) { |
| 149 | + // IntelliJ incorrectly flags inHeredoc as a constant 'true', but it's obviously not. |
| 150 | + if (!currentElement.toString().endsWith(heredoc.indicator())) { |
| 151 | + afterBuilder.append(c); |
| 152 | + // this check allows us to accumulate "after" newlines and whitespace after for the last element |
| 153 | + if (currentElement.length() > 0) { |
| 154 | + elements.add(DockerRightPadded.build(elementCreator.apply(currentElement.toString())) |
| 155 | + .withAfter(Space.build(afterBuilder.toString()))); |
| 156 | + currentElement.setLength(0); |
| 157 | + afterBuilder.setLength(0); |
| 158 | + } |
| 159 | + |
| 160 | + lastChar = c; |
| 161 | + continue; |
| 162 | + } |
| 163 | + |
| 164 | + // if we have a heredoc name, we are done with the heredoc |
| 165 | + inHeredoc = false; |
| 166 | + } |
| 167 | + |
| 168 | + // "peek": if the current character is an escape and the next character is newline or carriage return, 'after' and advance |
| 169 | + int nextCharIndex = i + 1; |
| 170 | + if (c == escape && nextCharIndex < input.length() && (input.charAt(nextCharIndex) == '\n' || input.charAt(nextCharIndex) == '\r')) { |
| 171 | + // if we had already collected some whitespace (only whitespace), add it as 'after' to the last element |
| 172 | + if (StringUtils.isBlank(currentElement.toString())) { |
| 173 | + afterBuilder.append(currentElement); |
| 174 | + currentElement.setLength(0); |
| 175 | + } |
| 176 | + |
| 177 | + char next = input.charAt(nextCharIndex); |
| 178 | + afterBuilder.append(escape).append(next); |
| 179 | + |
| 180 | + // manually advance |
| 181 | + lastChar = next; |
| 182 | + i++; |
| 183 | + continue; |
| 184 | + } |
| 185 | + |
| 186 | + // if 'after' builder is not empty and the character is whitespace, accumulate it |
| 187 | + if (afterBuilder.length() > 0 && (c == ' ' || c == '\t' || c == '\n')) { |
| 188 | + afterBuilder.append(c); |
| 189 | + lastChar = c; |
| 190 | + continue; |
| 191 | + } |
| 192 | + |
| 193 | + // Drop escape character if it is followed by a space |
| 194 | + // other situations will retain the escape character |
| 195 | + if (lastChar == escape && c == ' ') { |
| 196 | + currentElement.setLength(currentElement.length() - 1); |
| 197 | + } |
| 198 | + |
| 199 | + // no longer accumulating a prefix, add as "after" to the last element |
| 200 | + if (!elements.isEmpty() && afterBuilder.length() > 0) { |
| 201 | + int idx = elements.size() - 1; |
| 202 | + DockerRightPadded<T> element = elements.get(idx); |
| 203 | + elements.set(idx, element.withAfter(Space.append(element.getAfter(), Space.build(afterBuilder.toString())))); |
| 204 | + afterBuilder.setLength(0); |
| 205 | + } |
| 206 | + |
| 207 | + // Only collect the current element if we're not "in a prefix" situation |
| 208 | + if (afterBuilder.length() == 0) { |
| 209 | + currentElement.append(c); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + lastChar = c; |
| 214 | + } |
| 215 | + |
| 216 | + if (currentElement.length() > 0) { |
| 217 | + // if it's whitespace only, add it as "after" to the last element |
| 218 | + if (StringUtils.isBlank(currentElement.toString())) { |
| 219 | + if (!elements.isEmpty()) { |
| 220 | + int idx = elements.size() - 1; |
| 221 | + elements.set(idx, elements.get(idx).withAfter(Space.build(currentElement.toString()))); |
| 222 | + } |
| 223 | + } else { |
| 224 | + DockerRightPadded<T> element = DockerRightPadded.build(elementCreator.apply(currentElement.toString())); |
| 225 | + if (appendRightPadding) { |
| 226 | + element = element.withAfter(state.rightPadding()); |
| 227 | + } |
| 228 | + elements.add(element); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + if (afterBuilder.length() > 0) { |
| 233 | + int idx = elements.size() - 1; |
| 234 | + if (idx >= 0) { |
| 235 | + DockerRightPadded<T> element = elements.get(idx); |
| 236 | + elements.set(idx, element.withAfter(Space.append(element.getAfter(), Space.build(afterBuilder.toString())))); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + return elements; |
| 241 | + } |
| 242 | + |
| 243 | + public static List<DockerRightPadded<Docker.Port>> parsePorts(String input, ParserState state) { |
| 244 | + return parseElements(input, SPACE + TAB, true, state, ParserUtils::stringToPorts); |
| 245 | + } |
| 246 | + |
| 247 | + public static List<DockerRightPadded<Docker.KeyArgs>> parseArgs(String input, ParserState state) { |
| 248 | + return parseElements(input, SPACE + TAB, true, state, ParserUtils::stringToKeyArgs); |
| 249 | + } |
| 250 | + |
| 251 | + public static List<DockerRightPadded<Docker.Literal>> parseLiterals(String input, ParserState state) { |
| 252 | + return parseElements(input, SPACE, true, state, ParserUtils::createLiteral); |
| 253 | + } |
| 254 | + |
| 255 | + public static List<DockerRightPadded<Docker.Literal>> parseLiterals(Form form, String input, ParserState state) { |
| 256 | + // appendRightPadding is true for shell form, false for exec form |
| 257 | + // exec form is a JSON array, so we need to parse it differently where right padding is after the ']'. |
| 258 | + return parseElements(input, form == Form.EXEC ? COMMA : SPACE, form == Form.SHELL, state, ParserUtils::createLiteral); |
| 259 | + } |
| 260 | + |
| 261 | + public static Docker.Literal createLiteral(String s) { |
| 262 | + if (s == null || s.isEmpty()) { |
| 263 | + return null; |
| 264 | + } |
| 265 | + StringWithPadding stringWithPadding = StringWithPadding.of(s); |
| 266 | + String content = stringWithPadding.content(); |
| 267 | + Quoting q = Quoting.UNQUOTED; |
| 268 | + if (content.startsWith(DOUBLE_QUOTE) && content.endsWith(DOUBLE_QUOTE)) { |
| 269 | + q = Quoting.DOUBLE_QUOTED; |
| 270 | + content = content.substring(1, content.length() - 1); |
| 271 | + } else if (content.startsWith(SINGLE_QUOTE) && content.endsWith(SINGLE_QUOTE)) { |
| 272 | + q = Quoting.SINGLE_QUOTED; |
| 273 | + content = content.substring(1, content.length() - 1); |
| 274 | + } |
| 275 | + return Docker.Literal.build( |
| 276 | + q, stringWithPadding.prefix(), |
| 277 | + content, |
| 278 | + stringWithPadding.suffix() |
| 279 | + ); |
| 280 | + } |
65 | 281 | } |
0 commit comments