2
2
// FIXME: consider parsing [[ exr ]] as keywords? otherwise [[ foo && bar ]] won't get parsed right
3
3
// FIXME: consider parsing out array index syntax? (e.g. ${array[some index]}
4
4
// FIXME: arithmetic substitutions don't currently support param/comand substitutions
5
+ // TODO: To retrieve source/iterator positions, see commit 3e4fac7 or earlier
5
6
6
7
use std:: error:: Error ;
7
8
use std:: fmt;
@@ -290,10 +291,9 @@ impl<I: Iterator<Item = Token>> Parser<I> {
290
291
/// token in the iterator will be used (or `UnexpectedEOF` if none left).
291
292
#[ inline]
292
293
fn make_unexpected_err ( & mut self ) -> ParseError {
293
- let pos = self . iter . pos ( ) ;
294
294
self . iter
295
295
. next ( )
296
- . map_or ( ParseError :: UnexpectedEOF , |t| ParseError :: Unexpected ( t ) )
296
+ . map_or ( ParseError :: UnexpectedEOF , ParseError :: Unexpected )
297
297
}
298
298
299
299
/// Parses a single complete command.
@@ -496,7 +496,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
496
496
let mut list = Vec :: new ( ) ;
497
497
loop {
498
498
self . skip_whitespace ( ) ;
499
- let start_pos = self . iter . pos ( ) ;
500
499
match self . redirect ( ) ? {
501
500
Some ( Ok ( io) ) => list. push ( io) ,
502
501
Some ( Err ( _) ) => return Err ( ParseError :: BadFd ) ,
@@ -608,7 +607,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
608
607
dash. to_string( ) ,
609
608
) ) )
610
609
} else {
611
- let path_start_pos = $parser. iter. pos( ) ;
612
610
let path = if let Some ( p) = $parser. word_preserve_trailing_whitespace( ) ? {
613
611
p
614
612
} else {
@@ -899,7 +897,7 @@ impl<I: Iterator<Item = Token>> Parser<I> {
899
897
900
898
let mut tok_backup = TokenIterWrapper :: Buffered ( tok_iter) ;
901
899
mem:: swap ( & mut self . iter , & mut tok_backup) ;
902
- let mut body = self . word_interpolated_raw ( None , heredoc_start_pos ) ?;
900
+ let mut body = self . word_interpolated_raw ( None ) ?;
903
901
let _ = mem:: replace ( & mut self . iter , tok_backup) ;
904
902
905
903
if body. len ( ) > 1 {
@@ -1025,7 +1023,7 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1025
1023
}
1026
1024
1027
1025
DoubleQuote => ast:: Word :: DoubleQuoted (
1028
- self . word_interpolated_raw ( Some ( ( DoubleQuote , DoubleQuote ) ) , start_pos ) ?,
1026
+ self . word_interpolated_raw ( Some ( ( DoubleQuote , DoubleQuote ) ) ) ?,
1029
1027
) ,
1030
1028
1031
1029
// Parameters and backticks should have been
@@ -1070,7 +1068,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1070
1068
fn word_interpolated_raw (
1071
1069
& mut self ,
1072
1070
delim : Option < ( Token , Token ) > ,
1073
- start_pos : SourcePos ,
1074
1071
) -> ParseResult < Vec < ast:: SimpleWord > > {
1075
1072
let ( delim_open, delim_close) = match delim {
1076
1073
Some ( ( o, c) ) => ( Some ( o) , Some ( c) ) ,
@@ -1188,7 +1185,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1188
1185
/// returns an `Word`, which will capture both cases where a literal or
1189
1186
/// parameter is parsed.
1190
1187
fn parameter_raw ( & mut self ) -> ParseResult < ast:: SimpleWord > {
1191
- let start_pos = self . iter . pos ( ) ;
1192
1188
match self . iter . next ( ) {
1193
1189
Some ( ParamPositional ( p) ) => {
1194
1190
Ok ( ast:: SimpleWord :: Param ( Parameter :: Positional ( p as usize ) ) )
@@ -1215,10 +1211,7 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1215
1211
///
1216
1212
/// All tokens that normally cannot be part of a word will be treated
1217
1213
/// as literals.
1218
- fn parameter_substitution_word_raw (
1219
- & mut self ,
1220
- curly_open_pos : SourcePos ,
1221
- ) -> ParseResult < Option < ast:: ComplexWord > > {
1214
+ fn parameter_substitution_word_raw ( & mut self ) -> ParseResult < Option < ast:: ComplexWord > > {
1222
1215
let mut words = Vec :: new ( ) ;
1223
1216
' capture_words: loop {
1224
1217
' capture_literals: loop {
@@ -1337,14 +1330,12 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1337
1330
fn parameter_substitution_body_raw (
1338
1331
& mut self ,
1339
1332
param : Parameter ,
1340
- curly_open_pos : SourcePos ,
1341
1333
) -> ParseResult < ast:: SimpleWord > {
1342
1334
let has_colon = eat_maybe ! ( self , {
1343
1335
Colon => { true } ;
1344
1336
_ => { false } ,
1345
1337
} ) ;
1346
1338
1347
- let op_pos = self . iter . pos ( ) ;
1348
1339
let op = match self . iter . next ( ) {
1349
1340
Some ( tok @ Dash ) | Some ( tok @ Equals ) | Some ( tok @ Question ) | Some ( tok @ Plus ) => tok,
1350
1341
@@ -1354,7 +1345,7 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1354
1345
None => return Err ( ParseError :: Unmatched ( CurlyOpen ) ) ,
1355
1346
} ;
1356
1347
1357
- let word = self . parameter_substitution_word_raw ( curly_open_pos ) ?;
1348
+ let word = self . parameter_substitution_word_raw ( ) ?;
1358
1349
let maybe_len = param == Parameter :: Pound && !has_colon && word. is_none ( ) ;
1359
1350
1360
1351
// We must carefully check if we get ${#-} or ${#?}, in which case
@@ -1380,7 +1371,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1380
1371
/// Parses a parameter substitution in the form of `${...}`, `$(...)`, or `$((...))`.
1381
1372
/// Nothing is passed to the builder.
1382
1373
fn parameter_substitution_raw ( & mut self ) -> ParseResult < ast:: SimpleWord > {
1383
- let start_pos = self . iter . pos ( ) ;
1384
1374
match self . iter . peek ( ) {
1385
1375
Some ( & ParenOpen ) => {
1386
1376
let is_arith = {
@@ -1418,7 +1408,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1418
1408
}
1419
1409
1420
1410
Some ( & CurlyOpen ) => {
1421
- let curly_open_pos = start_pos;
1422
1411
self . iter . next ( ) ;
1423
1412
1424
1413
let param = self . parameter_inner ( ) ?;
@@ -1427,11 +1416,11 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1427
1416
self . iter . next ( ) ;
1428
1417
eat_maybe ! ( self , {
1429
1418
Percent => {
1430
- let word = self . parameter_substitution_word_raw( curly_open_pos ) ;
1419
+ let word = self . parameter_substitution_word_raw( ) ;
1431
1420
ast:: ParameterSubstitution :: RemoveLargestSuffix ( param, word?. map( Box :: new) )
1432
1421
} ;
1433
1422
_ => {
1434
- let word = self . parameter_substitution_word_raw( curly_open_pos ) ;
1423
+ let word = self . parameter_substitution_word_raw( ) ;
1435
1424
ast:: ParameterSubstitution :: RemoveSmallestSuffix ( param, word?. map( Box :: new) )
1436
1425
}
1437
1426
} )
@@ -1441,11 +1430,11 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1441
1430
self . iter . next ( ) ;
1442
1431
eat_maybe ! ( self , {
1443
1432
Pound => {
1444
- let word = self . parameter_substitution_word_raw( curly_open_pos ) ;
1433
+ let word = self . parameter_substitution_word_raw( ) ;
1445
1434
ast:: ParameterSubstitution :: RemoveLargestPrefix ( param, word?. map( Box :: new) )
1446
1435
} ;
1447
1436
_ => {
1448
- match self . parameter_substitution_word_raw( curly_open_pos ) ? {
1437
+ match self . parameter_substitution_word_raw( ) ? {
1449
1438
// Handle ${##} case
1450
1439
None if Parameter :: Pound == param => ast:: ParameterSubstitution :: Len ( Parameter :: Pound ) ,
1451
1440
w => ast:: ParameterSubstitution :: RemoveSmallestPrefix ( param, w. map( Box :: new) ) ,
@@ -1459,7 +1448,7 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1459
1448
| Some ( & CurlyClose )
1460
1449
if Parameter :: Pound == param =>
1461
1450
{
1462
- return self . parameter_substitution_body_raw ( param, curly_open_pos )
1451
+ return self . parameter_substitution_body_raw ( param)
1463
1452
}
1464
1453
1465
1454
// Otherwise we must have ${#param}
@@ -1468,7 +1457,7 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1468
1457
eat ! ( self , { CurlyClose => { ast:: ParameterSubstitution :: Len ( param) } } )
1469
1458
}
1470
1459
1471
- _ => return self . parameter_substitution_body_raw ( param, curly_open_pos ) ,
1460
+ _ => return self . parameter_substitution_body_raw ( param) ,
1472
1461
} ;
1473
1462
1474
1463
Ok ( ast:: SimpleWord :: Subst ( subst) )
@@ -1480,7 +1469,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1480
1469
1481
1470
/// Parses a valid parameter that can appear inside a set of curly braces.
1482
1471
fn parameter_inner ( & mut self ) -> ParseResult < Parameter > {
1483
- let start_pos = self . iter . pos ( ) ;
1484
1472
let param = match self . iter . next ( ) {
1485
1473
Some ( Star ) => Parameter :: Star ,
1486
1474
Some ( Pound ) => Parameter :: Pound ,
@@ -1507,7 +1495,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1507
1495
/// reserved words. Each of the reserved words must be a literal token, and cannot be
1508
1496
/// quoted or concatenated.
1509
1497
pub fn do_group ( & mut self ) -> ParseResult < CommandGroup < ast:: Command > > {
1510
- let start_pos = self . iter . pos ( ) ;
1511
1498
self . reserved_word ( & [ DO ] )
1512
1499
. map_err ( |_| self . make_unexpected_err ( ) ) ?;
1513
1500
let result = self . command_group ( CommandGroupDelimiters {
@@ -1524,7 +1511,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1524
1511
pub fn brace_group ( & mut self ) -> ParseResult < CommandGroup < ast:: Command > > {
1525
1512
// CurlyClose must be encountered as a stand alone word,
1526
1513
// even though it is represented as its own token
1527
- let start_pos = self . iter . pos ( ) ;
1528
1514
self . reserved_token ( & [ CurlyOpen ] ) ?;
1529
1515
let cmds = self . command_group ( CommandGroupDelimiters {
1530
1516
reserved_tokens : & [ CurlyClose ] ,
@@ -1548,7 +1534,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1548
1534
& mut self ,
1549
1535
empty_body_ok : bool ,
1550
1536
) -> ParseResult < CommandGroup < ast:: Command > > {
1551
- let start_pos = self . iter . pos ( ) ;
1552
1537
eat ! ( self , { ParenOpen => { } } ) ;
1553
1538
1554
1539
// Parens are always special tokens
@@ -1731,7 +1716,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1731
1716
/// the entire loop) this method returns the relevant parts of the loop command,
1732
1717
/// without constructing an AST node, it so that the caller can do so with redirections.
1733
1718
pub fn loop_command ( & mut self ) -> ParseResult < ( LoopKind , ast:: GuardBodyPair ) > {
1734
- let start_pos = self . iter . pos ( ) ;
1735
1719
let kind = match self
1736
1720
. reserved_word ( & [ WHILE , UNTIL ] )
1737
1721
. map_err ( |_| self . make_unexpected_err ( ) ) ?
@@ -1762,7 +1746,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1762
1746
/// method returns the relevant parts of the `if` command, without constructing an
1763
1747
/// AST node, it so that the caller can do so with redirections.
1764
1748
pub fn if_command ( & mut self ) -> ParseResult < IfFragments < ast:: Command > > {
1765
- let start_pos = self . iter . pos ( ) ;
1766
1749
self . reserved_word ( & [ IF ] )
1767
1750
. map_err ( |_| self . make_unexpected_err ( ) ) ?;
1768
1751
@@ -1825,7 +1808,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1825
1808
/// method returns the relevant parts of the `for` command, without constructing an
1826
1809
/// AST node, it so that the caller can do so with redirections.
1827
1810
pub fn for_command ( & mut self ) -> ParseResult < ForFragments < ast:: ComplexWord , ast:: Command > > {
1828
- let start_pos = self . iter . pos ( ) ;
1829
1811
self . reserved_word ( & [ FOR ] )
1830
1812
. map_err ( |_| self . make_unexpected_err ( ) ) ?;
1831
1813
@@ -1836,7 +1818,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1836
1818
_ => return Err ( self . make_unexpected_err ( ) ) ,
1837
1819
}
1838
1820
1839
- let var_pos = self . iter . pos ( ) ;
1840
1821
let var = match self . iter . next ( ) {
1841
1822
Some ( Name ( v) ) => v,
1842
1823
Some ( Literal ( s) ) => return Err ( ParseError :: BadIdent ( s) ) ,
@@ -1901,8 +1882,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
1901
1882
/// method returns the relevant parts of the `case` command, without constructing an
1902
1883
/// AST node, it so that the caller can do so with redirections.
1903
1884
pub fn case_command ( & mut self ) -> ParseResult < CaseFragments < ast:: ComplexWord > > {
1904
- let start_pos = self . iter . pos ( ) ;
1905
-
1906
1885
macro_rules! missing_in {
1907
1886
( ) => {
1908
1887
|_| ParseError :: IncompleteCmd ( CASE , IN )
@@ -2054,7 +2033,6 @@ impl<I: Iterator<Item = Token>> Parser<I> {
2054
2033
_ => return Err ( self . make_unexpected_err ( ) ) ,
2055
2034
}
2056
2035
2057
- let ident_pos = self . iter . pos ( ) ;
2058
2036
let name = match self . iter . next ( ) {
2059
2037
Some ( Name ( n) ) => n,
2060
2038
Some ( Literal ( s) ) => return Err ( ParseError :: BadIdent ( s) ) ,
0 commit comments