Skip to main content

Module parser

Module parser 

Source
Expand description

§COS object parser (ISO 32000-1 §7.3 object grammar)

Recursive-descent parser from the token stream (crate::lexer) to the object model (crate::object). Spec sources: iso32000__s__7.3.md (grammar), iso32000__s__7.3.10.md (indirect objects, the N G R lookahead), iso32000__s__7.3.8.md (stream framing) in the PDF-spec RAG. Clause numbers are ISO 32000-1:2008.

§What this layer does and doesn’t

Parser parses one syntactic region — a direct object, or one complete N G obj … endobj indirect-object definition at a known offset. It does NOT walk cross-reference tables (that’s crate::xref), does not resolve references (document layer), and does not decode stream data (filter layer). The one place it needs outside help is a stream’s /Length, which may itself be an indirect reference (§7.3.10 EXAMPLE 3 sanctions this for single-pass writers) — the caller supplies a resolver callback for that case, keeping the parser xref-agnostic.

§The N G R lookahead (§7.3.10)

An indirect reference is three tokens — Integer Integer Keyword(R) — indistinguishable from two array elements 1 0 followed by more content until the third token is seen. The parser therefore keeps a two-token peek buffer: on reading an Integer, it peeks up to two tokens ahead and commits to a reference only when it sees Integer + R. (This ambiguity is exactly why §7.8.2 bans indirect references inside content streams.)

§Stream framing rules enforced (§7.3.8.1)

  • The stream keyword shall be followed by CRLF or LF alone — CR alone is a hard error (the spec’s own NOTE 2 explains the ambiguity it would create). The RAG calls this “the single highest-value byte rule in the whole file-structure area.”
  • Data is exactly /Length bytes from the byte after that EOL.
  • After the data: optional EOL, then endstream, then endobj. Anything else is a /Length inconsistency (§7.3.8.2 “an error”) — fail-clean under the default StreamLengthPolicy::Strict.

§The one strictness knob: /Length vs endstream

The corpus-driven recovery heuristic the point above used to defer is now here, as an explicit, opt-in StreamLengthPolicy rather than a hidden default. Under StreamLengthPolicy::RecoverFromEndstream an unusable /Length causes the data extent to be re-derived by scanning for the endstream keyword — §7.3.8.2’s own definition of /Length is written in terms of that keyword, so it is the other half of the same normative statement, not a guess. Only crate::recover and the recovered branch of crate::document::Document::from_bytes select it; the clean load path is byte-for-byte unchanged, which is what keeps the round-trip invariant (ARCHITECTURE.md §5) safe. Every repair is counted (Parser::stream_lengths_recovered) and disclosed.

§Guards (ARCHITECTURE.md §10 — pdfcer policy, not spec)

MAX_NESTING_DEPTH bounds recursion (the spec bounds nothing here; a [[[[… bomb must not overflow the stack).

Structs§

DuplicateKeyRecord
One duplicate-key decision: the key, the value kept, the value discarded (Pass 283.0).
ParseError
A structural parse error: what went wrong and where.
Parser
Recursive-descent parser over a byte buffer.

Enums§

DuplicateKeyPolicy
What to do when one dictionary names the same key twice (§7.3.7).
ParseErrorKind
Classification of structural parse errors.
StreamLengthPolicy
How a stream whose /Length disagrees with the file should be handled.
TerminatorPolicy
What to do when an indirect object’s endobj keyword is missing.

Constants§

MAX_NESTING_DEPTH
Maximum container (array/dictionary) nesting depth.

Type Aliases§

LengthResolver
Resolver for indirect /Length values (§7.3.10 EXAMPLE 3 pattern).