source: webkit/trunk/JavaScriptCore/kjs/Parser.cpp@ 13153

Last change on this file since 13153 was 13153, checked in by darin, 19 years ago

Reviewed by Maciej.

  • kjs/Parser.cpp: Added.
  • kjs/Parser.h: Added.
  • kjs/internal.cpp: Removed the Parser class.
  • kjs/internal.h: Ditto. Also removed unnecessary declarations of classes not used in this header.
  • kjs/nodes.h: Added an include of "Parser.h".
  • kjs/function.h: Added a declaration of FunctionBodyNode.
  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// -*- c-basic-offset: 4 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001 Harri Porten ([email protected])
5 * Copyright (C) 2001 Peter Kelly ([email protected])
6 * Copyright (C) 2003, 2006 Apple Computer, 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#include "config.h"
26#include "Parser.h"
27
28#include "lexer.h"
29#include "nodes.h"
30#include <kxmlcore/HashSet.h>
31#include <kxmlcore/Vector.h>
32
33extern int kjsyyparse();
34
35namespace KJS {
36
37int Parser::sid = 0;
38
39static RefPtr<ProgramNode>* progNode;
40static Vector<RefPtr<Node> >* newNodes;
41static HashSet<Node*>* nodeCycles;
42
43void Parser::saveNewNode(Node *node)
44{
45 if (!newNodes)
46 newNodes = new Vector<RefPtr<Node> >;
47 newNodes->append(node);
48}
49
50void Parser::noteNodeCycle(Node *node)
51{
52 if (!nodeCycles)
53 nodeCycles = new HashSet<Node*>;
54 nodeCycles->add(node);
55}
56
57void Parser::removeNodeCycle(Node *node)
58{
59 ASSERT(nodeCycles);
60 nodeCycles->remove(node);
61}
62
63static void clearNewNodes()
64{
65 if (nodeCycles) {
66 for (HashSet<Node*>::iterator it = nodeCycles->begin(); it != nodeCycles->end(); ++it)
67 (*it)->breakCycle();
68 delete nodeCycles;
69 nodeCycles = 0;
70 }
71
72 delete newNodes;
73 newNodes = 0;
74}
75
76PassRefPtr<ProgramNode> Parser::parse(const UString& sourceURL, int startingLineNumber,
77 const UChar* code, unsigned length,
78 int* sourceId, int* errLine, UString* errMsg)
79{
80 if (errLine)
81 *errLine = -1;
82 if (errMsg)
83 *errMsg = 0;
84 if (!progNode)
85 progNode = new RefPtr<ProgramNode>;
86
87 Lexer::curr()->setCode(sourceURL, startingLineNumber, code, length);
88 *progNode = 0;
89 sid++;
90 if (sourceId)
91 *sourceId = sid;
92
93 // Enable this and the #define YYDEBUG in grammar.y to debug a parse error
94 //extern int kjsyydebug;
95 //kjsyydebug=1;
96
97 int parseError = kjsyyparse();
98 bool lexError = Lexer::curr()->sawError();
99 Lexer::curr()->doneParsing();
100 PassRefPtr<ProgramNode> prog = progNode->release();
101 *progNode = 0;
102
103 clearNewNodes();
104
105 if (parseError || lexError) {
106 int eline = Lexer::curr()->lineNo();
107 if (errLine)
108 *errLine = eline;
109 if (errMsg)
110 *errMsg = "Parse error";
111 return 0;
112 }
113
114 return prog;
115}
116
117void Parser::accept(PassRefPtr<ProgramNode> prog)
118{
119 *progNode = prog;
120}
121
122}
Note: See TracBrowser for help on using the repository browser.