blob: 0ebef74eee879431d45ddb1ffd150621a6968e40 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_PARSER_H_
29#define V8_PARSER_H_
30
31#include "scanner.h"
32
33namespace v8 { namespace internal {
34
35
36class ParserMessage : public Malloced {
37 public:
38 ParserMessage(Scanner::Location loc, const char* message,
39 Vector<const char*> args)
40 : loc_(loc),
41 message_(message),
42 args_(args) { }
43 ~ParserMessage();
44 Scanner::Location location() { return loc_; }
45 const char* message() { return message_; }
46 Vector<const char*> args() { return args_; }
47 private:
48 Scanner::Location loc_;
49 const char* message_;
50 Vector<const char*> args_;
51};
52
53
54class FunctionEntry BASE_EMBEDDED {
55 public:
56 explicit FunctionEntry(Vector<unsigned> backing) : backing_(backing) { }
57 FunctionEntry() : backing_(Vector<unsigned>::empty()) { }
58
59 int start_pos() { return backing_[kStartPosOffset]; }
60 void set_start_pos(int value) { backing_[kStartPosOffset] = value; }
61
62 int end_pos() { return backing_[kEndPosOffset]; }
63 void set_end_pos(int value) { backing_[kEndPosOffset] = value; }
64
65 int literal_count() { return backing_[kLiteralCountOffset]; }
66 void set_literal_count(int value) { backing_[kLiteralCountOffset] = value; }
67
68 int property_count() { return backing_[kPropertyCountOffset]; }
69 void set_property_count(int value) { backing_[kPropertyCountOffset] = value; }
70
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000071 bool contains_array_literal() {
72 return backing_[kContainsArrayLiteralOffset] != 0;
73 }
74 void set_contains_array_literal(bool value) {
75 backing_[kContainsArrayLiteralOffset] = value ? 1 : 0;
76 }
77
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078 bool is_valid() { return backing_.length() > 0; }
79
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000080 static const int kSize = 5;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081
82 private:
83 Vector<unsigned> backing_;
84 static const int kStartPosOffset = 0;
85 static const int kEndPosOffset = 1;
86 static const int kLiteralCountOffset = 2;
87 static const int kPropertyCountOffset = 3;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000088 static const int kContainsArrayLiteralOffset = 4;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089};
90
91
92class ScriptDataImpl : public ScriptData {
93 public:
94 explicit ScriptDataImpl(Vector<unsigned> store)
95 : store_(store),
96 last_entry_(0) { }
97 virtual ~ScriptDataImpl();
98 virtual int Length();
99 virtual unsigned* Data();
100 FunctionEntry GetFunctionEnd(int start);
101 bool SanityCheck();
102
103 Scanner::Location MessageLocation();
104 const char* BuildMessage();
105 Vector<const char*> BuildArgs();
106
107 bool has_error() { return store_[kHasErrorOffset]; }
108 unsigned magic() { return store_[kMagicOffset]; }
109 unsigned version() { return store_[kVersionOffset]; }
110
111 static const unsigned kMagicNumber = 0xBadDead;
112 static const unsigned kCurrentVersion = 1;
113
114 static const unsigned kMagicOffset = 0;
115 static const unsigned kVersionOffset = 1;
116 static const unsigned kHasErrorOffset = 2;
117 static const unsigned kSizeOffset = 3;
118 static const unsigned kHeaderSize = 4;
119
120 private:
121 unsigned Read(int position);
122 unsigned* ReadAddress(int position);
123 int EntryCount();
124 FunctionEntry nth(int n);
125
126 Vector<unsigned> store_;
127
128 // The last entry returned. This is used to make lookup faster:
129 // the next entry to return is typically the next entry so lookup
130 // will usually be much faster if we start from the last entry.
131 int last_entry_;
132};
133
134
135// The parser: Takes a script and and context information, and builds a
136// FunctionLiteral AST node. Returns NULL and deallocates any allocated
137// AST nodes if parsing failed.
138FunctionLiteral* MakeAST(bool compile_in_global_context,
139 Handle<Script> script,
140 v8::Extension* extension,
141 ScriptDataImpl* pre_data);
142
143
144ScriptDataImpl* PreParse(unibrow::CharacterStream* stream,
145 v8::Extension* extension);
146
147
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000148bool ParseRegExp(FlatStringReader* input,
149 bool multiline,
150 RegExpParseResult* result);
151
152
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153// Support for doing lazy compilation. The script is the script containing full
154// source of the script where the function is declared. The start_position and
155// end_position specifies the part of the script source which has the source
156// for the function decleration in the form:
157//
158// (<formal parameters>) { <function body> }
159//
160// without any function keyword or name.
161//
162FunctionLiteral* MakeLazyAST(Handle<Script> script,
163 Handle<String> name,
164 int start_position,
165 int end_position,
166 bool is_expression);
167
168} } // namespace v8::internal
169
170#endif // V8_PARSER_H_