mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-05-11 01:22:43 +02:00
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.
40 lines
1.1 KiB
Python
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
|