blob: 07a6df1855edfa53ec141ad5a246e44533e0efb4 [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"
Akron1a5fccd2025-05-27 09:54:09 +020022 RewriteNode NodeType = "rewrite"
Akron753f49a2025-05-26 16:53:18 +020023 AndRelation RelationType = "and"
24 OrRelation RelationType = "or"
25 MatchEqual MatchType = "eq"
26 MatchNotEqual MatchType = "ne"
Akronb7e1f352025-05-16 15:45:23 +020027)
28
29// Node represents a node in the AST
30type Node interface {
31 Type() NodeType
32}
33
Akron1a5fccd2025-05-27 09:54:09 +020034// Rewrite represents a koral:rewrite
35type Rewrite struct {
36 Editor string `json:"editor,omitempty"`
37 Operation string `json:"operation,omitempty"`
38 Scope string `json:"scope,omitempty"`
39 Src string `json:"src,omitempty"`
40 Comment string `json:"_comment,omitempty"`
41}
42
43func (r *Rewrite) Type() NodeType {
44 return RewriteNode
45}
46
Akron753f49a2025-05-26 16:53:18 +020047// Token represents a koral:token
Akronb7e1f352025-05-16 15:45:23 +020048type Token struct {
Akron1a5fccd2025-05-27 09:54:09 +020049 Wrap Node `json:"wrap"`
50 Rewrites []Rewrite `json:"rewrites,omitempty"`
Akronb7e1f352025-05-16 15:45:23 +020051}
52
53func (t *Token) Type() NodeType {
54 return TokenNode
55}
56
Akron753f49a2025-05-26 16:53:18 +020057// TermGroup represents a koral:termGroup
Akronb7e1f352025-05-16 15:45:23 +020058type TermGroup struct {
59 Operands []Node `json:"operands"`
60 Relation RelationType `json:"relation"`
Akron1a5fccd2025-05-27 09:54:09 +020061 Rewrites []Rewrite `json:"rewrites,omitempty"`
Akronb7e1f352025-05-16 15:45:23 +020062}
63
64func (tg *TermGroup) Type() NodeType {
65 return TermGroupNode
66}
67
Akron753f49a2025-05-26 16:53:18 +020068// Term represents a koral:term
Akronb7e1f352025-05-16 15:45:23 +020069type Term struct {
Akron1a5fccd2025-05-27 09:54:09 +020070 Foundry string `json:"foundry"`
71 Key string `json:"key"`
72 Layer string `json:"layer"`
73 Match MatchType `json:"match"`
74 Value string `json:"value,omitempty"`
75 Rewrites []Rewrite `json:"rewrites,omitempty"`
Akronb7e1f352025-05-16 15:45:23 +020076}
77
78func (t *Term) Type() NodeType {
79 return TermNode
80}
81
82// Pattern represents a pattern to match in the AST
83type Pattern struct {
84 Root Node
85}
86
87// Replacement represents a replacement pattern
88type Replacement struct {
89 Root Node
90}
Akron32958422025-05-16 16:33:05 +020091
92// CatchallNode represents any node type not explicitly handled
93type CatchallNode struct {
94 NodeType string // The original @type value
95 RawContent json.RawMessage // The original JSON content
96 Wrap Node // Optional wrapped node
97 Operands []Node // Optional operands
98}
99
100func (c *CatchallNode) Type() NodeType {
101 return NodeType(c.NodeType)
102}