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.
30 #include <wtf/HashSet.h>
31 #include <wtf/Vector.h>
33 extern int kjsyyparse();
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
)
56 nodeCycles
= new HashSet
<Node
*>;
57 nodeCycles
->add(node
);
60 void Parser::removeNodeCycle(Node
*node
)
63 nodeCycles
->remove(node
);
66 static void clearNewNodes()
69 for (HashSet
<Node
*>::iterator it
= nodeCycles
->begin(); it
!= nodeCycles
->end(); ++it
)
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);
98 Lexer
& lexer
= KJS::lexer();
100 lexer
.setCode(sourceURL
, startingLineNumber
, code
, length
);
103 *sourceId
= m_sourceId
;
105 // Enable this and the #define YYDEBUG in grammar.y to debug a parse error
106 //extern int kjsyydebug;
109 int parseError
= kjsyyparse();
111 bool lexError
= lexer
.sawError();
116 if (parseError
|| lexError
) {
118 *errLine
= lexer
.lineNo();
120 *errMsg
= "Parse error";
126 fprintf( stderr
, "%s\n", m_progNode
->toString().ascii() );
130 void Parser::didFinishParsing(PassRefPtr
<ProgramNode
> progNode
)
132 m_progNode
= progNode
;
137 // ASSERT(JSLock::currentThreadIsHoldingLock());
139 static Parser staticParser
;