initial import
[vuplus_webkit] / Source / JavaScriptCore / parser / Parser.h
1 /*
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.
5  *
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.
10  *
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.
15  *
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.
20  *
21  */
22
23 #ifndef Parser_h
24 #define Parser_h
25
26 #include "Debugger.h"
27 #include "ExceptionHelpers.h"
28 #include "Executable.h"
29 #include "JSGlobalObject.h"
30 #include "Lexer.h"
31 #include "Nodes.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>
38
39 namespace JSC {
40
41     class FunctionBodyNode;
42     
43     class ProgramNode;
44     class UString;
45
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; };
49
50     class Parser {
51         WTF_MAKE_NONCOPYABLE(Parser); WTF_MAKE_FAST_ALLOCATED;
52     public:
53         Parser() { }
54         template <class ParsedNode>
55         PassRefPtr<ParsedNode> parse(JSGlobalObject* lexicalGlobalObject, Debugger*, ExecState*, const SourceCode& source, FunctionParameters*, JSParserStrictness strictness, JSObject** exception);
56
57         void didFinishParsing(SourceElements*, ParserArenaData<DeclarationStacks::VarStack>*, 
58                               ParserArenaData<DeclarationStacks::FunctionStack>*, CodeFeatures features,
59                               int lastLine, int numConstants, IdentifierSet&);
60
61         ParserArena& arena() { return m_arena; }
62
63     private:
64         void parse(JSGlobalData*, FunctionParameters*, JSParserStrictness strictness, JSParserMode mode, int* errLine, UString* errMsg);
65
66         // Used to determine type of error to report.
67         bool isFunctionBodyNode(ScopeNode*) { return false; }
68         bool isFunctionBodyNode(FunctionBodyNode*) { return true; }
69
70         ParserArena m_arena;
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;
77         int m_lastLine;
78         int m_numConstants;
79     };
80
81     template <class ParsedNode>
82     PassRefPtr<ParsedNode> Parser::parse(JSGlobalObject* lexicalGlobalObject, Debugger* debugger, ExecState* debuggerExecState, const SourceCode& source, FunctionParameters* parameters, JSParserStrictness strictness, JSObject** exception)
83     {
84         ASSERT(lexicalGlobalObject);
85         ASSERT(exception && !*exception);
86         int errLine;
87         UString errMsg;
88
89         m_source = &source;
90         if (ParsedNode::scopeIsFunction)
91             lexicalGlobalObject->globalData().lexer->setIsReparsing();
92         parse(&lexicalGlobalObject->globalData(), parameters, strictness, ParsedNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, &errLine, &errMsg);
93
94         RefPtr<ParsedNode> result;
95         if (m_sourceElements) {
96             result = ParsedNode::create(&lexicalGlobalObject->globalData(),
97                 m_sourceElements,
98                 m_varDeclarations ? &m_varDeclarations->data : 0,
99                 m_funcDeclarations ? &m_funcDeclarations->data : 0,
100                 m_capturedVariables,
101                 source,
102                 m_features,
103                 m_numConstants);
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);
116             else
117                 *exception = addErrorInfo(&lexicalGlobalObject->globalData(), createSyntaxError(lexicalGlobalObject, errMsg), errLine, source);
118         }
119
120         m_arena.reset();
121
122         m_source = 0;
123         m_sourceElements = 0;
124         m_varDeclarations = 0;
125         m_funcDeclarations = 0;
126
127         if (debugger && !ParsedNode::scopeIsFunction)
128             debugger->sourceParsed(debuggerExecState, source.provider(), errLine, errMsg);
129         return result.release();
130     }
131
132 } // namespace JSC
133
134 #endif // Parser_h