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