namespace EvilEngine.Tailwind; public abstract class AstNode : ITreeNode { public virtual List? Children => null; public abstract AstNode Clone(); } public abstract class ParentNode : AstNode { public List Nodes { get; set; } = []; public override List? Children => Nodes; } public abstract class Rule : ParentNode; public sealed class StyleRule : Rule { public string Selector { get; set; } = ""; public override AstNode Clone() { return new StyleRule { Selector = Selector, Nodes = Nodes.Select(n => n.Clone()).ToList() }; } public StyleRule With(List nodes) { return new StyleRule { Selector = Selector, Nodes = nodes }; } } public sealed class AtRule : Rule { public string Name { get; set; } = ""; public string Params { get; set; } = ""; public override AstNode Clone() { return new AtRule { Name = Name, Params = Params, Nodes = Nodes.Select(n => n.Clone()).ToList() }; } public AtRule With(List nodes) { return new AtRule { Name = Name, Params = Params, Nodes = nodes }; } } public sealed class Declaration : AstNode { public string Property { get; set; } = ""; public string? Value { get; set; } public bool Important { get; set; } public override AstNode Clone() { return new Declaration { Property = Property, Value = Value, Important = Important }; } } public sealed class Comment : AstNode { public string Value { get; set; } = ""; public override AstNode Clone() { return new Comment { Value = Value }; } } public sealed class Context : ParentNode { public Dictionary Values { get; set; } = []; public override AstNode Clone() { return new Context { Values = new Dictionary(Values), Nodes = Nodes.Select(n => n.Clone()).ToList() }; } } public sealed class AtRoot : ParentNode { public override AstNode Clone() { return new AtRoot { Nodes = Nodes.Select(n => n.Clone()).ToList() }; } } public static class Ast { public static StyleRule StyleRuleOf(string selector, List? nodes = null) { return new StyleRule { Selector = selector, Nodes = nodes ?? [] }; } public static AtRule AtRuleOf(string name, string @params = "", List? nodes = null) { return new AtRule { Name = name, Params = @params, Nodes = nodes ?? [] }; } public static Rule RuleOf(string selector, List? nodes = null) { return selector.Length > 0 && selector[0] == '@' ? CssParser.ParseAtRule(selector, nodes ?? []) : StyleRuleOf(selector, nodes); } public static Declaration Decl(string property, string? value, bool important = false) { return new Declaration { Property = property, Value = value, Important = important }; } public static Comment CommentOf(string value) { return new Comment { Value = value }; } public static Context ContextOf(Dictionary context, List nodes) { return new Context { Values = context, Nodes = nodes }; } public static AtRoot AtRootOf(List nodes) { return new AtRoot { Nodes = nodes }; } public static T CloneNode(T node) where T : AstNode { return (T)node.Clone(); } }