NativeElixirPdfUtilities.Tokenizer (native_elixir_pdf_utilities v0.6.0)
View SourceA PDF tokenizer in pure Elixir.
Produces a stream of tokens from a PDF byte stream. It skips whitespace and comments. It recognizes:
- numbers (integers and reals)
- keywords:
obj,endobj,stream,endstream,xref,trailer,startxref - booleans
true/false,null - names
/Name(with#xxhex escapes decoded) - strings: literal
( ... )with escapes and nesting; hex<...> - punctuation:
[]<<>>andR - operators: any other keyword returned as
{:op, word}
Streams:
- After emitting
:stream, the next token is{:stream_data, binary}either read by the preceding dictionary/Length(if an integer) or by scanning untilendstream. - The tokenizer does not resolve indirect
/Lengthreferences.
Summary
Functions
Initialize the tokenizer from a binary.
Return the next token and updated tokenizer state.
Return the next token along with its byte span in the original binary.
Look at the next token without advancing the tokenizer state.
If called after a :stream token, returns a hint for the stream length
Tokenize the entire binary, returning a list of tokens.
Tokenize the entire binary, returning a list of {token, span}.
Types
@type t() :: %NativeElixirPdfUtilities.Tokenizer{ bin: binary(), dict_depth: term(), in_stream: term(), last_length: term(), last_length_ref: term(), last_name: term(), pending_length_first: term(), pending_length_second: term(), pos: non_neg_integer(), size: non_neg_integer() }
@type token() :: {:int, integer()} | {:real, float()} | {:name, binary()} | {:string, binary()} | {:hex_string, binary()} | {:stream_data, binary()} | {:op, binary()} | :null | true | false | :lbracket | :rbracket | :dict_start | :dict_end | :obj | :endobj | :stream | :endstream | :xref | :trailer | :startxref | :R | {:error, tokenizer_error()} | {:eof, nil}
@type tokenizer_error() :: {:unexpected_char, byte(), non_neg_integer()} | {:unexpected_gt, non_neg_integer()} | {:not_a_number, binary()} | {:unterminated_literal_string, non_neg_integer()} | {:unterminated_hex_string, non_neg_integer()}
Functions
Initialize the tokenizer from a binary.
Return the next token and updated tokenizer state.
@spec next_with_span(t()) :: {{token(), %{ from: non_neg_integer(), to: non_neg_integer(), stream_mode?: atom() | nil }}, t()}
Return the next token along with its byte span in the original binary.
The span is a map with :from and :to (exclusive end index). For {:stream_data, _}
tokens, from excludes the EOL after the stream keyword.
Look at the next token without advancing the tokenizer state.
@spec pending_stream_length(t()) :: {:direct, non_neg_integer()} | {:indirect, {integer(), integer()}} | :unknown
If called after a :stream token, returns a hint for the stream length:
{:direct, int}when/Lengthwas a direct integer{:indirect, {obj, gen}}when/Lengthwas an indirect reference:unknownwhen no hint is available
Tokenize the entire binary, returning a list of tokens.
@spec tokenize_all_with_spans(t()) :: [ {token(), %{from: non_neg_integer(), to: non_neg_integer(), stream_mode?: atom() | nil}} ]
Tokenize the entire binary, returning a list of {token, span}.