|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.bundle; |
| 16 | + |
| 17 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableList; |
| 20 | +import dev.cel.bundle.CelEnvironment.TypeDecl; |
| 21 | +import dev.cel.common.formats.ParserContext; |
| 22 | + |
| 23 | +/** |
| 24 | + * Parses a type specifier shorthand string (e.g. {@code "map<string, int>"}, {@code "list<~T>"}, |
| 25 | + * {@code "int"}) into a {@link TypeDecl}. |
| 26 | + */ |
| 27 | +final class TypeSpecifierParser { |
| 28 | + private static final int MAX_RECURSION_DEPTH = 64; |
| 29 | + static final TypeDecl ERROR_TYPE_DECL = TypeDecl.create("*error*"); |
| 30 | + |
| 31 | + private final String text; |
| 32 | + private final int length; |
| 33 | + private int pos; |
| 34 | + |
| 35 | + static TypeDecl parse(String text) { |
| 36 | + checkNotNull(text); |
| 37 | + TypeSpecifierParser parser = new TypeSpecifierParser(text); |
| 38 | + return parser.parse(); |
| 39 | + } |
| 40 | + |
| 41 | + static TypeDecl parse(ParserContext<?> ctx, long nodeId, String text) { |
| 42 | + checkNotNull(ctx); |
| 43 | + checkNotNull(text); |
| 44 | + try { |
| 45 | + return parse(text); |
| 46 | + } catch (IllegalArgumentException e) { |
| 47 | + ctx.reportError(nodeId, e.getMessage()); |
| 48 | + return ERROR_TYPE_DECL; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private TypeDecl parse() { |
| 53 | + TypeDecl res = parseTypeElem(0); |
| 54 | + skipWhitespace(); |
| 55 | + if (pos < length) { |
| 56 | + throw new IllegalArgumentException( |
| 57 | + String.format( |
| 58 | + "unexpected character '%c' at position %d in %s", |
| 59 | + text.charAt(pos), pos, formatQuoted(text))); |
| 60 | + } |
| 61 | + return res; |
| 62 | + } |
| 63 | + |
| 64 | + private TypeSpecifierParser(String text) { |
| 65 | + this.text = text; |
| 66 | + this.length = text.length(); |
| 67 | + this.pos = 0; |
| 68 | + } |
| 69 | + |
| 70 | + private TypeDecl parseTypeElem(int depth) { |
| 71 | + if (depth > MAX_RECURSION_DEPTH) { |
| 72 | + throw new IllegalArgumentException( |
| 73 | + String.format("exceeded maximum type specifier recursion depth at position %d", pos)); |
| 74 | + } |
| 75 | + skipWhitespace(); |
| 76 | + if (pos < length && text.charAt(pos) == '~') { |
| 77 | + pos++; // consume '~' |
| 78 | + String id = parseTypeParamIdent(); |
| 79 | + return TypeDecl.ofTypeParam(id); |
| 80 | + } |
| 81 | + return parseConcreteType(depth); |
| 82 | + } |
| 83 | + |
| 84 | + private TypeDecl parseConcreteType(int depth) { |
| 85 | + String id = parseNamespaceIdentifier(); |
| 86 | + skipWhitespace(); |
| 87 | + if (pos < length && text.charAt(pos) == '<') { |
| 88 | + pos++; // consume '<' |
| 89 | + ImmutableList.Builder<TypeDecl> params = ImmutableList.builder(); |
| 90 | + while (true) { |
| 91 | + TypeDecl param = parseTypeElem(depth + 1); |
| 92 | + params.add(param); |
| 93 | + skipWhitespace(); |
| 94 | + if (pos < length && text.charAt(pos) == ',') { |
| 95 | + pos++; // consume ',' |
| 96 | + continue; |
| 97 | + } |
| 98 | + if (pos < length && text.charAt(pos) == '>') { |
| 99 | + pos++; // consume '>' |
| 100 | + break; |
| 101 | + } |
| 102 | + throw new IllegalArgumentException( |
| 103 | + String.format("expected ',' or '>' at position %d", pos)); |
| 104 | + } |
| 105 | + return TypeDecl.newBuilder().setName(id).addParams(params.build()).build(); |
| 106 | + } |
| 107 | + return TypeDecl.create(id); |
| 108 | + } |
| 109 | + |
| 110 | + private String parseNamespaceIdentifier() { |
| 111 | + StringBuilder id = new StringBuilder(); |
| 112 | + while (pos < length && text.charAt(pos) != '<') { |
| 113 | + char c = text.charAt(pos); |
| 114 | + if (c == '.') { |
| 115 | + id.append('.'); |
| 116 | + pos++; // consume '.' |
| 117 | + } |
| 118 | + String ident = parseIdentifier(); |
| 119 | + id.append(ident); |
| 120 | + if (pos < length && text.charAt(pos) != '.') { |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + String identifier = id.toString(); |
| 125 | + if (identifier.isEmpty()) { |
| 126 | + throw new IllegalArgumentException(String.format("missing identifier at position %d", pos)); |
| 127 | + } |
| 128 | + return identifier; |
| 129 | + } |
| 130 | + |
| 131 | + private String parseIdentifier() { |
| 132 | + if (pos >= length) { |
| 133 | + throw new IllegalArgumentException("unexpected end of input"); |
| 134 | + } |
| 135 | + int start = pos; |
| 136 | + while (pos < length) { |
| 137 | + char c = text.charAt(pos); |
| 138 | + boolean isValid = (pos == start) ? (isAlpha(c) || c == '_') : (isAlphaNumeric(c) || c == '_'); |
| 139 | + if (isValid) { |
| 140 | + pos++; |
| 141 | + continue; |
| 142 | + } |
| 143 | + if (pos == start) { |
| 144 | + throw new IllegalArgumentException( |
| 145 | + String.format("identifier is expected, but '%c' was found at position %d", c, pos)); |
| 146 | + } |
| 147 | + break; |
| 148 | + } |
| 149 | + return text.substring(start, pos); |
| 150 | + } |
| 151 | + |
| 152 | + private String parseTypeParamIdent() { |
| 153 | + if (pos >= length) { |
| 154 | + throw new IllegalArgumentException("unexpected end of input"); |
| 155 | + } |
| 156 | + char c = text.charAt(pos); |
| 157 | + if (c < 'A' || c > 'Z') { |
| 158 | + throw new IllegalArgumentException( |
| 159 | + String.format( |
| 160 | + "invalid type parameter identifier '%c' at position %d, must be a single character" |
| 161 | + + " from A-Z", |
| 162 | + c, pos)); |
| 163 | + } |
| 164 | + pos++; |
| 165 | + if (pos < length) { |
| 166 | + char next = text.charAt(pos); |
| 167 | + if (isAlphaNumeric(next) || next == '_') { |
| 168 | + throw new IllegalArgumentException( |
| 169 | + String.format( |
| 170 | + "invalid type parameter identifier '%c' at position %d, must be a single character" |
| 171 | + + " from A-Z", |
| 172 | + next, pos)); |
| 173 | + } |
| 174 | + } |
| 175 | + return String.valueOf(c); |
| 176 | + } |
| 177 | + |
| 178 | + private void skipWhitespace() { |
| 179 | + while (pos < length) { |
| 180 | + char c = text.charAt(pos); |
| 181 | + if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { |
| 182 | + pos++; |
| 183 | + } else { |
| 184 | + break; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private static boolean isAlpha(char c) { |
| 190 | + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); |
| 191 | + } |
| 192 | + |
| 193 | + private static boolean isAlphaNumeric(char c) { |
| 194 | + return isAlpha(c) || (c >= '0' && c <= '9'); |
| 195 | + } |
| 196 | + |
| 197 | + private static String formatQuoted(String s) { |
| 198 | + return "\"" + s + "\""; |
| 199 | + } |
| 200 | +} |
0 commit comments