Amo/src/parser/declaration.rs

51 lines
1.1 KiB
Rust
Raw Normal View History

2022-04-06 13:23:46 +00:00
use std::mem::{Discriminant, discriminant};
use crate::token::Token;
use super::{Parsable, WrappedLexer, ParseError, absorb_token_or_error, expr::Expr};
2022-04-06 13:23:46 +00:00
#[derive(Debug)]
pub struct Declaration {
name: String,
type_: Expr,
name2: String,
args: Vec<String>,
value: Expr,
2022-04-06 13:23:46 +00:00
}
impl Parsable for Declaration {
fn expected() -> (Vec<Discriminant<Token>>, bool) {
(vec![
discriminant(&Token::Symbol("".to_string())),
], false)
}
fn parse(l: &mut WrappedLexer) -> Result<Self, ParseError> {
let span = l.span();
match l.monch() {
// Symbol Colon <full_type> DeclarationStart Symbol { Symbol } Assign <expr>
Token::Symbol(name) => {
absorb_token_or_error(l, discriminant(&Token::Colon))?;
let type_ = Expr::parse(l)?;
2022-04-06 13:23:46 +00:00
absorb_token_or_error(l, discriminant(&Token::DeclarationStart))?;
let name2 = String::parse(l)?;
let args = Vec::parse(l)?;
absorb_token_or_error(l, discriminant(&Token::Assign))?;
let value = Expr::parse(l)?;
Ok(Self {
2022-04-06 13:23:46 +00:00
name,
name2,
args,
type_,
value,
})
},
_ => {
Err(ParseError {
expected: Self::expected().0,
location: span,
})
}
}
}
}