mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
implement single line comments (#2248)
This commit is contained in:
@@ -2,7 +2,7 @@ Screen {
|
|||||||
overflow: auto;
|
overflow: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
#calculator {
|
#calculator {
|
||||||
layout: grid;
|
layout: grid;
|
||||||
grid-size: 4;
|
grid-size: 4;
|
||||||
grid-gutter: 1 2;
|
grid-gutter: 1 2;
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ HEX_COLOR = r"\#[0-9a-fA-F]{8}|\#[0-9a-fA-F]{6}|\#[0-9a-fA-F]{4}|\#[0-9a-fA-F]{3
|
|||||||
RGB_COLOR = rf"rgb{OPEN_BRACE}{DECIMAL}{COMMA}{DECIMAL}{COMMA}{DECIMAL}{CLOSE_BRACE}|rgba{OPEN_BRACE}{DECIMAL}{COMMA}{DECIMAL}{COMMA}{DECIMAL}{COMMA}{DECIMAL}{CLOSE_BRACE}"
|
RGB_COLOR = rf"rgb{OPEN_BRACE}{DECIMAL}{COMMA}{DECIMAL}{COMMA}{DECIMAL}{CLOSE_BRACE}|rgba{OPEN_BRACE}{DECIMAL}{COMMA}{DECIMAL}{COMMA}{DECIMAL}{COMMA}{DECIMAL}{CLOSE_BRACE}"
|
||||||
HSL_COLOR = rf"hsl{OPEN_BRACE}{DECIMAL}{COMMA}{PERCENT}{COMMA}{PERCENT}{CLOSE_BRACE}|hsla{OPEN_BRACE}{DECIMAL}{COMMA}{PERCENT}{COMMA}{PERCENT}{COMMA}{DECIMAL}{CLOSE_BRACE}"
|
HSL_COLOR = rf"hsl{OPEN_BRACE}{DECIMAL}{COMMA}{PERCENT}{COMMA}{PERCENT}{CLOSE_BRACE}|hsla{OPEN_BRACE}{DECIMAL}{COMMA}{PERCENT}{COMMA}{PERCENT}{COMMA}{DECIMAL}{CLOSE_BRACE}"
|
||||||
|
|
||||||
|
COMMENT_LINE = r"\# .*$"
|
||||||
COMMENT_START = r"\/\*"
|
COMMENT_START = r"\/\*"
|
||||||
SCALAR = rf"{DECIMAL}(?:fr|%|w|h|vw|vh)"
|
SCALAR = rf"{DECIMAL}(?:fr|%|w|h|vw|vh)"
|
||||||
DURATION = r"\d+\.?\d*(?:ms|s)"
|
DURATION = r"\d+\.?\d*(?:ms|s)"
|
||||||
@@ -46,6 +47,7 @@ DECLARATION_VALUES = {
|
|||||||
expect_root_scope = Expect(
|
expect_root_scope = Expect(
|
||||||
whitespace=r"\s+",
|
whitespace=r"\s+",
|
||||||
comment_start=COMMENT_START,
|
comment_start=COMMENT_START,
|
||||||
|
comment_line=COMMENT_LINE,
|
||||||
selector_start_id=r"\#" + IDENTIFIER,
|
selector_start_id=r"\#" + IDENTIFIER,
|
||||||
selector_start_class=r"\." + IDENTIFIER,
|
selector_start_class=r"\." + IDENTIFIER,
|
||||||
selector_start_universal=r"\*",
|
selector_start_universal=r"\*",
|
||||||
@@ -59,6 +61,7 @@ expect_variable_name_continue = Expect(
|
|||||||
variable_value_end=r"\n|;",
|
variable_value_end=r"\n|;",
|
||||||
whitespace=r"\s+",
|
whitespace=r"\s+",
|
||||||
comment_start=COMMENT_START,
|
comment_start=COMMENT_START,
|
||||||
|
comment_line=COMMENT_LINE,
|
||||||
**DECLARATION_VALUES,
|
**DECLARATION_VALUES,
|
||||||
).expect_eof(True)
|
).expect_eof(True)
|
||||||
|
|
||||||
@@ -71,6 +74,7 @@ expect_comment_end = Expect(
|
|||||||
expect_selector_continue = Expect(
|
expect_selector_continue = Expect(
|
||||||
whitespace=r"\s+",
|
whitespace=r"\s+",
|
||||||
comment_start=COMMENT_START,
|
comment_start=COMMENT_START,
|
||||||
|
comment_line=COMMENT_LINE,
|
||||||
pseudo_class=r"\:[a-zA-Z_-]+",
|
pseudo_class=r"\:[a-zA-Z_-]+",
|
||||||
selector_id=r"\#[a-zA-Z_\-][a-zA-Z0-9_\-]*",
|
selector_id=r"\#[a-zA-Z_\-][a-zA-Z0-9_\-]*",
|
||||||
selector_class=r"\.[a-zA-Z_\-][a-zA-Z0-9_\-]*",
|
selector_class=r"\.[a-zA-Z_\-][a-zA-Z0-9_\-]*",
|
||||||
@@ -86,6 +90,7 @@ expect_selector_continue = Expect(
|
|||||||
expect_declaration = Expect(
|
expect_declaration = Expect(
|
||||||
whitespace=r"\s+",
|
whitespace=r"\s+",
|
||||||
comment_start=COMMENT_START,
|
comment_start=COMMENT_START,
|
||||||
|
comment_line=COMMENT_LINE,
|
||||||
declaration_name=r"[a-zA-Z_\-]+\:",
|
declaration_name=r"[a-zA-Z_\-]+\:",
|
||||||
declaration_set_end=r"\}",
|
declaration_set_end=r"\}",
|
||||||
)
|
)
|
||||||
@@ -93,6 +98,7 @@ expect_declaration = Expect(
|
|||||||
expect_declaration_solo = Expect(
|
expect_declaration_solo = Expect(
|
||||||
whitespace=r"\s+",
|
whitespace=r"\s+",
|
||||||
comment_start=COMMENT_START,
|
comment_start=COMMENT_START,
|
||||||
|
comment_line=COMMENT_LINE,
|
||||||
declaration_name=r"[a-zA-Z_\-]+\:",
|
declaration_name=r"[a-zA-Z_\-]+\:",
|
||||||
declaration_set_end=r"\}",
|
declaration_set_end=r"\}",
|
||||||
).expect_eof(True)
|
).expect_eof(True)
|
||||||
@@ -103,6 +109,7 @@ expect_declaration_content = Expect(
|
|||||||
declaration_end=r";",
|
declaration_end=r";",
|
||||||
whitespace=r"\s+",
|
whitespace=r"\s+",
|
||||||
comment_start=COMMENT_START,
|
comment_start=COMMENT_START,
|
||||||
|
comment_line=COMMENT_LINE,
|
||||||
**DECLARATION_VALUES,
|
**DECLARATION_VALUES,
|
||||||
important=r"\!important",
|
important=r"\!important",
|
||||||
comma=",",
|
comma=",",
|
||||||
@@ -113,6 +120,7 @@ expect_declaration_content_solo = Expect(
|
|||||||
declaration_end=r";",
|
declaration_end=r";",
|
||||||
whitespace=r"\s+",
|
whitespace=r"\s+",
|
||||||
comment_start=COMMENT_START,
|
comment_start=COMMENT_START,
|
||||||
|
comment_line=COMMENT_LINE,
|
||||||
**DECLARATION_VALUES,
|
**DECLARATION_VALUES,
|
||||||
important=r"\!important",
|
important=r"\!important",
|
||||||
comma=",",
|
comma=",",
|
||||||
@@ -157,7 +165,9 @@ class TokenizerState:
|
|||||||
while True:
|
while True:
|
||||||
token = get_token(expect)
|
token = get_token(expect)
|
||||||
name = token.name
|
name = token.name
|
||||||
if name == "comment_start":
|
if name == "comment_line":
|
||||||
|
continue
|
||||||
|
elif name == "comment_start":
|
||||||
tokenizer.skip_to(expect_comment_end)
|
tokenizer.skip_to(expect_comment_end)
|
||||||
continue
|
continue
|
||||||
elif name == "eof":
|
elif name == "eof":
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from textual.css.parse import parse
|
||||||
from textual.css.tokenize import tokenize
|
from textual.css.tokenize import tokenize
|
||||||
from textual.css.tokenizer import Token, TokenError
|
from textual.css.tokenizer import Token, TokenError
|
||||||
|
|
||||||
@@ -175,6 +176,127 @@ def test_variable_declaration_multiple_values():
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_single_line_comment():
|
||||||
|
css = """\
|
||||||
|
# Ignored
|
||||||
|
#foo { # Ignored
|
||||||
|
color: red; # Also ignored
|
||||||
|
} # Nada"""
|
||||||
|
# Check the css parses
|
||||||
|
# list(parse(css, "<foo>"))
|
||||||
|
result = list(tokenize(css, ""))
|
||||||
|
|
||||||
|
print(result)
|
||||||
|
expected = [
|
||||||
|
Token(
|
||||||
|
name="whitespace",
|
||||||
|
value="\n",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(0, 9),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="selector_start_id",
|
||||||
|
value="#foo",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(1, 0),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="whitespace",
|
||||||
|
value=" ",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(1, 4),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="declaration_set_start",
|
||||||
|
value="{",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(1, 5),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="whitespace",
|
||||||
|
value=" ",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(1, 6),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="whitespace",
|
||||||
|
value="\n",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(1, 16),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="whitespace",
|
||||||
|
value=" ",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(2, 0),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="declaration_name",
|
||||||
|
value="color:",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(2, 4),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="whitespace",
|
||||||
|
value=" ",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(2, 10),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="token",
|
||||||
|
value="red",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(2, 11),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="declaration_end",
|
||||||
|
value=";",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(2, 14),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="whitespace",
|
||||||
|
value=" ",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(2, 15),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="whitespace",
|
||||||
|
value="\n",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(2, 30),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="declaration_set_end",
|
||||||
|
value="}",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(3, 0),
|
||||||
|
),
|
||||||
|
Token(
|
||||||
|
name="whitespace",
|
||||||
|
value=" ",
|
||||||
|
path="",
|
||||||
|
code=css,
|
||||||
|
location=(3, 1),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
assert result == expected
|
||||||
|
|
||||||
|
|
||||||
def test_variable_declaration_comment_ignored():
|
def test_variable_declaration_comment_ignored():
|
||||||
css = "$x: red; /* comment */"
|
css = "$x: red; /* comment */"
|
||||||
assert list(tokenize(css, "")) == [
|
assert list(tokenize(css, "")) == [
|
||||||
|
|||||||
Reference in New Issue
Block a user