MCPcopy Index your code
hub / github.com/endbasic/endbasic / parse_callable_body

Method parse_callable_body

core/src/parser.rs:1386–1452  ·  view source on GitHub ↗

Parses the body of a callable and returns the collection of statements and the position of the end of the body.

(
        &mut self,
        start_pos: LineCol,
        exp_token: Token,
    )

Source from the content-addressed store, hash-verified

1384 /// Parses the body of a callable and returns the collection of statements and the position
1385 /// of the end of the body.
1386 fn parse_callable_body(
1387 &mut self,
1388 start_pos: LineCol,
1389 exp_token: Token,
1390 ) -> Result<(Vec<Statement>, LineCol)> {
1391 debug_assert!(matches!(exp_token, Token::Function | Token::Sub));
1392
1393 let mut body = vec![];
1394 let end_pos;
1395 loop {
1396 let peeked = self.lexer.peek()?;
1397 match peeked.token {
1398 Token::Eof => {
1399 end_pos = peeked.pos;
1400 break;
1401 }
1402
1403 Token::Eol => {
1404 self.lexer.consume_peeked();
1405 }
1406
1407 Token::Function | Token::Sub => {
1408 return Err(Error::Bad(
1409 peeked.pos,
1410 "Cannot nest FUNCTION or SUB definitions".to_owned(),
1411 ));
1412 }
1413
1414 Token::End => {
1415 let end_span = self.lexer.consume_peeked();
1416 match self.maybe_parse_end(end_span.pos)? {
1417 Ok(stmt) => {
1418 body.push(stmt);
1419 }
1420 Err(token) if token == exp_token => {
1421 end_pos = end_span.pos;
1422 break;
1423 }
1424 Err(token) => {
1425 return Err(Error::Bad(
1426 end_span.pos,
1427 format!("END {} without {}", token, token),
1428 ));
1429 }
1430 }
1431 }
1432
1433 _ => match self.parse_one_safe()? {
1434 Some(stmt) => body.push(stmt),
1435 None => {
1436 return Err(Error::Bad(
1437 start_pos,
1438 format!("{} without END {}", exp_token, exp_token),
1439 ));
1440 }
1441 },
1442 }
1443 }

Callers 2

parse_functionMethod · 0.80
parse_subMethod · 0.80

Calls 6

consume_peekedMethod · 0.80
maybe_parse_endMethod · 0.80
pushMethod · 0.80
parse_one_safeMethod · 0.80
peekMethod · 0.45

Tested by

no test coverage detected