The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / parser.h @ 40c0f755

History | View | Annotate | Download (6.67 KB)

1 40c0f755 Ryan
// Copyright 2006-2008 the V8 project authors. 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
#include "allocation.h"
33
34
namespace v8 { namespace internal {
35
36
37
class ParserMessage : public Malloced {
38
 public:
39
  ParserMessage(Scanner::Location loc, const char* message,
40
                Vector<const char*> args)
41
      : loc_(loc),
42
        message_(message),
43
        args_(args) { }
44
  ~ParserMessage();
45
  Scanner::Location location() { return loc_; }
46
  const char* message() { return message_; }
47
  Vector<const char*> args() { return args_; }
48
 private:
49
  Scanner::Location loc_;
50
  const char* message_;
51
  Vector<const char*> args_;
52
};
53
54
55
class FunctionEntry BASE_EMBEDDED {
56
 public:
57
  explicit FunctionEntry(Vector<unsigned> backing) : backing_(backing) { }
58
  FunctionEntry() : backing_(Vector<unsigned>::empty()) { }
59
60
  int start_pos() { return backing_[kStartPosOffset]; }
61
  void set_start_pos(int value) { backing_[kStartPosOffset] = value; }
62
63
  int end_pos() { return backing_[kEndPosOffset]; }
64
  void set_end_pos(int value) { backing_[kEndPosOffset] = value; }
65
66
  int literal_count() { return backing_[kLiteralCountOffset]; }
67
  void set_literal_count(int value) { backing_[kLiteralCountOffset] = value; }
68
69
  int property_count() { return backing_[kPropertyCountOffset]; }
70
  void set_property_count(int value) { backing_[kPropertyCountOffset] = value; }
71
72
  bool contains_array_literal() {
73
    return backing_[kContainsArrayLiteralOffset] != 0;
74
  }
75
  void set_contains_array_literal(bool value) {
76
    backing_[kContainsArrayLiteralOffset] = value ? 1 : 0;
77
  }
78
79
  bool is_valid() { return backing_.length() > 0; }
80
81
  static const int kSize = 5;
82
83
 private:
84
  Vector<unsigned> backing_;
85
  static const int kStartPosOffset = 0;
86
  static const int kEndPosOffset = 1;
87
  static const int kLiteralCountOffset = 2;
88
  static const int kPropertyCountOffset = 3;
89
  static const int kContainsArrayLiteralOffset = 4;
90
};
91
92
93
class ScriptDataImpl : public ScriptData {
94
 public:
95
  explicit ScriptDataImpl(Vector<unsigned> store)
96
      : store_(store),
97
        last_entry_(0) { }
98
  virtual ~ScriptDataImpl();
99
  virtual int Length();
100
  virtual unsigned* Data();
101
  FunctionEntry GetFunctionEnd(int start);
102
  bool SanityCheck();
103
104
  Scanner::Location MessageLocation();
105
  const char* BuildMessage();
106
  Vector<const char*> BuildArgs();
107
108
  bool has_error() { return store_[kHasErrorOffset]; }
109
  unsigned magic() { return store_[kMagicOffset]; }
110
  unsigned version() { return store_[kVersionOffset]; }
111
112
  static const unsigned kMagicNumber = 0xBadDead;
113
  static const unsigned kCurrentVersion = 1;
114
115
  static const unsigned kMagicOffset = 0;
116
  static const unsigned kVersionOffset = 1;
117
  static const unsigned kHasErrorOffset = 2;
118
  static const unsigned kSizeOffset = 3;
119
  static const unsigned kHeaderSize = 4;
120
121
 private:
122
  unsigned Read(int position);
123
  unsigned* ReadAddress(int position);
124
  int EntryCount();
125
  FunctionEntry nth(int n);
126
127
  Vector<unsigned> store_;
128
129
  // The last entry returned.  This is used to make lookup faster:
130
  // the next entry to return is typically the next entry so lookup
131
  // will usually be much faster if we start from the last entry.
132
  int last_entry_;
133
};
134
135
136
// The parser: Takes a script and and context information, and builds a
137
// FunctionLiteral AST node. Returns NULL and deallocates any allocated
138
// AST nodes if parsing failed.
139
FunctionLiteral* MakeAST(bool compile_in_global_context,
140
                         Handle<Script> script,
141
                         v8::Extension* extension,
142
                         ScriptDataImpl* pre_data);
143
144
145
ScriptDataImpl* PreParse(unibrow::CharacterStream* stream,
146
                         v8::Extension* extension);
147
148
149
bool ParseRegExp(FlatStringReader* input,
150
                 bool multiline,
151
                 RegExpCompileData* result);
152
153
154
// Support for doing lazy compilation. The script is the script containing full
155
// source of the script where the function is declared. The start_position and
156
// end_position specifies the part of the script source which has the source
157
// for the function declaration in the form:
158
//
159
//    (<formal parameters>) { <function body> }
160
//
161
// without any function keyword or name.
162
//
163
FunctionLiteral* MakeLazyAST(Handle<Script> script,
164
                             Handle<String> name,
165
                             int start_position,
166
                             int end_position,
167
                             bool is_expression);
168
169
170
// Support for handling complex values (array and object literals) that
171
// can be fully handled at compile time.
172
class CompileTimeValue: public AllStatic {
173
 public:
174
  enum Type {
175
    OBJECT_LITERAL,
176
    ARRAY_LITERAL
177
  };
178
179
  static bool IsCompileTimeValue(Expression* expression);
180
181
  // Get the value as a compile time value.
182
  static Handle<FixedArray> GetValue(Expression* expression);
183
184
  // Get the type of a compile time value returned by GetValue().
185
  static Type GetType(Handle<FixedArray> value);
186
187
  // Get the elements array of a compile time value returned by GetValue().
188
  static Handle<FixedArray> GetElements(Handle<FixedArray> value);
189
190
 private:
191
  static const int kTypeSlot = 0;
192
  static const int kElementsSlot = 1;
193
194
  DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue);
195
};
196
197
198
} }  // namespace v8::internal
199
200
#endif  // V8_PARSER_H_