source: webkit/trunk/JavaScriptCore/parser/Parser.h@ 47412

Last change on this file since 47412 was 47307, checked in by [email protected], 16 years ago
  • parser/Parser.h:

(JSC::EvalExecutable::parse):
(JSC::ProgramExecutable::parse):

  • runtime/Executable.h:

Reviewed by NOBODY (build fix).

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1/*
2 * Copyright (C) 1999-2001 Harri Porten ([email protected])
3 * Copyright (C) 2001 Peter Kelly ([email protected])
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 "Executable.h"
28#include "JSGlobalObject.h"
29#include "Nodes.h"
30#include "SourceProvider.h"
31#include <wtf/Forward.h>
32#include <wtf/Noncopyable.h>
33#include <wtf/OwnPtr.h>
34#include <wtf/RefPtr.h>
35
36namespace JSC {
37
38 class FunctionBodyNode;
39 class ProgramNode;
40 class UString;
41
42 template <typename T> struct ParserArenaData : ParserArenaDeletable { T data; };
43
44 class Parser : public Noncopyable {
45 public:
46 template <class ParsedNode>
47 PassRefPtr<ParsedNode> parse(ExecState*, Debugger*, const SourceCode&, int* errLine = 0, UString* errMsg = 0);
48 template <class ParsedNode>
49 PassRefPtr<ParsedNode> reparse(JSGlobalData*, ParsedNode*);
50 void reparseInPlace(JSGlobalData*, FunctionBodyNode*);
51 PassRefPtr<FunctionBodyNode> parseFunctionFromGlobalCode(ExecState*, Debugger*, const SourceCode&, int* errLine = 0, UString* errMsg = 0);
52
53 void didFinishParsing(SourceElements*, ParserArenaData<DeclarationStacks::VarStack>*,
54 ParserArenaData<DeclarationStacks::FunctionStack>*, CodeFeatures features, int lastLine, int numConstants);
55
56 ParserArena& arena() { return m_arena; }
57
58 private:
59 void parse(JSGlobalData*, int* errLine, UString* errMsg);
60
61 ParserArena m_arena;
62 const SourceCode* m_source;
63 SourceElements* m_sourceElements;
64 ParserArenaData<DeclarationStacks::VarStack>* m_varDeclarations;
65 ParserArenaData<DeclarationStacks::FunctionStack>* m_funcDeclarations;
66 CodeFeatures m_features;
67 int m_lastLine;
68 int m_numConstants;
69 };
70
71 template <class ParsedNode>
72 PassRefPtr<ParsedNode> Parser::parse(ExecState* exec, Debugger* debugger, const SourceCode& source, int* errLine, UString* errMsg)
73 {
74 m_source = &source;
75 parse(&exec->globalData(), errLine, errMsg);
76 RefPtr<ParsedNode> result;
77 if (m_sourceElements) {
78 result = ParsedNode::create(&exec->globalData(),
79 m_sourceElements,
80 m_varDeclarations ? &m_varDeclarations->data : 0,
81 m_funcDeclarations ? &m_funcDeclarations->data : 0,
82 *m_source,
83 m_features,
84 m_numConstants);
85 result->setLoc(m_source->firstLine(), m_lastLine);
86 }
87
88 m_arena.reset();
89
90 m_source = 0;
91 m_varDeclarations = 0;
92 m_funcDeclarations = 0;
93
94 if (debugger)
95 debugger->sourceParsed(exec, source, *errLine, *errMsg);
96 return result.release();
97 }
98
99 template <class ParsedNode>
100 PassRefPtr<ParsedNode> Parser::reparse(JSGlobalData* globalData, ParsedNode* oldParsedNode)
101 {
102 m_source = &oldParsedNode->source();
103 parse(globalData, 0, 0);
104 RefPtr<ParsedNode> result;
105 if (m_sourceElements) {
106 result = ParsedNode::create(globalData,
107 m_sourceElements,
108 m_varDeclarations ? &m_varDeclarations->data : 0,
109 m_funcDeclarations ? &m_funcDeclarations->data : 0,
110 *m_source,
111 oldParsedNode->features(),
112 m_numConstants);
113 result->setLoc(m_source->firstLine(), m_lastLine);
114 }
115
116 m_arena.reset();
117
118 m_source = 0;
119 m_varDeclarations = 0;
120 m_funcDeclarations = 0;
121
122 return result.release();
123 }
124
125 inline PassRefPtr<FunctionBodyNode> Parser::parseFunctionFromGlobalCode(ExecState* exec, Debugger* debugger, const SourceCode& source, int* errLine, UString* errMsg)
126 {
127 RefPtr<ProgramNode> program = parse<ProgramNode>(exec, debugger, source, errLine, errMsg);
128
129 if (!program)
130 return 0;
131
132 StatementVector& children = program->children();
133 if (children.size() != 1)
134 return 0;
135
136 StatementNode* exprStatement = children[0];
137 ASSERT(exprStatement);
138 ASSERT(exprStatement->isExprStatement());
139 if (!exprStatement || !exprStatement->isExprStatement())
140 return 0;
141
142 ExpressionNode* funcExpr = static_cast<ExprStatementNode*>(exprStatement)->expr();
143 ASSERT(funcExpr);
144 ASSERT(funcExpr->isFuncExprNode());
145 if (!funcExpr || !funcExpr->isFuncExprNode())
146 return 0;
147
148 RefPtr<FunctionBodyNode> body = static_cast<FuncExprNode*>(funcExpr)->body();
149 ASSERT(body);
150 return body.release();
151 }
152
153 inline JSObject* EvalExecutable::parse(ExecState* exec, bool allowDebug)
154 {
155 int errLine;
156 UString errMsg;
157 m_node = exec->globalData().parser->parse<EvalNode>(exec, allowDebug ? exec->dynamicGlobalObject()->debugger() : 0, m_source, &errLine, &errMsg);
158 if (!m_node)
159 return Error::create(exec, SyntaxError, errMsg, errLine, m_source.provider()->asID(), m_source.provider()->url());
160 return 0;
161 }
162
163 inline JSObject* ProgramExecutable::parse(ExecState* exec, bool allowDebug)
164 {
165 int errLine;
166 UString errMsg;
167 m_node = exec->globalData().parser->parse<ProgramNode>(exec, allowDebug ? exec->dynamicGlobalObject()->debugger() : 0, m_source, &errLine, &errMsg);
168 if (!m_node)
169 return Error::create(exec, SyntaxError, errMsg, errLine, m_source.provider()->asID(), m_source.provider()->url());
170 return 0;
171 }
172
173} // namespace JSC
174
175#endif // Parser_h
Note: See TracBrowser for help on using the repository browser.