iv-phonic 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -71,7 +71,6 @@ class BasicAstVisitor
71
71
  virtual void Visit(typename add<NullLiteral<Factory> >::type literal) = 0; //NOLINT
72
72
  virtual void Visit(typename add<TrueLiteral<Factory> >::type literal) = 0; //NOLINT
73
73
  virtual void Visit(typename add<FalseLiteral<Factory> >::type literal) = 0; //NOLINT
74
- virtual void Visit(typename add<Undefined<Factory> >::type literal) = 0; //NOLINT
75
74
  virtual void Visit(typename add<RegExpLiteral<Factory> >::type literal) = 0; //NOLINT
76
75
  virtual void Visit(typename add<ArrayLiteral<Factory> >::type literal) = 0; //NOLINT
77
76
  virtual void Visit(typename add<ObjectLiteral<Factory> >::type literal) = 0; //NOLINT
@@ -0,0 +1,72 @@
1
+ #ifndef _IV_MAYBE_H_
2
+ #define _IV_MAYBE_H_
3
+ #include <cassert>
4
+ #include <algorithm>
5
+ #include <tr1/type_traits>
6
+ #include "enable_if.h"
7
+
8
+ // Maybe NULL pointer wrapper class
9
+ // not have ownership
10
+
11
+ namespace iv {
12
+ namespace core {
13
+
14
+ template<class T>
15
+ class Maybe {
16
+ public:
17
+ typedef Maybe<T> this_type;
18
+ typedef void (this_type::*bool_type)() const;
19
+
20
+ Maybe(T* ptr) : ptr_(ptr) { }
21
+ Maybe() : ptr_(NULL) { }
22
+
23
+ template<class U>
24
+ Maybe(const Maybe<U>& rhs,
25
+ typename enable_if<std::tr1::is_convertible<U*, T*> >::type* = 0)
26
+ : ptr_(rhs.get_address_maybe_null()) {
27
+ }
28
+
29
+ T& operator*() const {
30
+ assert(ptr_);
31
+ return *ptr_;
32
+ }
33
+
34
+ T* Address() const {
35
+ assert(ptr_);
36
+ return ptr_;
37
+ }
38
+
39
+ operator bool_type() const {
40
+ return ptr_ != NULL ?
41
+ &this_type::this_type_does_not_support_comparisons : 0;
42
+ }
43
+
44
+ template<typename U>
45
+ this_type& operator=(const Maybe<U>& rhs) {
46
+ if (this != &rhs) {
47
+ this_type(rhs).swap(*this);
48
+ }
49
+ return *this;
50
+ }
51
+
52
+ inline void swap(this_type& rhs) {
53
+ using std::swap;
54
+ swap(ptr_, rhs.ptr_);
55
+ }
56
+
57
+ inline friend void swap(this_type& lhs, this_type& rhs) {
58
+ return lhs.swap(rhs);
59
+ }
60
+
61
+ inline T* get_address_maybe_null() const {
62
+ return ptr_;
63
+ }
64
+
65
+ private:
66
+ void this_type_does_not_support_comparisons() const { }
67
+
68
+ T* ptr_;
69
+ };
70
+
71
+ } } // namespace iv::core
72
+ #endif // _IV_MAYBE_H_
@@ -8,6 +8,7 @@
8
8
  #include <tr1/type_traits>
9
9
  #include <tr1/array>
10
10
  #include "static_assert.h"
11
+ #include "maybe.h"
11
12
  #include "ast.h"
12
13
  #include "ast_factory.h"
13
14
  #include "lexer.h"
@@ -152,7 +153,7 @@ class Parser
152
153
  #define V(AST) typedef typename ast::AST<Factory> AST;
153
154
  AST_NODE_LIST(V)
154
155
  #undef V
155
- #define V(X, XS) typedef typename SpaceVector<Factory, X *>::type XS;
156
+ #define V(XS) typedef typename ast::AstNode<Factory>::XS XS;
156
157
  AST_LIST_LIST(V)
157
158
  #undef V
158
159
  #define V(S) typedef typename SpaceUString<Factory>::type S;
@@ -263,6 +264,11 @@ class Parser
263
264
  Next();
