Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(457)

Side by Side Diff: regexp2000/src/jsregexp.cc

Issue 8760: Experimental regexp: Use new RegExp parser to test syntax. (Closed)
Patch Set: Parses regexp using new parser. Rebased patch. Created 12 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « regexp2000/src/ast.cc ('k') | regexp2000/src/objects.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 21 matching lines...) Expand all
32 #include "ast.h" 32 #include "ast.h"
33 #include "execution.h" 33 #include "execution.h"
34 #include "factory.h" 34 #include "factory.h"
35 #include "jsregexp.h" 35 #include "jsregexp.h"
36 #include "third_party/jscre/pcre.h" 36 #include "third_party/jscre/pcre.h"
37 #include "platform.h" 37 #include "platform.h"
38 #include "runtime.h" 38 #include "runtime.h"
39 #include "top.h" 39 #include "top.h"
40 #include "compilation-cache.h" 40 #include "compilation-cache.h"
41 #include "string-stream.h" 41 #include "string-stream.h"
42 42 #include "parser.h"
43 43
44 namespace v8 { namespace internal { 44 namespace v8 { namespace internal {
45 45
46 46
47 #define CAPTURE_INDEX 0 47 #define CAPTURE_INDEX 0
48 #define INTERNAL_INDEX 1 48 #define INTERNAL_INDEX 1
49 49
50 static Failure* malloc_failure; 50 static Failure* malloc_failure;
51 51
52 static void* JSREMalloc(size_t size) { 52 static void* JSREMalloc(size_t size) {
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 break; 161 break;
162 case 'm': 162 case 'm':
163 flags |= JSRegExp::MULTILINE; 163 flags |= JSRegExp::MULTILINE;
164 break; 164 break;
165 } 165 }
166 } 166 }
167 return JSRegExp::Flags(flags); 167 return JSRegExp::Flags(flags);
168 } 168 }
169 169
170 170
171 static inline Handle<Object> CreateRegExpException(Handle<JSRegExp> re,
172 Handle<String> pattern,
173 Handle<String> error_text,
174 const char* message) {
175 Handle<JSArray> array = Factory::NewJSArray(2);
176 SetElement(array, 0, pattern);
177 SetElement(array, 1, error_text);
178 Handle<Object> regexp_err = Factory::NewSyntaxError(message, array);
179 return Handle<Object>(Top::Throw(*regexp_err));
180 }
181
182
171 unibrow::Predicate<unibrow::RegExpSpecialChar, 128> is_reg_exp_special_char; 183 unibrow::Predicate<unibrow::RegExpSpecialChar, 128> is_reg_exp_special_char;
172 184
173
174 Handle<Object> RegExpImpl::Compile(Handle<JSRegExp> re, 185 Handle<Object> RegExpImpl::Compile(Handle<JSRegExp> re,
175 Handle<String> pattern, 186 Handle<String> pattern,
176 Handle<String> flag_str) { 187 Handle<String> flag_str) {
177 JSRegExp::Flags flags = RegExpFlagsFromString(flag_str); 188 JSRegExp::Flags flags = RegExpFlagsFromString(flag_str);
178 Handle<FixedArray> cached = CompilationCache::LookupRegExp(pattern, flags); 189 Handle<FixedArray> cached = CompilationCache::LookupRegExp(pattern, flags);
179 bool in_cache = !cached.is_null(); 190 bool in_cache = !cached.is_null();
180 Handle<Object> result; 191 Handle<Object> result;
181 if (in_cache) { 192 if (in_cache) {
182 re->set_data(*cached); 193 re->set_data(*cached);
183 result = re; 194 result = re;
184 } else { 195 } else {
185 bool is_atom = !flags.is_ignore_case(); 196 SafeStringInputBuffer buffer(pattern.location());
186 for (int i = 0; is_atom && i < pattern->length(); i++) { 197 Handle<String> error_text;
187 if (is_reg_exp_special_char.get(pattern->Get(i))) 198 RegExpTree* ast = ParseRegExp(&buffer, &error_text);
188 is_atom = false; 199 if (!error_text.is_null()) {
200 // Throw an exception if we fail to parse the pattern.
201 return CreateRegExpException(re, pattern, error_text, "malformed_regexp");
189 } 202 }
190 if (is_atom) { 203
191 result = AtomCompile(re, pattern, flags); 204 RegExpAtom* atom = ast->AsAtom();
205 if (atom != NULL && !flags.is_ignore_case()) {
206 Vector<const uc16> atom_pattern = atom->data();
207 // Test if pattern equals atom_pattern and reuse pattern if it does.
208 Handle<String> atom_string = Factory::NewStringFromTwoByte(atom_pattern);
209 result = AtomCompile(re, atom_string, flags);
192 } else { 210 } else {
193 result = JsreCompile(re, pattern, flags); 211 result = JsreCompile(re, pattern, flags);
194 } 212 }
195 Object* data = re->data(); 213 Object* data = re->data();
196 if (data->IsFixedArray()) { 214 if (data->IsFixedArray()) {
197 // If compilation succeeded then the data is set on the regexp 215 // If compilation succeeded then the data is set on the regexp
198 // and we can store it in the cache. 216 // and we can store it in the cache.
199 Handle<FixedArray> data(FixedArray::cast(re->data())); 217 Handle<FixedArray> data(FixedArray::cast(re->data()));
200 CompilationCache::PutRegExp(pattern, flags, data); 218 CompilationCache::PutRegExp(pattern, flags, data);
201 } 219 }
(...skipping 29 matching lines...) Expand all
231 default: 249 default:
232 UNREACHABLE(); 250 UNREACHABLE();
233 return Handle<Object>(); 251 return Handle<Object>();
234 } 252 }
235 } 253 }
236 254
237 255
238 Handle<Object> RegExpImpl::AtomCompile(Handle<JSRegExp> re, 256 Handle<Object> RegExpImpl::AtomCompile(Handle<JSRegExp> re,
239 Handle<String> pattern, 257 Handle<String> pattern,
240 JSRegExp::Flags flags) { 258 JSRegExp::Flags flags) {
259 ASSERT(!flags.is_ignore_case());
241 Factory::SetRegExpData(re, JSRegExp::ATOM, pattern, flags, pattern); 260 Factory::SetRegExpData(re, JSRegExp::ATOM, pattern, flags, pattern);
242 return re; 261 return re;
243 } 262 }
244 263
245 264
246 Handle<Object> RegExpImpl::AtomExec(Handle<JSRegExp> re, 265 Handle<Object> RegExpImpl::AtomExec(Handle<JSRegExp> re,
247 Handle<String> subject, 266 Handle<String> subject,
248 Handle<Object> index) { 267 Handle<Object> index) {
249 Handle<String> needle(String::cast(re->DataAt(JSRegExp::kAtomPatternIndex))); 268 Handle<String> needle(String::cast(re->DataAt(JSRegExp::kAtomPatternIndex)));
250 269
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 index = end; 318 index = end;
300 if (needle_length == 0) index++; 319 if (needle_length == 0) index++;
301 } 320 }
302 return result; 321 return result;
303 } 322 }
304 323
305 324
306 Handle<Object> RegExpImpl::JsreCompile(Handle<JSRegExp> re, 325 Handle<Object> RegExpImpl::JsreCompile(Handle<JSRegExp> re,
307 Handle<String> pattern, 326 Handle<String> pattern,
308 JSRegExp::Flags flags) { 327 JSRegExp::Flags flags) {
328 // Change this to not compile immediately, but defer the compilation
329 // until the first execution.
309 JSRegExpIgnoreCaseOption case_option = flags.is_ignore_case() 330 JSRegExpIgnoreCaseOption case_option = flags.is_ignore_case()
310 ? JSRegExpIgnoreCase 331 ? JSRegExpIgnoreCase
311 : JSRegExpDoNotIgnoreCase; 332 : JSRegExpDoNotIgnoreCase;
312 JSRegExpMultilineOption multiline_option = flags.is_multiline() 333 JSRegExpMultilineOption multiline_option = flags.is_multiline()
313 ? JSRegExpMultiline 334 ? JSRegExpMultiline
314 : JSRegExpSingleLine; 335 : JSRegExpSingleLine;
315 336
316 Handle<String> two_byte_pattern = StringToTwoByte(pattern); 337 Handle<String> two_byte_pattern = StringToTwoByte(pattern);
317 338
318 unsigned number_of_captures; 339 unsigned number_of_captures;
(...skipping 702 matching lines...) Expand 10 before | Expand all | Expand 10 after
1021 template 1042 template
1022 bool RegExpEngine::Execute<const char>(RegExpNode<const char>* start, 1043 bool RegExpEngine::Execute<const char>(RegExpNode<const char>* start,
1023 Vector<const char> input); 1044 Vector<const char> input);
1024 1045
1025 template 1046 template
1026 bool RegExpEngine::Execute<const uc16>(RegExpNode<const uc16>* start, 1047 bool RegExpEngine::Execute<const uc16>(RegExpNode<const uc16>* start,
1027 Vector<const uc16> input); 1048 Vector<const uc16> input);
1028 1049
1029 1050
1030 }} // namespace v8::internal 1051 }} // namespace v8::internal
OLDNEW
« no previous file with comments | « regexp2000/src/ast.cc ('k') | regexp2000/src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698