simplify.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the GO-LICENSE file.
  4. // Modifications of this file, if any, are
  5. //
  6. // Copyright 2023 The Regexp Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style
  8. // license that can be found in the LICENSE file.
  9. package syntax // modernc.org/regexp/syntax
  10. // Simplify returns a regexp equivalent to re but without counted repetitions
  11. // and with various other simplifications, such as rewriting /(?:a+)+/ to /a+/.
  12. // The resulting regexp will execute correctly but its string representation
  13. // will not produce the same parse tree, because capturing parentheses
  14. // may have been duplicated or removed. For example, the simplified form
  15. // for /(x){1,2}/ is /(x)(x)?/ but both parentheses capture as $1.
  16. // The returned regexp may share structure with or be the original.
  17. func (re *Regexp) Simplify() *Regexp {
  18. if re == nil {
  19. return nil
  20. }
  21. switch re.Op {
  22. case OpCapture, OpConcat, OpAlternate:
  23. // Simplify children, building new Regexp if children change.
  24. nre := re
  25. for i, sub := range re.Sub {
  26. nsub := sub.Simplify()
  27. if nre == re && nsub != sub {
  28. // Start a copy.
  29. nre = new(Regexp)
  30. *nre = *re
  31. nre.Rune = nil
  32. nre.Sub = append(nre.Sub0[:0], re.Sub[:i]...)
  33. }
  34. if nre != re {
  35. nre.Sub = append(nre.Sub, nsub)
  36. }
  37. }
  38. return nre
  39. case OpStar, OpPlus, OpQuest:
  40. sub := re.Sub[0].Simplify()
  41. return simplify1(re.Op, re.Flags, sub, re)
  42. case OpRepeat:
  43. // Special special case: x{0} matches the empty string
  44. // and doesn't even need to consider x.
  45. if re.Min == 0 && re.Max == 0 {
  46. return &Regexp{Op: OpEmptyMatch}
  47. }
  48. // The fun begins.
  49. sub := re.Sub[0].Simplify()
  50. // x{n,} means at least n matches of x.
  51. if re.Max == -1 {
  52. // Special case: x{0,} is x*.
  53. if re.Min == 0 {
  54. return simplify1(OpStar, re.Flags, sub, nil)
  55. }
  56. // Special case: x{1,} is x+.
  57. if re.Min == 1 {
  58. return simplify1(OpPlus, re.Flags, sub, nil)
  59. }
  60. // General case: x{4,} is xxxx+.
  61. nre := &Regexp{Op: OpConcat}
  62. nre.Sub = nre.Sub0[:0]
  63. for i := 0; i < re.Min-1; i++ {
  64. nre.Sub = append(nre.Sub, sub)
  65. }
  66. nre.Sub = append(nre.Sub, simplify1(OpPlus, re.Flags, sub, nil))
  67. return nre
  68. }
  69. // Special case x{0} handled above.
  70. // Special case: x{1} is just x.
  71. if re.Min == 1 && re.Max == 1 {
  72. return sub
  73. }
  74. // General case: x{n,m} means n copies of x and m copies of x?
  75. // The machine will do less work if we nest the final m copies,
  76. // so that x{2,5} = xx(x(x(x)?)?)?
  77. // Build leading prefix: xx.
  78. var prefix *Regexp
  79. if re.Min > 0 {
  80. prefix = &Regexp{Op: OpConcat}
  81. prefix.Sub = prefix.Sub0[:0]
  82. for i := 0; i < re.Min; i++ {
  83. prefix.Sub = append(prefix.Sub, sub)
  84. }
  85. }
  86. // Build and attach suffix: (x(x(x)?)?)?
  87. if re.Max > re.Min {
  88. suffix := simplify1(OpQuest, re.Flags, sub, nil)
  89. for i := re.Min + 1; i < re.Max; i++ {
  90. nre2 := &Regexp{Op: OpConcat}
  91. nre2.Sub = append(nre2.Sub0[:0], sub, suffix)
  92. suffix = simplify1(OpQuest, re.Flags, nre2, nil)
  93. }
  94. if prefix == nil {
  95. return suffix
  96. }
  97. prefix.Sub = append(prefix.Sub, suffix)
  98. }
  99. if prefix != nil {
  100. return prefix
  101. }
  102. // Some degenerate case like min > max or min < max < 0.
  103. // Handle as impossible match.
  104. return &Regexp{Op: OpNoMatch}
  105. }
  106. return re
  107. }
  108. // simplify1 implements Simplify for the unary OpStar,
  109. // OpPlus, and OpQuest operators. It returns the simple regexp
  110. // equivalent to
  111. //
  112. // Regexp{Op: op, Flags: flags, Sub: {sub}}
  113. //
  114. // under the assumption that sub is already simple, and
  115. // without first allocating that structure. If the regexp
  116. // to be returned turns out to be equivalent to re, simplify1
  117. // returns re instead.
  118. //
  119. // simplify1 is factored out of Simplify because the implementation
  120. // for other operators generates these unary expressions.
  121. // Letting them call simplify1 makes sure the expressions they
  122. // generate are simple.
  123. func simplify1(op Op, flags Flags, sub, re *Regexp) *Regexp {
  124. // Special case: repeat the empty string as much as
  125. // you want, but it's still the empty string.
  126. if sub.Op == OpEmptyMatch {
  127. return sub
  128. }
  129. // The operators are idempotent if the flags match.
  130. if op == sub.Op && flags&NonGreedy == sub.Flags&NonGreedy {
  131. return sub
  132. }
  133. if re != nil && re.Op == op && re.Flags&NonGreedy == flags&NonGreedy && sub == re.Sub[0] {
  134. return re
  135. }
  136. re = &Regexp{Op: op, Flags: flags}
  137. re.Sub = append(re.Sub0[:0], sub)
  138. return re
  139. }