264
265
  const bool strict = ParseSourceElements(Token::EOS, body, CHECK);
265
266
  const std::size_t end_position = lexer_.end_position();
267
+ #ifdef DEBUG
268
+ if (error_flag) {
269
+ assert(params && body && scope);
270
+ }
271
+ #endif
266
272
  return (error_flag) ?
267
273
  factory_->NewFunctionLiteral(FunctionLiteral::GLOBAL,
268
274
  NULL,
@@ -464,6 +470,7 @@ class Parser
464
470
  FunctionLiteral::GENERAL, CHECK);
465
471
  // define named function as FunctionDeclaration
466
472
  scope_->AddFunctionDeclaration(expr);
473
+ assert(expr);
467
474
  return factory_->NewFunctionDeclaration(expr);
468
475
  }
469
476
 
@@ -486,6 +493,7 @@ class Parser
486
493
  body->push_back(stmt);
487
494
  }
488
495
  Next();
496
+ assert(body);
489
497
  Block* const block = factory_->NewBlock(body,
490
498
  begin,
491
499
  lexer_.previous_end_position());
@@ -503,6 +511,7 @@ class Parser
503
511
  Declarations* const decls = factory_->template NewVector<Declaration*>();
504
512
  ParseVariableDeclarations(decls, token_ == Token::CONST, true, CHECK);
505
513
  ExpectSemicolon(CHECK);
514
+ assert(decls);
506
515
  return factory_->NewVariableStatement(op,
507
516
  decls,
508
517
  begin,
@@ -552,10 +561,12 @@ class Parser
552
561
  Next();
553
562
  // AssignmentExpression
554
563
  expr = ParseAssignmentExpression(contains_in, CHECK);
564
+ assert(name);
555
565
  decl = factory_->NewDeclaration(name, expr);
556
566
  } else {
557
567
  // Undefined Expression
558
- decl = factory_->NewDeclaration(name, factory_->NewUndefined());
568
+ assert(name);
569
+ decl = factory_->NewDeclaration(name, NULL);
559
570
  }
560
571
  decls->push_back(decl);
561
572
  scope_->AddUnresolved(name, is_const);
@@ -593,6 +604,7 @@ class Parser
593
604
  Next();
594
605
  else_statement = ParseStatement(CHECK);
595
606
  }
