MCPcopy Create free account
hub / github.com/LibPDF-js/core / parseDict

Method parseDict

src/parser/object-parser.ts:278–367  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

276 }
277
278 private parseDict(): ParseResult {
279 this.shift(); // consume '<<'
280
281 // Collect entries in array to pass to constructor (avoids setting dirty flag)
282 const entries: [PdfName, PdfObject][] = [];
283
284 while (true) {
285 this.ensureBufferFilled();
286
287 if (this.buf1 === null || this.buf1.type === "eof") {
288 if (this.recoveryMode) {
289 this.warn("Unterminated dictionary at EOF");
290 break;
291 }
292
293 throw new ObjectParseError("Unterminated dictionary at EOF");
294 }
295
296 if (this.buf1.type === "delimiter" && this.buf1.value === ">>") {
297 // Partial shift: move buf2 to buf1, but DON'T fill buf2.
298 // This prevents reading past a "stream" keyword into binary data.
299 this.buf1 = this.buf2;
300 this.buf2 = null;
301 break;
302 }
303
304 // Key must be a name
305 if (this.buf1.type !== "name") {
306 if (this.recoveryMode) {
307 this.warn(`Invalid dictionary key: expected name, got ${this.buf1.type}`);
308 // Skip the invalid key AND its would-be value (pair recovery)
309 this.skipInvalidPair();
310 continue;
311 }
312
313 throw new ObjectParseError(`Invalid dictionary key: expected name, got ${this.buf1.type}`);
314 }
315
316 const key = PdfName.of(this.buf1.value);
317
318 this.shift(); // consume key
319
320 // Check if value is missing (>> immediately after key)
321 this.ensureBufferFilled();
322 const valueToken = this.current();
323 if (valueToken !== null && valueToken.type === "delimiter" && valueToken.value === ">>") {
324 if (this.recoveryMode) {
325 this.warn(`Missing value for key ${key.value}`);
326 // Don't consume >>, let the loop handle it
327 continue;
328 }
329
330 throw new ObjectParseError(`Missing value for key ${key.value}`);
331 }
332
333 // Parse value
334 const valueResult = this.parseObject();
335

Callers 1

parseDelimiterMethod · 0.95

Calls 9

shiftMethod · 0.95
ensureBufferFilledMethod · 0.95
warnMethod · 0.95
skipInvalidPairMethod · 0.95
currentMethod · 0.95
parseObjectMethod · 0.95
pushMethod · 0.80
ofMethod · 0.45
nextTokenMethod · 0.45

Tested by

no test coverage detected