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
34namespace JSC {
35
36class ParserError {
37public:
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 ExecState* exec = globalObject->globalExec();
94 switch (m_type) {
95 case ErrorNone:
96 return nullptr;
97 case SyntaxError:
98 return addErrorInfo(
99 exec,
100 createSyntaxError(exec, m_message),
101 overrideLineNumber == -1 ? m_line : overrideLineNumber, source);
102 case EvalError:
103 return createSyntaxError(exec, m_message);
104 case StackOverflow: {
105 ErrorHandlingScope errorScope(globalObject->vm());
106 return createStackOverflowError(exec);
107 }
108 case OutOfMemory:
109 return createOutOfMemoryError(exec);
110 }
111 CRASH();
112 return nullptr;
113 }
114
115private:
116 JSToken m_token;
117 String m_message;
118 int m_line { -1 };
119 ErrorType m_type;
120 SyntaxErrorType m_syntaxErrorType;
121};
122
123} // namespace JSC
124
125namespace WTF {
126
127inline void printInternal(PrintStream& out, JSC::ParserError::SyntaxErrorType type)
128{
129 switch (type) {
130 case JSC::ParserError::SyntaxErrorNone:
131 out.print("SyntaxErrorNone");
132 return;
133 case JSC::ParserError::SyntaxErrorIrrecoverable:
134 out.print("SyntaxErrorIrrecoverable");
135 return;
136 case JSC::ParserError::SyntaxErrorUnterminatedLiteral:
137 out.print("SyntaxErrorUnterminatedLiteral");
138 return;
139 case JSC::ParserError::SyntaxErrorRecoverable:
140 out.print("SyntaxErrorRecoverable");
141 return;
142 }
143
144 RELEASE_ASSERT_NOT_REACHED();
145}
146
147inline void printInternal(PrintStream& out, JSC::ParserError::ErrorType type)
148{
149 switch (type) {
150 case JSC::ParserError::ErrorNone:
151 out.print("ErrorNone");
152 return;
153 case JSC::ParserError::StackOverflow:
154 out.print("StackOverflow");
155 return;
156 case JSC::ParserError::EvalError:
157 out.print("EvalError");
158 return;
159 case JSC::ParserError::OutOfMemory:
160 out.print("OutOfMemory");
161 return;
162 case JSC::ParserError::SyntaxError:
163 out.print("SyntaxError");
164 return;
165 }
166
167 RELEASE_ASSERT_NOT_REACHED();
168}
169
170} // namespace WTF
171