cleaned up parser class
This commit is contained in:
1 parent
754e248aac
commit
cfc2dfca47
1 file changed
+33
-21
@@ -1,6 +1,6 @@
|
|||||||
import io
|
import io
|
||||||
from pdfmajor.parser.objects.stream import PDFStream
|
from pdfmajor.parser.objects.stream import PDFStream
|
||||||
from typing import Iterator, Optional, Union
|
from typing import Iterator, List, Optional, Union
|
||||||
|
|
||||||
from pdfmajor.lexer import PDFLexer
|
from pdfmajor.lexer import PDFLexer
|
||||||
from pdfmajor.lexer.token import (
|
from pdfmajor.lexer.token import (
|
||||||
@@ -79,12 +79,7 @@ class PDFParser:
|
|||||||
for token in self.lexer.iter_tokens():
|
for token in self.lexer.iter_tokens():
|
||||||
attempt_prim = attempt_parse_prim(token, self.state)
|
attempt_prim = attempt_parse_prim(token, self.state)
|
||||||
if attempt_prim.success:
|
if attempt_prim.success:
|
||||||
cur_ctx = self.state.current_context
|
yield from self._feed_or_yield_objs(attempt_prim.next_objs)
|
||||||
for obj in attempt_prim.next_objs:
|
|
||||||
if cur_ctx is not None:
|
|
||||||
cur_ctx.pass_item(obj)
|
|
||||||
else:
|
|
||||||
yield obj
|
|
||||||
elif isinstance(token, TokenComment):
|
elif isinstance(token, TokenComment):
|
||||||
yield self.state.set_last_obj(
|
yield self.state.set_last_obj(
|
||||||
PDFComment(
|
PDFComment(
|
||||||
@@ -94,26 +89,15 @@ class PDFParser:
|
|||||||
)
|
)
|
||||||
elif isinstance(token, TokenKeyword):
|
elif isinstance(token, TokenKeyword):
|
||||||
if token.value == b"R":
|
if token.value == b"R":
|
||||||
obj = self.state.get_indobjects()
|
yield from self._on_indirect_ref_close()
|
||||||
cur_ctx = self.state.current_context
|
|
||||||
if cur_ctx is not None:
|
|
||||||
cur_ctx.pass_item(obj)
|
|
||||||
else:
|
|
||||||
yield obj
|
|
||||||
elif token.value == b"obj":
|
elif token.value == b"obj":
|
||||||
self.state.initialize_indirect_obj()
|
self.state.initialize_indirect_obj()
|
||||||
elif token.value == b"endobj":
|
elif token.value == b"endobj":
|
||||||
last_ctx = self.state.context_stack.pop()
|
yield from self._on_endobj(token)
|
||||||
if isinstance(last_ctx, IndirectObject):
|
|
||||||
yield last_ctx.clone()
|
|
||||||
elif self.strict:
|
|
||||||
raise InvalidKeywordPos(token)
|
|
||||||
elif token.value == b"stream":
|
elif token.value == b"stream":
|
||||||
self._on_stream(token)
|
self._on_stream(token)
|
||||||
elif token.value == b"endstream":
|
elif token.value == b"endstream":
|
||||||
stream: PDFContextualObject = self.state.context_stack.pop()
|
self._on_endstream(token)
|
||||||
if not isinstance(stream, PDFStream):
|
|
||||||
raise InvalidKeywordPos(token)
|
|
||||||
else:
|
else:
|
||||||
raise InvalidKeywordPos(token)
|
raise InvalidKeywordPos(token)
|
||||||
elif isinstance(token, TokenArray):
|
elif isinstance(token, TokenArray):
|
||||||
@@ -131,6 +115,29 @@ class PDFParser:
|
|||||||
strict=self.strict,
|
strict=self.strict,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _feed_or_yield_objs(self, next_objs: List[PDFObject]):
|
||||||
|
cur_ctx = self.state.current_context
|
||||||
|
for obj in next_objs:
|
||||||
|
if cur_ctx is not None:
|
||||||
|
cur_ctx.pass_item(obj)
|
||||||
|
else:
|
||||||
|
yield obj
|
||||||
|
|
||||||
|
def _on_indirect_ref_close(self):
|
||||||
|
obj = self.state.get_indobjects()
|
||||||
|
cur_ctx = self.state.current_context
|
||||||
|
if cur_ctx is not None:
|
||||||
|
cur_ctx.pass_item(obj)
|
||||||
|
else:
|
||||||
|
yield obj
|
||||||
|
|
||||||
|
def _on_endobj(self, token: TokenKeyword):
|
||||||
|
last_ctx = self.state.context_stack.pop()
|
||||||
|
if isinstance(last_ctx, IndirectObject):
|
||||||
|
yield last_ctx.clone()
|
||||||
|
elif self.strict:
|
||||||
|
raise InvalidKeywordPos(token)
|
||||||
|
|
||||||
def _on_stream(self, token: TokenKeyword):
|
def _on_stream(self, token: TokenKeyword):
|
||||||
last_ctx = self.state.current_context
|
last_ctx = self.state.current_context
|
||||||
if last_ctx is not None and isinstance(last_ctx, IndirectObject):
|
if last_ctx is not None and isinstance(last_ctx, IndirectObject):
|
||||||
@@ -141,3 +148,8 @@ class PDFParser:
|
|||||||
self.lexer.seek(stream.offset + stream.length)
|
self.lexer.seek(stream.offset + stream.length)
|
||||||
else:
|
else:
|
||||||
raise ParserError(f"PDFStream is missing initilization dictionary")
|
raise ParserError(f"PDFStream is missing initilization dictionary")
|
||||||
|
|
||||||
|
def _on_endstream(self, token: TokenKeyword):
|
||||||
|
stream: PDFContextualObject = self.state.context_stack.pop()
|
||||||
|
if not isinstance(stream, PDFStream):
|
||||||
|
raise InvalidKeywordPos(token)
|
||||||
Reference in new issue
Block a user