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 | |
| 96 | // If this node matches the pattern |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 97 | if m.Match(node) { |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 98 | // For TermGroups that contain a matching Term, preserve unmatched operands |
| 99 | if tg, ok := node.(*ast.TermGroup); ok { |
| 100 | // Check if any operand matches the pattern exactly |
| 101 | hasExactMatch := false |
| 102 | for _, op := range tg.Operands { |
| 103 | if m.matchNode(op, m.pattern.Root) { |
| 104 | hasExactMatch = true |
| 105 | break |
| 106 | } |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 107 | } |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 108 | |
| 109 | // If we have an exact match, replace matching operands |
| 110 | if hasExactMatch { |
| 111 | hasMatch := false |
| 112 | newOperands := make([]ast.Node, 0, len(tg.Operands)) |
| 113 | for _, op := range tg.Operands { |
| 114 | if m.matchNode(op, m.pattern.Root) { |
| 115 | if !hasMatch { |
| 116 | newOperands = append(newOperands, m.cloneNode(m.replacement.Root)) |
| 117 | hasMatch = true |
| 118 | } else { |
| 119 | newOperands = append(newOperands, m.replaceNode(op)) |
| 120 | } |
| 121 | } else { |
| 122 | newOperands = append(newOperands, m.replaceNode(op)) |
| 123 | } |
| 124 | } |
| 125 | return &ast.TermGroup{ |
| 126 | Operands: newOperands, |
| 127 | Relation: tg.Relation, |
| 128 | } |
| 129 | } |
| 130 | // Otherwise, replace the entire TermGroup |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 131 | return m.cloneNode(m.replacement.Root) |
| 132 | } |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 133 | // For other nodes, return the replacement |
| 134 | return m.cloneNode(m.replacement.Root) |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 137 | // Otherwise recursively process children |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 138 | switch n := node.(type) { |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 139 | case *ast.TermGroup: |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 140 | // Check if any operand matches the pattern exactly |
| 141 | hasExactMatch := false |
| 142 | for _, op := range n.Operands { |
| 143 | if m.matchNode(op, m.pattern.Root) { |
| 144 | hasExactMatch = true |
| 145 | break |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // If we have an exact match, replace matching operands |
| 150 | if hasExactMatch { |
| 151 | hasMatch := false |
| 152 | newOperands := make([]ast.Node, 0, len(n.Operands)) |
| 153 | for _, op := range n.Operands { |
| 154 | if m.matchNode(op, m.pattern.Root) { |
| 155 | if !hasMatch { |
| 156 | newOperands = append(newOperands, m.cloneNode(m.replacement.Root)) |
| 157 | hasMatch = true |
| 158 | } else { |
| 159 | newOperands = append(newOperands, m.replaceNode(op)) |
| 160 | } |
| 161 | } else { |
| 162 | newOperands = append(newOperands, m.replaceNode(op)) |
| 163 | } |
| 164 | } |
| 165 | return &ast.TermGroup{ |
| 166 | Operands: newOperands, |
| 167 | Relation: n.Relation, |
| 168 | } |
| 169 | } |
| 170 | // Otherwise, recursively process operands |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 171 | newOperands := make([]ast.Node, len(n.Operands)) |
| 172 | for i, op := range n.Operands { |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 173 | newOperands[i] = m.replaceNode(op) |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 174 | } |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 175 | return &ast.TermGroup{ |
| 176 | Operands: newOperands, |
| 177 | Relation: n.Relation, |
| 178 | } |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 179 | |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 180 | case *ast.CatchallNode: |
| 181 | newNode := &ast.CatchallNode{ |
| 182 | NodeType: n.NodeType, |
| 183 | RawContent: n.RawContent, |
| 184 | } |
| 185 | if n.Wrap != nil { |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 186 | newNode.Wrap = m.replaceNode(n.Wrap) |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 187 | } |
| 188 | if len(n.Operands) > 0 { |
| 189 | newNode.Operands = make([]ast.Node, len(n.Operands)) |
| 190 | for i, op := range n.Operands { |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 191 | newNode.Operands[i] = m.replaceNode(op) |
| 192 | } |
| 193 | } |
| 194 | return newNode |
| 195 | |
| 196 | default: |
| 197 | return node |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // simplifyNode removes unnecessary wrappers and empty nodes |
| 202 | func (m *Matcher) simplifyNode(node ast.Node) ast.Node { |
| 203 | if node == nil { |
| 204 | return nil |
| 205 | } |
| 206 | |
| 207 | switch n := node.(type) { |
| 208 | case *ast.Token: |
| 209 | if n.Wrap == nil { |
| 210 | return nil |
| 211 | } |
| 212 | simplified := m.simplifyNode(n.Wrap) |
| 213 | if simplified == nil { |
| 214 | return nil |
| 215 | } |
| 216 | return &ast.Token{Wrap: simplified} |
| 217 | |
| 218 | case *ast.TermGroup: |
| 219 | // First simplify all operands |
| 220 | simplified := make([]ast.Node, 0, len(n.Operands)) |
| 221 | for _, op := range n.Operands { |
| 222 | if s := m.simplifyNode(op); s != nil { |
| 223 | simplified = append(simplified, s) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Handle special cases |
| 228 | if len(simplified) == 0 { |
| 229 | return nil |
| 230 | } |
| 231 | if len(simplified) == 1 { |
| 232 | // If we have a single operand, return it directly |
| 233 | // But only if we're not inside a Token |
| 234 | if _, isToken := node.(*ast.Token); !isToken { |
| 235 | return simplified[0] |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return &ast.TermGroup{ |
| 240 | Operands: simplified, |
| 241 | Relation: n.Relation, |
| 242 | } |
| 243 | |
| 244 | case *ast.CatchallNode: |
| 245 | newNode := &ast.CatchallNode{ |
| 246 | NodeType: n.NodeType, |
| 247 | RawContent: n.RawContent, |
| 248 | } |
| 249 | if n.Wrap != nil { |
| 250 | newNode.Wrap = m.simplifyNode(n.Wrap) |
| 251 | } |
| 252 | if len(n.Operands) > 0 { |
| 253 | simplified := make([]ast.Node, 0, len(n.Operands)) |
| 254 | for _, op := range n.Operands { |
| 255 | if s := m.simplifyNode(op); s != nil { |
| 256 | simplified = append(simplified, s) |
| 257 | } |
| 258 | } |
| 259 | if len(simplified) > 0 { |
| 260 | newNode.Operands = simplified |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | return newNode |
| 264 | |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 265 | default: |
| 266 | return node |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // matchNode recursively checks if two nodes match |
| 271 | func (m *Matcher) matchNode(node, pattern ast.Node) bool { |
| 272 | if pattern == nil { |
| 273 | return true |
| 274 | } |
| 275 | if node == nil { |
| 276 | return false |
| 277 | } |
| 278 | |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 279 | // Handle pattern being a Token |
| 280 | if pToken, ok := pattern.(*ast.Token); ok { |
| 281 | if nToken, ok := node.(*ast.Token); ok { |
| 282 | return m.matchNode(nToken.Wrap, pToken.Wrap) |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 283 | } |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 284 | return false |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 285 | } |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 286 | |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 287 | // Handle pattern being a Term |
| 288 | if pTerm, ok := pattern.(*ast.Term); ok { |
| 289 | // Direct term to term matching |
| 290 | if t, ok := node.(*ast.Term); ok { |
| 291 | return t.Foundry == pTerm.Foundry && |
| 292 | t.Key == pTerm.Key && |
| 293 | t.Layer == pTerm.Layer && |
| 294 | t.Match == pTerm.Match && |
| 295 | (pTerm.Value == "" || t.Value == pTerm.Value) |
| 296 | } |
| 297 | // If node is a Token, check its wrap |
| 298 | if tkn, ok := node.(*ast.Token); ok { |
| 299 | if tkn.Wrap == nil { |
| 300 | return false |
| 301 | } |
| 302 | return m.matchNode(tkn.Wrap, pattern) |
| 303 | } |
| 304 | // If node is a TermGroup, check its operands |
| 305 | if tg, ok := node.(*ast.TermGroup); ok { |
| 306 | for _, op := range tg.Operands { |
| 307 | if m.matchNode(op, pattern) { |
| 308 | return true |
| 309 | } |
| 310 | } |
| 311 | return false |
| 312 | } |
| 313 | // If node is a CatchallNode, check its wrap and operands |
| 314 | if c, ok := node.(*ast.CatchallNode); ok { |
| 315 | if c.Wrap != nil && m.matchNode(c.Wrap, pattern) { |
| 316 | return true |
| 317 | } |
| 318 | for _, op := range c.Operands { |
| 319 | if m.matchNode(op, pattern) { |
| 320 | return true |
| 321 | } |
| 322 | } |
| 323 | return false |
| 324 | } |
| 325 | return false |
| 326 | } |
| 327 | |
| 328 | // Handle pattern being a TermGroup |
| 329 | if pGroup, ok := pattern.(*ast.TermGroup); ok { |
| 330 | // For OR relations, check if any operand matches the node |
| 331 | if pGroup.Relation == ast.OrRelation { |
| 332 | for _, pOp := range pGroup.Operands { |
| 333 | if m.matchNode(node, pOp) { |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 334 | return true |
| 335 | } |
| 336 | } |
| 337 | return false |
| 338 | } |
| 339 | |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 340 | // For AND relations, node must be a TermGroup with matching relation |
| 341 | if tg, ok := node.(*ast.TermGroup); ok { |
| 342 | if tg.Relation != pGroup.Relation { |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 343 | return false |
| 344 | } |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 345 | // Check that all pattern operands match in any order |
| 346 | if len(tg.Operands) < len(pGroup.Operands) { |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 347 | return false |
| 348 | } |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 349 | matched := make([]bool, len(tg.Operands)) |
| 350 | for _, pOp := range pGroup.Operands { |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 351 | found := false |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 352 | for j, tOp := range tg.Operands { |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 353 | if !matched[j] && m.matchNode(tOp, pOp) { |
| 354 | matched[j] = true |
| 355 | found = true |
| 356 | break |
| 357 | } |
| 358 | } |
| 359 | if !found { |
| 360 | return false |
| 361 | } |
| 362 | } |
| 363 | return true |
| 364 | } |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 365 | |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 366 | // If node is a Token, check its wrap |
| 367 | if tkn, ok := node.(*ast.Token); ok { |
| 368 | if tkn.Wrap == nil { |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 369 | return false |
| 370 | } |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 371 | return m.matchNode(tkn.Wrap, pattern) |
| 372 | } |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 373 | |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 374 | // If node is a CatchallNode, check its wrap and operands |
| 375 | if c, ok := node.(*ast.CatchallNode); ok { |
| 376 | if c.Wrap != nil && m.matchNode(c.Wrap, pattern) { |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 377 | return true |
| 378 | } |
Akron | bf5149c | 2025-05-20 15:53:41 +0200 | [diff] [blame] | 379 | for _, op := range c.Operands { |
| 380 | if m.matchNode(op, pattern) { |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 381 | return true |
| 382 | } |
| 383 | } |
| 384 | return false |
| 385 | } |
| 386 | |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 387 | return false |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | return false |
| 391 | } |
| 392 | |
| 393 | // cloneNode creates a deep copy of a node |
| 394 | func (m *Matcher) cloneNode(node ast.Node) ast.Node { |
| 395 | if node == nil { |
| 396 | return nil |
| 397 | } |
| 398 | |
| 399 | switch n := node.(type) { |
| 400 | case *ast.Token: |
| 401 | return &ast.Token{ |
| 402 | Wrap: m.cloneNode(n.Wrap), |
| 403 | } |
| 404 | |
| 405 | case *ast.TermGroup: |
| 406 | operands := make([]ast.Node, len(n.Operands)) |
| 407 | for i, op := range n.Operands { |
| 408 | operands[i] = m.cloneNode(op) |
| 409 | } |
| 410 | return &ast.TermGroup{ |
| 411 | Operands: operands, |
| 412 | Relation: n.Relation, |
| 413 | } |
| 414 | |
| 415 | case *ast.Term: |
| 416 | return &ast.Term{ |
| 417 | Foundry: n.Foundry, |
| 418 | Key: n.Key, |
| 419 | Layer: n.Layer, |
| 420 | Match: n.Match, |
| 421 | Value: n.Value, |
| 422 | } |
| 423 | |
Akron | 3295842 | 2025-05-16 16:33:05 +0200 | [diff] [blame] | 424 | case *ast.CatchallNode: |
| 425 | newNode := &ast.CatchallNode{ |
| 426 | NodeType: n.NodeType, |
| 427 | RawContent: n.RawContent, |
| 428 | } |
| 429 | if n.Wrap != nil { |
| 430 | newNode.Wrap = m.cloneNode(n.Wrap) |
| 431 | } |
| 432 | if len(n.Operands) > 0 { |
| 433 | newNode.Operands = make([]ast.Node, len(n.Operands)) |
| 434 | for i, op := range n.Operands { |
| 435 | newNode.Operands[i] = m.cloneNode(op) |
| 436 | } |
| 437 | } |
| 438 | return newNode |
| 439 | |
Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame] | 440 | default: |
| 441 | return nil |
| 442 | } |
| 443 | } |