Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python gram fixes #97

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions data/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,13 @@ class Parser(Parser):
node.ctx = context
return node

def ensure_real(self, number: ast.Constant) -> float:
def ensure_real(self, number: tokenize.TokenInfo) -> float:
value = ast.literal_eval(number.string)
if type(value) is complex:
self.raise_syntax_error_known_location("real number required in complex literal", number)
return value

def ensure_imaginary(self, number: ast.Constant) -> complex:
def ensure_imaginary(self, number: tokenize.TokenInfo) -> complex:
value = ast.literal_eval(number.string)
if type(value) is not complex:
self.raise_syntax_error_known_location("imaginary number required in complex literal", number)
Expand Down Expand Up @@ -340,7 +340,7 @@ class Parser(Parser):
args += (err.end_lineno + line_offset - 2, err.end_offset)
err_args = (err.msg, args)
# Ensure we do not keep the frame alive longer than necessary
# by explicitely deleting the error once we got what we needed out
# by explicitly deleting the error once we got what we needed out
# of it
del err

Expand Down Expand Up @@ -434,7 +434,7 @@ class Parser(Parser):
message: str,
start: Optional[Tuple[int, int]] = None,
end: Optional[Tuple[int, int]] = None
) -> None:
) -> SyntaxError:
line_from_token = start is None and end is None
if start is None or end is None:
tok = self._tokenizer.diagnose()
Expand Down Expand Up @@ -466,14 +466,20 @@ class Parser(Parser):
) -> NoReturn:
raise self._build_syntax_error(message, start, end)

def make_syntax_error(self, message: str) -> None:
def make_syntax_error(self, message: str) -> SyntaxError:
return self._build_syntax_error(message)

def expect_forced(self, res: Any, expectation: str) -> Optional[tokenize.TokenInfo]:
if res is None:
last_token = self._tokenizer.diagnose()
end = last_token.start
if sys.version_info >= (3, 12) or (sys.version_info >= (3, 11) and last_token.type != 4): # i.e. not a \n
if (
sys.version_info >= (3, 12)
or (
sys.version_info >= (3, 11)
and last_token.type != 4
)
): # i.e. not a \n
end = last_token.end
self.raise_raw_syntax_error(
f"expected {expectation}", last_token.start, end
Expand All @@ -483,7 +489,10 @@ class Parser(Parser):
def raise_syntax_error(self, message: str) -> NoReturn:
"""Raise a syntax error."""
tok = self._tokenizer.diagnose()
raise self._build_syntax_error(message, tok.start, tok.end if sys.version_info >= (3, 12) or tok.type != 4 else tok.start)
raise self._build_syntax_error(
message, tok.start,
tok.end if sys.version_info >= (3, 12) or tok.type != 4 else tok.start
)

def raise_syntax_error_known_location(
self, message: str, node: Union[ast.AST, tokenize.TokenInfo]
Expand Down Expand Up @@ -532,7 +541,7 @@ class Parser(Parser):

def raise_syntax_error_invalid_target(
self, target: Target, node: Optional[ast.AST]
) -> NoReturn:
) -> None:
invalid_target = self.get_invalid_target(target, node)

if invalid_target is None:
Expand Down
2 changes: 1 addition & 1 deletion tests/python_parser/data/fstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@


f'{expr:}'

f'{expr:d}'
foo = 3.14159
verbosePrint(f'Foo {foo:.3} bar.')