0.2.0-dev Download ZIP
134 lines ·3.4 KB Raw
| 1 | namespace EvilEngine.Tailwind; |
| 2 | |
| 3 | public abstract class AstNode : ITreeNode<AstNode> |
| 4 | { |
| 5 | public virtual List<AstNode>? Children => null; |
| 6 | |
| 7 | public abstract AstNode Clone(); |
| 8 | } |
| 9 | |
| 10 | public abstract class ParentNode : AstNode |
| 11 | { |
| 12 | public List<AstNode> Nodes { get; set; } = []; |
| 13 | |
| 14 | public override List<AstNode>? Children => Nodes; |
| 15 | } |
| 16 | |
| 17 | public abstract class Rule : ParentNode; |
| 18 | |
| 19 | public sealed class StyleRule : Rule |
| 20 | { |
| 21 | public string Selector { get; set; } = ""; |
| 22 | |
| 23 | public override AstNode Clone() |
| 24 | { |
| 25 | return new StyleRule { Selector = Selector, Nodes = Nodes.Select(n => n.Clone()).ToList() }; |
| 26 | } |
| 27 | |
| 28 | public StyleRule With(List<AstNode> nodes) |
| 29 | { |
| 30 | return new StyleRule { Selector = Selector, Nodes = nodes }; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public sealed class AtRule : Rule |
| 35 | { |
| 36 | public string Name { get; set; } = ""; |
| 37 | public string Params { get; set; } = ""; |
| 38 | |
| 39 | public override AstNode Clone() |
| 40 | { |
| 41 | return new AtRule { Name = Name, Params = Params, Nodes = Nodes.Select(n => n.Clone()).ToList() }; |
| 42 | } |
| 43 | |
| 44 | public AtRule With(List<AstNode> nodes) |
| 45 | { |
| 46 | return new AtRule { Name = Name, Params = Params, Nodes = nodes }; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public sealed class Declaration : AstNode |
| 51 | { |
| 52 | public string Property { get; set; } = ""; |
| 53 | public string? Value { get; set; } |
| 54 | public bool Important { get; set; } |
| 55 | |
| 56 | public override AstNode Clone() |
| 57 | { |
| 58 | return new Declaration { Property = Property, Value = Value, Important = Important }; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public sealed class Comment : AstNode |
| 63 | { |
| 64 | public string Value { get; set; } = ""; |
| 65 | |
| 66 | public override AstNode Clone() |
| 67 | { |
| 68 | return new Comment { Value = Value }; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public sealed class Context : ParentNode |
| 73 | { |
| 74 | public Dictionary<string, object> Values { get; set; } = []; |
| 75 | |
| 76 | public override AstNode Clone() |
| 77 | { |
| 78 | return new Context |
| 79 | { Values = new Dictionary<string, object>(Values), Nodes = Nodes.Select(n => n.Clone()).ToList() }; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | public sealed class AtRoot : ParentNode |
| 84 | { |
| 85 | public override AstNode Clone() |
| 86 | { |
| 87 | return new AtRoot { Nodes = Nodes.Select(n => n.Clone()).ToList() }; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public static class Ast |
| 92 | { |
| 93 | public static StyleRule StyleRuleOf(string selector, List<AstNode>? nodes = null) |
| 94 | { |
| 95 | return new StyleRule { Selector = selector, Nodes = nodes ?? [] }; |
| 96 | } |
| 97 | |
| 98 | public static AtRule AtRuleOf(string name, string @params = "", List<AstNode>? nodes = null) |
| 99 | { |
| 100 | return new AtRule { Name = name, Params = @params, Nodes = nodes ?? [] }; |
| 101 | } |
| 102 | |
| 103 | public static Rule RuleOf(string selector, List<AstNode>? nodes = null) |
| 104 | { |
| 105 | return selector.Length > 0 && selector[0] == '@' |
| 106 | ? CssParser.ParseAtRule(selector, nodes ?? []) |
| 107 | : StyleRuleOf(selector, nodes); |
| 108 | } |
| 109 | |
| 110 | public static Declaration Decl(string property, string? value, bool important = false) |
| 111 | { |
| 112 | return new Declaration { Property = property, Value = value, Important = important }; |
| 113 | } |
| 114 | |
| 115 | public static Comment CommentOf(string value) |
| 116 | { |
| 117 | return new Comment { Value = value }; |
| 118 | } |
| 119 | |
| 120 | public static Context ContextOf(Dictionary<string, object> context, List<AstNode> nodes) |
| 121 | { |
| 122 | return new Context { Values = context, Nodes = nodes }; |
| 123 | } |
| 124 | |
| 125 | public static AtRoot AtRootOf(List<AstNode> nodes) |
| 126 | { |
| 127 | return new AtRoot { Nodes = nodes }; |
| 128 | } |
| 129 | |
| 130 | public static T CloneNode<T>(T node) where T : AstNode |
| 131 | { |
| 132 | return (T)node.Clone(); |
| 133 | } |
| 134 | } |