OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-regexp-named-captures | 5 // Flags: --harmony-regexp-named-captures |
6 | 6 |
7 // Malformed named captures. | 7 // Malformed named captures. |
8 assertThrows("/(?<>a)/u"); // Empty name. | 8 assertThrows("/(?<>a)/u"); // Empty name. |
9 assertThrows("/(?<aa)/u"); // Unterminated name. | 9 assertThrows("/(?<aa)/u"); // Unterminated name. |
10 assertThrows("/(?<42a>a)/u"); // Name starting with digits. | 10 assertThrows("/(?<42a>a)/u"); // Name starting with digits. |
11 assertThrows("/(?<:a>a)/u"); // Name starting with invalid char. | 11 assertThrows("/(?<:a>a)/u"); // Name starting with invalid char. |
12 assertThrows("/(?<a:>a)/u"); // Name containing with invalid char. | 12 assertThrows("/(?<a:>a)/u"); // Name containing with invalid char. |
13 assertThrows("/(?<a>a)(?<a>a)/u"); // Duplicate name. | 13 assertThrows("/(?<a>a)(?<a>a)/u"); // Duplicate name. |
14 assertThrows("/(?<a>a)(?<b>b)(?<a>a)/u"); // Duplicate name. | 14 assertThrows("/(?<a>a)(?<b>b)(?<a>a)/u"); // Duplicate name. |
15 assertThrows("/\\k<a>/u"); // Invalid reference. | 15 assertThrows("/\\k<a>/u"); // Invalid reference. |
16 assertThrows("/(?<a>a)\\k<ab>/u"); // Invalid reference. | 16 assertThrows("/(?<a>a)\\k<ab>/u"); // Invalid reference. |
17 assertThrows("/(?<ab>a)\\k<a>/u"); // Invalid reference. | 17 assertThrows("/(?<ab>a)\\k<a>/u"); // Invalid reference. |
18 assertThrows("/\\k<a>(?<ab>a)/u"); // Invalid reference. | 18 assertThrows("/\\k<a>(?<ab>a)/u"); // Invalid reference. |
19 | 19 |
20 // Fallback behavior in non-unicode mode. | 20 // Fallback behavior in non-unicode mode. |
21 assertThrows("/(?<>a)/", SyntaxError); | 21 assertThrows("/(?<>a)/"); |
22 assertThrows("/(?<aa)/", SyntaxError); | 22 assertThrows("/(?<aa)/"); |
23 assertThrows("/(?<42a>a)/", SyntaxError); | 23 assertThrows("/(?<42a>a)/"); |
24 assertThrows("/(?<:a>a)/", SyntaxError); | 24 assertThrows("/(?<:a>a)/"); |
25 assertThrows("/(?<a:>a)/", SyntaxError); | 25 assertThrows("/(?<a:>a)/"); |
26 assertThrows("/(?<a>a)(?<a>a)/", SyntaxError); | 26 assertThrows("/(?<a>a)(?<a>a)/"); |
27 assertThrows("/(?<a>a)(?<b>b)(?<a>a)/", SyntaxError); | 27 assertThrows("/(?<a>a)(?<b>b)(?<a>a)/"); |
28 assertThrows("/(?<a>a)\\k<ab>/", SyntaxError); | 28 assertThrows("/(?<a>a)\\k<ab>/"); |
29 assertThrows("/(?<ab>a)\\k<a>/", SyntaxError); | 29 assertThrows("/(?<ab>a)\\k<a>/"); |
30 | 30 |
31 assertEquals(["k<a>"], "xxxk<a>xxx".match(/\k<a>/)); | 31 assertEquals(["k<a>"], "xxxk<a>xxx".match(/\k<a>/)); |
32 assertEquals(["k<a"], "xxxk<a>xxx".match(/\k<a/)); | 32 assertEquals(["k<a"], "xxxk<a>xxx".match(/\k<a/)); |
33 | 33 |
34 // Basic named groups. | 34 // Basic named groups. |
35 assertEquals(["a", "a"], "bab".match(/(?<a>a)/u)); | 35 assertEquals(["a", "a"], "bab".match(/(?<a>a)/u)); |
36 assertEquals(["a", "a"], "bab".match(/(?<a42>a)/u)); | 36 assertEquals(["a", "a"], "bab".match(/(?<a42>a)/u)); |
37 assertEquals(["a", "a"], "bab".match(/(?<_>a)/u)); | 37 assertEquals(["a", "a"], "bab".match(/(?<_>a)/u)); |
38 assertEquals(["a", "a"], "bab".match(/(?<$>a)/u)); | 38 assertEquals(["a", "a"], "bab".match(/(?<$>a)/u)); |
39 assertEquals(["bab", "a"], "bab".match(/.(?<$>a)./u)); | 39 assertEquals(["bab", "a"], "bab".match(/.(?<$>a)./u)); |
(...skipping 27 matching lines...) Expand all Loading... |
67 | 67 |
68 // Nested groups. | 68 // Nested groups. |
69 assertEquals(["bab", "bab", "ab", "b"], "bab".match(/(?<a>.(?<b>.(?<c>.)))/u)); | 69 assertEquals(["bab", "bab", "ab", "b"], "bab".match(/(?<a>.(?<b>.(?<c>.)))/u)); |
70 | 70 |
71 // Reference inside group. | 71 // Reference inside group. |
72 assertEquals(["bab", "b"], "bab".match(/(?<a>\k<a>\w)../u)); | 72 assertEquals(["bab", "b"], "bab".match(/(?<a>\k<a>\w)../u)); |
73 | 73 |
74 // Reference before group. | 74 // Reference before group. |
75 assertEquals(["bab", "b"], "bab".match(/\k<a>(?<a>b)\w\k<a>/u)); | 75 assertEquals(["bab", "b"], "bab".match(/\k<a>(?<a>b)\w\k<a>/u)); |
76 assertEquals(["bab", "b", "a"], "bab".match(/(?<b>b)\k<a>(?<a>a)\k<b>/u)); | 76 assertEquals(["bab", "b", "a"], "bab".match(/(?<b>b)\k<a>(?<a>a)\k<b>/u)); |
77 | |
78 // Reference properties. | |
79 assertEquals("a", /(?<a>a)(?<b>b)\k<a>/u.exec("aba").group.a); | |
80 assertEquals("b", /(?<a>a)(?<b>b)\k<a>/u.exec("aba").group.b); | |
81 assertEquals(undefined, /(?<a>a)(?<b>b)\k<a>/u.exec("aba").group.c); | |
82 assertFalse(/(?<a>a)(?<b>b)\k<a>/u.exec("aba").group.hasOwnProperty("c")); | |
83 assertEquals(undefined, /(?<a>a)(?<b>b)\k<a>|(?<c>c)/u.exec("aba").group.c); | |
84 assertTrue(/(?<a>a)(?<b>b)\k<a>|(?<c>c)/u | |
85 .exec("aba").group.hasOwnProperty("c")); | |
86 | |
87 // Unicode names. | |
88 assertEquals("a", /(?<π>a)/u.exec("bab").group.π); | |
89 assertEquals("a", /(?<\u{03C0}>a)/u.exec("bab").group.\u03C0); | |
90 assertEquals("a", /(?<$>a)/u.exec("bab").group.$); | |
91 assertEquals("a", /(?<_>a)/u.exec("bab").group._); | |
92 assertEquals("a", /(?<$𐒤>a)/u.exec("bab").group.$𐒤); | |
93 assertEquals("a", /(?<_\u200C>a)/u.exec("bab").group._\u200C); | |
94 assertEquals("a", /(?<_\u200D>a)/u.exec("bab").group._\u200D); | |
95 assertEquals("a", /(?<ಠ_ಠ>a)/u.exec("bab").group.ಠ_ಠ); | |
96 assertThrows('/(?<❤>a)/u', SyntaxError); | |
97 assertThrows('/(?<𐒤>a)/u', SyntaxError); // ID_Continue but not ID_Start. | |
98 | |
99 // Capture name conflicts. | |
100 assertThrows(() => /(?<__proto__>a)/u, SyntaxError); | |
101 assertEquals("a", /(?<__proto_>a)/u.exec("a").group.__proto_); | |
102 assertEquals("a", /(?<__proto___>a)/u.exec("a").group.__proto___); | |
OLD | NEW |