@@ -1075,10 +1075,13 @@ where
1075
1075
self . next_char ( ) ;
1076
1076
let tok_end = self . get_pos ( ) ;
1077
1077
1078
- // Depending on the nesting level, we emit newline or not:
1078
+ // Depending on the nesting level, we emit a logical or
1079
+ // non-logical newline:
1079
1080
if self . nesting == 0 {
1080
1081
self . at_begin_of_line = true ;
1081
1082
self . emit ( ( tok_start, Tok :: Newline , tok_end) ) ;
1083
+ } else {
1084
+ self . emit ( ( tok_start, Tok :: NonLogicalNewline , tok_end) ) ;
1082
1085
}
1083
1086
}
1084
1087
' ' | '\t' | '\x0C' => {
@@ -1464,7 +1467,16 @@ mod tests {
1464
1467
$(
1465
1468
#[ test]
1466
1469
fn $name( ) {
1467
- let source = format!( "x = [{} 1,2{}]{}" , $eol, $eol, $eol) ;
1470
+ let source = r"x = [
1471
+
1472
+ 1,2
1473
+ ,(3,
1474
+ 4,
1475
+ ), {
1476
+ 5,
1477
+ 6,\
1478
+ 7}]
1479
+ " . replace( "\n " , $eol) ;
1468
1480
let tokens = lex_source( & source) ;
1469
1481
assert_eq!(
1470
1482
tokens,
@@ -1474,9 +1486,32 @@ mod tests {
1474
1486
} ,
1475
1487
Tok :: Equal ,
1476
1488
Tok :: Lsqb ,
1489
+ Tok :: NonLogicalNewline ,
1490
+ Tok :: NonLogicalNewline ,
1477
1491
Tok :: Int { value: BigInt :: from( 1 ) } ,
1478
1492
Tok :: Comma ,
1479
1493
Tok :: Int { value: BigInt :: from( 2 ) } ,
1494
+ Tok :: NonLogicalNewline ,
1495
+ Tok :: Comma ,
1496
+ Tok :: Lpar ,
1497
+ Tok :: Int { value: BigInt :: from( 3 ) } ,
1498
+ Tok :: Comma ,
1499
+ Tok :: NonLogicalNewline ,
1500
+ Tok :: Int { value: BigInt :: from( 4 ) } ,
1501
+ Tok :: Comma ,
1502
+ Tok :: NonLogicalNewline ,
1503
+ Tok :: Rpar ,
1504
+ Tok :: Comma ,
1505
+ Tok :: Lbrace ,
1506
+ Tok :: NonLogicalNewline ,
1507
+ Tok :: Int { value: BigInt :: from( 5 ) } ,
1508
+ Tok :: Comma ,
1509
+ Tok :: NonLogicalNewline ,
1510
+ Tok :: Int { value: BigInt :: from( 6 ) } ,
1511
+ Tok :: Comma ,
1512
+ // Continuation here - no NonLogicalNewline.
1513
+ Tok :: Int { value: BigInt :: from( 7 ) } ,
1514
+ Tok :: Rbrace ,
1480
1515
Tok :: Rsqb ,
1481
1516
Tok :: Newline ,
1482
1517
]
@@ -1492,6 +1527,50 @@ mod tests {
1492
1527
test_newline_in_brackets_unix_eol: UNIX_EOL ,
1493
1528
}
1494
1529
1530
+ #[ test]
1531
+ fn test_non_logical_newline_in_string_continuation ( ) {
1532
+ let source = r"(
1533
+ 'a'
1534
+ 'b'
1535
+
1536
+ 'c' \
1537
+ 'd'
1538
+ )" ;
1539
+ let tokens = lex_source ( source) ;
1540
+ assert_eq ! (
1541
+ tokens,
1542
+ vec![
1543
+ Tok :: Lpar ,
1544
+ Tok :: NonLogicalNewline ,
1545
+ stok( "a" ) ,
1546
+ Tok :: NonLogicalNewline ,
1547
+ stok( "b" ) ,
1548
+ Tok :: NonLogicalNewline ,
1549
+ Tok :: NonLogicalNewline ,
1550
+ stok( "c" ) ,
1551
+ stok( "d" ) ,
1552
+ Tok :: NonLogicalNewline ,
1553
+ Tok :: Rpar ,
1554
+ Tok :: Newline ,
1555
+ ]
1556
+ ) ;
1557
+ }
1558
+
1559
+ #[ test]
1560
+ fn test_logical_newline_line_comment ( ) {
1561
+ let source = "#Hello\n #World" ;
1562
+ let tokens = lex_source ( source) ;
1563
+ assert_eq ! (
1564
+ tokens,
1565
+ vec![
1566
+ Tok :: Comment ( "#Hello" . to_owned( ) ) ,
1567
+ // tokenize.py does put an NL here...
1568
+ Tok :: Comment ( "#World" . to_owned( ) ) ,
1569
+ // ... and here, but doesn't seem very useful.
1570
+ ]
1571
+ ) ;
1572
+ }
1573
+
1495
1574
#[ test]
1496
1575
fn test_operators ( ) {
1497
1576
let source = "//////=/ /" ;
0 commit comments