use super::*;
use leo_errors::{ParserError, Result};
impl ParserContext<'_> {
pub(crate) fn parse_input_file(&mut self) -> Result<InputAst> {
self.allow_identifier_underscores = true;
let mut sections = Vec::new();
while self.has_next() {
if self.check(&Token::LeftSquare) {
sections.push(self.parse_section()?);
} else {
return Err(ParserError::unexpected_token(self.token.token.clone(), self.token.span).into());
}
}
self.allow_identifier_underscores = false;
Ok(InputAst { sections })
}
fn parse_section(&mut self) -> Result<Section> {
self.expect(&Token::LeftSquare)?;
let section = self.expect_identifier()?;
self.expect(&Token::RightSquare)?;
let mut definitions = Vec::new();
while let Token::Constant | Token::Public | Token::Identifier(_) = self.token.token {
definitions.push(self.parse_input_definition()?);
}
Ok(Section { name: section.name, span: section.span, definitions })
}
fn parse_input_definition(&mut self) -> Result<Definition> {
let mode = self.parse_mode()?;
let name = self.expect_identifier()?;
self.expect(&Token::Colon)?;
let (type_, span) = self.parse_type()?;
self.expect(&Token::Assign)?;
let value = self.parse_unary_expression()?;
self.expect(&Token::Semicolon)?;
Ok(Definition { mode, name, type_, value, span })
}
}