Parses the signature of a callable declaration or definition.
(
&mut self,
is_declare: bool,
is_function: bool,
)
| 1471 | |
| 1472 | /// Parses the signature of a callable declaration or definition. |
| 1473 | fn parse_callable_signature( |
| 1474 | &mut self, |
| 1475 | is_declare: bool, |
| 1476 | is_function: bool, |
| 1477 | ) -> Result<(VarRef, LineCol, Vec<VarRef>)> { |
| 1478 | let kw_name = if is_function { "FUNCTION" } else { "SUB" }; |
| 1479 | let token_span = self.lexer.read()?; |
| 1480 | let name = match token_span.token { |
| 1481 | Token::Symbol(name) => match (name.ref_type, is_function) { |
| 1482 | (None, true) => VarRef::new(name.name, Some(ExprType::Integer)), |
| 1483 | (Some(..), true) => name, |
| 1484 | (None, false) => name, |
| 1485 | (Some(..), false) => { |
| 1486 | return Err(Error::Bad( |
| 1487 | token_span.pos, |
| 1488 | "SUBs cannot return a value so type annotations are not allowed".to_owned(), |
| 1489 | )); |
| 1490 | } |
| 1491 | }, |
| 1492 | _ => { |
| 1493 | return Err(Error::Bad( |
| 1494 | token_span.pos, |
| 1495 | format!("Expected a name after {}", kw_name), |
| 1496 | )); |
| 1497 | } |
| 1498 | }; |
| 1499 | let name_pos = token_span.pos; |
| 1500 | |
| 1501 | let params = self.parse_callable_args()?; |
| 1502 | |
| 1503 | let peeked = self.lexer.peek()?; |
| 1504 | match peeked.token { |
| 1505 | Token::Eol => (), |
| 1506 | Token::Eof if is_declare => (), |
| 1507 | _ => { |
| 1508 | return Err(Error::Bad( |
| 1509 | peeked.pos, |
| 1510 | format!("Expected newline after {} name", kw_name), |
| 1511 | )); |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | Ok((name, name_pos, params)) |
| 1516 | } |
| 1517 | |
| 1518 | /// Parses a `FUNCTION` definition. |
| 1519 | fn parse_function(&mut self, function_pos: LineCol) -> Result<Statement> { |
no test coverage detected