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

Last change on this file since 28595 was 28595, checked in by [email protected], 17 years ago

Reviewed by Sam Weinig.


Merged different implementations of Parser::parse into a single,
templatized implementation, in preparation for adding yet another
implementation for "eval" code.


JS and layout tests pass.

  • Property svn:eol-style set to native
File size: 2.6 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, 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#include "config.h"
26#include "Parser.h"
27
28#include "lexer.h"
29#include <wtf/HashSet.h>
30#include <wtf/Vector.h>
31
32extern int kjsyyparse();
33
34namespace KJS {
35
36Parser::Parser()
37 : m_sourceId(0)
38{
39}
40
41template <class ParsedNode>
42PassRefPtr<ParsedNode> Parser::parse(const UString& sourceURL, int startingLineNumber,
43 const UChar* code, unsigned length,
44 int* sourceId, int* errLine, UString* errMsg)
45{
46 m_sourceURL = sourceURL;
47 parse(startingLineNumber, code, length, sourceId, errLine, errMsg);
48 if (!m_sourceElements) {
49 m_sourceURL = UString();
50 return 0;
51 }
52 RefPtr<ParsedNode> node = new ParsedNode(m_sourceElements.release());
53 m_sourceURL = UString();
54 node->setLoc(startingLineNumber, m_lastLine);
55 return node.release();
56}
57
58void Parser::parse(int startingLineNumber,
59 const UChar* code, unsigned length,
60 int* sourceId, int* errLine, UString* errMsg)
61{
62 ASSERT(!m_sourceElements);
63
64 if (errLine)
65 *errLine = -1;
66 if (errMsg)
67 *errMsg = 0;
68
69 Lexer& lexer = KJS::lexer();
70
71 lexer.setCode(startingLineNumber, code, length);
72 m_sourceId++;
73 if (sourceId)
74 *sourceId = m_sourceId;
75
76 int parseError = kjsyyparse();
77 bool lexError = lexer.sawError();
78 lexer.clear();
79
80 Node::clearNewNodes();
81
82 if (parseError || lexError) {
83 if (errLine)
84 *errLine = lexer.lineNo();
85 if (errMsg)
86 *errMsg = "Parse error";
87 m_sourceElements.clear();
88 }
89}
90
91Parser& parser()
92{
93 ASSERT(JSLock::currentThreadIsHoldingLock());
94
95 static Parser& staticParser = *new Parser;
96 return staticParser;
97}
98
99} // namespace KJS
Note: See TracBrowser for help on using the repository browser.