1/* ***** BEGIN LICENSE BLOCK *****
2* Version: NPL 1.1/GPL 2.0/LGPL 2.1
3*
4* The contents of this file are subject to the Netscape Public License
5* Version 1.1 (the "License"); you may not use this file except in
6* compliance with the License. You may obtain a copy of the License at
7* http://www.mozilla.org/NPL/
8*
9* Software distributed under the License is distributed on an "AS IS" basis,
10* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11* for the specific language governing rights and limitations under the
12* License.
13*
14* The Original Code is JavaScript Engine testing utilities.
15*
16* The Initial Developer of the Original Code is Netscape Communications Corp.
17* Portions created by the Initial Developer are Copyright (C) 2003
18* the Initial Developer. All Rights Reserved.
19*
20* Contributor(s): scole@planetweb.com, pschwartau@netscape.com
21*
22* Alternatively, the contents of this file may be used under the terms of
23* either the GNU General Public License Version 2 or later (the "GPL"), or
24* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25* in which case the provisions of the GPL or the LGPL are applicable instead
26* of those above. If you wish to allow use of your version of this file only
27* under the terms of either the GPL or the LGPL, and not to allow others to
28* use your version of this file under the terms of the NPL, indicate your
29* decision by deleting the provisions above and replace them with the notice
30* and other provisions required by the GPL or the LGPL. If you do not delete
31* the provisions above, a recipient may use your version of this file under
32* the terms of any one of the NPL, the GPL or the LGPL.
33*
34* ***** END LICENSE BLOCK *****
35*
36*
37* Date:    21 January 2003
38* SUMMARY: Invalid use of regexp quantifiers should generate SyntaxErrors
39*
40* See http://bugzilla.mozilla.org/show_bug.cgi?id=188206
41* and http://bugzilla.mozilla.org/show_bug.cgi?id=85721#c48 etc.
42* and http://bugzilla.mozilla.org/show_bug.cgi?id=190685
43* and http://bugzilla.mozilla.org/show_bug.cgi?id=197451
44*/
45//-----------------------------------------------------------------------------
46var UBound = 0;
47var bug = 188206;
48var summary = 'Invalid use of regexp quantifiers should generate SyntaxErrors';
49var TEST_PASSED = 'SyntaxError';
50var TEST_FAILED = 'Generated an error, but NOT a SyntaxError!';
51var TEST_FAILED_BADLY = 'Did not generate ANY error!!!';
52var CHECK_PASSED = 'Should not generate an error';
53var CHECK_FAILED = 'Generated an error!';
54var status = '';
55var statusitems = [];
56var actual = '';
57var actualvalues = [];
58var expect= '';
59var expectedvalues = [];
60
61
62/*
63 * All the following are invalid uses of regexp quantifiers and
64 * should generate SyntaxErrors. That's what we're testing for.
65 *
66 * To allow the test to compile and run, we have to hide the errors
67 * inside eval strings, and check they are caught at run-time -
68 *
69 */
70status = inSection(1);
71testThis(' /a**/ ');
72
73status = inSection(2);
74testThis(' /a***/ ');
75
76status = inSection(3);
77testThis(' /a++/ ');
78
79status = inSection(4);
80testThis(' /a+++/ ');
81
82/*
83 * The ? quantifier, unlike * or +, may appear twice in succession.
84 * Thus we need at least three in a row to provoke a SyntaxError -
85 */
86
87status = inSection(5);
88testThis(' /a???/ ');
89
90status = inSection(6);
91testThis(' /a????/ ');
92
93
94/*
95 * Now do some weird things on the left side of the regexps -
96 */
97status = inSection(7);
98testThis(' /*a/ ');
99
100status = inSection(8);
101testThis(' /**a/ ');
102
103status = inSection(9);
104testThis(' /+a/ ');
105
106status = inSection(10);
107testThis(' /++a/ ');
108
109status = inSection(11);
110testThis(' /?a/ ');
111
112status = inSection(12);
113testThis(' /??a/ ');
114
115
116/*
117 * Misusing the {DecmalDigits} quantifier - according to ECMA,
118 * but not according to Perl.
119 *
120 * ECMA-262 Edition 3 prohibits the use of unescaped braces in
121 * regexp patterns, unless they form part of a quantifier.
122 *
123 * Hovever, Perl does not prohibit this. If not used as part
124 * of a quantifer, Perl treats braces literally.
125 *
126 * We decided to follow Perl on this for backward compatibility.
127 * See http://bugzilla.mozilla.org/show_bug.cgi?id=190685.
128 *
129 * Therefore NONE of the following ECMA violations should generate
130 * a SyntaxError. Note we use checkThis() instead of testThis().
131 */
132status = inSection(13);
133checkThis(' /a*{/ ');
134
135status = inSection(14);
136checkThis(' /a{}/ ');
137
138status = inSection(15);
139checkThis(' /{a/ ');
140
141status = inSection(16);
142checkThis(' /}a/ ');
143
144status = inSection(17);
145checkThis(' /x{abc}/ ');
146
147status = inSection(18);
148checkThis(' /{{0}/ ');
149
150status = inSection(19);
151checkThis(' /{{1}/ ');
152
153status = inSection(20);
154checkThis(' /x{{0}/ ');
155
156status = inSection(21);
157checkThis(' /x{{1}/ ');
158
159status = inSection(22);
160checkThis(' /x{{0}}/ ');
161
162status = inSection(23);
163checkThis(' /x{{1}}/ ');
164
165status = inSection(24);
166checkThis(' /x{{0}}/ ');
167
168status = inSection(25);
169checkThis(' /x{{1}}/ ');
170
171status = inSection(26);
172checkThis(' /x{{0}}/ ');
173
174status = inSection(27);
175checkThis(' /x{{1}}/ ');
176
177
178/*
179 * Misusing the {DecmalDigits} quantifier - according to BOTH ECMA and Perl.
180 *
181 * Just as with the * and + quantifiers above, can't have two {DecmalDigits}
182 * quantifiers in succession - it's a SyntaxError.
183 */
184status = inSection(28);
185testThis(' /x{1}{1}/ ');
186
187status = inSection(29);
188testThis(' /x{1,}{1}/ ');
189
190status = inSection(30);
191testThis(' /x{1,2}{1}/ ');
192
193status = inSection(31);
194testThis(' /x{1}{1,}/ ');
195
196status = inSection(32);
197testThis(' /x{1,}{1,}/ ');
198
199status = inSection(33);
200testThis(' /x{1,2}{1,}/ ');
201
202status = inSection(34);
203testThis(' /x{1}{1,2}/ ');
204
205status = inSection(35);
206testThis(' /x{1,}{1,2}/ ');
207
208status = inSection(36);
209testThis(' /x{1,2}{1,2}/ ');
210
211
212
213//-----------------------------------------------------------------------------
214test();
215//-----------------------------------------------------------------------------
216
217
218
219/*
220 * Invalid syntax should generate a SyntaxError
221 */
222function testThis(sInvalidSyntax)
223{
224  expect = TEST_PASSED;
225  actual = TEST_FAILED_BADLY;
226
227  try
228  {
229    eval(sInvalidSyntax);
230  }
231  catch(e)
232  {
233    if (e instanceof SyntaxError)
234      actual = TEST_PASSED;
235    else
236      actual = TEST_FAILED;
237  }
238
239  statusitems[UBound] = status;
240  expectedvalues[UBound] = expect;
241  actualvalues[UBound] = actual;
242  UBound++;
243}
244
245
246/*
247 * Allowed syntax shouldn't generate any errors
248 */
249function checkThis(sAllowedSyntax)
250{
251  expect = CHECK_PASSED;
252  actual = CHECK_PASSED;
253
254  try
255  {
256    eval(sAllowedSyntax);
257  }
258  catch(e)
259  {
260    actual = CHECK_FAILED;
261  }
262
263  statusitems[UBound] = status;
264  expectedvalues[UBound] = expect;
265  actualvalues[UBound] = actual;
266  UBound++;
267}
268
269
270function test()
271{
272  enterFunc('test');
273  printBugNumber(bug);
274  printStatus(summary);
275
276  for (var i=0; i<UBound; i++)
277  {
278    reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
279  }
280
281  exitFunc ('test');
282}
283