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

Side by Side Diff: src/parsing/parser.cc

Issue 2268413002: [parser] Clean up (pre)parser traits, part 3 (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@nickie-2273693002-ref-traits
Patch Set: Created 4 years, 4 months 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
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/parsing/parser.h" 5 #include "src/parsing/parser.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "src/api.h" 9 #include "src/api.h"
10 #include "src/ast/ast-expression-rewriter.h" 10 #include "src/ast/ast-expression-rewriter.h"
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 has_dot); 360 has_dot);
361 return true; 361 return true;
362 } 362 }
363 default: 363 default:
364 break; 364 break;
365 } 365 }
366 } 366 }
367 return false; 367 return false;
368 } 368 }
369 369
370 Expression* ParserBaseTraits<Parser>::BuildUnaryExpression( 370 Expression* Parser::BuildUnaryExpression(Expression* expression,
371 Expression* expression, Token::Value op, int pos, AstNodeFactory* factory) { 371 Token::Value op, int pos) {
372 DCHECK(expression != NULL); 372 DCHECK(expression != NULL);
373 if (expression->IsLiteral()) { 373 if (expression->IsLiteral()) {
374 const AstValue* literal = expression->AsLiteral()->raw_value(); 374 const AstValue* literal = expression->AsLiteral()->raw_value();
375 if (op == Token::NOT) { 375 if (op == Token::NOT) {
376 // Convert the literal to a boolean condition and negate it. 376 // Convert the literal to a boolean condition and negate it.
377 bool condition = literal->BooleanValue(); 377 bool condition = literal->BooleanValue();
378 return factory->NewBooleanLiteral(!condition, pos); 378 return factory()->NewBooleanLiteral(!condition, pos);
379 } else if (literal->IsNumber()) { 379 } else if (literal->IsNumber()) {
380 // Compute some expressions involving only number literals. 380 // Compute some expressions involving only number literals.
381 double value = literal->AsNumber(); 381 double value = literal->AsNumber();
382 bool has_dot = literal->ContainsDot(); 382 bool has_dot = literal->ContainsDot();
383 switch (op) { 383 switch (op) {
384 case Token::ADD: 384 case Token::ADD:
385 return expression; 385 return expression;
386 case Token::SUB: 386 case Token::SUB:
387 return factory->NewNumberLiteral(-value, pos, has_dot); 387 return factory()->NewNumberLiteral(-value, pos, has_dot);
388 case Token::BIT_NOT: 388 case Token::BIT_NOT:
389 return factory->NewNumberLiteral(~DoubleToInt32(value), pos, has_dot); 389 return factory()->NewNumberLiteral(~DoubleToInt32(value), pos,
390 has_dot);
390 default: 391 default:
391 break; 392 break;
392 } 393 }
393 } 394 }
394 } 395 }
395 // Desugar '+foo' => 'foo*1' 396 // Desugar '+foo' => 'foo*1'
396 if (op == Token::ADD) { 397 if (op == Token::ADD) {
397 return factory->NewBinaryOperation( 398 return factory()->NewBinaryOperation(
398 Token::MUL, expression, factory->NewNumberLiteral(1, pos, true), pos); 399 Token::MUL, expression, factory()->NewNumberLiteral(1, pos, true), pos);
399 } 400 }
400 // The same idea for '-foo' => 'foo*(-1)'. 401 // The same idea for '-foo' => 'foo*(-1)'.
401 if (op == Token::SUB) { 402 if (op == Token::SUB) {
402 return factory->NewBinaryOperation( 403 return factory()->NewBinaryOperation(
403 Token::MUL, expression, factory->NewNumberLiteral(-1, pos), pos); 404 Token::MUL, expression, factory()->NewNumberLiteral(-1, pos), pos);
404 } 405 }
405 // ...and one more time for '~foo' => 'foo^(~0)'. 406 // ...and one more time for '~foo' => 'foo^(~0)'.
406 if (op == Token::BIT_NOT) { 407 if (op == Token::BIT_NOT) {
407 return factory->NewBinaryOperation( 408 return factory()->NewBinaryOperation(
408 Token::BIT_XOR, expression, factory->NewNumberLiteral(~0, pos), pos); 409 Token::BIT_XOR, expression, factory()->NewNumberLiteral(~0, pos), pos);
409 } 410 }
410 return factory->NewUnaryOperation(op, expression, pos); 411 return factory()->NewUnaryOperation(op, expression, pos);
411 } 412 }
412 413
413 Expression* ParserBaseTraits<Parser>::BuildIteratorResult(Expression* value, 414 Expression* Parser::BuildIteratorResult(Expression* value, bool done) {
414 bool done) {
415 int pos = kNoSourcePosition; 415 int pos = kNoSourcePosition;
416 AstNodeFactory* factory = delegate()->factory();
417 Zone* zone = delegate()->zone();
418 416
419 if (value == nullptr) value = factory->NewUndefinedLiteral(pos); 417 if (value == nullptr) value = factory()->NewUndefinedLiteral(pos);
420 418
421 auto args = new (zone) ZoneList<Expression*>(2, zone); 419 auto args = new (zone()) ZoneList<Expression*>(2, zone());
422 args->Add(value, zone); 420 args->Add(value, zone());
423 args->Add(factory->NewBooleanLiteral(done, pos), zone); 421 args->Add(factory()->NewBooleanLiteral(done, pos), zone());
424 422
425 return factory->NewCallRuntime(Runtime::kInlineCreateIterResultObject, args, 423 return factory()->NewCallRuntime(Runtime::kInlineCreateIterResultObject, args,
426 pos); 424 pos);
427 }
428
429 Expression* ParserBaseTraits<Parser>::NewThrowReferenceError(
430 MessageTemplate::Template message, int pos) {
431 return delegate()->NewThrowError(
432 Runtime::kNewReferenceError, message,
433 delegate()->ast_value_factory()->empty_string(), pos);
434 }
435
436 Expression* ParserBaseTraits<Parser>::NewThrowSyntaxError(
437 MessageTemplate::Template message, const AstRawString* arg, int pos) {
438 return delegate()->NewThrowError(Runtime::kNewSyntaxError, message, arg, pos);
439 }
440
441 Expression* ParserBaseTraits<Parser>::NewThrowTypeError(
442 MessageTemplate::Template message, const AstRawString* arg, int pos) {
443 return delegate()->NewThrowError(Runtime::kNewTypeError, message, arg, pos);
444 } 425 }
445 426
446 Expression* Parser::NewThrowError(Runtime::FunctionId id, 427 Expression* Parser::NewThrowError(Runtime::FunctionId id,
447 MessageTemplate::Template message, 428 MessageTemplate::Template message,
448 const AstRawString* arg, int pos) { 429 const AstRawString* arg, int pos) {
449 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(2, zone()); 430 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(2, zone());
450 args->Add(factory()->NewSmiLiteral(message, pos), zone()); 431 args->Add(factory()->NewSmiLiteral(message, pos), zone());
451 args->Add(factory()->NewStringLiteral(arg, pos), zone()); 432 args->Add(factory()->NewStringLiteral(arg, pos), zone());
452 CallRuntime* call_constructor = factory()->NewCallRuntime(id, args, pos); 433 CallRuntime* call_constructor = factory()->NewCallRuntime(id, args, pos);
453 return factory()->NewThrow(call_constructor, pos); 434 return factory()->NewThrow(call_constructor, pos);
454 } 435 }
455 436
456 void ParserBaseTraits<Parser>::ReportMessageAt(
457 Scanner::Location source_location, MessageTemplate::Template message,
458 const char* arg, ParseErrorType error_type) {
459 if (delegate()->stack_overflow()) {
460 // Suppress the error message (syntax error or such) in the presence of a
461 // stack overflow. The isolate allows only one pending exception at at time
462 // and we want to report the stack overflow later.
463 return;
464 }
465 delegate()->pending_error_handler_.ReportMessageAt(source_location.beg_pos,
466 source_location.end_pos,
467 message, arg, error_type);
468 }
469
470 void ParserBaseTraits<Parser>::ReportMessageAt(
471 Scanner::Location source_location, MessageTemplate::Template message,
472 const AstRawString* arg, ParseErrorType error_type) {
473 if (delegate()->stack_overflow()) {
474 // Suppress the error message (syntax error or such) in the presence of a
475 // stack overflow. The isolate allows only one pending exception at at time
476 // and we want to report the stack overflow later.
477 return;
478 }
479 delegate()->pending_error_handler_.ReportMessageAt(source_location.beg_pos,
480 source_location.end_pos,
481 message, arg, error_type);
482 }
483
484 const AstRawString* ParserBaseTraits<Parser>::GetSymbol( 437 const AstRawString* ParserBaseTraits<Parser>::GetSymbol(
485 Scanner* scanner) const { 438 Scanner* scanner) const {
486 const AstRawString* result = 439 const AstRawString* result =
487 delegate()->scanner()->CurrentSymbol(delegate()->ast_value_factory()); 440 delegate()->scanner()->CurrentSymbol(delegate()->ast_value_factory());
488 DCHECK(result != NULL); 441 DCHECK(result != NULL);
489 return result; 442 return result;
490 } 443 }
491 444
492 const AstRawString* ParserBaseTraits<Parser>::GetNumberAsSymbol( 445 const AstRawString* ParserBaseTraits<Parser>::GetNumberAsSymbol(
493 Scanner* scanner) const { 446 Scanner* scanner) const {
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 int pos) { 552 int pos) {
600 Expression* iterator_symbol_literal = 553 Expression* iterator_symbol_literal =
601 factory->NewSymbolLiteral("iterator_symbol", kNoSourcePosition); 554 factory->NewSymbolLiteral("iterator_symbol", kNoSourcePosition);
602 Expression* prop = 555 Expression* prop =
603 factory->NewProperty(iterable, iterator_symbol_literal, pos); 556 factory->NewProperty(iterable, iterator_symbol_literal, pos);
604 Zone* zone = delegate()->zone(); 557 Zone* zone = delegate()->zone();
605 ZoneList<Expression*>* args = new (zone) ZoneList<Expression*>(0, zone); 558 ZoneList<Expression*>* args = new (zone) ZoneList<Expression*>(0, zone);
606 return factory->NewCall(prop, args, pos); 559 return factory->NewCall(prop, args, pos);
607 } 560 }
608 561
609 Literal* ParserBaseTraits<Parser>::GetLiteralTheHole(
610 int position, AstNodeFactory* factory) const {
611 return factory->NewTheHoleLiteral(kNoSourcePosition);
612 }
613
614 void Parser::MarkTailPosition(Expression* expression) { 562 void Parser::MarkTailPosition(Expression* expression) {
615 expression->MarkTail(); 563 expression->MarkTail();
616 } 564 }
617 565
618 Parser::Parser(ParseInfo* info) 566 Parser::Parser(ParseInfo* info)
619 : ParserBase<Parser>(info->zone(), &scanner_, info->stack_limit(), 567 : ParserBase<Parser>(info->zone(), &scanner_, info->stack_limit(),
620 info->extension(), info->ast_value_factory(), NULL), 568 info->extension(), info->ast_value_factory(), NULL),
621 scanner_(info->unicode_cache()), 569 scanner_(info->unicode_cache()),
622 reusable_preparser_(NULL), 570 reusable_preparser_(NULL),
623 original_scope_(NULL), 571 original_scope_(NULL),
(...skipping 3336 matching lines...) Expand 10 before | Expand all | Expand 10 after
3960 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, 3908 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc,
3961 const Scope::Snapshot& scope_snapshot, bool* ok) { 3909 const Scope::Snapshot& scope_snapshot, bool* ok) {
3962 if (expr->IsEmptyParentheses()) return; 3910 if (expr->IsEmptyParentheses()) return;
3963 3911
3964 delegate()->ParseArrowFunctionFormalParameters( 3912 delegate()->ParseArrowFunctionFormalParameters(
3965 parameters, expr, params_loc.end_pos, CHECK_OK_VOID); 3913 parameters, expr, params_loc.end_pos, CHECK_OK_VOID);
3966 3914
3967 scope_snapshot.Reparent(parameters->scope); 3915 scope_snapshot.Reparent(parameters->scope);
3968 3916
3969 if (parameters->Arity() > Code::kMaxArguments) { 3917 if (parameters->Arity() > Code::kMaxArguments) {
3970 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList); 3918 delegate()->ReportMessageAt(params_loc,
3919 MessageTemplate::kMalformedArrowFunParamList);
3971 *ok = false; 3920 *ok = false;
3972 return; 3921 return;
3973 } 3922 }
3974 3923
3975 Type::ExpressionClassifier classifier(delegate()); 3924 Type::ExpressionClassifier classifier(delegate());
3976 if (!parameters->is_simple) { 3925 if (!parameters->is_simple) {
3977 classifier.RecordNonSimpleParameter(); 3926 classifier.RecordNonSimpleParameter();
3978 } 3927 }
3979 for (int i = 0; i < parameters->Arity(); ++i) { 3928 for (int i = 0; i < parameters->Arity(); ++i) {
3980 auto parameter = parameters->at(i); 3929 auto parameter = parameters->at(i);
(...skipping 2795 matching lines...) Expand 10 before | Expand all | Expand 10 after
6776 node->Print(Isolate::Current()); 6725 node->Print(Isolate::Current());
6777 } 6726 }
6778 #endif // DEBUG 6727 #endif // DEBUG
6779 6728
6780 #undef CHECK_OK 6729 #undef CHECK_OK
6781 #undef CHECK_OK_VOID 6730 #undef CHECK_OK_VOID
6782 #undef CHECK_FAILED 6731 #undef CHECK_FAILED
6783 6732
6784 } // namespace internal 6733 } // namespace internal
6785 } // namespace v8 6734 } // namespace v8
OLDNEW
« src/parsing/parser.h ('K') | « src/parsing/parser.h ('k') | src/parsing/parser-base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698