607
+ assert(expr && then_statement);
596
608
  return factory_->NewIfStatement(expr,
597
609
  then_statement,
598
610
  else_statement,
@@ -635,6 +647,7 @@ class Parser
635
647
  if (token_ == Token::SEMICOLON) {
636
648
  Next();
637
649
  }
650
+ assert(stmt && expr);
638
651
  DoWhileStatement* const dowhile = factory_->NewDoWhileStatement(
639
652
  stmt, expr, begin, lexer_.previous_end_position());
640
653
  target.set_node(dowhile);
@@ -655,6 +668,7 @@ class Parser
655
668
  EXPECT(Token::RPAREN);
656
669
 
657
670
  Statement* const stmt = ParseStatement(CHECK);
671
+ assert(stmt);
658
672
  WhileStatement* const whilestmt = factory_->NewWhileStatement(stmt,
659
673
  expr, begin);
660
674
 
@@ -687,6 +701,7 @@ class Parser
687
701
  factory_->template NewVector<Declaration*>();
688
702
  ParseVariableDeclarations(decls, token_ == Token::CONST, false, CHECK);
689
703
  if (token_ == Token::IN) {
704
+ assert(decls);
690
705
  VariableStatement* const var =
691
706
  factory_->NewVariableStatement(op,
692
707
  decls,
@@ -705,12 +720,14 @@ class Parser
705
720
  EXPECT(Token::RPAREN);
706
721
  Target target(this, Target::kIterationStatement);
707
722
  Statement* const body = ParseStatement(CHECK);
723
+ assert(body && init && enumerable);
708
724
  ForInStatement* const forstmt =
709
725
  factory_->NewForInStatement(body, init, enumerable,
710
726
  for_stmt_begin);
711
727
  target.set_node(forstmt);
712
728
  return forstmt;
713
729
  } else {
730
+ assert(decls);
714
731
  init = factory_->NewVariableStatement(op, decls,
715
732
  begin,
716
733
  lexer_.end_position());
@@ -719,6 +736,7 @@ class Parser
719
736
  Expression* const init_expr = ParseExpression(false, CHECK);
720
737
  if (token_ == Token::IN) {
721
738
  // for in loop
739
+ assert(init_expr);
722
740
  init = factory_->NewExpressionStatement(init_expr,
723
741
  lexer_.previous_end_position());
724
742
  if (!init_expr->IsValidLeftHandSide()) {
@@ -729,12 +747,14 @@ class Parser
729
747
  EXPECT(Token::RPAREN);
730
748
  Target target(this, Target::kIterationStatement);
731
749
  Statement* const body = ParseStatement(CHECK);
750
+ assert(body && init && enumerable);
732
751
  ForInStatement* const forstmt =
733
752
  factory_->NewForInStatement(body, init, enumerable,
734
753
  for_stmt_begin);
735
754
  target.set_node(forstmt);
736
755
  return forstmt;
737
756
  } else {
757
+ assert(init_expr);
738
758
  init = factory_->NewExpressionStatement(init_expr,
739
759
  lexer_.end_position());
740
760
  }
@@ -757,7 +777,8 @@ class Parser
757
777
  if (token_ == Token::RPAREN) {
758
778
  Next();
759
779
  } else {
760
- Expression* next_expr = ParseExpression(true, CHECK);
780
+ Expression* const next_expr = ParseExpression(true, CHECK);
781
+ assert(next_expr);
761
782
  next = factory_->NewExpressionStatement(next_expr,
762
783
  lexer_.previous_end_position());
763
784
  EXPECT(Token::RPAREN);
@@ -765,6 +786,7 @@ class Parser
765
786
 
766
787
  Target target(this, Target::kIterationStatement);
767
788
  Statement* const body = ParseStatement(CHECK);
789
+ assert(body);
768
790
  ForStatement* const forstmt =
769
791
  factory_->NewForStatement(body, init, cond, next, for_stmt_begin);
770
792
  target.set_node(forstmt);
@@ -845,7 +867,6 @@ class Parser
845
867
  // ReturnStatement
846
868
  // : RETURN Expression_opt ';'
847
869
  Statement* ParseReturnStatement(bool *res) {
848
- // TODO(Constellation) NewUndefined -> NULL
849
870
  assert(token_ == Token::RETURN);
850
871
  const std::size_t begin = lexer_.begin_position();
851
872
  Next();
@@ -861,11 +882,11 @@ class Parser
861
882
  token_ == Token::RBRACE ||
862
883
  token_ == Token::EOS) {
863
884
  ExpectSemicolon(CHECK);
864
- return factory_->NewReturnStatement(factory_->NewUndefined(),
885
+ return factory_->NewReturnStatement(NULL,
865
886
  begin,
866
887
  lexer_.previous_end_position());
867
888
  }
868
- Expression *expr = ParseExpression(true, CHECK);
889
+ Expression* const expr = ParseExpression(true, CHECK);
869
890
  ExpectSemicolon(CHECK);
870
891
  return factory_->NewReturnStatement(expr, begin,
871
892
  lexer_.previous_end_position());
@@ -891,6 +912,7 @@ class Parser
891
912
  EXPECT(Token::RPAREN);
892
913
 
893
914
  Statement *stmt = ParseStatement(CHECK);
915
+ assert(expr && stmt);
894
916
  return factory_->NewWithStatement(expr, stmt, begin);
895
917
  }
896
918
 
@@ -934,6 +956,7 @@ class Parser
934
956
  clauses->push_back(case_clause);
935
957
  }
936
958
  Next();
959
+ assert(expr && clauses);
937
960
  SwitchStatement* const switch_stmt =
938
961
  factory_->NewSwitchStatement(expr, clauses,
939
962
  begin,
@@ -973,6 +996,7 @@ class Parser
973
996
  body->push_back(stmt);
974
997
  }
975
998
 
999
+ assert(body);
976
1000
  return factory_->NewCaseClause(expr == NULL,
977
1001
  expr, body,
978
1002
  begin, lexer_.previous_end_position());
@@ -990,6 +1014,7 @@ class Parser
990
1014
  }
991
1015
  Expression* const expr = ParseExpression(true, CHECK);
992
1016
  ExpectSemicolon(CHECK);
1017
+ assert(expr);
993
1018
  return factory_->NewThrowStatement(expr,
994
1019
  begin, lexer_.previous_end_position());
995
1020
  }
@@ -1055,6 +1080,7 @@ class Parser
1055
1080
  RAISE("missing catch or finally after try statement");
1056
1081
  }
1057
1082
 
1083
+ assert(try_block);
1058
1084
  return factory_->NewTryStatement(try_block,
1059
1085
  name, catch_block,
1060
1086
  finally_block, begin);
@@ -1074,6 +1100,7 @@ class Parser
1074
1100
  Statement* ParseExpressionStatement(bool *res) {
1075
1101
  Expression* const expr = ParseExpression(true, CHECK);
1076
1102
  ExpectSemicolon(CHECK);
1103
+ assert(expr);
1077
1104
  return factory_->NewExpressionStatement(expr,
1078
1105
  lexer_.previous_end_position());
1079
1106
  }
@@ -1105,9 +1132,11 @@ class Parser
1105
1132
  const LabelSwitcher label_switcher(this, labels, exist_labels);
1106
1133
 
1107
1134
  Statement* const stmt = ParseStatement(CHECK);
1135
+ assert(expr && stmt);
1108
1136
  return factory_->NewLabelledStatement(expr, stmt);
1109
1137
  }
