1 | /* |
2 | * Copyright (C) 2013 Apple Inc. All Rights Reserved. |
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 | #pragma once |
27 | |
28 | #include "Error.h" |
29 | #include "ErrorHandlingScope.h" |
30 | #include "ExceptionHelpers.h" |
31 | #include "ParserTokens.h" |
32 | #include <wtf/text/WTFString.h> |
33 | |
34 | namespace JSC { |
35 | |
36 | class ParserError { |
37 | public: |
38 | enum SyntaxErrorType : uint8_t { |
39 | SyntaxErrorNone, |
40 | SyntaxErrorIrrecoverable, |
41 | SyntaxErrorUnterminatedLiteral, |
42 | SyntaxErrorRecoverable |
43 | }; |
44 | |
45 | enum ErrorType : uint8_t { |
46 | ErrorNone, |
47 | StackOverflow, |
48 | EvalError, |
49 | OutOfMemory, |
50 | SyntaxError |
51 | }; |
52 | |
53 | ParserError() |
54 | : m_type(ErrorNone) |
55 | , m_syntaxErrorType(SyntaxErrorNone) |
56 | { |
57 | } |
58 | |
59 | explicit ParserError(ErrorType type) |
60 | : m_type(type) |
61 | , m_syntaxErrorType(SyntaxErrorNone) |
62 | { |
63 | } |
64 | |
65 | ParserError(ErrorType type, SyntaxErrorType syntaxError, JSToken token) |
66 | : m_token(token) |
67 | , m_type(type) |
68 | , m_syntaxErrorType(syntaxError) |
69 | { |
70 | } |
71 | |
72 | ParserError(ErrorType type, SyntaxErrorType syntaxError, JSToken token, const String& msg, int line) |
73 | : m_token(token) |
74 | , m_message(msg) |
75 | , m_line(line) |
76 | , m_type(type) |
77 | , m_syntaxErrorType(syntaxError) |
78 | { |
79 | } |
80 | |
81 | bool isValid() const { return m_type != ErrorNone; } |
82 | SyntaxErrorType syntaxErrorType() const { return m_syntaxErrorType; } |
83 | const JSToken& token() const { return m_token; } |
84 | const String& message() const { return m_message; } |
85 | int line() const { return m_line; } |
86 | ErrorType type() const { return m_type; } |
87 | |
88 | JSObject* toErrorObject( |
89 | JSGlobalObject* globalObject, |
90 | SourceCode source, // Note: We must copy the source here, since the objects that pass in their SourceCode field may be destroyed in addErrorInfo. |
91 | int overrideLineNumber = -1) |
92 | { |
93 | switch (m_type) { |
94 | case ErrorNone: |
95 | return nullptr; |
96 | case SyntaxError: |
97 | return addErrorInfo( |
98 | globalObject->vm(), |
99 | createSyntaxError(globalObject, m_message), |
100 | overrideLineNumber == -1 ? m_line : overrideLineNumber, source); |
101 | case EvalError: |
102 | return createSyntaxError(globalObject, m_message); |
103 | case StackOverflow: { |
104 | ErrorHandlingScope errorScope(getVM(globalObject)); |
105 | return createStackOverflowError(globalObject); |
106 | } |
107 | case OutOfMemory: |
108 | return createOutOfMemoryError(globalObject); |
109 | } |
110 | CRASH(); |
111 | return nullptr; |
112 | } |
113 | |
114 | private: |
115 | JSToken m_token; |
116 | String m_message; |
117 | int m_line { -1 }; |
118 | ErrorType m_type; |
119 | SyntaxErrorType m_syntaxErrorType; |
120 | }; |
121 | |
122 | } // namespace JSC |
123 | |
124 | namespace WTF { |
125 | |
126 | inline void printInternal(PrintStream& out, JSC::ParserError::SyntaxErrorType type) |
127 | { |
128 | switch (type) { |
129 | case JSC::ParserError::SyntaxErrorNone: |
130 | out.print("SyntaxErrorNone" ); |
131 | return; |
132 | case JSC::ParserError::SyntaxErrorIrrecoverable: |
133 | out.print("SyntaxErrorIrrecoverable" ); |
134 | return; |
135 | case JSC::ParserError::SyntaxErrorUnterminatedLiteral: |
136 | out.print("SyntaxErrorUnterminatedLiteral" ); |
137 | return; |
138 | case JSC::ParserError::SyntaxErrorRecoverable: |
139 | out.print("SyntaxErrorRecoverable" ); |
140 | return; |
141 | } |
142 | |
143 | RELEASE_ASSERT_NOT_REACHED(); |
144 | } |
145 | |
146 | inline void printInternal(PrintStream& out, JSC::ParserError::ErrorType type) |
147 | { |
148 | switch (type) { |
149 | case JSC::ParserError::ErrorNone: |
150 | out.print("ErrorNone" ); |
151 | return; |
152 | case JSC::ParserError::StackOverflow: |
153 | out.print("StackOverflow" ); |
154 | return; |
155 | case JSC::ParserError::EvalError: |
156 | out.print("EvalError" ); |
157 | return; |
158 | case JSC::ParserError::OutOfMemory: |
159 | out.print("OutOfMemory" ); |
160 | return; |
161 | case JSC::ParserError::SyntaxError: |
162 | out.print("SyntaxError" ); |
163 | return; |
164 | } |
165 | |
166 | RELEASE_ASSERT_NOT_REACHED(); |
167 | } |
168 | |
169 | } // namespace WTF |
170 | |