blob: 1bc8bb4ebe9a76ded09ee3b533ced43e49ab2356 [file] [log] [blame]
Akronb7e1f352025-05-16 15:45:23 +02001package ast
2
Akron32958422025-05-16 16:33:05 +02003import (
4 "encoding/json"
5)
6
Akronb7e1f352025-05-16 15:45:23 +02007// NodeType represents the type of a node in the AST
8type NodeType string
9
10const (
11 TokenNode NodeType = "token"
12 TermGroupNode NodeType = "termGroup"
13 TermNode NodeType = "term"
14)
15
16// RelationType represents the type of relation between nodes
17type RelationType string
18
19const (
20 AndRelation RelationType = "and"
21 OrRelation RelationType = "or"
22)
23
24// MatchType represents the type of match operation
25type MatchType string
26
27const (
28 MatchEqual MatchType = "eq"
29 MatchNotEqual MatchType = "ne"
30)
31
32// Node represents a node in the AST
33type Node interface {
34 Type() NodeType
35}
36
37// Token represents a token node in the query
38type Token struct {
39 Wrap Node `json:"wrap"`
40}
41
42func (t *Token) Type() NodeType {
43 return TokenNode
44}
45
46// TermGroup represents a group of terms with a relation
47type TermGroup struct {
48 Operands []Node `json:"operands"`
49 Relation RelationType `json:"relation"`
50}
51
52func (tg *TermGroup) Type() NodeType {
53 return TermGroupNode
54}
55
56// Term represents a terminal node with matching criteria
57type 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
65func (t *Term) Type() NodeType {
66 return TermNode
67}
68
69// Pattern represents a pattern to match in the AST
70type Pattern struct {
71 Root Node
72}
73
74// Replacement represents a replacement pattern
75type Replacement struct {
76 Root Node
77}
Akron32958422025-05-16 16:33:05 +020078
79// CatchallNode represents any node type not explicitly handled
80type 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
87func (c *CatchallNode) Type() NodeType {
88 return NodeType(c.NodeType)
89}