| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.javascript.rhino.Node; |
| 20 |
|
| 21 |
public class PeepholeSimplifyRegExpTest extends CompilerTestCase { |
| 22 |
|
| 23 |
public final void testWaysOfMatchingEmptyString() { |
| 24 |
testSame("/(?:)/"); |
| 25 |
test("/(?:)/i", "/(?:)/"); |
| 26 |
test("/.{0}/i", "/(?:)/"); |
| 27 |
test("/[^\\0-\\uffff]{0}/", "/(?:)/"); |
| 28 |
|
| 29 |
testSame("/(){0}/"); |
| 30 |
} |
| 31 |
|
| 32 |
public final void testCharsetOptimizations() { |
| 33 |
testSame("/./"); |
| 34 |
test("/[\\0-\\uffff]/", "/[\\S\\s]/"); |
| 35 |
test("/[^\\0-\\uffff]/", "/(?!)/"); |
| 36 |
test("/[^\\0-\\x40\\x42-\\uffff]/", "/A/"); |
| 37 |
test("/[0-9a-fA-F]/i", "/[\\da-f]/i"); |
| 38 |
test("/[0-9a-zA-Z_$]/i", "/[\\w$]/"); |
| 39 |
test("/[()*+\\-,]/g", "/[(--]/g"); |
| 40 |
test("/[()*+\\-,z]/g", "/[(--z]/g"); |
| 41 |
test("/[\\-\\.\\/0]/g", "/[--0]/g"); |
| 42 |
test("/[\\-\\.\\/0\\n]/g", "/[\\n\\--0]/g"); |
| 43 |
test("/[\\[\\\\\\]]/g", "/[[-\\]]/g"); |
| 44 |
test("/[\\[\\\\\\]\\^]/g", "/[[-^]/g"); |
| 45 |
test("/[\\^`_]/g", "/[\\^-`]/g"); |
| 46 |
test("/[^\\^`_]/g", "/[^^-`]/g"); |
| 47 |
test("/^(?=[^a-z])/i", "/^(?=[\\W\\d_])/"); |
| 48 |
test("/^[^a-z0-9]/i", "/^[\\W_]/"); |
| 49 |
test("/[0-FA-Z]/", "/[0-Z]/"); |
| 50 |
test("/[0-9]/", "/\\d/"); |
| 51 |
test("/[^0-9]/", "/\\D/"); |
| 52 |
testSame("/\\D/"); |
| 53 |
test("/[_a-z0-9]/i", "/\\w/"); |
| 54 |
test("/[0-9_a-z]/i", "/\\w/"); |
| 55 |
test("/[_a-z0-9]/", "/[\\d_a-z]/"); |
| 56 |
test("/[_E-Za-f0-9]/i", "/\\w/"); |
| 57 |
test("/[E-Za-f]/i", "/[a-z]/i"); |
| 58 |
test("/[_E-Za-f0-9]/", "/[\\dE-Z_a-f]/"); |
| 59 |
|
| 60 |
|
| 61 |
test("/[\\u00ca\\u00ea]/", "/[\\xca\\xea]/"); |
| 62 |
test("/[\\u00ca\\u00ea]/i", "/\\xca/i"); |
| 63 |
|
| 64 |
|
| 65 |
testSame("/^[\\s\\xa0]*$/"); |
| 66 |
test("/^(?:\\s|\\xA0)*$/", "/^[\\s\\xa0]*$/"); |
| 67 |
} |
| 68 |
|
| 69 |
public final void testCharsetFixup() { |
| 70 |
testSame("/[a-z]/i"); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
test("/[^\\0-`{-\\uffff]/i", "/(?!)/"); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
test("/[^a-z]/i", "/[\\W\\d_]/"); |
| 85 |
} |
| 86 |
|
| 87 |
public final void testGroups() { |
| 88 |
testSame("/foo(bar)baz/"); |
| 89 |
} |
| 90 |
|
| 91 |
public final void testBackReferences() { |
| 92 |
testSame("/foo(bar)baz(?:\\1|\\x01)boo/"); |
| 93 |
|
| 94 |
|
| 95 |
test("/foo(?:bar)baz(?:\\1|\\x01)boo/", "/foobarbaz\\x01boo/"); |
| 96 |
|
| 97 |
|
| 98 |
test("/foo(?:bar)baz(?:\\8|8)boo/", "/foobarbaz8boo/"); |
| 99 |
|
| 100 |
test("/(1?)(2?)(3?)(4?)(5?)(6?)(7?)(8?)(9?)(A?)(B?)" |
| 101 |
+ "\\12\\11\\10\\9\\8\\7\\6\\5\\4\\3\\2\\1\\0/", |
| 102 |
"/(1?)(2?)(3?)(4?)(5?)(6?)(7?)(8?)(9?)(A?)(B?)" |
| 103 |
|
| 104 |
|
| 105 |
+ "\\1(?:2)\\11\\10\\9\\8\\7\\6\\5\\4\\3\\2\\1\\0/"); |
| 106 |
|
| 107 |
test("/(1?)(2?)(3?)(4?)(5?)(6?)(7?)(8?)(9?)(A?)(B?)(?:\\1)0/", |
| 108 |
"/(1?)(2?)(3?)(4?)(5?)(6?)(7?)(8?)(9?)(A?)(B?)\\1(?:0)/"); |
| 109 |
|
| 110 |
test("/(1?)(2?)(3?)(4?)(5?)(6?)(7?)(8?)(9?)(A?)(B?)(C?)" |
| 111 |
+ "\\012\\11\\10\\9\\8\\7\\6\\5\\4\\3\\2\\1\\0/", |
| 112 |
"/(1?)(2?)(3?)(4?)(5?)(6?)(7?)(8?)(9?)(A?)(B?)(C?)" |
| 113 |
+ "\\n\\11\\10\\9\\8\\7\\6\\5\\4\\3\\2\\1\\0/"); |
| 114 |
} |
| 115 |
|
| 116 |
public final void testSingleCharAlterations() { |
| 117 |
test("/a|B|c|D/i", "/[a-d]/i"); |
| 118 |
test("/a|B|c|D/", "/[BDac]/"); |
| 119 |
test("/a|[Bc]|D/", "/[BDac]/"); |
| 120 |
test("/[aB]|[cD]/", "/[BDac]/"); |
| 121 |
test("/a|B|c|D|a|B/i", "/[a-d]/i"); |
| 122 |
test("/a|A|/i", "/a?/i"); |
| 123 |
} |
| 124 |
|
| 125 |
public final void testAlterations() { |
| 126 |
testSame("/foo|bar/"); |
| 127 |
test("/Foo|BAR/i", "/foo|bar/i"); |
| 128 |
test("/Foo||BAR/", "/Foo||BAR/"); |
| 129 |
test("/Foo|BAR|/", "/Foo|BAR|/"); |
| 130 |
} |
| 131 |
|
| 132 |
public final void testNestedAlterations() { |
| 133 |
test("/foo|bar|(?:baz|boo)|far/", "/foo|bar|baz|boo|far/"); |
| 134 |
} |
| 135 |
|
| 136 |
public final void testEscapeSequencesAndNonLatinChars() { |
| 137 |
test("/\u1234/i", "/\\u1234/"); |
| 138 |
testSame("/\\u1234/"); |
| 139 |
test("/\u00A0/", "/\\xa0/"); |
| 140 |
test("/\\u00A0/", "/\\xa0/"); |
| 141 |
test("/\\u00a0/", "/\\xa0/"); |
| 142 |
} |
| 143 |
|
| 144 |
public final void testAnchors() { |
| 145 |
|
| 146 |
testSame("/foo(?!$)/gm"); |
| 147 |
test("/./m", "/./"); |
| 148 |
test("/\\^/m", "/\\^/"); |
| 149 |
test("/[\\^]/m", "/\\^/"); |
| 150 |
testSame("/(^|foo)bar/"); |
| 151 |
testSame("/^.|.$/gm"); |
| 152 |
test("/foo(?=)$/m", "/foo$/m"); |
| 153 |
|
| 154 |
|
| 155 |
test("/^foo$/g", "/^foo$/"); |
| 156 |
} |
| 157 |
|
| 158 |
public final void testRepetitions() { |
| 159 |
testSame("/a*/"); |
| 160 |
testSame("/a+/"); |
| 161 |
testSame("/a+?/"); |
| 162 |
testSame("/a?/"); |
| 163 |
testSame("/a{6}/"); |
| 164 |
testSame("/a{4,}/"); |
| 165 |
test("/a{3,}/", |
| 166 |
"/aaa+/"); |
| 167 |
testSame("/a{4,6}/"); |
| 168 |
testSame("/a{4,6}?/"); |
| 169 |
test("/(?:a?)?/", "/a?/"); |
| 170 |
test("/(?:a?)*/", "/a*/"); |
| 171 |
test("/(?:a*)?/", "/a*/"); |
| 172 |
test("/a(?:a*)?/", "/a+/"); |
| 173 |
test("/(?:a{2,3}){3,4}/", "/a{6,12}/"); |
| 174 |
test("/a{2,3}a{3,4}/", "/a{5,7}/"); |
| 175 |
testSame("/a{5,7}b{5,6}/"); |
| 176 |
test("/a{2,3}b{3,4}/", |
| 177 |
"/aaa?bbbb?/"); |
| 178 |
test("/a{3}b{3,4}/", |
| 179 |
"/aaabbbb?/"); |
| 180 |
testSame("/[a-z]{1,2}/"); |
| 181 |
test("/\\d{1,2}/", |
| 182 |
"/\\d\\d?/"); |
| 183 |
test("/a*a*/", "/a*/"); |
| 184 |
test("/a+a+/", "/aa+/"); |
| 185 |
test("/a+a*/", "/a+/"); |
| 186 |
|
| 187 |
testSame("/a\\{3,1}/"); |
| 188 |
test("/a(?:{3,1})/", "/a\\{3,1}/"); |
| 189 |
test("/a{3\\,1}/", "/a\\{3,1}/"); |
| 190 |
testSame("/a\\{3}/"); |
| 191 |
testSame("/a\\{3,}/"); |
| 192 |
testSame("/a\\{1,3}/"); |
| 193 |
|
| 194 |
testSame("/a{/"); |
| 195 |
testSame("/a{}/"); |
| 196 |
testSame("/a{x}/"); |
| 197 |
testSame("/a{-1}/"); |
| 198 |
testSame("/a{,3}/"); |
| 199 |
testSame("/{{[a-z]+}}/"); |
| 200 |
testSame("/{\\{0}}/"); |
| 201 |
testSame("/{\\{0?}}/"); |
| 202 |
} |
| 203 |
|
| 204 |
public final void testMoreCharsets() { |
| 205 |
test("var a = /[\\x00\\x22\\x26\\x27\\x3c\\x3e]/g", |
| 206 |
"var a = /[\\0\"&'<>]/g"); |
| 207 |
test("var b = /[\\x00\\x22\\x27\\x3c\\x3e]/g", |
| 208 |
"var b = /[\\0\"'<>]/g"); |
| 209 |
test("var c = /[\\x00\\x09-\\x0d \\x22\\x26\\x27\\x2d\\/\\x3c-\\x3e`" |
| 210 |
+ "\\x85\\xa0\\u2028\\u2029]/g", |
| 211 |
"var c = /[\\0\\t-\\r \"&'/<->`\\x85\\xa0\\u2028\\u2029-]/g"); |
| 212 |
test("var d = /[\\x00\\x09-\\x0d \\x22\\x27\\x2d\\/\\x3c-\\x3e`" |
| 213 |
+ "\\x85\\xa0\\u2028\\u2029]/g", |
| 214 |
"var d = /[\\0\\t-\\r \"'/<->`\\x85\\xa0\\u2028\\u2029-]/g"); |
| 215 |
test("var e = /[\\x00\\x08-\\x0d\\x22\\x26\\x27\\/\\x3c-\\x3e\\\\" |
| 216 |
+ "\\x85\\u2028\\u2029]/g", |
| 217 |
"var e = /[\\0\\b-\\r\"&'/<->\\\\\\x85\\u2028\\u2029]/g"); |
| 218 |
test("var f = /[\\x00\\x08-\\x0d\\x22\\x24\\x26-\\/\\x3a\\x3c-\\x3f" |
| 219 |
+ "\\x5b-\\x5e\\x7b-\\x7d\\x85\\u2028\\u2029]/g", |
| 220 |
"var f = /[\\0\\b-\\r\"$&-/:<-?[-^{-}\\x85\\u2028\\u2029]/g"); |
| 221 |
test("var g = /[\\x00\\x08-\\x0d\\x22\\x26-\\x2a\\/\\x3a-\\x3e@\\\\" |
| 222 |
+ "\\x7b\\x7d\\x85\\xa0\\u2028\\u2029]/g", |
| 223 |
"var g = /[\\0\\b-\\r\"&-*/:->@\\\\{}\\x85\\xa0\\u2028\\u2029]/g"); |
| 224 |
test("var h = /^(?!-*(?:expression|(?:moz-)?binding))(?:[.#]?-?" |
| 225 |
+ "(?:[_a-z0-9][_a-z0-9-]*)(?:-[_a-z][_a-z0-9-]*)*-?|-?" |
| 226 |
+ "(?:[0-9]+(?:\\.[0-9]*)?|\\.[0-9])(?:[a-z]{1,2}|%)?|!important|)$/i", |
| 227 |
"var h = /^(?!-*(?:expression|(?:moz-)?binding))(?:[#.]?-?" |
| 228 |
+ "\\w[\\w-]*(?:-[_a-z][\\w-]*)*-?|-?" |
| 229 |
+ "(?:\\d+(?:\\.\\d*)?|\\.\\d)(?:[a-z]{1,2}|%)?|!important|)$/i"); |
| 230 |
test("var i = /^(?:(?:https?|mailto):|[^&:\\/?#]*(?:[\\/?#]|$))/i", |
| 231 |
"var i = /^(?:(?:https?|mailto):|[^#&/:?]*(?:[#/?]|$))/i"); |
| 232 |
test("var j = /^(?!style|on|action|archive|background|cite|classid" |
| 233 |
+ "|codebase|data|dsync|href|longdesc|src|usemap)(?:[a-z0-9_$:-]*" |
| 234 |
+ "|dir=(?:ltr|rtl))$/i", |
| 235 |
"var j = /^(?!style|on|action|archive|background|cite|classid" |
| 236 |
+ "|codebase|data|dsync|href|longdesc|src|usemap)(?:[\\w$:-]*" |
| 237 |
+ "|dir=(?:ltr|rtl))$/i"); |
| 238 |
test("var k = /^(?!script|style|title|textarea|xmp|no)[a-z0-9_$:-]*$/i", |
| 239 |
"var k = /^(?!script|style|title|textarea|xmp|no)[\\w$:-]*$/i"); |
| 240 |
test("var l = /<(?:!|\\/?[a-z])(?:[^>'\"]|\"[^\"]*\"|'[^']*')*>/gi", |
| 241 |
"var l = /<(?:!|\\/?[a-z])(?:[^\"'>]|\"[^\"]*\"|'[^']*')*>/gi"); |
| 242 |
} |
| 243 |
|
| 244 |
public final void testMoreRegularExpression() { |
| 245 |
testSame("/\"/"); |
| 246 |
testSame("/'/"); |
| 247 |
test("/(?:[^<\\/\"'\\s\\\\]|<(?!\\/script))+/i", |
| 248 |
"/(?:[^\\s\"'/<\\\\]|<(?!\\/script))+/i"); |
| 249 |
testSame("/-->/"); |
| 250 |
testSame("/<!--/"); |
| 251 |
testSame("/<\\/(\\w+)\\b/"); |
| 252 |
testSame("/<\\/?/"); |
| 253 |
test("/<script(?=[\\s>\\/]|$)/i", "/<script(?=[\\s/>]|$)/i"); |
| 254 |
test("/<style(?=[\\s>\\/]|$)/i", "/<style(?=[\\s/>]|$)/i"); |
| 255 |
test("/<textarea(?=[\\s>\\/]|$)/i", "/<textarea(?=[\\s/>]|$)/i"); |
| 256 |
test("/<title(?=[\\s>\\/]|$)/i", "/<title(?=[\\s/>]|$)/i"); |
| 257 |
test("/<xmp(?=[\\s>\\/]|$)/i", "/<xmp(?=[\\s/>]|$)/i"); |
| 258 |
testSame("/[\"']/"); |
| 259 |
test("/[\\\\)\\s]/", "/[\\s)\\\\]/"); |
| 260 |
test("/[\\f\\r\\n\\u2028\\u2029]/", "/[\\n\\f\\r\\u2028\\u2029]/"); |
| 261 |
test("/[\\n\\r\\f]/", "/[\\n\\f\\r]/"); |
| 262 |
testSame("/\\*\\//"); |
| 263 |
testSame("/\\//"); |
| 264 |
testSame("/\\/\\*/"); |
| 265 |
testSame("/\\/\\//"); |
| 266 |
testSame("/\\\\(?:\\r\\n?|[\\n\\f\"])/"); |
| 267 |
testSame("/\\\\(?:\\r\\n?|[\\n\\f'])/"); |
| 268 |
testSame("/\\burl\\s*\\(\\s*([\"']?)/i"); |
| 269 |
testSame("/\\s+/"); |
| 270 |
test("/^(?:[^'\\\\\\n\\r\\u2028\\u2029<]|\\\\(?:\\r\\n?|[^\\r<]" |
| 271 |
+ "|<(?!\\/script))|<(?!\\/script))/i", |
| 272 |
"/^(?:[^\\n\\r'<\\\\\\u2028\\u2029]|\\\\(?:\\r\\n?|[^\\r<]" |
| 273 |
+ "|<(?!\\/script))|<(?!\\/script))/i"); |
| 274 |
test("/^(?:[^\\\"\\\\\\n\\r\\u2028\\u2029<]|\\\\(?:\\r\\n?" |
| 275 |
+ "|[^\\r<]|<(?!\\/script))|<(?!\\/script))/i", |
| 276 |
"/^(?:[^\\n\\r\"<\\\\\\u2028\\u2029]|\\\\(?:\\r\\n?" |
| 277 |
+ "|[^\\r<]|<(?!\\/script))|<(?!\\/script))/i"); |
| 278 |
test("/^(?:[^\\[\\\\\\/<\\n\\r\\u2028\\u2029]|\\\\[^\\n\\r\\u2028\\u2029]" |
| 279 |
+ "|\\\\?<(?!\\/script)|\\[(?:[^\\]\\\\<\\n\\r\\u2028\\u2029]|" |
| 280 |
+ "\\\\(?:[^\\n\\r\\u2028\\u2029]))*|\\\\?<(?!\\/script)\\])/i", |
| 281 |
"/^(?:[^\\n\\r/<[\\\\\\u2028\\u2029]|\\\\." |
| 282 |
+ "|\\\\?<(?!\\/script)|\\[(?:[^\\n\\r<\\\\\\]\\u2028\\u2029]|" |
| 283 |
+ "\\\\.)*|\\\\?<(?!\\/script)])/i"); |
| 284 |
testSame("/^(?=>|\\s+[\\w-]+\\s*=)/"); |
| 285 |
test("/^(?=[\\/\\s>])/", "/^(?=[\\s/>])/"); |
| 286 |
test("/^(?=[^\"'\\s>])/", "/^(?=[^\\s\"'>])/"); |
| 287 |
testSame("/^/"); |
| 288 |
testSame("/^[^<]+/"); |
| 289 |
test("/^[a-z0-9:-]*(?:[a-z0-9]|$)/i", "/^[\\d:a-z-]*(?:[^\\W_]|$)/i"); |
| 290 |
testSame("/^[a-z]+/i"); |
| 291 |
testSame("/^\\s*\"/"); |
| 292 |
testSame("/^\\s*'/"); |
| 293 |
testSame("/^\\s*([a-z][\\w-]*)/i"); |
| 294 |
testSame("/^\\s*=/"); |
| 295 |
testSame("/^\\s*\\/?>/"); |
| 296 |
testSame("/^\\s+$/"); |
| 297 |
testSame("/^\\s+/"); |
| 298 |
} |
| 299 |
|
| 300 |
public final void testPrecedence() { |
| 301 |
|
| 302 |
testSame("/ab?/"); |
| 303 |
testSame("/(?:ab)?/"); |
| 304 |
|
| 305 |
testSame("/foo|bar/"); |
| 306 |
testSame("/f(?:oo|ba)r/"); |
| 307 |
} |
| 308 |
|
| 309 |
public final void testMalformedRegularExpressions() { |
| 310 |
test( |
| 311 |
"/(?<!foo)/", "/(?<!foo)/", |
| 312 |
null, |
| 313 |
CheckRegExp.MALFORMED_REGEXP); |
| 314 |
test( |
| 315 |
"/(/", "/(/", |
| 316 |
null, |
| 317 |
CheckRegExp.MALFORMED_REGEXP); |
| 318 |
test( |
| 319 |
"/)/", "/)/", |
| 320 |
null, |
| 321 |
CheckRegExp.MALFORMED_REGEXP); |
| 322 |
test( |
| 323 |
"/\\uabc/", "/\\uabc/", |
| 324 |
null, |
| 325 |
CheckRegExp.MALFORMED_REGEXP); |
| 326 |
test( |
| 327 |
"/\\uabcg/", "/\\uabcg/", |
| 328 |
null, |
| 329 |
CheckRegExp.MALFORMED_REGEXP); |
| 330 |
} |
| 331 |
|
| 332 |
@Override |
| 333 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 334 |
final CompilerPass simplifier = new PeepholeOptimizationsPass( |
| 335 |
compiler, new PeepholeSimplifyRegExp()); |
| 336 |
final CompilerPass checker = new CheckRegExp(compiler); |
| 337 |
|
| 338 |
return new CompilerPass() { |
| 339 |
@Override |
| 340 |
public void process(Node externs, Node root) { |
| 341 |
checker.process(externs, root); |
| 342 |
simplifier.process(externs, root); |
| 343 |
} |
| 344 |
}; |
| 345 |
} |
| 346 |
} |