Block/scope parsing.
This commit is contained in:
+13
@@ -5,6 +5,7 @@ pub enum Stmt {
|
||||
Let(Let),
|
||||
Assign(Assign),
|
||||
Expr(Expr),
|
||||
Block(Block),
|
||||
Fn(FnDef),
|
||||
}
|
||||
|
||||
@@ -26,6 +27,12 @@ impl From<Expr> for Stmt {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Block> for Stmt {
|
||||
fn from(block: Block) -> Self {
|
||||
Stmt::Block(block)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FnDef> for Stmt {
|
||||
fn from(fn_def: FnDef) -> Self {
|
||||
Stmt::Fn(fn_def)
|
||||
@@ -55,6 +62,12 @@ pub struct FnDef {
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Block {
|
||||
pub stmts: Vec<Stmt>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct FnParam {
|
||||
pub name: Ident,
|
||||
|
||||
+42
-1
@@ -1,7 +1,7 @@
|
||||
use std::iter::Peekable;
|
||||
|
||||
use crate::{
|
||||
ast::{Assign, Expr, FnCall, Ident, Let, LitNumber, LitString, Stmt},
|
||||
ast::{Assign, Block, Expr, FnCall, Ident, Let, LitNumber, LitString, Stmt},
|
||||
lexer::Token,
|
||||
span::{Span, Spanned},
|
||||
};
|
||||
@@ -114,6 +114,19 @@ where
|
||||
}
|
||||
.into())
|
||||
}
|
||||
Some(Token::CurlyBracketL) => {
|
||||
let lbrace = self.next().unwrap();
|
||||
let mut stmts = Vec::new();
|
||||
while self.peek() != Some(&Token::CurlyBracketR) {
|
||||
stmts.push(self.parse_stmt()?);
|
||||
}
|
||||
let rbrace = self.next_expected(Token::CurlyBracketR)?;
|
||||
Ok(Block {
|
||||
stmts,
|
||||
span: lbrace.span_to(&rbrace),
|
||||
}
|
||||
.into())
|
||||
}
|
||||
Some(Token::Fn) => {
|
||||
unimplemented!()
|
||||
}
|
||||
@@ -397,6 +410,34 @@ mod tests {
|
||||
pretty_assertions::assert_eq!(parser.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stmt_block() {
|
||||
let input = "{ x = 42; }";
|
||||
let lexer = Lexer::new(input);
|
||||
let mut parser = Parser::new(input, lexer);
|
||||
pretty_assertions::assert_eq!(
|
||||
parser.parse_stmt(),
|
||||
Ok(Stmt::Block(Block {
|
||||
stmts: vec![Stmt::Assign(Assign {
|
||||
name: Ident {
|
||||
name: "x".to_string(),
|
||||
span: Span::new(2, 3),
|
||||
},
|
||||
value: Expr::Literal(
|
||||
LitNumber {
|
||||
value: 42.0,
|
||||
span: Span::new(6, 8)
|
||||
}
|
||||
.into()
|
||||
),
|
||||
span: Span::new(2, 9),
|
||||
})],
|
||||
span: Span::new(0, 11),
|
||||
}))
|
||||
);
|
||||
pretty_assertions::assert_eq!(parser.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expr_numbers() {
|
||||
let input = "500.0";
|
||||
|
||||
Reference in New Issue
Block a user