2022-04-06 13:23:46 +00:00
|
|
|
use std::mem::{Discriminant, discriminant};
|
|
|
|
|
|
|
|
use crate::token::Token;
|
|
|
|
|
2022-04-22 15:49:28 +00:00
|
|
|
use super::{Parsable, WrappedLexer, ParseError, absorb_token_or_error, expr::Expr};
|
2022-04-06 13:23:46 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2022-04-22 15:49:28 +00:00
|
|
|
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))?;
|
2022-04-22 15:49:28 +00:00
|
|
|
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)?;
|
2022-04-22 15:49:28 +00:00
|
|
|
Ok(Self {
|
2022-04-06 13:23:46 +00:00
|
|
|
name,
|
|
|
|
name2,
|
|
|
|
args,
|
|
|
|
type_,
|
|
|
|
value,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
Err(ParseError {
|
|
|
|
expected: Self::expected().0,
|
|
|
|
location: span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|