blob: a96d0ba89a45d89aacba142c1143c92ca30e6227 [file] [log] [blame]
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001// Copyright 2006-2008 Google Inc. All Rights Reserved.
2// 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
71 bool is_valid() { return backing_.length() > 0; }
72
73 static const int kSize = 4;
74
75 private:
76 Vector<unsigned> backing_;
77 static const int kStartPosOffset = 0;
78 static const int kEndPosOffset = 1;
79 static const int kLiteralCountOffset = 2;
80 static const int kPropertyCountOffset = 3;
81};
82
83
84class ScriptDataImpl : public ScriptData {
85 public:
86 explicit ScriptDataImpl(Vector<unsigned> store)
87 : store_(store),
88 last_entry_(0) { }
89 virtual ~ScriptDataImpl();
90 virtual int Length();
91 virtual unsigned* Data();
92 FunctionEntry GetFunctionEnd(int start);
93 bool SanityCheck();
94
95 Scanner::Location MessageLocation();
96 const char* BuildMessage();
97 Vector<const char*> BuildArgs();
98
99 bool has_error() { return store_[kHasErrorOffset]; }
100 unsigned magic() { return store_[kMagicOffset]; }
101 unsigned version() { return store_[kVersionOffset]; }
102
103 static const unsigned kMagicNumber = 0xBadDead;
104 static const unsigned kCurrentVersion = 1;
105
106 static const unsigned kMagicOffset = 0;
107 static const unsigned kVersionOffset = 1;
108 static const unsigned kHasErrorOffset = 2;
109 static const unsigned kSizeOffset = 3;
110 static const unsigned kHeaderSize = 4;
111
112 private:
113 unsigned Read(int position);
114 unsigned* ReadAddress(int position);
115 int EntryCount();
116 FunctionEntry nth(int n);
117
118 Vector<unsigned> store_;
119
120 // The last entry returned. This is used to make lookup faster:
121 // the next entry to return is typically the next entry so lookup
122 // will usually be much faster if we start from the last entry.
123 int last_entry_;
124};
125
126
127// The parser: Takes a script and and context information, and builds a
128// FunctionLiteral AST node. Returns NULL and deallocates any allocated
129// AST nodes if parsing failed.
130FunctionLiteral* MakeAST(bool compile_in_global_context,
131 Handle<Script> script,
132 v8::Extension* extension,
133 ScriptDataImpl* pre_data);
134
135
136ScriptDataImpl* PreParse(unibrow::CharacterStream* stream,
137 v8::Extension* extension);
138
139
140// Support for doing lazy compilation. The script is the script containing full
141// source of the script where the function is declared. The start_position and
142// end_position specifies the part of the script source which has the source
143// for the function decleration in the form:
144//
145// (<formal parameters>) { <function body> }
146//
147// without any function keyword or name.
148//
149FunctionLiteral* MakeLazyAST(Handle<Script> script,
150 Handle<String> name,
151 int start_position,
152 int end_position,
153 bool is_expression);
154
155} } // namespace v8::internal
156
157#endif // V8_PARSER_H_