Skip to content

Commit 64f5f6e

Browse files
committed
feat: parse match
1 parent dbdf8ac commit 64f5f6e

File tree

6 files changed

+192
-67
lines changed

6 files changed

+192
-67
lines changed

packages/engine/src/component.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,31 @@ pub trait ComponentIter<'a, C, T, I> where
8282

8383
/// Expects a item to be there
8484
fn expect(&mut self, expected: &C) -> std::result::Result<T, Option<T>> {
85-
let Some(item) = self.next_if_eq(expected) else {
85+
let Some(item) = self.peek() else {
8686
return Err(None);
8787
};
8888

89-
if &item == expected {
90-
Ok(item)
89+
if expected == item {
90+
let cloned = item.clone();
91+
self.next();
92+
Ok(cloned)
93+
} else {
94+
Err(Some(item.clone()))
95+
}
96+
}
97+
98+
/// Expects one of an item to be there
99+
fn expect_any(&mut self, expected: &[C]) -> std::result::Result<T, Option<T>> {
100+
let Some(item) = self.peek() else {
101+
return Err(None);
102+
};
103+
104+
if expected.iter().any(|t| t == item) {
105+
let cloned = item.clone();
106+
self.next();
107+
Ok(cloned)
91108
} else {
92-
Err(Some(item))
109+
Err(Some(item.clone()))
93110
}
94111
}
95112

packages/engine/src/cursor.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt::Debug;
1+
use std::{fmt::Debug, hash::Hash};
22

33
#[derive(Clone, Copy, Eq)]
44
pub struct Cursor(u16, u16, u32);
@@ -15,6 +15,13 @@ impl PartialEq for Cursor {
1515
}
1616
}
1717

18+
impl Hash for Cursor {
19+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
20+
self.0.hash(state);
21+
self.1.hash(state);
22+
}
23+
}
24+
1825
impl Cursor {
1926
pub fn create() -> Self {
2027
Self::from(1, 1)
@@ -61,7 +68,7 @@ impl Cursor {
6168
}
6269
}
6370

64-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6572
pub struct WithCursor<T> {
6673
pub value: T,
6774
pub start: Cursor,

packages/engine/src/parser/ast.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt::Debug;
1+
use std::{collections::HashMap, fmt::Debug, rc::Rc};
22

33
use crate::{cursor::WithCursor, lexer::tokens::{LexerToken, LexerTokenKind, ShellCommand}};
44

@@ -17,10 +17,12 @@ pub enum Expression {
1717
Identifier(Box<Identifier>),
1818
FunctionCall(Box<(Identifier, Vec<WithCursor<Expression>>)>),
1919
If(Box<(WithCursor<Expression>, WithCursor<Block>, Option<Else>)>),
20-
Match(Box<(Expression, Vec<(Expression, Expression)>)>),
20+
Match(Box<(WithCursor<Expression>, MatchCase)>),
21+
Block(Box<Block>),
2122
}
2223

2324
pub type Else = WithCursor<Block>;
25+
pub type MatchCase = HashMap<WithCursor<Literal>, Rc<WithCursor<Expression>>>;
2426

2527

2628
#[derive(lang_macro::EnumVariants, Debug, Clone, PartialEq, Eq)]
@@ -29,6 +31,7 @@ pub enum Statement {
2931
For(Box<(Variable, WithCursor<Expression>, WithCursor<Block>)>),
3032
Return(Box<Option<WithCursor<Expression>>>),
3133
If(Box<WithCursor<Expression>>),
34+
Match(Box<WithCursor<Expression>>),
3235
Expression(Box<WithCursor<Expression>>),
3336
Continue,
3437
Break,
@@ -44,7 +47,7 @@ pub type ProgramTree = Block;
4447
pub type Identifier = String;
4548
pub type Block = Vec<Statement>;
4649

47-
#[derive(lang_macro::EnumVariants, Debug, Clone, PartialEq, Eq)]
50+
#[derive(lang_macro::EnumVariants, Debug, Clone, PartialEq, Eq, Hash)]
4851
pub enum Literal {
4952
Integer(isize),
5053
Boolean(bool),

packages/engine/src/parser/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub enum ParserErrorKind {
88
EngineError(#[from] crate::error::EngineErrorKind),
99
#[error("couldn't convert lexer token {0} to ast node")]
1010
ConvertError(LexerTokenKind),
11-
#[error("expected token '{0:?}' but found {1:?}")]
11+
#[error("expected token '{0:?}' but found '{1:?}'")]
1212
ExpectedToken(Vec<LexerTokenKind>, Option<LexerTokenKind>),
1313
#[error("expected expression")]
1414
ExpectedExpression,

0 commit comments

Comments
 (0)