Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 1 | package matcher |
| 2 | |
| 3 | import ( |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 4 | "fmt" |
| 5 | |
Akron | fa55bb2 | 2025-05-26 15:10:42 +0200 | [diff] [blame] | 6 | "github.com/KorAP/KoralPipe-TermMapper/ast" |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | // Matcher handles pattern matching and replacement in the AST |
| 10 | type Matcher struct { |
| 11 | pattern ast.Pattern |
| 12 | replacement ast.Replacement |
| 13 | } |
| 14 | |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 15 | // validateNode checks if a node is valid for pattern/replacement ASTs |
| 16 | func validateNode(node ast.Node) error { |
| 17 | if node == nil { |
| 18 | return fmt.Errorf("nil node") |
| 19 | } |
| 20 | |
| 21 | switch n := node.(type) { |
| 22 | case *ast.Token: |
| 23 | if n.Wrap != nil { |
| 24 | return validateNode(n.Wrap) |
| 25 | } |
| 26 | return nil |
| 27 | case *ast.Term: |
| 28 | return nil |
| 29 | case *ast.TermGroup: |
| 30 | if len(n.Operands) == 0 { |
| 31 | return fmt.Errorf("empty term group") |
| 32 | } |
| 33 | for _, op := range n.Operands { |
| 34 | if err := validateNode(op); err != nil { |
| 35 | return fmt.Errorf("invalid operand: %v", err) |
| 36 | } |
| 37 | } |
| 38 | return nil |
| 39 | case *ast.CatchallNode: |
| 40 | return fmt.Errorf("catchall nodes are not allowed in pattern/replacement ASTs") |
| 41 | default: |
| 42 | return fmt.Errorf("unknown node type: %T", node) |
| 43 | } |
| 44 | } |
| 45 | |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 46 | // NewMatcher creates a new Matcher with the given pattern and replacement |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 47 | func NewMatcher(pattern ast.Pattern, replacement ast.Replacement) (*Matcher, error) { |
| 48 | if err := validateNode(pattern.Root); err != nil { |
| 49 | return nil, fmt.Errorf("invalid pattern: %v", err) |
| 50 | } |
| 51 | if err := validateNode(replacement.Root); err != nil { |
| 52 | return nil, fmt.Errorf("invalid replacement: %v", err) |
| 53 | } |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 54 | return &Matcher{ |
| 55 | pattern: pattern, |
| 56 | replacement: replacement, |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 57 | }, nil |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Match checks if the given node matches the pattern |
| 61 | func (m *Matcher) Match(node ast.Node) bool { |
| 62 | return m.matchNode(node, m.pattern.Root) |
| 63 | } |
| 64 | |
| 65 | // Replace replaces all occurrences of the pattern in the given node with the replacement |
| 66 | func (m *Matcher) Replace(node ast.Node) ast.Node { |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 67 | // First step: Create complete structure with replacements |
| 68 | replaced := m.replaceNode(node) |
| 69 | // Second step: Simplify the structure |
| 70 | simplified := m.simplifyNode(replaced) |
| 71 | // If the input was a Token, ensure the output is also a Token |
| 72 | if _, isToken := node.(*ast.Token); isToken { |
| 73 | if _, isToken := simplified.(*ast.Token); !isToken { |
| 74 | return &ast.Token{Wrap: simplified} |
| 75 | } |
| 76 | } |
| 77 | return simplified |
| 78 | } |
| 79 | |
| 80 | // replaceNode creates a complete structure with replacements |
| 81 | func (m *Matcher) replaceNode(node ast.Node) ast.Node { |
| 82 | if node == nil { |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | // First handle Token nodes specially to preserve their structure |
| 87 | if token, ok := node.(*ast.Token); ok { |
| 88 | if token.Wrap == nil { |
| 89 | return token |
| 90 | } |
| 91 | // Process the wrapped node |
| 92 | wrap := m.replaceNode(token.Wrap) |
| 93 | return &ast.Token{Wrap: wrap} |
| 94 | } |
| 95 | |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 96 | // Handle TermGroup nodes |
| 97 | if tg, ok := node.(*ast.TermGroup); ok { |
| 98 | // Check if any operand matches the pattern |
| 99 | hasMatch := false |
| 100 | newOperands := make([]ast.Node, 0, len(tg.Operands)) |
| 101 | for _, op := range tg.Operands { |
| 102 | if !hasMatch && m.matchNode(op, m.pattern.Root) { |
| 103 | newOperands = append(newOperands, m.cloneNode(m.replacement.Root)) |
| 104 | hasMatch = true |
| 105 | } else { |
| 106 | newOperands = append(newOperands, m.replaceNode(op)) |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 107 | } |
| 108 | } |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 109 | // If we found a match, return the modified TermGroup |
| 110 | if hasMatch { |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 111 | return &ast.TermGroup{ |
| 112 | Operands: newOperands, |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 113 | Relation: tg.Relation, |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 114 | } |
| 115 | } |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 116 | // If this TermGroup matches the pattern exactly, replace it |
| 117 | if m.matchNode(node, m.pattern.Root) { |
| 118 | return m.cloneNode(m.replacement.Root) |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 119 | } |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 120 | // Otherwise, return the modified TermGroup |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 121 | return &ast.TermGroup{ |
| 122 | Operands: newOperands, |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 123 | Relation: tg.Relation, |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 124 | } |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 125 | } |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 126 | |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 127 | // Handle CatchallNode nodes |
| 128 | if c, ok := node.(*ast.CatchallNode); ok { |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 129 | newNode := &ast.CatchallNode{ |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 130 | NodeType: c.NodeType, |
| 131 | RawContent: c.RawContent, |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 132 | } |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 133 | if c.Wrap != nil { |
| 134 | newNode.Wrap = m.replaceNode(c.Wrap) |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 135 | } |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 136 | if len(c.Operands) > 0 { |
| 137 | newNode.Operands = make([]ast.Node, len(c.Operands)) |
| 138 | for i, op := range c.Operands { |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 139 | newNode.Operands[i] = m.replaceNode(op) |
| 140 | } |
| 141 | } |
| 142 | return newNode |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 143 | } |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 144 | |
| 145 | // If this node matches the pattern exactly, replace it |
| 146 | if m.matchNode(node, m.pattern.Root) { |
| 147 | return m.cloneNode(m.replacement.Root) |
| 148 | } |
| 149 | |
| 150 | return node |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // simplifyNode removes unnecessary wrappers and empty nodes |
| 154 | func (m *Matcher) simplifyNode(node ast.Node) ast.Node { |
| 155 | if node == nil { |
| 156 | return nil |
| 157 | } |
| 158 | |
| 159 | switch n := node.(type) { |
| 160 | case *ast.Token: |
| 161 | if n.Wrap == nil { |
| 162 | return nil |
| 163 | } |
| 164 | simplified := m.simplifyNode(n.Wrap) |
| 165 | if simplified == nil { |
| 166 | return nil |
| 167 | } |
| 168 | return &ast.Token{Wrap: simplified} |
| 169 | |
| 170 | case *ast.TermGroup: |
| 171 | // First simplify all operands |
| 172 | simplified := make([]ast.Node, 0, len(n.Operands)) |
| 173 | for _, op := range n.Operands { |
| 174 | if s := m.simplifyNode(op); s != nil { |
| 175 | simplified = append(simplified, s) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Handle special cases |
| 180 | if len(simplified) == 0 { |
| 181 | return nil |
| 182 | } |
| 183 | if len(simplified) == 1 { |
Akron | 21e4776 | 2025-07-03 14:11:11 +0200 | [diff] [blame^] | 184 | return simplified[0] |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return &ast.TermGroup{ |
| 188 | Operands: simplified, |
| 189 | Relation: n.Relation, |
| 190 | } |
| 191 | |
| 192 | case *ast.CatchallNode: |
| 193 | newNode := &ast.CatchallNode{ |
| 194 | NodeType: n.NodeType, |
| 195 | RawContent: n.RawContent, |
| 196 | } |
| 197 | if n.Wrap != nil { |
| 198 | newNode.Wrap = m.simplifyNode(n.Wrap) |
| 199 | } |
| 200 | if len(n.Operands) > 0 { |
| 201 | simplified := make([]ast.Node, 0, len(n.Operands)) |
| 202 | for _, op := range n.Operands { |
| 203 | if s := m.simplifyNode(op); s != nil { |
| 204 | simplified = append(simplified, s) |
| 205 | } |
| 206 | } |
| 207 | if len(simplified) > 0 { |
| 208 | newNode.Operands = simplified |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | return newNode |
| 212 | |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 213 | default: |
| 214 | return node |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // matchNode recursively checks if two nodes match |
| 219 | func (m *Matcher) matchNode(node, pattern ast.Node) bool { |
| 220 | if pattern == nil { |
| 221 | return true |
| 222 | } |
| 223 | if node == nil { |
| 224 | return false |
| 225 | } |
| 226 | |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 227 | // Handle wrapped nodes (Token and CatchallNode) |
| 228 | if m.tryMatchWrapped(node, pattern) { |
| 229 | return true |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 230 | } |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 231 | |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 232 | switch p := pattern.(type) { |
| 233 | case *ast.Token: |
| 234 | if n, ok := node.(*ast.Token); ok { |
| 235 | return m.matchNode(n.Wrap, p.Wrap) |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 236 | } |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 237 | |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 238 | case *ast.Term: |
| 239 | return m.matchTerm(node, p) |
| 240 | |
| 241 | case *ast.TermGroup: |
| 242 | if p.Relation == ast.OrRelation { |
| 243 | // For OR relations, check if any operand matches |
| 244 | for _, pOp := range p.Operands { |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 245 | if m.matchNode(node, pOp) { |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 246 | return true |
| 247 | } |
| 248 | } |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 249 | } else if tg, ok := node.(*ast.TermGroup); ok && tg.Relation == p.Relation { |
| 250 | // For AND relations, all pattern operands must match in any order |
| 251 | return m.matchAndTermGroup(tg, p) |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 252 | } |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | return false |
| 256 | } |
| 257 | |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 258 | // tryMatchWrapped attempts to match a node that might wrap other nodes |
| 259 | func (m *Matcher) tryMatchWrapped(node, pattern ast.Node) bool { |
| 260 | switch n := node.(type) { |
| 261 | case *ast.Token: |
Akron | 21e4776 | 2025-07-03 14:11:11 +0200 | [diff] [blame^] | 262 | return n.Wrap != nil && m.matchNode(n.Wrap, pattern) |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 263 | case *ast.CatchallNode: |
| 264 | if n.Wrap != nil && m.matchNode(n.Wrap, pattern) { |
| 265 | return true |
| 266 | } |
| 267 | for _, op := range n.Operands { |
| 268 | if m.matchNode(op, pattern) { |
| 269 | return true |
| 270 | } |
| 271 | } |
| 272 | case *ast.TermGroup: |
| 273 | for _, op := range n.Operands { |
| 274 | if m.matchNode(op, pattern) { |
| 275 | return true |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | return false |
| 280 | } |
| 281 | |
| 282 | // matchTerm checks if a node matches a term pattern |
| 283 | func (m *Matcher) matchTerm(node ast.Node, pattern *ast.Term) bool { |
| 284 | if t, ok := node.(*ast.Term); ok { |
| 285 | return t.Foundry == pattern.Foundry && |
| 286 | t.Key == pattern.Key && |
| 287 | t.Layer == pattern.Layer && |
| 288 | t.Match == pattern.Match && |
| 289 | (pattern.Value == "" || t.Value == pattern.Value) |
| 290 | } |
| 291 | return m.tryMatchWrapped(node, pattern) |
| 292 | } |
| 293 | |
| 294 | // matchAndTermGroup checks if a TermGroup matches an AND pattern |
| 295 | func (m *Matcher) matchAndTermGroup(node *ast.TermGroup, pattern *ast.TermGroup) bool { |
| 296 | if len(node.Operands) < len(pattern.Operands) { |
| 297 | return false |
| 298 | } |
| 299 | matched := make([]bool, len(node.Operands)) |
| 300 | for _, pOp := range pattern.Operands { |
| 301 | found := false |
| 302 | for j, tOp := range node.Operands { |
| 303 | if !matched[j] && m.matchNode(tOp, pOp) { |
| 304 | matched[j] = true |
| 305 | found = true |
| 306 | break |
| 307 | } |
| 308 | } |
| 309 | if !found { |
| 310 | return false |
| 311 | } |
| 312 | } |
| 313 | return true |
| 314 | } |
| 315 | |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 316 | // cloneNode creates a deep copy of a node |
| 317 | func (m *Matcher) cloneNode(node ast.Node) ast.Node { |
| 318 | if node == nil { |
| 319 | return nil |
| 320 | } |
| 321 | |
| 322 | switch n := node.(type) { |
| 323 | case *ast.Token: |
| 324 | return &ast.Token{ |
| 325 | Wrap: m.cloneNode(n.Wrap), |
| 326 | } |
| 327 | |
| 328 | case *ast.TermGroup: |
| 329 | operands := make([]ast.Node, len(n.Operands)) |
| 330 | for i, op := range n.Operands { |
| 331 | operands[i] = m.cloneNode(op) |
| 332 | } |
| 333 | return &ast.TermGroup{ |
| 334 | Operands: operands, |
| 335 | Relation: n.Relation, |
| 336 | } |
| 337 | |
| 338 | case *ast.Term: |
| 339 | return &ast.Term{ |
| 340 | Foundry: n.Foundry, |
| 341 | Key: n.Key, |
| 342 | Layer: n.Layer, |
| 343 | Match: n.Match, |
| 344 | Value: n.Value, |
| 345 | } |
| 346 | |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 347 | case *ast.CatchallNode: |
| 348 | newNode := &ast.CatchallNode{ |
| 349 | NodeType: n.NodeType, |
| 350 | RawContent: n.RawContent, |
| 351 | } |
| 352 | if n.Wrap != nil { |
| 353 | newNode.Wrap = m.cloneNode(n.Wrap) |
| 354 | } |
| 355 | if len(n.Operands) > 0 { |
| 356 | newNode.Operands = make([]ast.Node, len(n.Operands)) |
| 357 | for i, op := range n.Operands { |
| 358 | newNode.Operands[i] = m.cloneNode(op) |
| 359 | } |
| 360 | } |
| 361 | return newNode |
| 362 | |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 363 | default: |
| 364 | return nil |
| 365 | } |
| 366 | } |