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 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.
26 #include "SourceProvider.h"
28 #include <wtf/Forward.h>
29 #include <wtf/Noncopyable.h>
30 #include <wtf/OwnPtr.h>
31 #include <wtf/RefPtr.h>
35 class FunctionBodyNode;
40 struct ParserRefCountedData : ParserRefCounted {
41 ParserRefCountedData(JSGlobalData* globalData)
42 : ParserRefCounted(globalData)
49 class Parser : Noncopyable {
51 template <class ParsedNode>
52 PassRefPtr<ParsedNode> parse(ExecState*, const UString& sourceURL, int startingLineNumber, PassRefPtr<SourceProvider> source,
53 int* sourceId = 0, int* errLine = 0, UString* errMsg = 0);
55 UString sourceURL() const { return m_sourceURL; }
56 int sourceId() const { return m_sourceId; }
58 void didFinishParsing(SourceElements*, ParserRefCountedData<DeclarationStacks::VarStack>*,
59 ParserRefCountedData<DeclarationStacks::FunctionStack>*, bool usesEval, bool needsClosure, int lastLine);
62 friend class JSGlobalData;
65 void parse(ExecState*, const UString& sourceURL, int startingLineNumber, PassRefPtr<SourceProvider> source,
66 int* sourceId, int* errLine, UString* errMsg);
70 RefPtr<SourceElements> m_sourceElements;
71 RefPtr<ParserRefCountedData<DeclarationStacks::VarStack> > m_varDeclarations;
72 RefPtr<ParserRefCountedData<DeclarationStacks::FunctionStack> > m_funcDeclarations;
78 template <class ParsedNode>
79 PassRefPtr<ParsedNode> Parser::parse(ExecState* exec, const UString& sourceURL, int startingLineNumber, PassRefPtr<SourceProvider> source,
80 int* sourceId, int* errLine, UString* errMsg)
82 m_sourceURL = sourceURL;
83 RefPtr<SourceProvider> sourceProvider = source;
84 parse(exec, sourceURL, startingLineNumber, sourceProvider.get(), sourceId, errLine, errMsg);
85 if (!m_sourceElements) {
86 m_sourceURL = UString();
89 RefPtr<ParsedNode> node = ParsedNode::create(&exec->globalData(),
90 m_sourceElements.release().get(),
91 m_varDeclarations ? &m_varDeclarations->data : 0,
92 m_funcDeclarations ? &m_funcDeclarations->data : 0,
96 m_varDeclarations = 0;
97 m_funcDeclarations = 0;
98 m_sourceURL = UString();
99 node->setLoc(startingLineNumber, m_lastLine);
100 return node.release();