Class: Origami::Parser
- Inherits:
-
Object
- Object
- Origami::Parser
- Defined in:
- lib/origami/parser.rb
Overview
:nodoc:
Direct Known Subclasses
Defined Under Namespace
Classes: ParsingError
Constant Summary collapse
- VERBOSE_QUIET =
Do not output debug information.
0
- VERBOSE_INFO =
Output some useful information.
1
- VERBOSE_DEBUG =
Output debug information.
2
- VERBOSE_TRACE =
Output every objects read
3
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
-
#debug(msg = "") ⇒ Object
:nodoc:.
-
#defer_type_cast(reference, type) ⇒ Object
:nodoc:.
-
#error(msg = "") ⇒ Object
:nodoc:.
-
#info(msg = "") ⇒ Object
:nodoc:.
-
#initialize(options = {}) ⇒ Parser
constructor
:nodoc:.
- #parse(stream) ⇒ Object
-
#parse_object(pos = @data.pos) ⇒ Object
:nodoc:.
-
#parse_trailer(pos = @data.pos) ⇒ Object
:nodoc:.
-
#parse_xreftable(pos = @data.pos) ⇒ Object
:nodoc:.
- #pos ⇒ Object
- #pos=(offset) ⇒ Object
- #target_data ⇒ Object
- #target_filename ⇒ Object
- #target_filesize ⇒ Object
-
#trace(msg = "") ⇒ Object
:nodoc:.
-
#warn(msg = "") ⇒ Object
:nodoc:.
Constructor Details
#initialize(options = {}) ⇒ Parser
:nodoc:
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/origami/parser.rb', line 51 def initialize( = {}) # :nodoc: # Type information for indirect objects. @deferred_casts = {} # Default options values @options = { verbosity: VERBOSE_INFO, # Verbose level. ignore_errors: true, # Try to keep on parsing when errors occur. callback: proc {}, # Callback procedure whenever a structure is read. logger: $stderr, # Where to output parser messages. colorize_log: true # Colorize parser output? } @options.update() @logger = @options[:logger] @data = nil end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
49 50 51 |
# File 'lib/origami/parser.rb', line 49 def @options end |
Class Method Details
.init_scanner(stream) ⇒ Object
205 206 207 208 209 210 211 212 213 |
# File 'lib/origami/parser.rb', line 205 def self.init_scanner(stream) if stream.is_a?(StringScanner) stream elsif stream.respond_to?(:to_str) StringScanner.new(stream.to_str) else raise TypeError, "Cannot initialize scanner from #{stream.class}" end end |
Instance Method Details
#debug(msg = "") ⇒ Object
:nodoc:
197 198 199 |
# File 'lib/origami/parser.rb', line 197 def debug(msg = "") # :nodoc: log(VERBOSE_DEBUG, 'debug', :magenta, msg) end |
#defer_type_cast(reference, type) ⇒ Object
:nodoc:
169 170 171 |
# File 'lib/origami/parser.rb', line 169 def defer_type_cast(reference, type) # :nodoc: @deferred_casts[reference] = type end |
#error(msg = "") ⇒ Object
:nodoc:
185 186 187 |
# File 'lib/origami/parser.rb', line 185 def error(msg = "") # :nodoc: log(VERBOSE_QUIET, 'error', :red, msg) end |
#info(msg = "") ⇒ Object
:nodoc:
193 194 195 |
# File 'lib/origami/parser.rb', line 193 def info(msg = "") # :nodoc: log(VERBOSE_INFO, 'info ', :green, msg) end |
#parse(stream) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/origami/parser.rb', line 82 def parse(stream) data = if stream.respond_to? :read StringScanner.new(stream.read.force_encoding('binary')) elsif stream.is_a? ::String @filename = stream StringScanner.new(File.binread(@filename)) elsif stream.is_a? StringScanner stream else raise TypeError end @data = data @data.pos = 0 end |
#parse_object(pos = @data.pos) ⇒ Object
:nodoc:
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/origami/parser.rb', line 99 def parse_object(pos = @data.pos) # :nodoc: @data.pos = pos begin obj = Object.parse(@data, self) return if obj.nil? obj = try_object_promotion(obj) trace "Read #{obj.type} object, #{obj.reference}" @options[:callback].call(obj) obj rescue UnterminatedObjectError error $!. obj = $!.obj Object.skip_until_next_obj(@data) @options[:callback].call(obj) obj rescue error "Breaking on: #{(@data.peek(10) + "...").inspect} at offset 0x#{@data.pos.to_s(16)}" error "Last exception: [#{$!.class}] #{$!.}" if !@options[:ignore_errors] error "Manually fix the file or set :ignore_errors parameter." raise end debug 'Skipping this indirect object.' raise if !Object.skip_until_next_obj(@data) retry end end |
#parse_trailer(pos = @data.pos) ⇒ Object
:nodoc:
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/origami/parser.rb', line 152 def parse_trailer(pos = @data.pos) # :nodoc: @data.pos = pos begin info "...Parsing trailer..." trailer = Trailer.parse(@data, self) @options[:callback].call(trailer) trailer rescue debug "Exception caught while parsing trailer : " + $!. warn "Unable to parse trailer!" raise end end |
#parse_xreftable(pos = @data.pos) ⇒ Object
:nodoc:
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/origami/parser.rb', line 133 def parse_xreftable(pos = @data.pos) # :nodoc: @data.pos = pos begin info "...Parsing xref table..." xreftable = XRef::Section.parse(@data) @options[:callback].call(xreftable) xreftable rescue debug "Exception caught while parsing xref table : " + $!. warn "Unable to parse xref table! Xrefs might be stored into an XRef stream." @data.pos -= 'trailer'.length unless @data.skip_until(/trailer/).nil? nil end end |
#pos ⇒ Object
70 71 72 73 74 |
# File 'lib/origami/parser.rb', line 70 def pos raise "Cannot get position, parser has no loaded data." if @data.nil? @data.pos end |
#pos=(offset) ⇒ Object
76 77 78 79 80 |
# File 'lib/origami/parser.rb', line 76 def pos=(offset) raise "Cannot set position, parser has no loaded data." if @data.nil? @data.pos = offset end |
#target_data ⇒ Object
181 182 183 |
# File 'lib/origami/parser.rb', line 181 def target_data @data.string.dup if @data end |
#target_filename ⇒ Object
173 174 175 |
# File 'lib/origami/parser.rb', line 173 def target_filename @filename end |
#target_filesize ⇒ Object
177 178 179 |
# File 'lib/origami/parser.rb', line 177 def target_filesize @data&.string&.size end |
#trace(msg = "") ⇒ Object
:nodoc:
201 202 203 |
# File 'lib/origami/parser.rb', line 201 def trace(msg = "") # :nodoc: log(VERBOSE_TRACE, 'trace', :cyan, msg) end |
#warn(msg = "") ⇒ Object
:nodoc:
189 190 191 |
# File 'lib/origami/parser.rb', line 189 def warn(msg = "") # :nodoc: log(VERBOSE_INFO, 'warn ', :yellow, msg) end |