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