1110
1138
  ExpectSemicolon(CHECK);
1139
+ assert(expr);
1111
1140
  return factory_->NewExpressionStatement(expr,
1112
1141
  lexer_.previous_end_position());
1113
1142
  }
@@ -1124,7 +1153,9 @@ class Parser
1124
1153
  FunctionLiteral::GENERAL,
1125
1154
  CHECK);
1126
1155
  // define named function as variable declaration
1127
- scope_->AddUnresolved(expr->name(), false);
1156
+ assert(expr);
1157
+ assert(expr->name());
1158
+ scope_->AddUnresolved(expr->name().Address(), false);
1128
1159
  return factory_->NewFunctionStatement(expr);
1129
1160
  }
1130
1161
 
@@ -1146,6 +1177,7 @@ class Parser
1146
1177
  while (token_ == Token::COMMA) {
1147
1178
  Next();
1148
1179
  right = ParseAssignmentExpression(contains_in, CHECK);
1180
+ assert(result && right);
1149
1181
  result = factory_->NewBinaryOperation(Token::COMMA, result, right);
1150
1182
  }
1151
1183
  return result;
@@ -1178,6 +1210,7 @@ class Parser
1178
1210
  const Token::Type op = token_;
1179
1211
  Next();
1180
1212
  Expression* const right = ParseAssignmentExpression(contains_in, CHECK);
1213
+ assert(result && right);
1181
1214
  return factory_->NewAssignment(op, result, right);
1182
1215
  }
1183
1216
 
@@ -1192,6 +1225,7 @@ class Parser
1192
1225
  Expression* const left = ParseAssignmentExpression(true, CHECK);
1193
1226
  EXPECT(Token::COLON);
1194
1227
  Expression* const right = ParseAssignmentExpression(contains_in, CHECK);
1228
+ assert(result && left && right);
1195
1229
  result = factory_->NewConditionalExpression(result, left, right);
1196
1230
  }
1197
1231
  return result;
@@ -1293,6 +1327,7 @@ class Parser
1293
1327
  op = token_;
1294
1328
  Next();
1295
1329
  right = ParseBinaryExpression(contains_in, 2, CHECK);
1330
+ assert(left && right);
1296
1331
  left = factory_->NewBinaryOperation(op, left, right);
1297
1332
  }
1298
1333
  if (prec < 4) return left;
@@ -1305,6 +1340,7 @@ class Parser
1305
1340
  op = token_;
1306
1341
  Next();
1307
1342
  right = ParseBinaryExpression(contains_in, 3, CHECK);
