Skip to content

Commit

Permalink
Add more parser errors (#21)
Browse files Browse the repository at this point in the history
# Objective

Work towards resolving
#4.

## Solution

Add more parser errors.

I removed parsing for `for` loops because they didn't do anything.
  • Loading branch information
doonv authored Mar 5, 2024
1 parent 512917e commit d9c601c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
4 changes: 4 additions & 0 deletions src/builtin_parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ pub enum Token {
#[token("&")]
Ampersand,

#[token("loop")]
Loop,
#[token("for")]
For,
#[token("while")]
While,

#[token("in")]
In,
Expand Down
76 changes: 37 additions & 39 deletions src/builtin_parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ pub enum ParseError {
number_kind: &'static str,
},
ExpectedObjectContinuation(Spanned<Option<Result<Token, FailedToLexCharacter>>>),
ExpectedIndexer {
got: Token,
span: Span,
},
UnsupportedLoop {
ty: &'static str,
span: Span,
},
}

impl ParseError {
Expand All @@ -223,6 +231,8 @@ impl ParseError {
E::PositiveIntOverflow { span, .. } => span,
E::NegativeIntOverflow { span, .. } => span,
E::ExpectedObjectContinuation(Spanned { span, value: _ }) => span,
E::ExpectedIndexer { got: _, span } => span,
E::UnsupportedLoop { ty: _, span } => span,
}
.clone()
}
Expand Down Expand Up @@ -254,6 +264,8 @@ impl std::fmt::Display for ParseError {
E::NegativeIntOverflow { span: _, number, number_kind } => write!(f, "{number} cannot be represented as a {number_kind} as it is too small."),
E::PositiveIntOverflow { span: _, number, number_kind } => write!(f, "{number} cannot be represented as a {number_kind} as it is too large."),
E::ExpectedObjectContinuation(Spanned { span: _, value: got }) => write!(f, "Expected a continuation to the object declaration (such as a comma or a closing bracket), but got {got:?} instead."),
E::ExpectedIndexer { got, span: _ } => write!(f, "Expected an identifier or integer when accessing member of variable, got {got:?} instead."),
E::UnsupportedLoop { ty, span : _} => write!(f, "{ty} loops are not yet supported. See issue #8.")
}
}
}
Expand Down Expand Up @@ -290,43 +302,18 @@ fn parse_expression(
environment: &Environment,
) -> Result<Spanned<Expression>, ParseError> {
match tokens.peek() {
Some(Ok(Token::For)) => {
let start = tokens.span().start;
tokens.next();

let index_name = tokens.slice().to_string();

tokens.next();

match tokens.next() {
Some(Ok(Token::In)) => {}
_ => todo!(),
}

let loop_count = match parse_additive(tokens, environment)?.value {
Expression::Number(Number::u8(number)) => number as u64,
Expression::Number(Number::u16(number)) => number as u64,
Expression::Number(Number::u32(number)) => number as u64,
Expression::Number(Number::u64(number)) => number,
Expression::Number(Number::i8(number)) => number as u64,
Expression::Number(Number::i16(number)) => number as u64,
Expression::Number(Number::i32(number)) => number as u64,
Expression::Number(Number::i64(number)) => number as u64,
t => todo!("{t:?}"),
};

let block = parse_block(tokens, environment)?;
let end = tokens.span().end;

Ok(Spanned {
value: Expression::ForLoop {
index_name,
loop_count,
block,
},
span: start..end,
})
}
Some(Ok(Token::Loop)) => Err(ParseError::UnsupportedLoop {
ty: "infinite",
span: tokens.peek_span(),
}),
Some(Ok(Token::While)) => Err(ParseError::UnsupportedLoop {
ty: "while",
span: tokens.peek_span(),
}),
Some(Ok(Token::For)) => Err(ParseError::UnsupportedLoop {
ty: "for",
span: tokens.peek_span(),
}),
Some(Ok(_)) => {
let expr = parse_additive(tokens, environment)?;

Expand All @@ -342,7 +329,7 @@ fn parse_expression(
}
}

fn parse_block(tokens: &mut TokenStream, environment: &Environment) -> Result<Ast, ParseError> {
fn _parse_block(tokens: &mut TokenStream, environment: &Environment) -> Result<Ast, ParseError> {
expect!(tokens, Token::LeftBracket);
let ast = parse(tokens, environment)?;
expect!(tokens, Token::RightBracket);
Expand Down Expand Up @@ -606,7 +593,18 @@ fn parse_value(
},
};
}
_ => todo!(),
Some(Ok(token)) => {
return Err(ParseError::ExpectedIndexer {
got: token,
span: tokens.span(),
})
}
Some(Err(FailedToLexCharacter)) => {
return Err(ParseError::FailedToLexCharacters(
tokens.span().wrap(tokens.slice().to_string()),
))
}
None => return Err(ParseError::ExpectedMoreTokens(tokens.span())),
}
}
Ok(expr)
Expand Down

0 comments on commit d9c601c

Please sign in to comment.