Skip to content

Commit

Permalink
Making parser more general
Browse files Browse the repository at this point in the history
so it can handle more hidden Tokenizer quirks automatically
* Removing newlines from end -> Those causes Paragraph class
to missbehave and enproduct had code line-number miss alignment
  • Loading branch information
Oliver Strait committed Aug 22, 2024
1 parent 6ed98f3 commit 34f8af4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
48 changes: 34 additions & 14 deletions manim/mobject/text/code_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ def style_token_support(token: _TokenType):
else:
return token

def parse_newlines_from_string(line: str):
if len(lastval) > 1:
parts = line.split("\n")

if parts[-1] == "":
parts.pop() # Otherwise there is one wrong newline
for literal in parts:
add_to_mapping(literal, lasttype)
self.mapping.append([])
else:
self.mapping.append([])

self.mapping: list[tuple[str, str]] = [[]]
self.tokens = self.lexer.get_tokens(self.code)
lasttype, lastval = next(self.tokens)
Expand All @@ -83,24 +95,33 @@ def style_token_support(token: _TokenType):
token_type = style_token_support(token_type)

if "\n" in lastval:
if len(lastval) > 1:
# There is cases in which Tokeniser returns values with newline attached into.
culled = lastval.removesuffix("\n")
add_to_mapping(culled, lasttype)

self.mapping.append([])
parse_newlines_from_string(lastval)
lastval = value
lasttype = token_type

elif value == " " or self.styles[token_type] == self.styles[lasttype]:
# NOTE This is hack for later efficiency, will broke token hierarchy if other style coding checks is added in future
# NOTE This conflates together whitespaces to other tokentype literals and same color string literals
# if other type of token styles than coloring is added, this needs to go.
lastval += value

else:
add_to_mapping(lastval, lasttype)
lastval = value
lasttype = token_type

if lastval:
if "\n" in lastval:
parse_newlines_from_string(lastval)
else:
add_to_mapping(lastval, lasttype)

# Removing empty lines from end
# It seems like Paragraph does not handle those well
# and causes line number missaligment
# In some situations Tokenazer throws newline to end that was not originally there
while self.mapping[-1] == []:
self.mapping.pop()

def get_mapping(self):
return self.mapping

Expand All @@ -110,6 +131,7 @@ def get_colors(self):
@staticmethod
def opposite_color(color: str) -> str:
"""Generate opposite color string"""
# TODO ManimColor may have some better methods to transform colors from string?
if color == "#000000":
return "#ffffff"
elif color == "#ffffff":
Expand Down Expand Up @@ -153,17 +175,15 @@ def get_style(cls, style: str | any) -> bool:
style = styles.get_style_by_name(style)
return style
else:
raise TypeError

logger.warning(
f'Style should be a string type. Used value {style} is type of {type(style)}. Using default type "{cls.DEFAULT_STYLE}" '
)
return styles.get_style_by_name(cls.DEFAULT_STYLE)
except ClassNotFound:
logger.warning(
f'{Code.__name__}: style "{style}" is not supported, using default style: "{cls.DEFAULT_STYLE}" '
)
return styles.get_style_by_name(cls.DEFAULT_STYLE)
except TypeError:
logger.warning(
f'Style should be a string type. Used value {style} is type of {type(style)}. Using default type "{cls.DEFAULT_STYLE}" '
)

return styles.get_style_by_name(cls.DEFAULT_STYLE)


Expand Down
8 changes: 6 additions & 2 deletions tests/utility_for_code_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""
Docstring
# This file is to test file functionalities of Code-Class
# test_code_mobject.py test will call this file
# test_code_mobject.py test is using this file
"""

import numpy as np

CODE_1 = """\
Expand Down Expand Up @@ -46,4 +50,4 @@ def fun_gus(
recovery = "Why not"

print("Resolution")
# Last bit
# lastbit

0 comments on commit 34f8af4

Please sign in to comment.