fix logic
[personal-kdelibs.git] / kjs / Parser.cpp
blob049c947b3b7b337ad0801e3b8e39cb5d5ede8c88
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.
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.
25 #include "Parser.h"
26 #include <config.h>
28 #include "lexer.h"
29 #include "nodes.h"
30 #include <wtf/HashSet.h>
31 #include <wtf/Vector.h>
33 extern int kjsyyparse();
35 namespace KJS {
37 Parser::Parser()
38 : m_sourceId(0)
42 PassRefPtr<ProgramNode> Parser::parseProgram(const UString& sourceURL, int startingLineNumber,
43 const UChar* code, unsigned length,
44 int* sourceId, int* errLine, UString* errMsg)
46 parse(sourceURL, startingLineNumber, code, length, sourceId, errLine, errMsg);
47 return m_progNode.release();
50 static HashSet<Node*>* nodeCycles;
53 void Parser::noteNodeCycle(Node *node)
55 if (!nodeCycles)
56 nodeCycles = new HashSet<Node*>;
57 nodeCycles->add(node);
60 void Parser::removeNodeCycle(Node *node)
62 ASSERT(nodeCycles);
63 nodeCycles->remove(node);
66 static void clearNewNodes()
68 if (nodeCycles) {
69 for (HashSet<Node*>::iterator it = nodeCycles->begin(); it != nodeCycles->end(); ++it)
70 (*it)->breakCycle();
71 delete nodeCycles;
72 nodeCycles = 0;
74 Node::clearNewNodes();
77 PassRefPtr<FunctionBodyNode> Parser::parseFunctionBody(const UString& sourceURL, int startingLineNumber,
78 const UChar* code, unsigned length,
79 int* sourceId, int* errLine, UString* errMsg)
81 parse(sourceURL, startingLineNumber, code, length, sourceId, errLine, errMsg);
82 return m_progNode.release();
85 void Parser::parse(const UString& sourceURL, int startingLineNumber,
86 const UChar* code, unsigned length,
87 int* sourceId, int* errLine, UString* errMsg)
89 pushFunctionContext(0);
91 ASSERT(!m_progNode);
93 if (errLine)
94 *errLine = -1;
95 if (errMsg)
96 *errMsg = 0;
98 Lexer& lexer = KJS::lexer();
100 lexer.setCode(sourceURL, startingLineNumber, code, length);
101 m_sourceId++;
102 if (sourceId)
103 *sourceId = m_sourceId;
105 // Enable this and the #define YYDEBUG in grammar.y to debug a parse error
106 //extern int kjsyydebug;
107 //kjsyydebug=1;
109 int parseError = kjsyyparse();
111 bool lexError = lexer.sawError();
112 lexer.clear();
114 clearNewNodes();
116 if (parseError || lexError) {
117 if (errLine)
118 *errLine = lexer.lineNo();
119 if (errMsg)
120 *errMsg = "Parse error";
121 m_progNode = 0;
122 return;
125 #ifdef KJS_VERBOSE
126 fprintf( stderr, "%s\n", m_progNode->toString().ascii() );
127 #endif
130 void Parser::didFinishParsing(PassRefPtr<ProgramNode> progNode)
132 m_progNode = progNode;
135 Parser& parser()
137 // ASSERT(JSLock::currentThreadIsHoldingLock());
139 static Parser staticParser;
140 return staticParser;
143 } // namespace KJS