diff options
Diffstat (limited to 'sloth/src/parser/mod.rs')
| -rw-r--r-- | sloth/src/parser/mod.rs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/sloth/src/parser/mod.rs b/sloth/src/parser/mod.rs index 748a0da..6dd534a 100644 --- a/sloth/src/parser/mod.rs +++ b/sloth/src/parser/mod.rs @@ -3,7 +3,7 @@ pub mod expr; pub mod graph; pub mod stmt; -use self::ast::{Literal, Stmt, StmtKind}; +use self::ast::{Literal, Stmt, StmtKind, TypeIdentifier}; use crate::lexer::{Token, TokenType}; use crate::symtable::SymbolTable; @@ -117,6 +117,31 @@ impl<'a> AstParser<'a> { Ok(identifier) } + pub fn consume_type(&mut self) -> Result<TypeIdentifier, ParsingError> { + let is_list = self.peek().tt == TokenType::OpeningBracket; + + if is_list { + self.consume(TokenType::OpeningBracket, "Expected '['")?; + } + + let name = self.consume_identifier()?; + + let mut list_len = 0; + if is_list { + if let Literal::Integer(i) = self.consume_literal()? { + list_len = i as u32; + } + + self.consume(TokenType::ClosingBracket, "Expected ']'")?; + } + + Ok(TypeIdentifier { + name, + is_list, + list_len, + }) + } + pub fn reserve_id(&mut self) -> i32 { let id = self.id; self.id += 1; |
