Skip to content

Commit

Permalink
feat: additional node kind checks
Browse files Browse the repository at this point in the history
  • Loading branch information
viddrobnic committed May 28, 2024
1 parent 360cc0b commit 8c16d52
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
9 changes: 9 additions & 0 deletions parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ impl Node {
}
}

impl Display for NodeKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NodeKind::Expression => write!(f, "EXPRESSION"),
NodeKind::Statement => write!(f, "STATEMENT"),
}
}
}

impl NodeValue {
pub fn kind(&self) -> NodeKind {
match self {
Expand Down
4 changes: 2 additions & 2 deletions parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ impl Display for ErrorKind {
ErrorKind::InvalidExpression(token) => write!(f, "not a valid expression: {:?}", token),
ErrorKind::ExpectedEol => write!(f, "expression must end with new line"),
ErrorKind::InvalidNodeKind { expected, got } => {
write!(f, "invalid node kind, expected: {expected:?}, got: {got:?}")
write!(f, "invalid node kind, expected: {expected}, got: {got}")
}
ErrorKind::InvalidTokenKind { expected, got } => {
write!(f, "invalid token, expected: {expected:?}, got: {got:?}")
write!(f, "invalid token, expected: {expected}, got: {got}")
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ impl Parser<'_> {
fn parse_grouped(&mut self, start_range: Range) -> Result<(ast::NodeValue, Position)> {
let token = self.next_token(start_range)?;
let node = self.parse_node(token, Precedence::Lowest)?;
if node.kind() != NodeKind::Expression {
return Err(Error {
kind: ErrorKind::InvalidNodeKind {
expected: NodeKind::Expression,
got: node.kind(),
},
range: node.range,
});
}

let closing_token = self.next_token(Range {
start: start_range.start,
Expand Down Expand Up @@ -329,6 +338,18 @@ impl Parser<'_> {
Ok((item, end))
})?;

for it in &items {
if it.kind() != NodeKind::Expression {
return Err(Error {
kind: ErrorKind::InvalidNodeKind {
expected: NodeKind::Statement,
got: it.kind(),
},
range: it.range,
});
}
}

Ok((ast::NodeValue::ArrayLiteral(items), end))
}

Expand Down Expand Up @@ -359,6 +380,28 @@ impl Parser<'_> {
Ok((ast::HashLiteralPair { key, value }, end))
})?;

for it in &items {
if it.key.kind() != NodeKind::Expression {
return Err(Error {
kind: ErrorKind::InvalidNodeKind {
expected: NodeKind::Expression,
got: it.key.kind(),
},
range: it.key.range,
});
}

if it.value.kind() != NodeKind::Expression {
return Err(Error {
kind: ErrorKind::InvalidNodeKind {
expected: NodeKind::Expression,
got: it.value.kind(),
},
range: it.value.range,
});
}
}

Ok((ast::NodeValue::HashLiteral(items), end))
}

Expand Down
51 changes: 51 additions & 0 deletions parser/src/token.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use crate::position::Range;

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -95,3 +97,52 @@ pub struct Token {
pub kind: TokenKind,
pub range: Range,
}

impl Display for TokenKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TokenKind::Ident(_) => write!(f, "IDENT"),
TokenKind::Integer(_) => write!(f, "INTEGER"),
TokenKind::Float(_) => write!(f, "FLOAT"),
TokenKind::True => write!(f, "TRUE"),
TokenKind::False => write!(f, "FALSE"),
TokenKind::String(_) => write!(f, "STRING"),
TokenKind::LSquare => write!(f, "LSQUARE"),
TokenKind::RSquare => write!(f, "RSQUARE"),
TokenKind::LBracket => write!(f, "LBRACKET"),
TokenKind::RBracket => write!(f, "RBRACKET"),
TokenKind::LCurly => write!(f, "LCURLY"),
TokenKind::RCurly => write!(f, "RCURLY"),
TokenKind::Le => write!(f, "LE"),
TokenKind::Leq => write!(f, "LEQ"),
TokenKind::Ge => write!(f, "GE"),
TokenKind::Geq => write!(f, "GEQ"),
TokenKind::Eq => write!(f, "EQ"),
TokenKind::Neq => write!(f, "NEQ"),
TokenKind::Plus => write!(f, "PLUS"),
TokenKind::Minus => write!(f, "MINUS"),
TokenKind::Mult => write!(f, "MULT"),
TokenKind::Div => write!(f, "DIV"),
TokenKind::Modulo => write!(f, "MODULO"),
TokenKind::And => write!(f, "AND"),
TokenKind::Or => write!(f, "OR"),
TokenKind::Bang => write!(f, "BANG"),
TokenKind::Assign => write!(f, "ASSIGN"),
TokenKind::Colon => write!(f, "COLON"),
TokenKind::Semicolon => write!(f, "SEMICOLON"),
TokenKind::Comma => write!(f, "COMMA"),
TokenKind::Dot => write!(f, "DOT"),
TokenKind::If => write!(f, "IF"),
TokenKind::Else => write!(f, "ELSE"),
TokenKind::While => write!(f, "WHILE"),
TokenKind::For => write!(f, "FOR"),
TokenKind::Break => write!(f, "BREAK"),
TokenKind::Continue => write!(f, "CONTINUE"),
TokenKind::Return => write!(f, "RETURN"),
TokenKind::Fn => write!(f, "FN"),
TokenKind::Use => write!(f, "USE"),
TokenKind::Eol => write!(f, "EOL"),
TokenKind::Comment(_) => write!(f, "COMMENT"),
}
}
}

0 comments on commit 8c16d52

Please sign in to comment.