2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
27 #include "ExceptionHelpers.h"
28 #include "Executable.h"
29 #include "JSGlobalObject.h"
32 #include "ParserArena.h"
33 #include "SourceProvider.h"
34 #include <wtf/Forward.h>
35 #include <wtf/Noncopyable.h>
36 #include <wtf/OwnPtr.h>
37 #include <wtf/RefPtr.h>
41 class FunctionBodyNode;
46 template <typename T> inline bool isEvalNode() { return false; }
47 template <> inline bool isEvalNode<EvalNode>() { return true; }
48 template <typename T> struct ParserArenaData : ParserArenaDeletable { T data; };
51 WTF_MAKE_NONCOPYABLE(Parser); WTF_MAKE_FAST_ALLOCATED;
54 template <class ParsedNode>
55 PassRefPtr<ParsedNode> parse(JSGlobalObject* lexicalGlobalObject, Debugger*, ExecState*, const SourceCode& source, FunctionParameters*, JSParserStrictness strictness, JSObject** exception);
57 void didFinishParsing(SourceElements*, ParserArenaData<DeclarationStacks::VarStack>*,
58 ParserArenaData<DeclarationStacks::FunctionStack>*, CodeFeatures features,
59 int lastLine, int numConstants, IdentifierSet&);
61 ParserArena& arena() { return m_arena; }
64 void parse(JSGlobalData*, FunctionParameters*, JSParserStrictness strictness, JSParserMode mode, int* errLine, UString* errMsg);
66 // Used to determine type of error to report.
67 bool isFunctionBodyNode(ScopeNode*) { return false; }
68 bool isFunctionBodyNode(FunctionBodyNode*) { return true; }
71 const SourceCode* m_source;
72 SourceElements* m_sourceElements;
73 ParserArenaData<DeclarationStacks::VarStack>* m_varDeclarations;
74 ParserArenaData<DeclarationStacks::FunctionStack>* m_funcDeclarations;
75 IdentifierSet m_capturedVariables;
76 CodeFeatures m_features;
81 template <class ParsedNode>
82 PassRefPtr<ParsedNode> Parser::parse(JSGlobalObject* lexicalGlobalObject, Debugger* debugger, ExecState* debuggerExecState, const SourceCode& source, FunctionParameters* parameters, JSParserStrictness strictness, JSObject** exception)
84 ASSERT(lexicalGlobalObject);
85 ASSERT(exception && !*exception);
90 if (ParsedNode::scopeIsFunction)
91 lexicalGlobalObject->globalData().lexer->setIsReparsing();
92 parse(&lexicalGlobalObject->globalData(), parameters, strictness, ParsedNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, &errLine, &errMsg);
94 RefPtr<ParsedNode> result;
95 if (m_sourceElements) {
96 result = ParsedNode::create(&lexicalGlobalObject->globalData(),
98 m_varDeclarations ? &m_varDeclarations->data : 0,
99 m_funcDeclarations ? &m_funcDeclarations->data : 0,
104 result->setLoc(m_source->firstLine(), m_lastLine);
105 } else if (lexicalGlobalObject) {
106 // We can never see a syntax error when reparsing a function, since we should have
107 // reported the error when parsing the containing program or eval code. So if we're
108 // parsing a function body node, we assume that what actually happened here is that
109 // we ran out of stack while parsing. If we see an error while parsing eval or program
110 // code we assume that it was a syntax error since running out of stack is much less
111 // likely, and we are currently unable to distinguish between the two cases.
112 if (isFunctionBodyNode(static_cast<ParsedNode*>(0)))
113 *exception = createStackOverflowError(lexicalGlobalObject);
114 else if (isEvalNode<ParsedNode>())
115 *exception = createSyntaxError(lexicalGlobalObject, errMsg);
117 *exception = addErrorInfo(&lexicalGlobalObject->globalData(), createSyntaxError(lexicalGlobalObject, errMsg), errLine, source);
123 m_sourceElements = 0;
124 m_varDeclarations = 0;
125 m_funcDeclarations = 0;
127 if (debugger && !ParsedNode::scopeIsFunction)
128 debugger->sourceParsed(debuggerExecState, source.provider(), errLine, errMsg);
129 return result.release();