From c9b85152edcbd2ecb191c6df461da04c40d118aa Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 12 Jun 2022 21:23:09 +0200 Subject: [PATCH] i span my head right round right round --- parser/src/ast.rs | 14 +- parser/src/parser.rs | 109 ++++++---- .../parser__parser__tests__addition.snap | 46 ++-- .../parser__parser__tests__expression.snap | 77 ++++--- .../parser__parser__tests__function.snap | 46 ++-- .../parser__parser__tests__if_else.snap | 10 +- .../parser__parser__tests__if_no_else.snap | 10 +- .../parser__parser__tests__types.snap | 26 ++- .../parser__parser__tests__unary.snap | 196 +++++++++++------- .../parser__parser__tests__var_decl.snap | 14 +- .../parser__parser__tests__while_loop.snap | 10 +- 11 files changed, 342 insertions(+), 216 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 802eaee..367c485 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -104,7 +104,13 @@ pub struct LoopStmt { } #[derive(Debug, Clone, PartialEq)] -pub enum Expr { +pub struct Expr { + pub kind: ExprKind, + pub span: Span, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum ExprKind { BinOp(BinOp), UnaryOp(UnaryOp), FieldAccess(FieldAccess), @@ -123,7 +129,7 @@ pub struct BinOp { pub span: Span, } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum BinOpKind { Eq, Neq, @@ -152,7 +158,7 @@ pub struct UnaryOp { pub span: Span, } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum UnaryOpKind { Not, Neg, @@ -172,7 +178,7 @@ pub struct Call { pub args: Vec, } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Literal { String(String, Span), Integer(u64, Span), diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 7fa35ce..73cd54c 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -4,8 +4,9 @@ use chumsky::{prelude::*, Stream}; use crate::{ ast::{ - Assignment, BinOp, BinOpKind, Call, ElsePart, Expr, File, FnDecl, IfStmt, Item, Literal, - NameTyPair, Stmt, StructDecl, Ty, TyKind, UnaryOp, UnaryOpKind, VarDecl, WhileStmt, + Assignment, BinOp, BinOpKind, Call, ElsePart, Expr, ExprKind, File, FnDecl, IfStmt, Item, + Literal, NameTyPair, Stmt, StructDecl, Ty, TyKind, UnaryOp, UnaryOpKind, VarDecl, + WhileStmt, }, lexer::Token, }; @@ -52,13 +53,19 @@ fn ty_parser<'src>() -> impl Parser, Ty, Error = Error<'src>> + Clon fn expr_parser<'src>() -> impl Parser, Expr, Error = Error<'src>> + Clone { recursive(|expr| { - let literal = filter_map(|span, token| match token { - Token::String(str) => Ok(Expr::Literal(Literal::String( - str[1..str.len() - 2].to_owned(), + let literal = filter_map(|span: Span, token| match token { + Token::String(str) => Ok(Expr { + kind: ExprKind::Literal(Literal::String( + str[1..str.len() - 2].to_owned(), + span.clone(), + )), span, - ))), + }), // todo lol unwrap - Token::Integer(int) => Ok(Expr::Literal(Literal::Integer(int.parse().unwrap(), span))), + Token::Integer(int) => Ok(Expr { + kind: ExprKind::Literal(Literal::Integer(int.parse().unwrap(), span.clone())), + span, + }), _ => Err(Simple::expected_input_found(span, Vec::new(), Some(token))), }) .labelled("literal"); @@ -74,15 +81,20 @@ fn expr_parser<'src>() -> impl Parser, Expr, Error = Error<'src>> + let array = expr_list .clone() .delimited_by(just(Token::BracketO), just(Token::BracketC)) - .map(Expr::Array); + .map_with_span(|exprs: Vec, span| Expr { + kind: ExprKind::Array(exprs), + span, + }); let atom = literal - .or(ident_parser().map(Expr::Name)) + .or(ident_parser().map_with_span(|name, span| Expr { + kind: ExprKind::Name(name), + span, + })) .or(array) .or(expr .clone() .delimited_by(just(Token::ParenO), just(Token::ParenC))) - .debug("atom") .boxed(); let call = atom @@ -93,10 +105,15 @@ fn expr_parser<'src>() -> impl Parser, Expr, Error = Error<'src>> + .repeated(), ) .foldl(|callee: Expr, args: Vec| { - Expr::Call(Call { - callee: Box::new(callee), - args, - }) + let span = + callee.span.start..args.last().map(|e| e.span.end).unwrap_or(callee.span.end); + Expr { + kind: ExprKind::Call(Call { + callee: Box::new(callee), + args, + }), + span, + } }) .labelled("call") .boxed(); @@ -110,14 +127,17 @@ fn expr_parser<'src>() -> impl Parser, Expr, Error = Error<'src>> + .repeated() .then(call) .foldr(|kind, rhs| { - Expr::UnaryOp(UnaryOp { - expr: Box::new(rhs), - kind, - span: 0..0, // lol todo - }) + let span = rhs.span.clone(); + Expr { + kind: ExprKind::UnaryOp(UnaryOp { + expr: Box::new(rhs), + kind, + span: span.clone(), + }), + span, + } }) .labelled("unary") - .debug("unary") .boxed(); let op = just(Token::Asterisk) @@ -128,12 +148,16 @@ fn expr_parser<'src>() -> impl Parser, Expr, Error = Error<'src>> + .clone() .then(op.then(unary_op).repeated()) .foldl(|a, (kind, b)| { - Expr::BinOp(BinOp { - kind, - lhs: Box::new(a), - rhs: Box::new(b), - span: 0..0, // lol todo - }) + let span = a.span.start..b.span.end; + Expr { + kind: ExprKind::BinOp(BinOp { + kind, + lhs: Box::new(a), + rhs: Box::new(b), + span: span.clone(), + }), + span, + } }); // Sum ops (add and subtract) have equal precedence @@ -144,15 +168,18 @@ fn expr_parser<'src>() -> impl Parser, Expr, Error = Error<'src>> + .clone() .then(op.then(product).repeated()) .foldl(|a, (kind, b)| { - Expr::BinOp(BinOp { - kind, - lhs: Box::new(a), - rhs: Box::new(b), - span: 0..0, // lol todo - }) + let span = a.span.start..b.span.end; + Expr { + kind: ExprKind::BinOp(BinOp { + kind, + lhs: Box::new(a), + rhs: Box::new(b), + span: span.clone(), + }), + span, + } }) .labelled("product") - .debug("product") .boxed(); // Comparison ops (equal, not-equal) have equal precedence @@ -163,12 +190,16 @@ fn expr_parser<'src>() -> impl Parser, Expr, Error = Error<'src>> + .clone() .then(op.then(sum).repeated()) .foldl(|a, (kind, b)| { - Expr::BinOp(BinOp { - kind, - lhs: Box::new(a), - rhs: Box::new(b), - span: 0..0, // lol todo - }) + let span = a.span.start..b.span.end; + Expr { + kind: ExprKind::BinOp(BinOp { + kind, + lhs: Box::new(a), + rhs: Box::new(b), + span: span.clone(), + }), + span, + } }); compare.labelled("comparison").boxed() }) diff --git a/parser/src/snapshots/parser__parser__tests__addition.snap b/parser/src/snapshots/parser__parser__tests__addition.snap index 1da1d84..147a6ea 100644 --- a/parser/src/snapshots/parser__parser__tests__addition.snap +++ b/parser/src/snapshots/parser__parser__tests__addition.snap @@ -1,6 +1,5 @@ --- source: parser/src/parser.rs -assertion_line: 332 expression: r --- ( @@ -16,24 +15,33 @@ expression: r span: 0..20, body: [ Expr( - BinOp( - BinOp { - kind: Add, - lhs: Literal( - Integer( - 1, - 12..13, - ), - ), - rhs: Literal( - Integer( - 4, - 16..17, - ), - ), - span: 0..0, - }, - ), + Expr { + kind: BinOp( + BinOp { + kind: Add, + lhs: Expr { + kind: Literal( + Integer( + 1, + 12..13, + ), + ), + span: 12..13, + }, + rhs: Expr { + kind: Literal( + Integer( + 4, + 16..17, + ), + ), + span: 16..17, + }, + span: 12..17, + }, + ), + span: 12..17, + }, ), ], }, diff --git a/parser/src/snapshots/parser__parser__tests__expression.snap b/parser/src/snapshots/parser__parser__tests__expression.snap index bb296e6..5383760 100644 --- a/parser/src/snapshots/parser__parser__tests__expression.snap +++ b/parser/src/snapshots/parser__parser__tests__expression.snap @@ -1,6 +1,5 @@ --- source: parser/src/parser.rs -assertion_line: 338 expression: r --- ( @@ -16,38 +15,56 @@ expression: r span: 0..32, body: [ Expr( - BinOp( - BinOp { - kind: Add, - lhs: BinOp( - BinOp { - kind: Div, - lhs: Literal( - Integer( - 4, - 13..14, - ), - ), - rhs: Call( - Call { - callee: Name( - "hallo", - ), - args: [], + Expr { + kind: BinOp( + BinOp { + kind: Add, + lhs: Expr { + kind: BinOp( + BinOp { + kind: Div, + lhs: Expr { + kind: Literal( + Integer( + 4, + 13..14, + ), + ), + span: 13..14, + }, + rhs: Expr { + kind: Call( + Call { + callee: Expr { + kind: Name( + "hallo", + ), + span: 17..22, + }, + args: [], + }, + ), + span: 17..22, + }, + span: 13..22, }, ), - span: 0..0, + span: 13..22, }, - ), - rhs: Literal( - Integer( - 5, - 28..29, - ), - ), - span: 0..0, - }, - ), + rhs: Expr { + kind: Literal( + Integer( + 5, + 28..29, + ), + ), + span: 28..29, + }, + span: 13..29, + }, + ), + span: 13..29, + }, ), ], }, diff --git a/parser/src/snapshots/parser__parser__tests__function.snap b/parser/src/snapshots/parser__parser__tests__function.snap index 707c4ee..c7f2740 100644 --- a/parser/src/snapshots/parser__parser__tests__function.snap +++ b/parser/src/snapshots/parser__parser__tests__function.snap @@ -1,6 +1,5 @@ --- source: parser/src/parser.rs -assertion_line: 344 expression: r --- ( @@ -21,24 +20,33 @@ expression: r span: 0..26, body: [ Expr( - BinOp( - BinOp { - kind: Add, - lhs: Literal( - Integer( - 1, - 18..19, - ), - ), - rhs: Literal( - Integer( - 5, - 22..23, - ), - ), - span: 0..0, - }, - ), + Expr { + kind: BinOp( + BinOp { + kind: Add, + lhs: Expr { + kind: Literal( + Integer( + 1, + 18..19, + ), + ), + span: 18..19, + }, + rhs: Expr { + kind: Literal( + Integer( + 5, + 22..23, + ), + ), + span: 22..23, + }, + span: 18..23, + }, + ), + span: 18..23, + }, ), ], }, diff --git a/parser/src/snapshots/parser__parser__tests__if_else.snap b/parser/src/snapshots/parser__parser__tests__if_else.snap index 0788927..503eab1 100644 --- a/parser/src/snapshots/parser__parser__tests__if_else.snap +++ b/parser/src/snapshots/parser__parser__tests__if_else.snap @@ -1,6 +1,5 @@ --- source: parser/src/parser.rs -assertion_line: 356 expression: r --- ( @@ -22,9 +21,12 @@ expression: r body: [ IfStmt( IfStmt { - cond: Name( - "false", - ), + cond: Expr { + kind: Name( + "false", + ), + span: 21..26, + }, body: [], else_part: Some( Else( diff --git a/parser/src/snapshots/parser__parser__tests__if_no_else.snap b/parser/src/snapshots/parser__parser__tests__if_no_else.snap index dfd96ff..724f153 100644 --- a/parser/src/snapshots/parser__parser__tests__if_no_else.snap +++ b/parser/src/snapshots/parser__parser__tests__if_no_else.snap @@ -1,6 +1,5 @@ --- source: parser/src/parser.rs -assertion_line: 350 expression: r --- ( @@ -22,9 +21,12 @@ expression: r body: [ IfStmt( IfStmt { - cond: Name( - "false", - ), + cond: Expr { + kind: Name( + "false", + ), + span: 21..26, + }, body: [], else_part: None, span: 18..29, diff --git a/parser/src/snapshots/parser__parser__tests__types.snap b/parser/src/snapshots/parser__parser__tests__types.snap index f84cca4..727cae5 100644 --- a/parser/src/snapshots/parser__parser__tests__types.snap +++ b/parser/src/snapshots/parser__parser__tests__types.snap @@ -34,12 +34,15 @@ expression: r ), }, rhs: Some( - Literal( - Integer( - 2, - 36..37, + Expr { + kind: Literal( + Integer( + 2, + 36..37, + ), ), - ), + span: 36..37, + }, ), span: 0..0, }, @@ -57,12 +60,15 @@ expression: r ), }, rhs: Some( - Literal( - Integer( - 25, - 53..55, + Expr { + kind: Literal( + Integer( + 25, + 53..55, + ), ), - ), + span: 53..55, + }, ), span: 0..0, }, diff --git a/parser/src/snapshots/parser__parser__tests__unary.snap b/parser/src/snapshots/parser__parser__tests__unary.snap index 9fb6dbd..6f19a7e 100644 --- a/parser/src/snapshots/parser__parser__tests__unary.snap +++ b/parser/src/snapshots/parser__parser__tests__unary.snap @@ -15,96 +15,138 @@ expression: r span: 0..63, body: [ Expr( - UnaryOp( - UnaryOp { - expr: UnaryOp( - UnaryOp { - expr: Literal( + Expr { + kind: UnaryOp( + UnaryOp { + expr: Expr { + kind: UnaryOp( + UnaryOp { + expr: Expr { + kind: Literal( + Integer( + 5, + 19..20, + ), + ), + span: 19..20, + }, + kind: Deref, + span: 19..20, + }, + ), + span: 19..20, + }, + kind: Neg, + span: 19..20, + }, + ), + span: 19..20, + }, + ), + Expr( + Expr { + kind: UnaryOp( + UnaryOp { + expr: Expr { + kind: Literal( Integer( 5, - 19..20, + 28..29, ), ), - kind: Deref, - span: 0..0, + span: 28..29, }, - ), - kind: Neg, - span: 0..0, - }, - ), + kind: AddrOf, + span: 28..29, + }, + ), + span: 28..29, + }, ), Expr( - UnaryOp( - UnaryOp { - expr: Literal( - Integer( - 5, - 28..29, - ), - ), - kind: AddrOf, - span: 0..0, - }, - ), + Expr { + kind: BinOp( + BinOp { + kind: Add, + lhs: Expr { + kind: Literal( + Integer( + 2, + 35..36, + ), + ), + span: 35..36, + }, + rhs: Expr { + kind: UnaryOp( + UnaryOp { + expr: Expr { + kind: Literal( + Integer( + 8, + 40..41, + ), + ), + span: 40..41, + }, + kind: AddrOf, + span: 40..41, + }, + ), + span: 40..41, + }, + span: 35..41, + }, + ), + span: 35..41, + }, ), Expr( - BinOp( - BinOp { - kind: Add, - lhs: Literal( - Integer( - 2, - 35..36, - ), - ), - rhs: UnaryOp( - UnaryOp { - expr: Literal( - Integer( - 8, - 40..41, - ), + Expr { + kind: BinOp( + BinOp { + kind: Mul, + lhs: Expr { + kind: UnaryOp( + UnaryOp { + expr: Expr { + kind: Literal( + Integer( + 6, + 48..49, + ), + ), + span: 48..49, + }, + kind: Deref, + span: 48..49, + }, ), - kind: AddrOf, - span: 0..0, + span: 48..49, }, - ), - span: 0..0, - }, - ), - ), - Expr( - BinOp( - BinOp { - kind: Mul, - lhs: UnaryOp( - UnaryOp { - expr: Literal( - Integer( - 6, - 48..49, - ), + rhs: Expr { + kind: UnaryOp( + UnaryOp { + expr: Expr { + kind: Literal( + Integer( + 8, + 53..54, + ), + ), + span: 53..54, + }, + kind: Deref, + span: 53..54, + }, ), - kind: Deref, - span: 0..0, + span: 53..54, }, - ), - rhs: UnaryOp( - UnaryOp { - expr: Literal( - Integer( - 8, - 53..54, - ), - ), - kind: Deref, - span: 0..0, - }, - ), - span: 0..0, - }, - ), + span: 48..54, + }, + ), + span: 48..54, + }, ), ], }, diff --git a/parser/src/snapshots/parser__parser__tests__var_decl.snap b/parser/src/snapshots/parser__parser__tests__var_decl.snap index 3242ff7..69d191d 100644 --- a/parser/src/snapshots/parser__parser__tests__var_decl.snap +++ b/parser/src/snapshots/parser__parser__tests__var_decl.snap @@ -1,6 +1,5 @@ --- source: parser/src/parser.rs -assertion_line: 368 expression: r --- ( @@ -28,12 +27,15 @@ expression: r kind: U64, }, rhs: Some( - Literal( - Integer( - 5, - 30..31, + Expr { + kind: Literal( + Integer( + 5, + 30..31, + ), ), - ), + span: 30..31, + }, ), span: 0..0, }, diff --git a/parser/src/snapshots/parser__parser__tests__while_loop.snap b/parser/src/snapshots/parser__parser__tests__while_loop.snap index 59261cb..bfc574c 100644 --- a/parser/src/snapshots/parser__parser__tests__while_loop.snap +++ b/parser/src/snapshots/parser__parser__tests__while_loop.snap @@ -1,6 +1,5 @@ --- source: parser/src/parser.rs -assertion_line: 362 expression: r --- ( @@ -22,9 +21,12 @@ expression: r body: [ WhileStmt( WhileStmt { - cond: Name( - "false", - ), + cond: Expr { + kind: Name( + "false", + ), + span: 24..29, + }, body: [], span: 18..32, },