* Parse all operations from the content stream.
()
| 26 | * Parse all operations from the content stream. |
| 27 | */ |
| 28 | parse(): ParseResult { |
| 29 | const operations: AnyOperation[] = []; |
| 30 | const operands: ContentToken[] = []; |
| 31 | |
| 32 | while (!this.tokenizer.eof()) { |
| 33 | const token = this.tokenizer.nextToken(); |
| 34 | |
| 35 | if (token === null) { |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | if (token.type === "operator") { |
| 40 | if (token.name === "BI") { |
| 41 | // Special handling for inline images |
| 42 | const inlineImage = this.parseInlineImage(); |
| 43 | |
| 44 | operations.push(inlineImage); |
| 45 | } else { |
| 46 | operations.push({ |
| 47 | operator: token.name, |
| 48 | operands: [...operands], |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | operands.length = 0; |
| 53 | } else { |
| 54 | operands.push(token); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Trailing operands without operator |
| 59 | if (operands.length > 0) { |
| 60 | this.warnings.push( |
| 61 | `Content stream has ${operands.length} trailing operand(s) without operator`, |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | return { |
| 66 | operations, |
| 67 | warnings: this.warnings, |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Iterate operations lazily. |
no test coverage detected