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

Side by Side Diff: src/ast.h

Issue 14190: * Added min/max match length to irregexp ast (Closed)
Patch Set: Addressed review comments Created 12 years 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 | « no previous file | src/ast.cc » ('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 1195 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 virtual void Accept(AstVisitor* v); 1206 virtual void Accept(AstVisitor* v);
1207 }; 1207 };
1208 1208
1209 1209
1210 // ---------------------------------------------------------------------------- 1210 // ----------------------------------------------------------------------------
1211 // Regular expressions 1211 // Regular expressions
1212 1212
1213 1213
1214 class RegExpTree: public ZoneObject { 1214 class RegExpTree: public ZoneObject {
1215 public: 1215 public:
1216 static const int kInfinity = (1<<31)-1;
1216 virtual ~RegExpTree() { } 1217 virtual ~RegExpTree() { }
1217 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0; 1218 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
1218 virtual RegExpNode* ToNode(RegExpCompiler* compiler, 1219 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1219 RegExpNode* on_success) = 0; 1220 RegExpNode* on_success) = 0;
1220 virtual bool IsTextElement() { return false; } 1221 virtual bool IsTextElement() { return false; }
1222 virtual int min_match() = 0;
1223 virtual int max_match() = 0;
1221 virtual void AppendToText(RegExpText* text); 1224 virtual void AppendToText(RegExpText* text);
1222 SmartPointer<const char> ToString(); 1225 SmartPointer<const char> ToString();
1223 #define MAKE_ASTYPE(Name) \ 1226 #define MAKE_ASTYPE(Name) \
1224 virtual RegExp##Name* As##Name(); \ 1227 virtual RegExp##Name* As##Name(); \
1225 virtual bool Is##Name(); 1228 virtual bool Is##Name();
1226 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE) 1229 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
1227 #undef MAKE_ASTYPE 1230 #undef MAKE_ASTYPE
1228 }; 1231 };
1229 1232
1230 1233
1231 class RegExpDisjunction: public RegExpTree { 1234 class RegExpDisjunction: public RegExpTree {
1232 public: 1235 public:
1233 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives) 1236 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
1234 : alternatives_(alternatives) { }
1235 virtual void* Accept(RegExpVisitor* visitor, void* data); 1237 virtual void* Accept(RegExpVisitor* visitor, void* data);
1236 virtual RegExpNode* ToNode(RegExpCompiler* compiler, 1238 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1237 RegExpNode* on_success); 1239 RegExpNode* on_success);
1238 virtual RegExpDisjunction* AsDisjunction(); 1240 virtual RegExpDisjunction* AsDisjunction();
1239 virtual bool IsDisjunction(); 1241 virtual bool IsDisjunction();
1242 virtual int min_match() { return min_match_; }
1243 virtual int max_match() { return max_match_; }
1240 ZoneList<RegExpTree*>* alternatives() { return alternatives_; } 1244 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
1241 private: 1245 private:
1242 ZoneList<RegExpTree*>* alternatives_; 1246 ZoneList<RegExpTree*>* alternatives_;
1247 int min_match_;
1248 int max_match_;
1243 }; 1249 };
1244 1250
1245 1251
1246 class RegExpAlternative: public RegExpTree { 1252 class RegExpAlternative: public RegExpTree {
1247 public: 1253 public:
1248 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes) : nodes_(nodes) { } 1254 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
1249 virtual void* Accept(RegExpVisitor* visitor, void* data); 1255 virtual void* Accept(RegExpVisitor* visitor, void* data);
1250 virtual RegExpNode* ToNode(RegExpCompiler* compiler, 1256 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1251 RegExpNode* on_success); 1257 RegExpNode* on_success);
1252 virtual RegExpAlternative* AsAlternative(); 1258 virtual RegExpAlternative* AsAlternative();
1253 virtual bool IsAlternative(); 1259 virtual bool IsAlternative();
1260 virtual int min_match() { return min_match_; }
1261 virtual int max_match() { return max_match_; }
1254 ZoneList<RegExpTree*>* nodes() { return nodes_; } 1262 ZoneList<RegExpTree*>* nodes() { return nodes_; }
1255 private: 1263 private:
1256 ZoneList<RegExpTree*>* nodes_; 1264 ZoneList<RegExpTree*>* nodes_;
1265 int min_match_;
1266 int max_match_;
1257 }; 1267 };
1258 1268
1259 1269
1260 class RegExpText: public RegExpTree {
1261 public:
1262 RegExpText() : elements_(2) { }
1263 virtual void* Accept(RegExpVisitor* visitor, void* data);
1264 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1265 RegExpNode* on_success);
1266 virtual RegExpText* AsText();
1267 virtual bool IsText();
1268 virtual bool IsTextElement() { return true; }
1269 virtual void AppendToText(RegExpText* text);
1270 void AddElement(TextElement elm) { elements_.Add(elm); }
1271 ZoneList<TextElement>* elements() { return &elements_; }
1272 private:
1273 ZoneList<TextElement> elements_;
1274 };
1275
1276
1277 class RegExpAssertion: public RegExpTree { 1270 class RegExpAssertion: public RegExpTree {
1278 public: 1271 public:
1279 enum Type { 1272 enum Type {
1280 START_OF_LINE, 1273 START_OF_LINE,
1281 START_OF_INPUT, 1274 START_OF_INPUT,
1282 END_OF_LINE, 1275 END_OF_LINE,
1283 END_OF_INPUT, 1276 END_OF_INPUT,
1284 BOUNDARY, 1277 BOUNDARY,
1285 NON_BOUNDARY 1278 NON_BOUNDARY
1286 }; 1279 };
1287 explicit RegExpAssertion(Type type) : type_(type) { } 1280 explicit RegExpAssertion(Type type) : type_(type) { }
1288 virtual void* Accept(RegExpVisitor* visitor, void* data); 1281 virtual void* Accept(RegExpVisitor* visitor, void* data);
1289 virtual RegExpNode* ToNode(RegExpCompiler* compiler, 1282 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1290 RegExpNode* on_success); 1283 RegExpNode* on_success);
1291 virtual RegExpAssertion* AsAssertion(); 1284 virtual RegExpAssertion* AsAssertion();
1292 virtual bool IsAssertion(); 1285 virtual bool IsAssertion();
1286 virtual int min_match() { return 0; }
1287 virtual int max_match() { return 0; }
1293 Type type() { return type_; } 1288 Type type() { return type_; }
1294 private: 1289 private:
1295 Type type_; 1290 Type type_;
1296 }; 1291 };
1297 1292
1298 1293
1299 class RegExpCharacterClass: public RegExpTree { 1294 class RegExpCharacterClass: public RegExpTree {
1300 public: 1295 public:
1301 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated) 1296 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
1302 : ranges_(ranges), 1297 : ranges_(ranges),
1303 is_negated_(is_negated) { } 1298 is_negated_(is_negated) { }
1304 explicit RegExpCharacterClass(uc16 type) 1299 explicit RegExpCharacterClass(uc16 type)
1305 : ranges_(new ZoneList<CharacterRange>(2)), 1300 : ranges_(new ZoneList<CharacterRange>(2)),
1306 is_negated_(false) { 1301 is_negated_(false) {
1307 CharacterRange::AddClassEscape(type, ranges_); 1302 CharacterRange::AddClassEscape(type, ranges_);
1308 } 1303 }
1309 virtual void* Accept(RegExpVisitor* visitor, void* data); 1304 virtual void* Accept(RegExpVisitor* visitor, void* data);
1310 virtual RegExpNode* ToNode(RegExpCompiler* compiler, 1305 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1311 RegExpNode* on_success); 1306 RegExpNode* on_success);
1312 virtual RegExpCharacterClass* AsCharacterClass(); 1307 virtual RegExpCharacterClass* AsCharacterClass();
1313 virtual bool IsCharacterClass(); 1308 virtual bool IsCharacterClass();
1314 virtual bool IsTextElement() { return true; } 1309 virtual bool IsTextElement() { return true; }
1310 virtual int min_match() { return 1; }
1311 virtual int max_match() { return 1; }
1315 virtual void AppendToText(RegExpText* text); 1312 virtual void AppendToText(RegExpText* text);
1316 ZoneList<CharacterRange>* ranges() { return ranges_; } 1313 ZoneList<CharacterRange>* ranges() { return ranges_; }
1317 bool is_negated() { return is_negated_; } 1314 bool is_negated() { return is_negated_; }
1318 private: 1315 private:
1319 ZoneList<CharacterRange>* ranges_; 1316 ZoneList<CharacterRange>* ranges_;
1320 bool is_negated_; 1317 bool is_negated_;
1321 }; 1318 };
1322 1319
1323 1320
1324 class RegExpAtom: public RegExpTree { 1321 class RegExpAtom: public RegExpTree {
1325 public: 1322 public:
1326 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { } 1323 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
1327 virtual void* Accept(RegExpVisitor* visitor, void* data); 1324 virtual void* Accept(RegExpVisitor* visitor, void* data);
1328 virtual RegExpNode* ToNode(RegExpCompiler* compiler, 1325 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1329 RegExpNode* on_success); 1326 RegExpNode* on_success);
1330 virtual RegExpAtom* AsAtom(); 1327 virtual RegExpAtom* AsAtom();
1331 virtual bool IsAtom(); 1328 virtual bool IsAtom();
1332 virtual bool IsTextElement() { return true; } 1329 virtual bool IsTextElement() { return true; }
1330 virtual int min_match() { return data_.length(); }
1331 virtual int max_match() { return data_.length(); }
1333 virtual void AppendToText(RegExpText* text); 1332 virtual void AppendToText(RegExpText* text);
1334 Vector<const uc16> data() { return data_; } 1333 Vector<const uc16> data() { return data_; }
1334 int length() { return data_.length(); }
1335 private: 1335 private:
1336 Vector<const uc16> data_; 1336 Vector<const uc16> data_;
1337 }; 1337 };
1338 1338
1339 1339
1340 class RegExpText: public RegExpTree {
1341 public:
1342 RegExpText() : elements_(2), length_(0) {}
1343 virtual void* Accept(RegExpVisitor* visitor, void* data);
1344 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1345 RegExpNode* on_success);
1346 virtual RegExpText* AsText();
1347 virtual bool IsText();
1348 virtual bool IsTextElement() { return true; }
1349 virtual int min_match() { return length_; }
1350 virtual int max_match() { return length_; }
1351 virtual void AppendToText(RegExpText* text);
1352 void AddElement(TextElement elm) {
1353 elements_.Add(elm);
1354 length_ += elm.length();
1355 };
1356 ZoneList<TextElement>* elements() { return &elements_; }
1357 private:
1358 ZoneList<TextElement> elements_;
1359 int length_;
1360 };
1361
1362
1340 class RegExpQuantifier: public RegExpTree { 1363 class RegExpQuantifier: public RegExpTree {
1341 public: 1364 public:
1342 RegExpQuantifier(int min, int max, bool is_greedy, RegExpTree* body) 1365 RegExpQuantifier(int min, int max, bool is_greedy, RegExpTree* body)
1343 : min_(min), 1366 : min_(min),
1344 max_(max), 1367 max_(max),
1345 is_greedy_(is_greedy), 1368 is_greedy_(is_greedy),
1346 body_(body) { } 1369 body_(body),
1370 min_match_(min * body->min_match()) {
1371 if (max > 0 && body->max_match() > kInfinity / max) {
1372 max_match_ = kInfinity;
1373 } else {
1374 max_match_ = max * body->max_match();
1375 }
1376 }
1347 virtual void* Accept(RegExpVisitor* visitor, void* data); 1377 virtual void* Accept(RegExpVisitor* visitor, void* data);
1348 virtual RegExpNode* ToNode(RegExpCompiler* compiler, 1378 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1349 RegExpNode* on_success); 1379 RegExpNode* on_success);
1350 static RegExpNode* ToNode(int min, 1380 static RegExpNode* ToNode(int min,
1351 int max, 1381 int max,
1352 bool is_greedy, 1382 bool is_greedy,
1353 RegExpTree* body, 1383 RegExpTree* body,
1354 RegExpCompiler* compiler, 1384 RegExpCompiler* compiler,
1355 RegExpNode* on_success); 1385 RegExpNode* on_success);
1356 virtual RegExpQuantifier* AsQuantifier(); 1386 virtual RegExpQuantifier* AsQuantifier();
1357 virtual bool IsQuantifier(); 1387 virtual bool IsQuantifier();
1388 virtual int min_match() { return min_match_; }
1389 virtual int max_match() { return max_match_; }
1358 int min() { return min_; } 1390 int min() { return min_; }
1359 int max() { return max_; } 1391 int max() { return max_; }
1360 bool is_greedy() { return is_greedy_; } 1392 bool is_greedy() { return is_greedy_; }
1361 RegExpTree* body() { return body_; } 1393 RegExpTree* body() { return body_; }
1362 // We just use a very large integer value as infinity because 2^30
1363 // is infinite in practice.
1364 static const int kInfinity = (1 << 30);
1365 private: 1394 private:
1366 int min_; 1395 int min_;
1367 int max_; 1396 int max_;
1368 bool is_greedy_; 1397 bool is_greedy_;
1369 RegExpTree* body_; 1398 RegExpTree* body_;
1399 int min_match_;
1400 int max_match_;
1370 }; 1401 };
1371 1402
1372 1403
1373 enum CaptureAvailability { 1404 enum CaptureAvailability {
1374 CAPTURE_AVAILABLE, 1405 CAPTURE_AVAILABLE,
1375 CAPTURE_UNREACHABLE, 1406 CAPTURE_UNREACHABLE,
1376 CAPTURE_PERMANENTLY_UNREACHABLE 1407 CAPTURE_PERMANENTLY_UNREACHABLE
1377 }; 1408 };
1378 1409
1379 class RegExpCapture: public RegExpTree { 1410 class RegExpCapture: public RegExpTree {
1380 public: 1411 public:
1381 explicit RegExpCapture(RegExpTree* body, int index) 1412 explicit RegExpCapture(RegExpTree* body, int index)
1382 : body_(body), index_(index), available_(CAPTURE_AVAILABLE) { } 1413 : body_(body), index_(index), available_(CAPTURE_AVAILABLE) { }
1383 virtual void* Accept(RegExpVisitor* visitor, void* data); 1414 virtual void* Accept(RegExpVisitor* visitor, void* data);
1384 virtual RegExpNode* ToNode(RegExpCompiler* compiler, 1415 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1385 RegExpNode* on_success); 1416 RegExpNode* on_success);
1386 static RegExpNode* ToNode(RegExpTree* body, 1417 static RegExpNode* ToNode(RegExpTree* body,
1387 int index, 1418 int index,
1388 RegExpCompiler* compiler, 1419 RegExpCompiler* compiler,
1389 RegExpNode* on_success); 1420 RegExpNode* on_success);
1390 virtual RegExpCapture* AsCapture(); 1421 virtual RegExpCapture* AsCapture();
1391 virtual bool IsCapture(); 1422 virtual bool IsCapture();
1423 virtual int min_match() { return body_->min_match(); }
1424 virtual int max_match() { return body_->max_match(); }
1392 RegExpTree* body() { return body_; } 1425 RegExpTree* body() { return body_; }
1393 int index() { return index_; } 1426 int index() { return index_; }
1394 inline CaptureAvailability available() { return available_; } 1427 inline CaptureAvailability available() { return available_; }
1395 inline void set_available(CaptureAvailability availability) { 1428 inline void set_available(CaptureAvailability availability) {
1396 available_ = availability; 1429 available_ = availability;
1397 } 1430 }
1398 static int StartRegister(int index) { return index * 2; } 1431 static int StartRegister(int index) { return index * 2; }
1399 static int EndRegister(int index) { return index * 2 + 1; } 1432 static int EndRegister(int index) { return index * 2 + 1; }
1400 private: 1433 private:
1401 RegExpTree* body_; 1434 RegExpTree* body_;
1402 int index_; 1435 int index_;
1403 CaptureAvailability available_; 1436 CaptureAvailability available_;
1404 }; 1437 };
1405 1438
1406 1439
1407 class RegExpLookahead: public RegExpTree { 1440 class RegExpLookahead: public RegExpTree {
1408 public: 1441 public:
1409 RegExpLookahead(RegExpTree* body, bool is_positive) 1442 RegExpLookahead(RegExpTree* body, bool is_positive)
1410 : body_(body), 1443 : body_(body),
1411 is_positive_(is_positive) { } 1444 is_positive_(is_positive) { }
1412 virtual void* Accept(RegExpVisitor* visitor, void* data); 1445 virtual void* Accept(RegExpVisitor* visitor, void* data);
1413 virtual RegExpNode* ToNode(RegExpCompiler* compiler, 1446 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1414 RegExpNode* on_success); 1447 RegExpNode* on_success);
1415 virtual RegExpLookahead* AsLookahead(); 1448 virtual RegExpLookahead* AsLookahead();
1416 virtual bool IsLookahead(); 1449 virtual bool IsLookahead();
1450 virtual int min_match() { return 0; }
1451 virtual int max_match() { return 0; }
1417 RegExpTree* body() { return body_; } 1452 RegExpTree* body() { return body_; }
1418 bool is_positive() { return is_positive_; } 1453 bool is_positive() { return is_positive_; }
1419 private: 1454 private:
1420 RegExpTree* body_; 1455 RegExpTree* body_;
1421 bool is_positive_; 1456 bool is_positive_;
1422 }; 1457 };
1423 1458
1424 1459
1425 class RegExpBackReference: public RegExpTree { 1460 class RegExpBackReference: public RegExpTree {
1426 public: 1461 public:
1427 explicit RegExpBackReference(RegExpCapture* capture) 1462 explicit RegExpBackReference(RegExpCapture* capture)
1428 : capture_(capture) { } 1463 : capture_(capture) { }
1429 virtual void* Accept(RegExpVisitor* visitor, void* data); 1464 virtual void* Accept(RegExpVisitor* visitor, void* data);
1430 virtual RegExpNode* ToNode(RegExpCompiler* compiler, 1465 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1431 RegExpNode* on_success); 1466 RegExpNode* on_success);
1432 virtual RegExpBackReference* AsBackReference(); 1467 virtual RegExpBackReference* AsBackReference();
1433 virtual bool IsBackReference(); 1468 virtual bool IsBackReference();
1469 virtual int min_match() { return capture_->min_match(); }
1470 virtual int max_match() { return capture_->max_match(); }
1434 int index() { return capture_->index(); } 1471 int index() { return capture_->index(); }
1435 RegExpCapture* capture() { return capture_; } 1472 RegExpCapture* capture() { return capture_; }
1436 private: 1473 private:
1437 RegExpCapture* capture_; 1474 RegExpCapture* capture_;
1438 }; 1475 };
1439 1476
1440 1477
1441 class RegExpEmpty: public RegExpTree { 1478 class RegExpEmpty: public RegExpTree {
1442 public: 1479 public:
1443 RegExpEmpty() { } 1480 RegExpEmpty() { }
1444 virtual void* Accept(RegExpVisitor* visitor, void* data); 1481 virtual void* Accept(RegExpVisitor* visitor, void* data);
1445 virtual RegExpNode* ToNode(RegExpCompiler* compiler, 1482 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
1446 RegExpNode* on_success); 1483 RegExpNode* on_success);
1447 virtual RegExpEmpty* AsEmpty(); 1484 virtual RegExpEmpty* AsEmpty();
1448 virtual bool IsEmpty(); 1485 virtual bool IsEmpty();
1486 virtual int min_match() { return 0; }
1487 virtual int max_match() { return 0; }
1449 static RegExpEmpty* GetInstance() { return &kInstance; } 1488 static RegExpEmpty* GetInstance() { return &kInstance; }
1450 private: 1489 private:
1451 static RegExpEmpty kInstance; 1490 static RegExpEmpty kInstance;
1452 }; 1491 };
1453 1492
1454 1493
1455 class RegExpVisitor BASE_EMBEDDED { 1494 class RegExpVisitor BASE_EMBEDDED {
1456 public: 1495 public:
1457 virtual ~RegExpVisitor() { } 1496 virtual ~RegExpVisitor() { }
1458 #define MAKE_CASE(Name) \ 1497 #define MAKE_CASE(Name) \
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1500 #undef DEF_VISIT 1539 #undef DEF_VISIT
1501 1540
1502 private: 1541 private:
1503 bool stack_overflow_; 1542 bool stack_overflow_;
1504 }; 1543 };
1505 1544
1506 1545
1507 } } // namespace v8::internal 1546 } } // namespace v8::internal
1508 1547
1509 #endif // V8_AST_H_ 1548 #endif // V8_AST_H_
OLDNEW
« no previous file with comments | « no previous file | src/ast.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698