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