implement dd bracket

This commit is contained in:
ari committed 2020-10-16 16:55:54 -04:00
1 parent 370a1b40a1
commit 0eb5e4b8fc
4 files changed
+106 -9

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
from pdfmajor.tokenizer.token_parsers.util import PInput
from pdfmajor.safebufiterator import SafeBufferIt
from pdfmajor.lexer.exceptions import LexerEOF
from pdfmajor.lexer.regex import END_HEX_STRING, HEX_PAIR, SPC
from pdfmajor.lexer.token import TDictVaue, TokenDictionary, TokenHexString
from typing import Union
def parse_double_angled_bracket(
buffer: SafeBufferIt,
) -> Union[TokenHexString, TokenDictionary]:
"""Parses input stream into a hex string object or match a simple dictionary token
Args:
buffer (SafeBufferIt)
Returns:
Union[TokenHexString, TokenDictionary]
"""
initialpos = buffer.tell() - 1
step = next(buffer)
first_char = step.data[0:1]
if first_char == b"<":
buffer.seek(step.pos + 1)
return TokenDictionary(initialpos, 2, TDictVaue.OPEN)
buffer.seek(step.pos)
return parse_hexstring(buffer)
def parse_hexstring(buffer: SafeBufferIt) -> TokenHexString:
"""Parses the input string into hex string
Args:
buffer (SafeBufferIt)
Returns:
TokenHexString
"""
curtoken = b""
initialpos = buffer.tell() - 1
for pos, buf in buffer:
m = END_HEX_STRING.search(buf, 0)
if not m:
curtoken += buf
else:
j = m.start(0)
curtoken += buf[:j]
buffer.seek(pos + j + 1)
token = HEX_PAIR.sub(
lambda x: bytes([int(x.group(0), 16)]), SPC.sub(b"", curtoken)
)
return TokenHexString(initialpos, buffer.tell(), token)
raise LexerEOF
+3
View File
@@ -16,3 +16,6 @@ ESC_STRING = {
b")": 41,
b"\\": 92,
}
END_HEX_STRING = re.compile(br"[^\s0-9a-fA-F]")
HEX_PAIR = re.compile(br"[0-9a-fA-F]{2}|.")
SPC = re.compile(br"\s")
+5 -5
View File
@@ -61,17 +61,17 @@ def parse_string_escape(buffer: SafeBufferIt, state: StringParseState):
for pos, buf in buffer:
for ci in range(len(buf)):
c = buf[ci : ci + 1]
if len(state.escaped_value) < 3:
if len(state.escaped_value) < 3: # type: ignore
state.escaped_value += c
buffer.seek(pos + ci + 1)
if len(state.escaped_value) == 1:
if len(state.escaped_value) == 1: # type: ignore
if (ci < 1) and (c in ESC_STRING):
state.curtoken += bytes([ESC_STRING[c]])
state.escaped_value = None
return
elif OCT_STRING.match(state.escaped_value):
if len(state.escaped_value) == 3:
state.curtoken += bytes([int(state.escaped_value, 8)])
elif OCT_STRING.match(state.escaped_value): # type: ignore
if len(state.escaped_value) == 3: # type: ignore
state.curtoken += bytes([int(state.escaped_value, 8)]) # type: ignore
state.escaped_value = None
return
else: