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
> struct ParserArenaData
: ParserArenaDeletable
{ T data
; };
49 WTF_MAKE_NONCOPYABLE(Parser
); WTF_MAKE_FAST_ALLOCATED
;
52 template <class ParsedNode
>
53 PassRefPtr
<ParsedNode
> parse(JSGlobalObject
* lexicalGlobalObject
, Debugger
*, ExecState
*, const SourceCode
& source
, FunctionParameters
*, JSParserStrictness strictness
, JSObject
** exception
);
55 void didFinishParsing(SourceElements
*, ParserArenaData
<DeclarationStacks::VarStack
>*,
56 ParserArenaData
<DeclarationStacks::FunctionStack
>*, CodeFeatures features
,
57 int lastLine
, int numConstants
, IdentifierSet
&);
59 ParserArena
& arena() { return m_arena
; }
62 void parse(JSGlobalData
*, FunctionParameters
*, JSParserStrictness strictness
, JSParserMode mode
, int* errLine
, UString
* errMsg
);
64 // Used to determine type of error to report.
65 bool isFunctionBodyNode(ScopeNode
*) { return false; }
66 bool isFunctionBodyNode(FunctionBodyNode
*) { return true; }
69 const SourceCode
* m_source
;
70 SourceElements
* m_sourceElements
;
71 ParserArenaData
<DeclarationStacks::VarStack
>* m_varDeclarations
;
72 ParserArenaData
<DeclarationStacks::FunctionStack
>* m_funcDeclarations
;
73 IdentifierSet m_capturedVariables
;
74 CodeFeatures m_features
;
79 template <class ParsedNode
>
80 PassRefPtr
<ParsedNode
> Parser::parse(JSGlobalObject
* lexicalGlobalObject
, Debugger
* debugger
, ExecState
* debuggerExecState
, const SourceCode
& source
, FunctionParameters
* parameters
, JSParserStrictness strictness
, JSObject
** exception
)
82 ASSERT(lexicalGlobalObject
);
83 ASSERT(exception
&& !*exception
);
88 if (ParsedNode::scopeIsFunction
)
89 lexicalGlobalObject
->globalData().lexer
->setIsReparsing();
90 parse(&lexicalGlobalObject
->globalData(), parameters
, strictness
, ParsedNode::isFunctionNode
? JSParseFunctionCode
: JSParseProgramCode
, &errLine
, &errMsg
);
92 RefPtr
<ParsedNode
> result
;
93 if (m_sourceElements
) {
94 result
= ParsedNode::create(&lexicalGlobalObject
->globalData(),
96 m_varDeclarations
? &m_varDeclarations
->data
: 0,
97 m_funcDeclarations
? &m_funcDeclarations
->data
: 0,
102 result
->setLoc(m_source
->firstLine(), m_lastLine
);
103 } else if (lexicalGlobalObject
) {
104 // We can never see a syntax error when reparsing a function, since we should have
105 // reported the error when parsing the containing program or eval code. So if we're
106 // parsing a function body node, we assume that what actually happened here is that
107 // we ran out of stack while parsing. If we see an error while parsing eval or program
108 // code we assume that it was a syntax error since running out of stack is much less
109 // likely, and we are currently unable to distinguish between the two cases.
110 if (isFunctionBodyNode(static_cast<ParsedNode
*>(0)))
111 *exception
= createStackOverflowError(lexicalGlobalObject
);
113 *exception
= addErrorInfo(&lexicalGlobalObject
->globalData(), createSyntaxError(lexicalGlobalObject
, errMsg
), errLine
, source
);
119 m_sourceElements
= 0;
120 m_varDeclarations
= 0;
121 m_funcDeclarations
= 0;
123 if (debugger
&& !ParsedNode::scopeIsFunction
)
124 debugger
->sourceParsed(debuggerExecState
, source
.provider(), errLine
, errMsg
);
125 return result
.release();