function docs

This commit is contained in:
Jesse Bannon 2023-11-18 22:35:31 -08:00
parent 31b6d00af9
commit 356168792c
2 changed files with 25 additions and 7 deletions

View file

@ -480,6 +480,9 @@ class _Parser:
def parse(text: str) -> SyntaxTree: def parse(text: str) -> SyntaxTree:
"""
Entrypoint for parsing ytdl-sub code into a Syntax Tree
"""
return _Parser(text).ast return _Parser(text).ast

View file

@ -23,7 +23,10 @@ class ParserExceptionFormatter:
self._end = end self._end = end
self._exception = exception self._exception = exception
def exception_text(self, border: int): def _exception_text(self, border: int):
"""
Format for single-line exceptions
"""
text_left = max(0, self._start - border) text_left = max(0, self._start - border)
text_right = min(len(self._text), self._start + border) text_right = min(len(self._text), self._start + border)
relative_start = self._start - text_left relative_start = self._start - text_left
@ -44,10 +47,13 @@ class ParserExceptionFormatter:
return "\n" + exception_text return "\n" + exception_text
@property @property
def is_multi_line(self) -> bool: def _is_multi_line(self) -> bool:
return "\n" in self._text return "\n" in self._text
def exception_text_lines(self, border_lines: int = 0) -> str: def _exception_text_lines(self, border_lines: int = 0) -> str:
"""
Format for multi-line exceptions
"""
split_text = self._text.split("\n") split_text = self._text.split("\n")
start_line: int = sys.maxsize start_line: int = sys.maxsize
@ -78,10 +84,15 @@ class ParserExceptionFormatter:
return "\n" + "\n".join(to_return) return "\n" + "\n".join(to_return)
def highlight(self) -> TUserException: def highlight(self) -> TUserException:
if self.is_multi_line: """
invalid_syntax = self.exception_text_lines(border_lines=3) Returns
-------
Exception with human-readable error highlighting for invalid syntax
"""
if self._is_multi_line:
invalid_syntax = self._exception_text_lines(border_lines=3)
else: else:
invalid_syntax = self.exception_text(border=20) invalid_syntax = self._exception_text(border=20)
return self._exception.__class__(f"{invalid_syntax}\n{str(self._exception)}") return self._exception.__class__(f"{invalid_syntax}\n{str(self._exception)}")
@ -113,11 +124,15 @@ class FunctionArgumentsExceptionFormatter:
return "()" return "()"
def highlight(self) -> IncompatibleFunctionArguments: def highlight(self) -> IncompatibleFunctionArguments:
"""
Returns
-------
Exception with human-readable error highlighting for incompatible function arguments
"""
received_type_names: List[str] = [] received_type_names: List[str] = []
for arg in self._input_args: for arg in self._input_args:
if isinstance(arg, TypeHintedFunctionType): if isinstance(arg, TypeHintedFunctionType):
if is_union(arg.output_type()): if is_union(arg.output_type()):
# TODO: Move naming to separate function, deal with Union input naming
received_type_names.append( received_type_names.append(
f"%{arg.name}(...)->Union[" f"%{arg.name}(...)->Union["
f"{', '.join(type_.type_name() for type_ in arg.output_type().__args__)}" f"{', '.join(type_.type_name() for type_ in arg.output_type().__args__)}"