| 76 | } |
| 77 | |
| 78 | class Parser { |
| 79 | readonly source: string; |
| 80 | private pos = 0; |
| 81 | |
| 82 | constructor(source: string) { |
| 83 | this.source = source; |
| 84 | } |
| 85 | |
| 86 | parse(): ExprNode { |
| 87 | this.skipWhitespace(); |
| 88 | if (this.isEof()) { |
| 89 | this.fail("Unexpected end of input"); |
| 90 | } |
| 91 | const node = this.parseExpr(); |
| 92 | this.skipWhitespace(); |
| 93 | if (!this.isEof()) { |
| 94 | const tok = this.peek(); |
| 95 | this.fail(tok === null ? "Unexpected end of input" : `Unexpected token "${tok}"`); |
| 96 | } |
| 97 | return node; |
| 98 | } |
| 99 | |
| 100 | private parseExpr(): ExprNode { |
| 101 | return this.parseTernary(); |
| 102 | } |
| 103 | |
| 104 | private parseTernary(): ExprNode { |
| 105 | const condition = this.parseCompare(); |
| 106 | this.skipWhitespace(); |
| 107 | if (!this.consumeIf("?")) return condition; |
| 108 | const thenExpr = this.parseExpr(); |
| 109 | this.skipWhitespace(); |
| 110 | if (!this.consumeIf(":")) { |
| 111 | this.fail('Expected ":" in ternary expression'); |
| 112 | } |
| 113 | const elseExpr = this.parseExpr(); |
| 114 | return { |
| 115 | kind: "ternary", |
| 116 | condition, |
| 117 | // biome-ignore lint/suspicious/noThenProperty: "then" is an AST field (not a Promise-like thenable). |
| 118 | then: thenExpr, |
| 119 | else: elseExpr, |
| 120 | }; |
| 121 | } |
| 122 | |
| 123 | private parseCompare(): ExprNode { |
| 124 | const left = this.parseAdditive(); |
| 125 | this.skipWhitespace(); |
| 126 | const op = this.consumeCompareOp(); |
| 127 | if (op === null) return left; |
| 128 | const right = this.parseAdditive(); |
| 129 | this.skipWhitespace(); |
| 130 | const extra = this.consumeCompareOp(); |
| 131 | if (extra !== null) { |
| 132 | this.fail( |
| 133 | "Only one comparison operator is allowed in a compare expression", |
| 134 | this.pos - extra.length, |
| 135 | ); |
nothing calls this directly
no outgoing calls
no test coverage detected