1343
+ assert(left && right);
1308
1344
  left = factory_->NewBinaryOperation(op, left, right);
1309
1345
  }
1310
1346
  if (prec < 5) return left;
@@ -1341,6 +1377,7 @@ class Parser
1341
1377
  op = token_;
1342
1378
  Next();
1343
1379
  right = ParseBinaryExpression(contains_in, 7, CHECK);
1380
+ assert(left && right);
1344
1381
  left = factory_->NewBinaryOperation(op, left, right);
1345
1382
  }
1346
1383
  if (prec < 9) return left;
@@ -1350,6 +1387,7 @@ class Parser
1350
1387
  op = token_;
1351
1388
  Next();
1352
1389
  right = ParseBinaryExpression(contains_in, 8, CHECK);
1390
+ assert(left && right);
1353
1391
  left = factory_->NewBinaryOperation(op, left, right);
1354
1392
  }
1355
1393
  return left;
@@ -1420,11 +1458,13 @@ class Parser
1420
1458
  }
1421
1459
 
1422
1460
  default:
1461
+ assert(left && right);
1423
1462
  res = factory_->NewBinaryOperation(op, left, right);
1424
1463
  break;
1425
1464
  }
1426
1465
  return res;
1427
1466
  } else {
1467
+ assert(left && right);
1428
1468
  return factory_->NewBinaryOperation(op, left, right);
1429
1469
  }
1430
1470
  }
@@ -1434,6 +1474,7 @@ class Parser
1434
1474
  Expression* left,
1435
1475
  Expression* right,
1436
1476
  typename enable_if_c<!Reduce>::type* = 0) {
1477
+ assert(left && right);
1437
1478
  return factory_->NewBinaryOperation(op, left, right);
1438
1479
  }
1439
1480
 
@@ -1458,6 +1499,7 @@ class Parser
1458
1499
  case Token::TYPEOF:
1459
1500
  Next();
1460
1501
  expr = ParseUnaryExpression(CHECK);
1502
+ assert(expr);
1461
1503
  result = factory_->NewUnaryOperation(op, expr, begin);
1462
1504
  break;
1463
1505
 
@@ -1471,6 +1513,7 @@ class Parser
1471
1513
  expr->AsIdentifier()) {
1472
1514
  RAISE("delete to direct identifier not allowed in strict code");
1473
1515
  }
1516
+ assert(expr);
1474
1517
  result = factory_->NewUnaryOperation(op, expr, begin);
1475
1518
  break;
1476
1519
 
@@ -1481,6 +1524,7 @@ class Parser
1481
1524
  result = factory_->NewReducedNumberLiteral(
1482
1525
  ~DoubleToInt32(expr->AsNumberLiteral()->value()));
1483
1526
  } else {
1527
+ assert(expr);
1484
1528
  result = factory_->NewUnaryOperation(op, expr, begin);
1485
1529
  }
1486
1530
  break;
@@ -1491,6 +1535,7 @@ class Parser
1491
1535
  if (expr->AsNumberLiteral()) {
1492
1536
  result = expr;
1493
1537
  } else {
1538
+ assert(expr);
1494
1539
  result = factory_->NewUnaryOperation(op, expr, begin);
1495
1540
  }
1496
1541
  break;
@@ -1502,6 +1547,7 @@ class Parser
1502
1547
  result = factory_->NewReducedNumberLiteral(
1503
1548
  -(expr->AsNumberLiteral()->value()));
1504
1549
  } else {
1550
+ assert(expr);
1505
1551
  result = factory_->NewUnaryOperation(op, expr, begin);
1506
1552
  }
1507
1553
  break;
@@ -1528,6 +1574,7 @@ class Parser
1528
1574
  }
1529
1575
  }
1530
1576
  }
1577
+ assert(expr);
1531
1578
  result = factory_->NewUnaryOperation(op, expr, begin);
1532
1579
  break;
1533
1580
 
@@ -1563,6 +1610,7 @@ class Parser
1563
1610
  }
1564
1611
  }
1565
1612
  }
1613
+ assert(expr);
1566
1614
  expr = factory_->NewPostfixExpression(token_, expr,
1567
1615
  lexer_.end_position());
