From 39b316de4052cc49de3efc655a8908c89e0e5785 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Tue, 16 Aug 2022 12:19:55 +0100 Subject: [PATCH 1/2] Improve error message for CSS syntax error --- sandbox/darren/just_a_box.css | 7 +++---- src/textual/css/tokenizer.py | 4 +++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/sandbox/darren/just_a_box.css b/sandbox/darren/just_a_box.css index 062dff0ec..760747506 100644 --- a/sandbox/darren/just_a_box.css +++ b/sandbox/darren/just_a_box.css @@ -1,6 +1,4 @@ Screen { - height: 100vh; - width: 100%; background: red; } @@ -10,12 +8,13 @@ Screen { .box { height: 5; - width: 5; + width: 5 margin: 1 10; } #left_pane { - width: 1fr; + width: 30; + height: 100; background: $background; } diff --git a/src/textual/css/tokenizer.py b/src/textual/css/tokenizer.py index d2e0f34cc..c675652e4 100644 --- a/src/textual/css/tokenizer.py +++ b/src/textual/css/tokenizer.py @@ -203,11 +203,13 @@ class Tokenizer: line = self.lines[line_no] match = expect.match(line, col_no) if match is None: + expected = ", ".join(" ".join(name.split("_")) for name in expect.names) + message = f"Expected one of {expected}.; Did you forget a semicolon at the end of a line?" raise TokenError( self.path, self.code, (line_no, col_no), - "expected " + ", ".join(name.upper() for name in expect.names), + message, ) iter_groups = iter(match.groups()) next(iter_groups) From 113ac87c9e49b74ca9a73aa5c82e0723bb0dd6ce Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 18 Aug 2022 10:28:55 +0100 Subject: [PATCH 2/2] Use friendly_list in syntax error message --- sandbox/darren/just_a_box.css | 2 +- src/textual/css/tokenizer.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/sandbox/darren/just_a_box.css b/sandbox/darren/just_a_box.css index 881e436bf..3a09b9c02 100644 --- a/sandbox/darren/just_a_box.css +++ b/sandbox/darren/just_a_box.css @@ -4,7 +4,7 @@ Screen { #left_pane { background: red; - width: 20; + width: 20 overflow: scroll scroll; } diff --git a/src/textual/css/tokenizer.py b/src/textual/css/tokenizer.py index c675652e4..cf090866c 100644 --- a/src/textual/css/tokenizer.py +++ b/src/textual/css/tokenizer.py @@ -12,6 +12,7 @@ import rich.repr from rich.syntax import Syntax from rich.text import Text +from ._error_tools import friendly_list from .._loop import loop_last @@ -203,7 +204,7 @@ class Tokenizer: line = self.lines[line_no] match = expect.match(line, col_no) if match is None: - expected = ", ".join(" ".join(name.split("_")) for name in expect.names) + expected = friendly_list(" ".join(name.split("_")) for name in expect.names) message = f"Expected one of {expected}.; Did you forget a semicolon at the end of a line?" raise TokenError( self.path,