(minPrec = 0)
| 211 | } |
| 212 | |
| 213 | parseExpression(minPrec = 0): Json { |
| 214 | let lhs = this.parsePrefix(); |
| 215 | for (;;) { |
| 216 | const t = this.peek(); |
| 217 | if (!t) break; |
| 218 | if (t.kind === 'punct') { |
| 219 | const bin = BINARY_OPS[t.value]; |
| 220 | if (bin && bin.prec >= minPrec) { |
| 221 | this.next(); |
| 222 | const rhs = this.parseExpression(bin.right ? bin.prec : bin.prec + 1); |
| 223 | lhs = bin.flat ? flatBinaryOp(bin.op, lhs, rhs) : [bin.op, lhs, rhs]; |
| 224 | continue; |
| 225 | } |
| 226 | if (t.value === '+' && PREC_PLUS >= minPrec) { |
| 227 | this.next(); |
| 228 | lhs = flatBinary('Add', lhs, this.parseExpression(PREC_PLUS + 1)); |
| 229 | continue; |
| 230 | } |
| 231 | if (t.value === '-' && PREC_PLUS >= minPrec) { |
| 232 | this.next(); |
| 233 | lhs = ['Subtract', lhs, this.parseExpression(PREC_PLUS + 1)]; |
| 234 | continue; |
| 235 | } |
| 236 | if (t.value === '*' && PREC_TIMES >= minPrec) { |
| 237 | this.next(); |
| 238 | lhs = flatBinary( |
| 239 | 'Multiply', |
| 240 | lhs, |
| 241 | this.parseExpression(PREC_TIMES + 1) |
| 242 | ); |
| 243 | continue; |
| 244 | } |
| 245 | if (t.value === '/' && PREC_TIMES >= minPrec) { |
| 246 | this.next(); |
| 247 | lhs = divide(lhs, this.parseExpression(PREC_TIMES + 1)); |
| 248 | continue; |
| 249 | } |
| 250 | if (t.value === '^' && PREC_POWER >= minPrec) { |
| 251 | this.next(); |
| 252 | // right-associative |
| 253 | lhs = ['Power', lhs, this.parseExpression(PREC_POWER)]; |
| 254 | continue; |
| 255 | } |
| 256 | if (t.value === '!' && PREC_FACTORIAL >= minPrec) { |
| 257 | this.next(); |
| 258 | lhs = ['Factorial', lhs]; |
| 259 | continue; |
| 260 | } |
| 261 | } |
| 262 | // Juxtaposition = multiplication: `2 x`, `a (b + c)` — an operand |
| 263 | // token follows directly. Only when we can still bind at Times level. |
| 264 | if ( |
| 265 | PREC_TIMES >= minPrec && |
| 266 | (t.kind === 'number' || |
| 267 | t.kind === 'symbol' || |
| 268 | t.kind === 'pattern' || |
| 269 | (t.kind === 'punct' && t.value === '(')) |
| 270 | ) { |
no test coverage detected