(expression: string)
| 173 | } |
| 174 | |
| 175 | export function parseKeybindingWhenExpression(expression: string): KeybindingWhenNode | null { |
| 176 | const tokens = tokenizeWhenExpression(expression); |
| 177 | if (!tokens || tokens.length === 0) return null; |
| 178 | let index = 0; |
| 179 | |
| 180 | const parsePrimary = (depth: number): KeybindingWhenNode | null => { |
| 181 | if (depth > MAX_WHEN_EXPRESSION_DEPTH) { |
| 182 | return null; |
| 183 | } |
| 184 | const token = tokens[index]; |
| 185 | if (!token) return null; |
| 186 | |
| 187 | if (token.type === "identifier") { |
| 188 | index += 1; |
| 189 | return { type: "identifier", name: token.value }; |
| 190 | } |
| 191 | |
| 192 | if (token.type === "lparen") { |
| 193 | index += 1; |
| 194 | const expressionNode = parseOr(depth + 1); |
| 195 | const closeToken = tokens[index]; |
| 196 | if (!expressionNode || !closeToken || closeToken.type !== "rparen") { |
| 197 | return null; |
| 198 | } |
| 199 | index += 1; |
| 200 | return expressionNode; |
| 201 | } |
| 202 | |
| 203 | return null; |
| 204 | }; |
| 205 | |
| 206 | const parseUnary = (depth: number): KeybindingWhenNode | null => { |
| 207 | let notCount = 0; |
| 208 | while (tokens[index]?.type === "not") { |
| 209 | index += 1; |
| 210 | notCount += 1; |
| 211 | if (notCount > MAX_WHEN_EXPRESSION_DEPTH) { |
| 212 | return null; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | let node = parsePrimary(depth); |
| 217 | if (!node) return null; |
| 218 | |
| 219 | while (notCount > 0) { |
| 220 | node = { type: "not", node }; |
| 221 | notCount -= 1; |
| 222 | } |
| 223 | |
| 224 | return node; |
| 225 | }; |
| 226 | |
| 227 | const parseAnd = (depth: number): KeybindingWhenNode | null => { |
| 228 | let left = parseUnary(depth); |
| 229 | if (!left) return null; |
| 230 | |
| 231 | while (tokens[index]?.type === "and") { |
| 232 | index += 1; |
no test coverage detected