Skip to content

Commit

Permalink
Added error message for accidentally having multiple statements on th…
Browse files Browse the repository at this point in the history
…e same line
  • Loading branch information
IsaacShelton committed Oct 22, 2024
1 parent fc26095 commit c777582
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,22 @@ impl<T: Text + Send> Lexer<T> {

let mut comment = String::new();

while let Character::At(c, _) = self.characters.next() {
while let Character::At(c, _) = self.characters.peek() {
if c == '\n' {
break;
}
comment.push(c);
self.characters.next();
}

return Has(TokenKind::DocComment(comment).at(source));
}

while let Character::At(c, _) = self.characters.next() {
while let Character::At(c, _) = self.characters.peek() {
if c == '\n' {
break;
}
self.characters.next();
}

return Waiting;
Expand Down
11 changes: 11 additions & 0 deletions src/parser/parse_expr/post/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ use crate::{

impl<'a, I: Inflow<Token>> Parser<'a, I> {
pub fn parse_expr_primary_post(&mut self, mut base: Expr) -> Result<Expr, ParseError> {
let mut ate_newline;

loop {
ate_newline = self
.input
.peek()
.is_newline()
.then(|| self.input.peek().clone());
self.ignore_newlines();

match self.input.peek().kind {
Expand All @@ -21,6 +28,10 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
}
}

if let Some(newline) = ate_newline {
self.input.unadvance(newline);
}

Ok(base)
}
}
21 changes: 20 additions & 1 deletion src/parser/parse_stmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ mod parse_assignment;
mod parse_declaration;
mod parse_return;

use super::{error::ParseError, Parser};
use super::{
error::{ParseError, ParseErrorKind},
Parser,
};
use crate::{
ast::{Stmt, StmtKind},
inflow::Inflow,
Expand All @@ -11,6 +14,22 @@ use crate::{

impl<'a, I: Inflow<Token>> Parser<'a, I> {
pub fn parse_stmt(&mut self) -> Result<Stmt, ParseError> {
let stmt = self.parse_stmt_inner()?;

if !matches!(
self.input.peek().kind,
TokenKind::Newline | TokenKind::CloseCurly,
) {
return Err(ParseErrorKind::Other {
message: "Expected newline or '}' after statement".into(),
}
.at(self.input.peek().source));
}

Ok(stmt)
}

fn parse_stmt_inner(&mut self) -> Result<Stmt, ParseError> {
let source = self.source_here();

match self.input.peek().kind {
Expand Down

0 comments on commit c777582

Please sign in to comment.