Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 1 | package ast |
| 2 | |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame^] | 3 | import ( |
| 4 | "encoding/json" |
| 5 | ) |
| 6 | |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 7 | // NodeType represents the type of a node in the AST |
| 8 | type NodeType string |
| 9 | |
| 10 | const ( |
| 11 | TokenNode NodeType = "token" |
| 12 | TermGroupNode NodeType = "termGroup" |
| 13 | TermNode NodeType = "term" |
| 14 | ) |
| 15 | |
| 16 | // RelationType represents the type of relation between nodes |
| 17 | type RelationType string |
| 18 | |
| 19 | const ( |
| 20 | AndRelation RelationType = "and" |
| 21 | OrRelation RelationType = "or" |
| 22 | ) |
| 23 | |
| 24 | // MatchType represents the type of match operation |
| 25 | type MatchType string |
| 26 | |
| 27 | const ( |
| 28 | MatchEqual MatchType = "eq" |
| 29 | MatchNotEqual MatchType = "ne" |
| 30 | ) |
| 31 | |
| 32 | // Node represents a node in the AST |
| 33 | type Node interface { |
| 34 | Type() NodeType |
| 35 | } |
| 36 | |
| 37 | // Token represents a token node in the query |
| 38 | type Token struct { |
| 39 | Wrap Node `json:"wrap"` |
| 40 | } |
| 41 | |
| 42 | func (t *Token) Type() NodeType { |
| 43 | return TokenNode |
| 44 | } |
| 45 | |
| 46 | // TermGroup represents a group of terms with a relation |
| 47 | type TermGroup struct { |
| 48 | Operands []Node `json:"operands"` |
| 49 | Relation RelationType `json:"relation"` |
| 50 | } |
| 51 | |
| 52 | func (tg *TermGroup) Type() NodeType { |
| 53 | return TermGroupNode |
| 54 | } |
| 55 | |
| 56 | // Term represents a terminal node with matching criteria |
| 57 | type Term struct { |
| 58 | Foundry string `json:"foundry"` |
| 59 | Key string `json:"key"` |
| 60 | Layer string `json:"layer"` |
| 61 | Match MatchType `json:"match"` |
| 62 | Value string `json:"value,omitempty"` |
| 63 | } |
| 64 | |
| 65 | func (t *Term) Type() NodeType { |
| 66 | return TermNode |
| 67 | } |
| 68 | |
| 69 | // Pattern represents a pattern to match in the AST |
| 70 | type Pattern struct { |
| 71 | Root Node |
| 72 | } |
| 73 | |
| 74 | // Replacement represents a replacement pattern |
| 75 | type Replacement struct { |
| 76 | Root Node |
| 77 | } |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame^] | 78 | |
| 79 | // CatchallNode represents any node type not explicitly handled |
| 80 | type CatchallNode struct { |
| 81 | NodeType string // The original @type value |
| 82 | RawContent json.RawMessage // The original JSON content |
| 83 | Wrap Node // Optional wrapped node |
| 84 | Operands []Node // Optional operands |
| 85 | } |
| 86 | |
| 87 | func (c *CatchallNode) Type() NodeType { |
| 88 | return NodeType(c.NodeType) |
| 89 | } |