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" |
Akron | 1a5fccd | 2025-05-27 09:54:09 +0200 | [diff] [blame^] | 22 | RewriteNode NodeType = "rewrite" |
Akron | 753f49a | 2025-05-26 16:53:18 +0200 | [diff] [blame] | 23 | AndRelation RelationType = "and" |
| 24 | OrRelation RelationType = "or" |
| 25 | MatchEqual MatchType = "eq" |
| 26 | MatchNotEqual MatchType = "ne" |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | // Node represents a node in the AST |
| 30 | type Node interface { |
| 31 | Type() NodeType |
| 32 | } |
| 33 | |
Akron | 1a5fccd | 2025-05-27 09:54:09 +0200 | [diff] [blame^] | 34 | // Rewrite represents a koral:rewrite |
| 35 | type 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 | |
| 43 | func (r *Rewrite) Type() NodeType { |
| 44 | return RewriteNode |
| 45 | } |
| 46 | |
Akron | 753f49a | 2025-05-26 16:53:18 +0200 | [diff] [blame] | 47 | // Token represents a koral:token |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 48 | type Token struct { |
Akron | 1a5fccd | 2025-05-27 09:54:09 +0200 | [diff] [blame^] | 49 | Wrap Node `json:"wrap"` |
| 50 | Rewrites []Rewrite `json:"rewrites,omitempty"` |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | func (t *Token) Type() NodeType { |
| 54 | return TokenNode |
| 55 | } |
| 56 | |
Akron | 753f49a | 2025-05-26 16:53:18 +0200 | [diff] [blame] | 57 | // TermGroup represents a koral:termGroup |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 58 | type TermGroup struct { |
| 59 | Operands []Node `json:"operands"` |
| 60 | Relation RelationType `json:"relation"` |
Akron | 1a5fccd | 2025-05-27 09:54:09 +0200 | [diff] [blame^] | 61 | Rewrites []Rewrite `json:"rewrites,omitempty"` |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | func (tg *TermGroup) Type() NodeType { |
| 65 | return TermGroupNode |
| 66 | } |
| 67 | |
Akron | 753f49a | 2025-05-26 16:53:18 +0200 | [diff] [blame] | 68 | // Term represents a koral:term |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 69 | type Term struct { |
Akron | 1a5fccd | 2025-05-27 09:54:09 +0200 | [diff] [blame^] | 70 | 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"` |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | func (t *Term) Type() NodeType { |
| 79 | return TermNode |
| 80 | } |
| 81 | |
| 82 | // Pattern represents a pattern to match in the AST |
| 83 | type Pattern struct { |
| 84 | Root Node |
| 85 | } |
| 86 | |
| 87 | // Replacement represents a replacement pattern |
| 88 | type Replacement struct { |
| 89 | Root Node |
| 90 | } |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 91 | |
| 92 | // CatchallNode represents any node type not explicitly handled |
| 93 | type 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 | |
| 100 | func (c *CatchallNode) Type() NodeType { |
| 101 | return NodeType(c.NodeType) |
| 102 | } |