1// -*- c-basic-offset: 4 -*-
2/*
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.
7 *
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.
12 *
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.
17 *
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.
22 *
23 */
24
25#ifndef Parser_h
26#define Parser_h
27
28#include <wtf/Forward.h>
29#include <wtf/Noncopyable.h>
30#include <wtf/RefPtr.h>
31#include <wtf/Vector.h>
32
33namespace KJS {
34
35 class Node;
36 class FunctionBodyNode;
37 class ProgramNode;
38 class UString;
39
40 struct UChar;
41
42 /**
43 * @internal
44 *
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.
49 */
50 class Parser : Noncopyable {
51 public:
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);
55
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);
59
60 int sourceId() { return m_sourceId; }
61
62 void didFinishParsing(PassRefPtr<ProgramNode>);
63
64 static void noteNodeCycle(Node*);
65 static void removeNodeCycle(Node*);
66
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();
73
74 private:
75 friend Parser& parser();
76
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);
81
82 int m_sourceId;
83 RefPtr<ProgramNode> m_progNode;
84 WTF::Vector<unsigned, 8> m_functionFlags;
85 };
86
87 Parser& parser(); // Returns the singleton JavaScript parser.
88
89 inline void Parser::pushFunctionContext(unsigned initialFlags) {
90 m_functionFlags.append(initialFlags);
91 }
92
93 inline void Parser::setFunctionFlags(unsigned newFlags) {
94 m_functionFlags.last() |= newFlags;
95 }
96
97 inline unsigned Parser::popFunctionContext() {
98 unsigned flags = m_functionFlags.last();
99 m_functionFlags.removeLast();
100 return flags;
101 }
102
103} // namespace KJS
104
105#endif // Parser_h
106// kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;
107