private List tokenize()

in impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java [83:162]


    private List<String> tokenize(String expression) {
        List<String> tokens = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        char quoteType = 0;
        boolean inPropertyReference = false;

        for (int i = 0; i < expression.length(); i++) {
            char c = expression.charAt(i);

            if (quoteType != 0) {
                if (c == quoteType) {
                    quoteType = 0;
                    sb.append(c);
                    tokens.add(sb.toString());
                    sb.setLength(0);
                } else {
                    sb.append(c);
                }
                continue;
            }

            if (inPropertyReference) {
                if (c == '}') {
                    inPropertyReference = false;
                    tokens.add("${" + sb + "}");
                    sb.setLength(0);
                } else {
                    sb.append(c);
                }
                continue;
            }

            if (c == '$' && i + 1 < expression.length() && expression.charAt(i + 1) == '{') {
                if (!sb.isEmpty()) {
                    tokens.add(sb.toString());
                    sb.setLength(0);
                }
                inPropertyReference = true;
                i++; // Skip the '{'
                continue;
            }

            if (c == '"' || c == '\'') {
                if (!sb.isEmpty()) {
                    tokens.add(sb.toString());
                    sb.setLength(0);
                }
                quoteType = c;
                sb.append(c);
            } else if (c == ' ' || c == '(' || c == ')' || c == ',' || c == '+' || c == '>' || c == '<' || c == '='
                    || c == '!') {
                if (!sb.isEmpty()) {
                    tokens.add(sb.toString());
                    sb.setLength(0);
                }
                if (c != ' ') {
                    if ((c == '>' || c == '<' || c == '=' || c == '!')
                            && i + 1 < expression.length()
                            && expression.charAt(i + 1) == '=') {
                        tokens.add(c + "=");
                        i++; // Skip the next character
                    } else {
                        tokens.add(String.valueOf(c));
                    }
                }
            } else {
                sb.append(c);
            }
        }

        if (inPropertyReference) {
            throw new RuntimeException("Unclosed property reference: ${");
        }

        if (!sb.isEmpty()) {
            tokens.add(sb.toString());
        }

        return tokens;
    }