1568
1616
  Next();
@@ -1600,6 +1648,7 @@ class Parser
1600
1648
  if (token_ == Token::LPAREN) {
1601
1649
  ParseArguments(args, CHECK);
1602
1650
  }
1651
+ assert(target && args);
1603
1652
  expr = factory_->NewConstructorCall(target, args, lexer_.previous_end_position());
1604
1653
  }
1605
1654
  while (true) {
@@ -1607,6 +1656,7 @@ class Parser
1607
1656
  case Token::LBRACK: {
1608
1657
  Next();
1609
1658
  Expression* const index = ParseExpression(true, CHECK);
1659
+ assert(expr && index);
1610
1660
  expr = factory_->NewIndexAccess(expr, index);
1611
1661
  EXPECT(Token::RBRACK);
1612
1662
  break;
@@ -1616,6 +1666,7 @@ class Parser
1616
1666
  Next<IgnoreReservedWords>(); // IDENTIFIERNAME
1617
1667
  IS(Token::IDENTIFIER);
1618
1668
  Identifier* const ident = ParseIdentifier(lexer_.Buffer());
1669
+ assert(expr && ident);
1619
1670
  expr = factory_->NewIdentifierAccess(expr, ident);
1620
1671
  break;
1621
1672
  }
@@ -1625,6 +1676,7 @@ class Parser
1625
1676
  Expressions* const args =
1626
1677
  factory_->template NewVector<Expression*>();
1627
1678
  ParseArguments(args, CHECK);
1679
+ assert(expr && args);
1628
1680
  expr = factory_->NewFunctionCall(expr, args,
1629
1681
  lexer_.previous_end_position());
1630
1682
  } else {
@@ -1800,12 +1852,12 @@ class Parser
1800
1852
  // | Elision ','
1801
1853
  Expression* ParseArrayLiteral(bool *res) {
1802
1854
  const std::size_t begin = lexer_.begin_position();
1803
- Expressions* const items = factory_->template NewVector<Expression*>();
1855
+ MaybeExpressions* const items = factory_->template NewVector<Maybe<Expression> >();
1804
1856
  Next();
1805
1857
  while (token_ != Token::RBRACK) {
1806
1858
  if (token_ == Token::COMMA) {
1807
1859
  // when Token::COMMA, only increment length
1808
- items->push_back(NULL);
1860
+ items->push_back(Maybe<Expression>());
1809
1861
  } else {
1810
1862
  Expression* const expr = ParseAssignmentExpression(true, CHECK);
1811
1863
  items->push_back(expr);
@@ -1815,6 +1867,7 @@ class Parser
1815
1867
  }
1816
1868
  }
1817
1869
  Next();
1870
+ assert(items);
1818
1871
  return factory_->NewArrayLiteral(items,
1819
1872
  begin, lexer_.previous_end_position());
1820
1873
  }
@@ -1959,6 +2012,7 @@ class Parser
1959
2012
  }
1960
2013
  const std::size_t end = lexer_.begin_position();
1961
2014
  Next();
2015
+ assert(prop);
1962
2016
  return factory_->NewObjectLiteral(prop, begin, end);
1963
2017
  }
1964
2018
 
@@ -2135,6 +2189,7 @@ class Parser
2135
2189
  }
2136
2190
  Next();
2137
2191
  const std::size_t end_block_position = lexer_.previous_end_position();
2192
+ assert(params && body && scope);
2138
2193
  return factory_->NewFunctionLiteral(decl_type,
2139
2194
  name,
2140
2195
  params,
@@ -52,7 +52,7 @@ namespace phonic {
52
52
  #define V(AST) typedef core::ast::AST<AstFactory> AST;
53
53
  AST_NODE_LIST(V)
54
54
  #undef V
55
- #define V(X, XS) typedef core::SpaceVector<AstFactory, X *>::type XS;
55
+ #define V(XS) typedef core::ast::AstNode<AstFactory>::XS XS;
56
56
  AST_LIST_LIST(V)
57
57
  #undef V
58
58
  #define V(S) typedef core::SpaceUString<AstFactory>::type S;