Files
ladybird/Meta/Utils/CSSGrammar/Parser/grammar_node.py
Callum Law 09418e8c77 Meta: Implement CSS value type parsing code generation
To do this we parse the grammar supplied in `ValueTypes.json` and
generate the appropriate parsing function in `CSS::Parser::Parser`

Only a small subset of the CSS grammar (i.e. types `<foo>` and
alternatives `<foo> | <bar>`) is implemented so far, it will be
expanded in later commits.
2026-04-29 11:42:57 +01:00

40 lines
1.1 KiB
Python

from dataclasses import dataclass
from enum import Enum
from Utils.CSSGrammar.Parser.component_values import ComponentValue
class GrammarNode:
def dump(self, indent: int = 0) -> str:
raise NotImplementedError
# https://drafts.csswg.org/css-values-4/#component-types
@dataclass(frozen=True)
class ComponentValueGrammarNode(GrammarNode):
component_value: ComponentValue
def dump(self, indent: int = 0) -> str:
return f"{'': >{indent}}ComponentValue\n" + self.component_value.dump(indent + 2)
class CombinatorType(Enum):
# https://drafts.csswg.org/css-values-4/#comb-one
# A bar (|) separates two or more alternatives: exactly one of them must occur.
ALTERNATIVES = "Alternatives"
# https://drafts.csswg.org/css-values-4/#component-combinators
@dataclass(frozen=True)
class CombinatorGrammarNode(GrammarNode):
combinator_type: CombinatorType
children: list[GrammarNode]
def dump(self, indent: int = 0) -> str:
output = f"{'': >{indent}}Combinator({self.combinator_type.value}):\n"
for child in self.children:
output += child.dump(indent + 2)
return output