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 { |
| 184 | // If we have a single operand, return it directly |
| 185 | // But only if we're not inside a Token |
| 186 | if _, isToken := node.(*ast.Token); !isToken { |
| 187 | return simplified[0] |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return &ast.TermGroup{ |
| 192 | Operands: simplified, |
| 193 | Relation: n.Relation, |
| 194 | } |
| 195 | |
| 196 | case *ast.CatchallNode: |
| 197 | newNode := &ast.CatchallNode{ |
| 198 | NodeType: n.NodeType, |
| 199 | RawContent: n.RawContent, |
| 200 | } |
| 201 | if n.Wrap != nil { |
| 202 | newNode.Wrap = m.simplifyNode(n.Wrap) |
| 203 | } |
| 204 | if len(n.Operands) > 0 { |
| 205 | simplified := make([]ast.Node, 0, len(n.Operands)) |
| 206 | for _, op := range n.Operands { |
| 207 | if s := m.simplifyNode(op); s != nil { |
| 208 | simplified = append(simplified, s) |
| 209 | } |
| 210 | } |
| 211 | if len(simplified) > 0 { |
| 212 | newNode.Operands = simplified |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | return newNode |
| 216 | |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 217 | default: |
| 218 | return node |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // matchNode recursively checks if two nodes match |
| 223 | func (m *Matcher) matchNode(node, pattern ast.Node) bool { |
| 224 | if pattern == nil { |
| 225 | return true |
| 226 | } |
| 227 | if node == nil { |
| 228 | return false |
| 229 | } |
| 230 | |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 231 | // Handle wrapped nodes (Token and CatchallNode) |
| 232 | if m.tryMatchWrapped(node, pattern) { |
| 233 | return true |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 234 | } |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 235 | |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 236 | switch p := pattern.(type) { |
| 237 | case *ast.Token: |
| 238 | if n, ok := node.(*ast.Token); ok { |
| 239 | return m.matchNode(n.Wrap, p.Wrap) |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 240 | } |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 241 | |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 242 | case *ast.Term: |
| 243 | return m.matchTerm(node, p) |
| 244 | |
| 245 | case *ast.TermGroup: |
| 246 | if p.Relation == ast.OrRelation { |
| 247 | // For OR relations, check if any operand matches |
| 248 | for _, pOp := range p.Operands { |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 249 | if m.matchNode(node, pOp) { |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 250 | return true |
| 251 | } |
| 252 | } |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 253 | } else if tg, ok := node.(*ast.TermGroup); ok && tg.Relation == p.Relation { |
| 254 | // For AND relations, all pattern operands must match in any order |
| 255 | return m.matchAndTermGroup(tg, p) |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 256 | } |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | return false |
| 260 | } |
| 261 | |
Akron | 6f45515 | 2025-05-27 09:03:00 +0200 | [diff] [blame] | 262 | // tryMatchWrapped attempts to match a node that might wrap other nodes |
| 263 | func (m *Matcher) tryMatchWrapped(node, pattern ast.Node) bool { |
| 264 | switch n := node.(type) { |
| 265 | case *ast.Token: |
| 266 | if n.Wrap != nil { |
| 267 | return m.matchNode(n.Wrap, pattern) |
| 268 | } |
| 269 | case *ast.CatchallNode: |
| 270 | if n.Wrap != nil && m.matchNode(n.Wrap, pattern) { |
| 271 | return true |
| 272 | } |
| 273 | for _, op := range n.Operands { |
| 274 | if m.matchNode(op, pattern) { |
| 275 | return true |
| 276 | } |
| 277 | } |
| 278 | case *ast.TermGroup: |
| 279 | for _, op := range n.Operands { |
| 280 | if m.matchNode(op, pattern) { |
| 281 | return true |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | return false |
| 286 | } |
| 287 | |
| 288 | // matchTerm checks if a node matches a term pattern |
| 289 | func (m *Matcher) matchTerm(node ast.Node, pattern *ast.Term) bool { |
| 290 | if t, ok := node.(*ast.Term); ok { |
| 291 | return t.Foundry == pattern.Foundry && |
| 292 | t.Key == pattern.Key && |
| 293 | t.Layer == pattern.Layer && |
| 294 | t.Match == pattern.Match && |
| 295 | (pattern.Value == "" || t.Value == pattern.Value) |
| 296 | } |
| 297 | return m.tryMatchWrapped(node, pattern) |
| 298 | } |
| 299 | |
| 300 | // matchAndTermGroup checks if a TermGroup matches an AND pattern |
| 301 | func (m *Matcher) matchAndTermGroup(node *ast.TermGroup, pattern *ast.TermGroup) bool { |
| 302 | if len(node.Operands) < len(pattern.Operands) { |
| 303 | return false |
| 304 | } |
| 305 | matched := make([]bool, len(node.Operands)) |
| 306 | for _, pOp := range pattern.Operands { |
| 307 | found := false |
| 308 | for j, tOp := range node.Operands { |
| 309 | if !matched[j] && m.matchNode(tOp, pOp) { |
| 310 | matched[j] = true |
| 311 | found = true |
| 312 | break |
| 313 | } |
| 314 | } |
| 315 | if !found { |
| 316 | return false |
| 317 | } |
| 318 | } |
| 319 | return true |
| 320 | } |
| 321 | |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 322 | // cloneNode creates a deep copy of a node |
| 323 | func (m *Matcher) cloneNode(node ast.Node) ast.Node { |
| 324 | if node == nil { |
| 325 | return nil |
| 326 | } |
| 327 | |
| 328 | switch n := node.(type) { |
| 329 | case *ast.Token: |
| 330 | return &ast.Token{ |
| 331 | Wrap: m.cloneNode(n.Wrap), |
| 332 | } |
| 333 | |
| 334 | case *ast.TermGroup: |
| 335 | operands := make([]ast.Node, len(n.Operands)) |
| 336 | for i, op := range n.Operands { |
| 337 | operands[i] = m.cloneNode(op) |
| 338 | } |
| 339 | return &ast.TermGroup{ |
| 340 | Operands: operands, |
| 341 | Relation: n.Relation, |
| 342 | } |
| 343 | |
| 344 | case *ast.Term: |
| 345 | return &ast.Term{ |
| 346 | Foundry: n.Foundry, |
| 347 | Key: n.Key, |
| 348 | Layer: n.Layer, |
| 349 | Match: n.Match, |
| 350 | Value: n.Value, |
| 351 | } |
| 352 | |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 353 | case *ast.CatchallNode: |
| 354 | newNode := &ast.CatchallNode{ |
| 355 | NodeType: n.NodeType, |
| 356 | RawContent: n.RawContent, |
| 357 | } |
| 358 | if n.Wrap != nil { |
| 359 | newNode.Wrap = m.cloneNode(n.Wrap) |
| 360 | } |
| 361 | if len(n.Operands) > 0 { |
| 362 | newNode.Operands = make([]ast.Node, len(n.Operands)) |
| 363 | for i, op := range n.Operands { |
| 364 | newNode.Operands[i] = m.cloneNode(op) |
| 365 | } |
| 366 | } |
| 367 | return newNode |
| 368 | |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 369 | default: |
| 370 | return nil |
| 371 | } |
| 372 | } |