1/*
2 * Copyright (C) 2017 Yusuke Suzuki <[email protected]>.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "YarrErrorCode.h"
28
29#include "Error.h"
30
31namespace JSC { namespace Yarr {
32
33const char* errorMessage(ErrorCode error)
34{
35#define REGEXP_ERROR_PREFIX "Invalid regular expression: "
36 // The order of this array must match the ErrorCode enum.
37 static const char* errorMessages[] = {
38 nullptr, // NoError
39 REGEXP_ERROR_PREFIX "regular expression too large", // PatternTooLarge
40 REGEXP_ERROR_PREFIX "numbers out of order in {} quantifier", // QuantifierOutOfOrder
41 REGEXP_ERROR_PREFIX "nothing to repeat", // QuantifierWithoutAtom
42 REGEXP_ERROR_PREFIX "number too large in {} quantifier", // QuantifierTooLarge
43 REGEXP_ERROR_PREFIX "missing )", // MissingParentheses
44 REGEXP_ERROR_PREFIX "unmatched parentheses", // ParenthesesUnmatched
45 REGEXP_ERROR_PREFIX "unrecognized character after (?", // ParenthesesTypeInvalid
46 REGEXP_ERROR_PREFIX "invalid group specifier name", // InvalidGroupName
47 REGEXP_ERROR_PREFIX "duplicate group specifier name", // DuplicateGroupName
48 REGEXP_ERROR_PREFIX "missing terminating ] for character class", // CharacterClassUnmatched
49 REGEXP_ERROR_PREFIX "range out of order in character class", // CharacterClassOutOfOrder
50 REGEXP_ERROR_PREFIX "\\ at end of pattern", // EscapeUnterminated
51 REGEXP_ERROR_PREFIX "invalid unicode {} escape", // InvalidUnicodeEscape
52 REGEXP_ERROR_PREFIX "invalid backreference for unicode pattern", // InvalidBackreference
53 REGEXP_ERROR_PREFIX "invalid escaped character for unicode pattern", // InvalidIdentityEscape
54 REGEXP_ERROR_PREFIX "invalid property expression", // InvalidUnicodePropertyExpression
55 REGEXP_ERROR_PREFIX "too many nested disjunctions", // TooManyDisjunctions
56 REGEXP_ERROR_PREFIX "pattern exceeds string length limits", // OffsetTooLarge
57 REGEXP_ERROR_PREFIX "invalid flags" // InvalidRegularExpressionFlags
58 };
59
60 return errorMessages[static_cast<unsigned>(error)];
61}
62
63JSObject* errorToThrow(ExecState* exec, ErrorCode error)
64{
65 switch (error) {
66 case ErrorCode::NoError:
67 ASSERT_NOT_REACHED();
68 return nullptr;
69 case ErrorCode::PatternTooLarge:
70 case ErrorCode::QuantifierOutOfOrder:
71 case ErrorCode::QuantifierWithoutAtom:
72 case ErrorCode::QuantifierTooLarge:
73 case ErrorCode::MissingParentheses:
74 case ErrorCode::ParenthesesUnmatched:
75 case ErrorCode::ParenthesesTypeInvalid:
76 case ErrorCode::InvalidGroupName:
77 case ErrorCode::DuplicateGroupName:
78 case ErrorCode::CharacterClassUnmatched:
79 case ErrorCode::CharacterClassOutOfOrder:
80 case ErrorCode::EscapeUnterminated:
81 case ErrorCode::InvalidUnicodeEscape:
82 case ErrorCode::InvalidBackreference:
83 case ErrorCode::InvalidIdentityEscape:
84 case ErrorCode::InvalidUnicodePropertyExpression:
85 case ErrorCode::OffsetTooLarge:
86 case ErrorCode::InvalidRegularExpressionFlags:
87 return createSyntaxError(exec, errorMessage(error));
88 case ErrorCode::TooManyDisjunctions:
89 return createOutOfMemoryError(exec, errorMessage(error));
90 }
91
92 ASSERT_NOT_REACHED();
93 return nullptr;
94}
95
96} } // namespace JSC::Yarr
97