blob: 685483ccdb1992183c1f4dfecf07f11135fefb90 [file] [log] [blame]
Akronb7e1f352025-05-16 15:45:23 +02001package ast
2
Akron753f49a2025-05-26 16:53:18 +02003// ast is the abstract syntax tree for the query term mapper.
Akronbf5149c2025-05-20 15:53:41 +02004
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
Akronb7e1f352025-05-16 15:45:23 +020012// RelationType represents the type of relation between nodes
13type RelationType string
14
Akronb7e1f352025-05-16 15:45:23 +020015// MatchType represents the type of match operation
16type MatchType string
17
18const (
Akron753f49a2025-05-26 16:53:18 +020019 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"
Akronb7e1f352025-05-16 15:45:23 +020026)
27
28// Node represents a node in the AST
29type Node interface {
30 Type() NodeType
31}
32
Akron753f49a2025-05-26 16:53:18 +020033// Token represents a koral:token
Akronb7e1f352025-05-16 15:45:23 +020034type Token struct {
35 Wrap Node `json:"wrap"`
36}
37
38func (t *Token) Type() NodeType {
39 return TokenNode
40}
41
Akron753f49a2025-05-26 16:53:18 +020042// TermGroup represents a koral:termGroup
Akronb7e1f352025-05-16 15:45:23 +020043type TermGroup struct {
44 Operands []Node `json:"operands"`
45 Relation RelationType `json:"relation"`
46}
47
48func (tg *TermGroup) Type() NodeType {
49 return TermGroupNode
50}
51
Akron753f49a2025-05-26 16:53:18 +020052// Term represents a koral:term
Akronb7e1f352025-05-16 15:45:23 +020053type 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
61func (t *Term) Type() NodeType {
62 return TermNode
63}
64
65// Pattern represents a pattern to match in the AST
66type Pattern struct {
67 Root Node
68}
69
70// Replacement represents a replacement pattern
71type Replacement struct {
72 Root Node
73}
Akron32958422025-05-16 16:33:05 +020074
75// CatchallNode represents any node type not explicitly handled
76type 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
83func (c *CatchallNode) Type() NodeType {
84 return NodeType(c.NodeType)
85}