@@ -411,6 +411,7 @@ class positional_printer
411
411
adjusted_pos.colno = preempt_pos.colno ;
412
412
}
413
413
preempt_pos = {};
414
+ assert (preempt_pos == source_position{});
414
415
}
415
416
416
417
// (2) Otherwise, see if there's a previous line's offset to repeat
@@ -568,6 +569,12 @@ class cppfront
568
569
parameter_declaration_list_node single_anon;
569
570
// special value - hack for now to note single-anon-return type kind in this function_returns working list
570
571
572
+ struct text_with_pos {
573
+ std::string text;
574
+ source_position pos;
575
+ text_with_pos (std::string const & t, source_position p) : text{t}, pos{p} { }
576
+ };
577
+
571
578
public:
572
579
// -----------------------------------------------------------------------
573
580
// Constructor
@@ -613,21 +620,30 @@ class cppfront
613
620
614
621
// Parse
615
622
//
616
- for (auto const & [line, entry] : tokens.get_map ()) {
617
- if (!parser.parse (entry)) {
623
+ try
624
+ {
625
+ for (auto const & [line, entry] : tokens.get_map ()) {
626
+ if (!parser.parse (entry)) {
627
+ errors.emplace_back (
628
+ source_position (line, 0 ),
629
+ " parse failed for section starting here"
630
+ );
631
+ }
632
+ }
633
+
634
+ // Sema
635
+ parser.visit (sema);
636
+ if (!sema.apply_local_rules ()) {
618
637
errors.emplace_back (
619
- source_position (line, 0 ),
620
- " parse failed for section starting here "
638
+ source_position (- 1 , - 1 ),
639
+ " program violates initialization safety guarantee - see previous errors \n "
621
640
);
622
641
}
623
642
}
624
-
625
- // Sema
626
- parser.visit (sema);
627
- if (!sema.apply_local_rules ()) {
643
+ catch (std::runtime_error& e) {
628
644
errors.emplace_back (
629
645
source_position (-1 , -1 ),
630
- " program violates initialization safety guarantee - see previous errors\n "
646
+ " unexpected text or end-of-file - see previous errors\n "
631
647
);
632
648
}
633
649
}
@@ -671,13 +687,16 @@ class cppfront
671
687
672
688
// First, echo the non-Cpp2 parts
673
689
//
674
- auto cpp2_found = false ;
690
+ auto cpp2_found = false ;
675
691
676
- for (lineno_t curr_lineno = 0 ; auto const & line : source.get_lines ())
692
+ for (
693
+ lineno_t curr_lineno = 0 ;
694
+ auto const & line : source.get_lines ()
695
+ )
677
696
{
678
697
// Skip dummy line we added to make 0-vs-1-based offsets readable
679
- if (curr_lineno != 0 ) {
680
-
698
+ if (curr_lineno != 0 )
699
+ {
681
700
// If it's a Cpp1 line, emit it
682
701
if (line.cat != source_line::category::cpp2)
683
702
{
@@ -769,8 +788,6 @@ class cppfront
769
788
emit (*decl);
770
789
}
771
790
}
772
-
773
- // emit( parser.get_parse_tree() ); // starts at translation_unit_node
774
791
}
775
792
776
793
@@ -1056,7 +1073,7 @@ class cppfront
1056
1073
{
1057
1074
assert (n.identifier );
1058
1075
assert (*n.identifier == " return" );
1059
- printer.print_cpp2 (" return " , n.position ());
1076
+ printer.print_cpp2 (" return" , n.position ());
1060
1077
1061
1078
// Return with expression == single anonymous return type
1062
1079
//
@@ -1219,13 +1236,8 @@ class cppfront
1219
1236
1220
1237
// Otherwise, we're going to have to potentially do some work to change
1221
1238
// some Cpp2 postfix operators to Cpp1 prefix operators, so let's set up...
1222
- struct element {
1223
- std::string text;
1224
- source_position pos;
1225
- element (std::string const & t, source_position p) : text{t}, pos{p} { }
1226
- };
1227
- auto prefix = std::vector<element>{};
1228
- auto suffix = std::vector<element>{};
1239
+ auto prefix = std::vector<text_with_pos>{};
1240
+ auto suffix = std::vector<text_with_pos>{};
1229
1241
1230
1242
auto print = std::string{};
1231
1243
auto emitted_n = false ;
@@ -1359,10 +1371,10 @@ class cppfront
1359
1371
}
1360
1372
1361
1373
printer.print_cpp2 (" static_cast<" , n.position ());
1362
- printer.preempt_position (n.position ());
1374
+ // printer.preempt_position(n.position());
1363
1375
emit (*n.terms .front ().expr );
1364
1376
printer.print_cpp2 (" >(" , n.position ());
1365
- printer.preempt_position (n.position ());
1377
+ // printer.preempt_position(n.position());
1366
1378
emit (*n.expr );
1367
1379
printer.print_cpp2 (" )" , n.position ());
1368
1380
return ;
@@ -1408,29 +1420,39 @@ class cppfront
1408
1420
1409
1421
// -----------------------------------------------------------------------
1410
1422
//
1411
- auto emit (expression_list_node const & n) -> void
1423
+ auto emit (expression_list_node const & n, std::vector<text_with_pos>* as_text = nullptr ) -> void
1412
1424
{
1425
+ auto print = [&](std::string const & text, source_position pos) {
1426
+ if (as_text) {
1427
+ as_text->emplace_back ( text, pos );
1428
+ }
1429
+ else {
1430
+ printer.print_cpp2 ( text, pos );
1431
+
1432
+ }
1433
+ };
1434
+
1413
1435
auto first = true ;
1414
1436
for (auto const & x : n.expressions ) {
1415
1437
if (!first) {
1416
- printer. print_cpp2 (" , " , n.position ());
1438
+ print (" , " , n.position ());
1417
1439
}
1418
1440
first = false ;
1419
1441
1420
1442
if (x.pass != passing_style::in) {
1421
1443
assert (to_string_view (x.pass ) == " out" || to_string_view (x.pass ) == " move" );
1422
1444
if (to_string_view (x.pass ) == " out" ) {
1423
- printer. print_cpp2 (" &" , n.position ());
1445
+ print (" &" , n.position ());
1424
1446
}
1425
1447
else if (to_string_view (x.pass ) == " move" ) {
1426
- printer. print_cpp2 (" std::move(" , n.position ());
1448
+ print (" std::move(" , n.position ());
1427
1449
}
1428
- printer.add_pad_in_this_line (-3 );
1450
+ // printer.add_pad_in_this_line(-3);
1429
1451
}
1430
1452
assert (x.expr );
1431
1453
emit (*x.expr );
1432
1454
if (to_string_view (x.pass ) == " move" ) {
1433
- printer. print_cpp2 (" )" , n.position ());
1455
+ print (" )" , n.position ());
1434
1456
}
1435
1457
}
1436
1458
}
@@ -1635,8 +1657,9 @@ class cppfront
1635
1657
assert (*uid && (**uid).identifier );
1636
1658
if (
1637
1659
*(**uid).identifier == " Default" ||
1638
- *(**uid).identifier == " Bounds" ||
1639
- *(**uid).identifier == " Null" ||
1660
+ *(**uid).identifier == " Bounds" ||
1661
+ *(**uid).identifier == " Null" ||
1662
+ *(**uid).identifier == " Type" ||
1640
1663
*(**uid).identifier == " Testing"
1641
1664
)
1642
1665
{
@@ -1658,14 +1681,10 @@ class cppfront
1658
1681
printer.print_cpp2 (" .expects(" , n.position ());
1659
1682
assert (n.condition );
1660
1683
emit (*n.condition );
1684
+ printer.print_cpp2 (" , " , n.position ());
1685
+ assert (n.message );
1686
+ emit (*n.message );
1661
1687
printer.print_cpp2 (" );" , n.position ());
1662
-
1663
-
1664
-
1665
- // TODO: pass the message through
1666
-
1667
-
1668
-
1669
1688
}
1670
1689
1671
1690
0 commit comments