ensuring comments do not break parsing
This commit is contained in:
1 parent
cc0f802a10
commit
6e7c1f3888
4 files changed
+62
-12
No files matched your search
@@ -111,7 +111,7 @@ class PDFL1Parser:
|
||||
else:
|
||||
cur_ctx.pass_item(obj)
|
||||
elif isinstance(token, TokenComment):
|
||||
yield PDFComment(token)
|
||||
yield PDFComment(token.value, (token.start_loc, token.end_loc))
|
||||
elif isinstance(token, TokenKeyword):
|
||||
yield token
|
||||
elif isinstance(token, TokenNull):
|
||||
|
||||
@@ -2,6 +2,7 @@ import io
|
||||
from pdfmajor.parser_v2.l1 import PDFL1Parser
|
||||
from pdfmajor.parser_v2.exceptions import ParserError
|
||||
from pdfmajor.parser_v2.objects import (
|
||||
PDFComment,
|
||||
PDFContextualObject,
|
||||
PDFPrimitive,
|
||||
PDFObject,
|
||||
@@ -45,7 +46,7 @@ class PDFL2Parser:
|
||||
ctx_list: List[PDFContextualObject] = []
|
||||
for obj in self.l1.iter_objects():
|
||||
if isinstance(obj, TokenKeyword):
|
||||
if obj.get_value() == b"obj":
|
||||
if obj.value == b"obj":
|
||||
if len(object_list) < 2:
|
||||
raise ParserError("recieved 'obj' but missing leading tokens")
|
||||
ctx_list.append(
|
||||
@@ -53,14 +54,14 @@ class PDFL2Parser:
|
||||
*_get_indobj_values(object_list)
|
||||
)
|
||||
)
|
||||
elif obj.get_value() == b"endobj":
|
||||
elif obj.value == b"endobj":
|
||||
if len(ctx_list) > 0:
|
||||
last_ctx_obj = ctx_list.pop()
|
||||
if len(ctx_list) > 0:
|
||||
ctx_list[-1].pass_item(last_ctx_obj)
|
||||
else:
|
||||
yield last_ctx_obj.get_value()
|
||||
elif obj.get_value() == b"R":
|
||||
elif obj.value == b"R":
|
||||
cur_indobj = self.inobjects.get_indobject(
|
||||
*_get_indobj_values(object_list)
|
||||
)
|
||||
@@ -68,6 +69,8 @@ class PDFL2Parser:
|
||||
ctx_list[-1].pass_item(cur_indobj)
|
||||
else:
|
||||
yield cur_indobj.get_value()
|
||||
elif isinstance(obj, PDFComment):
|
||||
yield obj
|
||||
elif len(ctx_list) == 0:
|
||||
object_list.append(obj)
|
||||
elif len(ctx_list) > 0:
|
||||
|
||||
@@ -10,7 +10,7 @@ from pdfmajor.lexer.token import (
|
||||
TokenName,
|
||||
TokenPrimitive,
|
||||
)
|
||||
from typing import Any, Dict, Generic, List, Optional, TypeVar
|
||||
from typing import Any, Dict, Generic, List, Optional, Tuple, TypeVar
|
||||
|
||||
TToken = TypeVar("TToken", bound=TokenPrimitive)
|
||||
|
||||
@@ -33,6 +33,11 @@ class PDFObject(metaclass=ABCMeta):
|
||||
return False
|
||||
return o.to_python() == self.to_python()
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "{cls}({data})".format(
|
||||
cls=self.__class__.__name__, data=self.to_python()
|
||||
)
|
||||
|
||||
|
||||
class PDFContextualObject(PDFObject):
|
||||
"""An abstract class representing objects that require context for construction"""
|
||||
@@ -58,14 +63,20 @@ class PDFPrimitive(Generic[TToken], PDFObject):
|
||||
class PDFComment(PDFObject):
|
||||
"""A class representing a PDF comment"""
|
||||
|
||||
def __init__(self, token: TokenComment) -> None:
|
||||
self.token = token
|
||||
def __init__(self, comment: bytes, loc: Tuple[int, int] = (0, 0)) -> None:
|
||||
"""
|
||||
Args:
|
||||
comment (bytes): the comment data
|
||||
loc (Tuple[int, int], optional): location of the comment in the byte-stream. Defaults to (0, 0).
|
||||
"""
|
||||
self.comment = comment
|
||||
self.location = loc
|
||||
|
||||
def get_value(self):
|
||||
return self.token.value
|
||||
return self.comment
|
||||
|
||||
def to_python(self):
|
||||
return self.get_value()
|
||||
return (self.get_value(), self.location)
|
||||
|
||||
|
||||
class PDFNull(PDFObject):
|
||||
|
||||
Reference in new issue
Block a user