1 // -*- c-basic-offset: 4 -*-
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
5 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
6 * Copyright (C) 2003, 2006, 2007 Apple Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
28 #include <wtf/Forward.h>
29 #include <wtf/Noncopyable.h>
30 #include <wtf/RefPtr.h>
31 #include <wtf/Vector.h>
36 class FunctionBodyNode
;
45 * Parses ECMAScript source code and converts into ProgramNode objects, which
46 * represent the root of a parse tree. The tree is then semantically
47 * checked with a semantic analyzer. This class provides a convenient
48 * workaround for the problem of the bison parser working in a static context.
50 class Parser
: Noncopyable
{
52 PassRefPtr
<ProgramNode
> parseProgram(const UString
& sourceURL
, int startingLineNumber
,
53 const UChar
* code
, unsigned length
,
54 int* sourceId
= 0, int* errLine
= 0, UString
* errMsg
= 0);
56 PassRefPtr
<FunctionBodyNode
> parseFunctionBody(const UString
& sourceURL
, int startingLineNumber
,
57 const UChar
* code
, unsigned length
,
58 int* sourceId
= 0, int* errLine
= 0, UString
* errMsg
= 0);
60 int sourceId() { return m_sourceId
; }
62 void didFinishParsing(PassRefPtr
<ProgramNode
>);
64 static void noteNodeCycle(Node
*);
65 static void removeNodeCycle(Node
*);
67 // We keep track of various flags about the function body we're
68 // tracking on a stack; the FunctionBody ctor pops them off
69 // when we're done parsing and are making the body node.
70 void pushFunctionContext(unsigned initialFlags
);
71 void setFunctionFlags(unsigned newFlags
);
72 unsigned popFunctionContext();
75 friend Parser
& parser();
77 Parser(); // Use parser() instead.
78 void parse(const UString
& sourceURL
, int startingLineNumber
,
79 const UChar
* code
, unsigned length
,
80 int* sourceId
= 0, int* errLine
= 0, UString
* errMsg
= 0);
83 RefPtr
<ProgramNode
> m_progNode
;
84 WTF::Vector
<unsigned, 8> m_functionFlags
;
87 Parser
& parser(); // Returns the singleton JavaScript parser.
89 inline void Parser::pushFunctionContext(unsigned initialFlags
) {
90 m_functionFlags
.append(initialFlags
);
93 inline void Parser::setFunctionFlags(unsigned newFlags
) {
94 m_functionFlags
.last() |= newFlags
;
97 inline unsigned Parser::popFunctionContext() {
98 unsigned flags
= m_functionFlags
.last();
99 m_functionFlags
.removeLast();
106 // kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;