2024-08-09 01:59:09 +00:00
|
|
|
//! Parsing tools.
|
|
|
|
//!
|
|
|
|
//! This module provides two parsing options:
|
|
|
|
//! - `parse` convenience function
|
|
|
|
//! - `Parser` struct, which parses the input a statement at a time
|
2024-08-09 00:58:56 +00:00
|
|
|
use std::{
|
2024-08-24 14:13:16 +00:00
|
|
|
collections::VecDeque,
|
2024-08-09 00:58:56 +00:00
|
|
|
fmt::{self, Display, Formatter},
|
2024-08-09 18:01:01 +00:00
|
|
|
num::{ParseFloatError, ParseIntError},
|
|
|
|
str::ParseBoolError,
|
2024-08-09 00:58:56 +00:00
|
|
|
};
|
2024-08-05 04:40:51 +00:00
|
|
|
|
2024-08-30 22:06:58 +00:00
|
|
|
use crate::{
|
|
|
|
ast::*, Context, DustError, Identifier, LexError, Lexer, Token, TokenKind, TokenOwned, Type,
|
|
|
|
};
|
2024-08-04 00:23:52 +00:00
|
|
|
|
2024-08-07 16:32:18 +00:00
|
|
|
/// Parses the input into an abstract syntax tree.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// # use dust_lang::*;
|
2024-08-23 20:47:57 +00:00
|
|
|
/// let source = "42.to_string()";
|
2024-08-07 16:32:18 +00:00
|
|
|
///
|
|
|
|
/// assert_eq!(
|
2024-08-23 20:47:57 +00:00
|
|
|
/// parse(source),
|
|
|
|
/// Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
/// Statement::Expression(Expression::call(
|
|
|
|
/// Expression::field_access(
|
|
|
|
/// Expression::literal(42, (0, 2)),
|
|
|
|
/// Node::new(Identifier::new("to_string"), (3, 12)),
|
|
|
|
/// (0, 12)
|
|
|
|
/// ),
|
|
|
|
/// vec![],
|
|
|
|
/// (0, 14)
|
|
|
|
/// ))
|
|
|
|
/// ]))
|
2024-08-07 16:32:18 +00:00
|
|
|
/// );
|
|
|
|
/// ```
|
2024-08-11 21:59:52 +00:00
|
|
|
pub fn parse(source: &str) -> Result<AbstractSyntaxTree, DustError> {
|
2024-08-17 09:32:18 +00:00
|
|
|
let mut tree = AbstractSyntaxTree::new();
|
2024-08-04 23:25:44 +00:00
|
|
|
|
2024-08-17 09:32:18 +00:00
|
|
|
parse_into(source, &mut tree)?;
|
2024-08-05 00:08:43 +00:00
|
|
|
|
2024-08-17 09:32:18 +00:00
|
|
|
Ok(tree)
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 01:24:56 +00:00
|
|
|
pub fn parse_into<'src>(
|
|
|
|
source: &'src str,
|
|
|
|
tree: &mut AbstractSyntaxTree,
|
|
|
|
) -> Result<(), DustError<'src>> {
|
2024-08-23 20:47:57 +00:00
|
|
|
let lexer = Lexer::new(source);
|
|
|
|
let mut parser = Parser::new(lexer);
|
2024-08-14 01:24:56 +00:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let node = parser
|
2024-08-14 19:52:04 +00:00
|
|
|
.parse_statement()
|
2024-08-20 08:38:15 +00:00
|
|
|
.map_err(|parse_error| DustError::Parse {
|
2024-08-14 01:24:56 +00:00
|
|
|
parse_error,
|
|
|
|
source,
|
|
|
|
})?;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
tree.statements.push_back(node);
|
2024-08-14 01:24:56 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
if let Token::Eof = parser.current_token {
|
2024-08-14 01:24:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-08-07 16:32:18 +00:00
|
|
|
/// Low-level tool for parsing the input a statement at a time.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
|
|
|
/// # use std::collections::VecDeque;
|
|
|
|
/// # use dust_lang::*;
|
2024-08-14 19:52:04 +00:00
|
|
|
/// let source = "x = 42";
|
2024-08-23 20:47:57 +00:00
|
|
|
/// let lexer = Lexer::new(source);
|
|
|
|
/// let mut parser = Parser::new(lexer);
|
|
|
|
/// let mut statements = VecDeque::new();
|
2024-08-07 16:32:18 +00:00
|
|
|
///
|
|
|
|
/// loop {
|
2024-08-23 20:47:57 +00:00
|
|
|
/// let statement = parser.parse_statement().unwrap();
|
2024-08-07 16:32:18 +00:00
|
|
|
///
|
2024-08-23 20:47:57 +00:00
|
|
|
/// statements.push_back(statement);
|
2024-08-07 16:32:18 +00:00
|
|
|
///
|
2024-08-23 20:47:57 +00:00
|
|
|
/// if parser.is_eof() {
|
2024-08-07 16:32:18 +00:00
|
|
|
/// break;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
2024-08-23 20:47:57 +00:00
|
|
|
/// let tree = AbstractSyntaxTree { statements };
|
2024-08-13 21:34:45 +00:00
|
|
|
///
|
2024-08-07 16:32:18 +00:00
|
|
|
/// ```
|
2024-08-14 19:52:04 +00:00
|
|
|
pub struct Parser<'src> {
|
2024-08-17 09:32:18 +00:00
|
|
|
lexer: Lexer<'src>,
|
2024-08-14 18:28:39 +00:00
|
|
|
current_token: Token<'src>,
|
|
|
|
current_position: Span,
|
2024-08-16 01:34:47 +00:00
|
|
|
mode: ParserMode,
|
2024-08-30 22:06:58 +00:00
|
|
|
context: Context,
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
impl<'src> Parser<'src> {
|
2024-08-23 20:47:57 +00:00
|
|
|
pub fn new(mut lexer: Lexer<'src>) -> Self {
|
2024-08-17 09:32:18 +00:00
|
|
|
let (current_token, current_position) = lexer.next_token().unwrap_or((Token::Eof, (0, 0)));
|
2024-08-05 00:08:43 +00:00
|
|
|
|
2024-08-08 20:19:14 +00:00
|
|
|
Parser {
|
|
|
|
lexer,
|
2024-08-14 18:28:39 +00:00
|
|
|
current_token,
|
|
|
|
current_position,
|
2024-08-16 01:34:47 +00:00
|
|
|
mode: ParserMode::Normal,
|
2024-08-30 22:06:58 +00:00
|
|
|
context: Context::new(),
|
2024-08-08 20:19:14 +00:00
|
|
|
}
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
|
|
|
|
2024-08-23 20:47:57 +00:00
|
|
|
pub fn is_eof(&self) -> bool {
|
|
|
|
matches!(self.current_token, Token::Eof)
|
|
|
|
}
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
pub fn parse_statement(&mut self) -> Result<Statement, ParseError> {
|
|
|
|
let start_position = self.current_position;
|
|
|
|
|
2024-08-16 11:09:46 +00:00
|
|
|
if let Token::Let = self.current_token {
|
2024-08-17 08:06:13 +00:00
|
|
|
log::trace!("Parsing let statement");
|
|
|
|
|
2024-08-16 11:09:46 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
let is_mutable = if let Token::Mut = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
let identifier = self.parse_identifier()?;
|
|
|
|
|
|
|
|
if let Token::Equal = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
} else {
|
|
|
|
return Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Equal,
|
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let value = self.parse_expression(0)?;
|
|
|
|
|
2024-08-17 08:06:13 +00:00
|
|
|
let end = if let Token::Semicolon = self.current_token {
|
|
|
|
let end = self.current_position.1;
|
|
|
|
|
2024-08-16 13:22:36 +00:00
|
|
|
self.next_token()?;
|
2024-08-17 08:06:13 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
} else {
|
2024-08-20 19:55:35 +00:00
|
|
|
value.position().1
|
2024-08-17 08:06:13 +00:00
|
|
|
};
|
2024-08-16 11:09:46 +00:00
|
|
|
let r#let = if is_mutable {
|
|
|
|
LetStatement::LetMut { identifier, value }
|
|
|
|
} else {
|
|
|
|
LetStatement::Let { identifier, value }
|
|
|
|
};
|
2024-08-17 08:06:13 +00:00
|
|
|
let position = (start_position.0, end);
|
2024-08-16 11:09:46 +00:00
|
|
|
|
|
|
|
return Ok(Statement::Let(Node::new(r#let, position)));
|
|
|
|
}
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
if let Token::Struct = self.current_token {
|
2024-08-17 08:06:13 +00:00
|
|
|
log::trace!("Parsing struct definition");
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-17 14:07:38 +00:00
|
|
|
let name = if let Token::Identifier(_) = self.current_token {
|
|
|
|
self.parse_identifier()?
|
2024-08-14 19:52:04 +00:00
|
|
|
} else {
|
|
|
|
return Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Identifier,
|
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-08-17 14:07:38 +00:00
|
|
|
if let Token::Semicolon = self.current_token {
|
|
|
|
let end = self.current_position.1;
|
|
|
|
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
return Ok(Statement::struct_definition(
|
|
|
|
StructDefinition::Unit { name },
|
|
|
|
(start_position.0, end),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
if let Token::LeftParenthesis = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
let mut types = Vec::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
if let Token::RightParenthesis = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-17 14:07:38 +00:00
|
|
|
if let Token::Semicolon = self.current_token {
|
2024-08-23 15:44:47 +00:00
|
|
|
self.next_token()?;
|
2024-08-17 14:07:38 +00:00
|
|
|
}
|
2024-08-23 15:44:47 +00:00
|
|
|
|
|
|
|
break;
|
2024-08-14 19:52:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-17 14:07:38 +00:00
|
|
|
let type_node = self.parse_type()?;
|
|
|
|
|
|
|
|
types.push(type_node);
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
if let Token::Comma = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2024-08-17 14:07:38 +00:00
|
|
|
}
|
2024-08-14 19:52:04 +00:00
|
|
|
|
2024-08-17 14:07:38 +00:00
|
|
|
let position = (start_position.0, self.current_position.1);
|
2024-08-14 19:52:04 +00:00
|
|
|
|
2024-08-17 14:07:38 +00:00
|
|
|
return if types.is_empty() {
|
|
|
|
Ok(Statement::struct_definition(
|
|
|
|
StructDefinition::Unit { name },
|
|
|
|
position,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(Statement::struct_definition(
|
|
|
|
StructDefinition::Tuple { name, items: types },
|
|
|
|
position,
|
|
|
|
))
|
|
|
|
};
|
2024-08-14 19:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Token::LeftCurlyBrace = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
let mut fields = Vec::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
if let Token::RightCurlyBrace = self.current_token {
|
2024-08-23 15:44:47 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-17 14:07:38 +00:00
|
|
|
if let Token::Semicolon = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
}
|
2024-08-14 19:52:04 +00:00
|
|
|
|
2024-08-17 14:07:38 +00:00
|
|
|
break;
|
2024-08-14 19:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let field_name = self.parse_identifier()?;
|
|
|
|
|
|
|
|
if let Token::Colon = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
} else {
|
|
|
|
return Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Colon,
|
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let field_type = self.parse_type()?;
|
|
|
|
|
|
|
|
fields.push((field_name, field_type));
|
2024-08-17 14:07:38 +00:00
|
|
|
|
|
|
|
if let Token::Comma = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2024-08-14 19:52:04 +00:00
|
|
|
}
|
2024-08-17 14:07:38 +00:00
|
|
|
|
|
|
|
let position = (start_position.0, self.current_position.1);
|
|
|
|
|
|
|
|
return if fields.is_empty() {
|
|
|
|
Ok(Statement::struct_definition(
|
|
|
|
StructDefinition::Unit { name },
|
|
|
|
position,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(Statement::struct_definition(
|
|
|
|
StructDefinition::Fields { name, fields },
|
|
|
|
position,
|
|
|
|
))
|
|
|
|
};
|
2024-08-14 19:52:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-17 14:07:38 +00:00
|
|
|
return Err(ParseError::ExpectedTokenMultiple {
|
|
|
|
expected: vec![
|
|
|
|
TokenKind::LeftParenthesis,
|
|
|
|
TokenKind::LeftCurlyBrace,
|
|
|
|
TokenKind::Semicolon,
|
|
|
|
],
|
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
2024-08-14 19:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let expression = self.parse_expression(0)?;
|
2024-08-15 01:15:37 +00:00
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
if let Token::Semicolon = self.current_token {
|
|
|
|
let position = (start_position.0, self.current_position.1);
|
2024-08-15 01:15:37 +00:00
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
self.next_token()?;
|
2024-08-17 08:06:13 +00:00
|
|
|
|
|
|
|
Ok(Statement::ExpressionNullified(Node::new(
|
|
|
|
expression, position,
|
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
Ok(Statement::Expression(expression))
|
|
|
|
}
|
2024-08-10 04:01:50 +00:00
|
|
|
}
|
2024-08-09 05:43:58 +00:00
|
|
|
|
2024-08-10 04:01:50 +00:00
|
|
|
fn next_token(&mut self) -> Result<(), ParseError> {
|
2024-08-17 09:32:18 +00:00
|
|
|
let (token, position) = self.lexer.next_token()?;
|
2024-08-14 18:28:39 +00:00
|
|
|
|
|
|
|
self.current_token = token;
|
|
|
|
self.current_position = position;
|
2024-08-04 00:23:52 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
fn parse_expression(&mut self, mut precedence: u8) -> Result<Expression, ParseError> {
|
2024-08-12 12:35:08 +00:00
|
|
|
// Parse a statement starting from the current node.
|
2024-08-14 18:28:39 +00:00
|
|
|
let mut left = if self.current_token.is_prefix() {
|
2024-08-12 23:06:57 +00:00
|
|
|
self.parse_prefix()?
|
|
|
|
} else {
|
|
|
|
self.parse_primary()?
|
|
|
|
};
|
2024-08-04 00:23:52 +00:00
|
|
|
|
2024-08-12 12:35:08 +00:00
|
|
|
// While the current token has a higher precedence than the given precedence
|
2024-08-14 18:28:39 +00:00
|
|
|
while precedence < self.current_token.precedence() {
|
2024-08-12 12:35:08 +00:00
|
|
|
// Give precedence to postfix operations
|
2024-08-14 18:28:39 +00:00
|
|
|
left = if self.current_token.is_postfix() {
|
2024-08-12 23:06:57 +00:00
|
|
|
let statement = self.parse_postfix(left)?;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
precedence = self.current_token.precedence();
|
2024-08-12 23:06:57 +00:00
|
|
|
|
2024-08-14 07:53:15 +00:00
|
|
|
// Replace the left-hand side with the postfix operation
|
2024-08-12 23:06:57 +00:00
|
|
|
statement
|
2024-08-10 04:01:50 +00:00
|
|
|
} else {
|
2024-08-12 12:35:08 +00:00
|
|
|
// Replace the left-hand side with the infix operation
|
2024-08-12 23:06:57 +00:00
|
|
|
self.parse_infix(left)?
|
|
|
|
};
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
2024-08-10 04:01:50 +00:00
|
|
|
|
|
|
|
Ok(left)
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
fn parse_prefix(&mut self) -> Result<Expression, ParseError> {
|
2024-08-14 18:28:39 +00:00
|
|
|
log::trace!("Parsing {} as prefix operator", self.current_token);
|
2024-08-14 07:53:15 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let operator_start = self.current_position.0;
|
2024-08-14 07:53:15 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
match self.current_token {
|
|
|
|
Token::Bang => {
|
2024-08-12 09:44:05 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let operand = self.parse_expression(0)?;
|
|
|
|
let position = (operator_start, self.current_position.1);
|
2024-08-12 09:44:05 +00:00
|
|
|
|
2024-08-15 04:20:36 +00:00
|
|
|
Ok(Expression::not(operand, position))
|
2024-08-12 09:44:05 +00:00
|
|
|
}
|
2024-08-14 18:28:39 +00:00
|
|
|
Token::Minus => {
|
2024-08-12 09:44:05 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let operand = self.parse_expression(0)?;
|
|
|
|
let position = (operator_start, self.current_position.1);
|
2024-08-12 09:44:05 +00:00
|
|
|
|
2024-08-15 04:20:36 +00:00
|
|
|
Ok(Expression::negation(operand, position))
|
2024-08-12 09:44:05 +00:00
|
|
|
}
|
2024-08-28 15:31:47 +00:00
|
|
|
Token::Star => {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
let operand = self.parse_expression(0)?;
|
|
|
|
let position = (operator_start, self.current_position.1);
|
|
|
|
|
|
|
|
Ok(Expression::dereference(operand, position))
|
|
|
|
}
|
2024-08-17 08:06:13 +00:00
|
|
|
_ => Err(ParseError::ExpectedTokenMultiple {
|
2024-08-28 15:31:47 +00:00
|
|
|
expected: vec![TokenKind::Bang, TokenKind::Minus, TokenKind::Star],
|
2024-08-14 18:28:39 +00:00
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
2024-08-12 23:06:57 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
fn parse_primary(&mut self) -> Result<Expression, ParseError> {
|
2024-08-14 18:28:39 +00:00
|
|
|
log::trace!("Parsing {} as primary", self.current_token);
|
2024-08-14 07:53:15 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let start_position = self.current_position;
|
2024-08-14 03:45:17 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
match self.current_token {
|
|
|
|
Token::Async => {
|
|
|
|
let block = self.parse_block()?;
|
2024-08-17 08:06:13 +00:00
|
|
|
let position = (start_position.0, block.position.1);
|
2024-08-14 03:45:17 +00:00
|
|
|
|
2024-08-15 01:15:37 +00:00
|
|
|
Ok(Expression::block(block.inner, position))
|
2024-08-14 03:45:17 +00:00
|
|
|
}
|
2024-08-14 18:28:39 +00:00
|
|
|
Token::Boolean(text) => {
|
2024-08-07 14:50:19 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-17 16:15:47 +00:00
|
|
|
let boolean = text.parse::<bool>().map_err(|error| ParseError::Boolean {
|
2024-08-14 18:28:39 +00:00
|
|
|
error,
|
|
|
|
position: start_position,
|
|
|
|
})?;
|
2024-08-20 18:45:43 +00:00
|
|
|
let expression = Expression::literal(boolean, start_position);
|
2024-08-14 18:28:39 +00:00
|
|
|
|
2024-08-20 18:45:43 +00:00
|
|
|
Ok(expression)
|
|
|
|
}
|
|
|
|
Token::Break => {
|
|
|
|
let break_end = self.current_position.1;
|
|
|
|
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
let (expression_option, end) = if let Token::Semicolon = self.current_token {
|
|
|
|
// Do not consume the semicolon, allowing it to nullify the expression
|
|
|
|
|
|
|
|
(None, break_end)
|
|
|
|
} else if let Ok(expression) = self.parse_expression(0) {
|
|
|
|
let end = expression.position().1;
|
|
|
|
|
|
|
|
(Some(expression), end)
|
|
|
|
} else {
|
|
|
|
return Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Semicolon,
|
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
let position = (start_position.0, end);
|
|
|
|
|
|
|
|
Ok(Expression::r#break(expression_option, position))
|
2024-08-07 14:50:19 +00:00
|
|
|
}
|
2024-08-23 09:54:58 +00:00
|
|
|
Token::Character(character) => {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
let expression = Expression::literal(character, start_position);
|
|
|
|
|
|
|
|
Ok(expression)
|
|
|
|
}
|
2024-08-14 18:28:39 +00:00
|
|
|
Token::Float(text) => {
|
2024-08-04 00:23:52 +00:00
|
|
|
self.next_token()?;
|
2024-08-05 00:08:43 +00:00
|
|
|
|
2024-08-17 16:15:47 +00:00
|
|
|
let float = text.parse::<f64>().map_err(|error| ParseError::Float {
|
2024-08-14 18:28:39 +00:00
|
|
|
error,
|
|
|
|
position: start_position,
|
|
|
|
})?;
|
2024-08-09 18:01:01 +00:00
|
|
|
|
2024-08-17 16:15:47 +00:00
|
|
|
Ok(Expression::literal(float, start_position))
|
2024-08-05 00:08:43 +00:00
|
|
|
}
|
2024-08-14 18:28:39 +00:00
|
|
|
Token::Identifier(text) => {
|
|
|
|
self.next_token()?;
|
2024-08-13 23:41:36 +00:00
|
|
|
|
2024-08-15 01:15:37 +00:00
|
|
|
let identifier = Identifier::new(text);
|
|
|
|
|
2024-08-16 01:34:47 +00:00
|
|
|
if let ParserMode::Condition = self.mode {
|
|
|
|
return Ok(Expression::identifier(identifier, start_position));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Token::LeftCurlyBrace = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-20 04:15:19 +00:00
|
|
|
let name = Node::new(identifier, start_position);
|
2024-08-16 01:34:47 +00:00
|
|
|
let mut fields = Vec::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
if let Token::RightCurlyBrace = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let field_name = self.parse_identifier()?;
|
|
|
|
|
|
|
|
if let Token::Colon = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
} else {
|
|
|
|
return Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Colon,
|
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let field_value = self.parse_expression(0)?;
|
|
|
|
|
|
|
|
fields.push((field_name, field_value));
|
|
|
|
|
|
|
|
if let Token::Comma = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let position = (start_position.0, self.current_position.1);
|
|
|
|
|
|
|
|
return Ok(Expression::r#struct(
|
|
|
|
StructExpression::Fields { name, fields },
|
|
|
|
position,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-08-15 01:15:37 +00:00
|
|
|
Ok(Expression::identifier(identifier, start_position))
|
2024-08-13 23:41:36 +00:00
|
|
|
}
|
2024-08-14 18:28:39 +00:00
|
|
|
Token::Integer(text) => {
|
2024-08-05 00:08:43 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let integer = text.parse::<i64>().map_err(|error| ParseError::Integer {
|
|
|
|
error,
|
|
|
|
position: start_position,
|
|
|
|
})?;
|
2024-08-09 18:01:01 +00:00
|
|
|
|
2024-08-17 16:15:47 +00:00
|
|
|
Ok(Expression::literal(integer, start_position))
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
2024-08-14 18:28:39 +00:00
|
|
|
Token::If => {
|
2024-08-15 01:15:37 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
let r#if = self.parse_if()?;
|
2024-08-17 08:06:13 +00:00
|
|
|
let position = (start_position.0, self.current_position.1);
|
2024-08-11 18:35:33 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
Ok(Expression::r#if(r#if, position))
|
2024-08-11 18:35:33 +00:00
|
|
|
}
|
2024-08-14 18:28:39 +00:00
|
|
|
Token::String(text) => {
|
2024-08-08 17:11:32 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-17 16:15:47 +00:00
|
|
|
Ok(Expression::literal(text.to_string(), start_position))
|
2024-08-08 17:11:32 +00:00
|
|
|
}
|
2024-08-14 18:28:39 +00:00
|
|
|
Token::LeftCurlyBrace => {
|
|
|
|
let block_node = self.parse_block()?;
|
2024-08-11 17:16:16 +00:00
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
Ok(Expression::block(block_node.inner, block_node.position))
|
2024-08-09 10:09:59 +00:00
|
|
|
}
|
2024-08-14 18:28:39 +00:00
|
|
|
Token::LeftParenthesis => {
|
2024-08-04 00:23:52 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let node = self.parse_expression(0)?;
|
|
|
|
|
|
|
|
if let Token::RightParenthesis = self.current_token {
|
|
|
|
let position = (start_position.0, self.current_position.1);
|
2024-08-04 00:23:52 +00:00
|
|
|
|
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
Ok(Expression::grouped(node, position))
|
2024-08-04 00:23:52 +00:00
|
|
|
} else {
|
2024-08-09 10:09:59 +00:00
|
|
|
Err(ParseError::ExpectedToken {
|
2024-08-12 14:08:34 +00:00
|
|
|
expected: TokenKind::RightParenthesis,
|
2024-08-14 18:28:39 +00:00
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
2024-08-05 01:31:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-08-14 18:28:39 +00:00
|
|
|
Token::LeftSquareBrace => {
|
2024-08-05 01:31:18 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-14 20:07:32 +00:00
|
|
|
if let Token::RightSquareBrace = self.current_token {
|
|
|
|
let position = (start_position.0, self.current_position.1);
|
|
|
|
|
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-17 16:15:47 +00:00
|
|
|
return Ok(Expression::list(Vec::new(), position));
|
2024-08-14 20:07:32 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let first_expression = self.parse_expression(0)?;
|
2024-08-05 01:31:18 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
if let Token::Semicolon = self.current_token {
|
|
|
|
self.next_token()?;
|
2024-08-05 01:31:18 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let repeat_operand = self.parse_expression(0)?;
|
|
|
|
|
|
|
|
if let Token::RightSquareBrace = self.current_token {
|
|
|
|
let position = (start_position.0, self.current_position.1);
|
2024-08-05 01:31:18 +00:00
|
|
|
|
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-17 16:15:47 +00:00
|
|
|
return Ok(Expression::auto_fill_list(
|
|
|
|
first_expression,
|
|
|
|
repeat_operand,
|
2024-08-14 18:28:39 +00:00
|
|
|
position,
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
return Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::RightSquareBrace,
|
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
2024-08-05 01:31:18 +00:00
|
|
|
}
|
2024-08-07 22:43:24 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let mut expressions = vec![first_expression];
|
2024-08-07 22:43:24 +00:00
|
|
|
|
2024-08-08 17:49:40 +00:00
|
|
|
loop {
|
2024-08-14 18:28:39 +00:00
|
|
|
if let Token::RightSquareBrace = self.current_token {
|
|
|
|
let position = (start_position.0, self.current_position.1);
|
|
|
|
|
2024-08-08 17:49:40 +00:00
|
|
|
self.next_token()?;
|
2024-08-14 18:28:39 +00:00
|
|
|
|
2024-08-17 16:15:47 +00:00
|
|
|
return Ok(Expression::list(expressions, position));
|
2024-08-08 17:49:40 +00:00
|
|
|
}
|
2024-08-07 22:43:24 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
if let Token::Comma = self.current_token {
|
2024-08-08 17:49:40 +00:00
|
|
|
self.next_token()?;
|
2024-08-14 18:28:39 +00:00
|
|
|
|
2024-08-08 17:49:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-08-07 22:43:24 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let expression = self.parse_expression(0)?;
|
2024-08-07 22:43:24 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
expressions.push(expression);
|
2024-08-14 07:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-20 18:45:43 +00:00
|
|
|
Token::Loop => {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
let block = self.parse_block()?;
|
|
|
|
let position = (start_position.0, block.position.1);
|
|
|
|
|
|
|
|
Ok(Expression::infinite_loop(block, position))
|
|
|
|
}
|
2024-08-17 14:07:38 +00:00
|
|
|
Token::Map => {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
if let Token::LeftCurlyBrace = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
} else {
|
|
|
|
return Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::LeftCurlyBrace,
|
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut fields = Vec::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
if let Token::RightCurlyBrace = self.current_token {
|
|
|
|
let position = (start_position.0, self.current_position.1);
|
|
|
|
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
return Ok(Expression::map(fields, position));
|
|
|
|
}
|
|
|
|
|
|
|
|
let field_name = self.parse_identifier()?;
|
|
|
|
|
|
|
|
if let Token::Equal = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
} else {
|
|
|
|
return Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Equal,
|
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let field_value = self.parse_expression(0)?;
|
|
|
|
|
|
|
|
fields.push((field_name, field_value));
|
|
|
|
|
|
|
|
if let Token::Comma = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-14 18:28:39 +00:00
|
|
|
Token::While => {
|
2024-08-13 17:54:16 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let condition = self.parse_expression(0)?;
|
|
|
|
let block = self.parse_block()?;
|
2024-08-20 16:09:17 +00:00
|
|
|
let position = (start_position.0, block.position.1);
|
2024-08-10 09:23:43 +00:00
|
|
|
|
2024-08-15 04:20:36 +00:00
|
|
|
Ok(Expression::while_loop(condition, block, position))
|
2024-08-10 09:23:43 +00:00
|
|
|
}
|
2024-08-17 08:06:13 +00:00
|
|
|
_ => Err(ParseError::ExpectedTokenMultiple {
|
|
|
|
expected: vec![
|
|
|
|
TokenKind::Async,
|
|
|
|
TokenKind::Boolean,
|
|
|
|
TokenKind::Float,
|
|
|
|
TokenKind::Identifier,
|
|
|
|
TokenKind::Integer,
|
|
|
|
TokenKind::If,
|
|
|
|
TokenKind::LeftCurlyBrace,
|
|
|
|
TokenKind::LeftParenthesis,
|
|
|
|
TokenKind::LeftSquareBrace,
|
|
|
|
TokenKind::String,
|
|
|
|
TokenKind::While,
|
|
|
|
],
|
2024-08-14 18:28:39 +00:00
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
2024-08-09 04:49:17 +00:00
|
|
|
}),
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
fn parse_infix(&mut self, left: Expression) -> Result<Expression, ParseError> {
|
2024-08-14 18:28:39 +00:00
|
|
|
log::trace!("Parsing {} as infix operator", self.current_token);
|
2024-08-14 07:53:15 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let operator_precedence = self.current_token.precedence()
|
|
|
|
- if self.current_token.is_right_associative() {
|
2024-08-12 15:24:24 +00:00
|
|
|
1
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
2024-08-14 18:28:39 +00:00
|
|
|
let left_start = left.position().0;
|
2024-08-13 21:34:45 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
if let Token::Equal = &self.current_token {
|
2024-08-13 21:34:45 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let value = self.parse_expression(operator_precedence)?;
|
|
|
|
let position = (left_start, value.position().1);
|
2024-08-13 21:34:45 +00:00
|
|
|
|
2024-08-15 04:20:36 +00:00
|
|
|
return Ok(Expression::assignment(left, value, position));
|
2024-08-13 21:34:45 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
if let Token::PlusEqual | Token::MinusEqual = &self.current_token {
|
|
|
|
let math_operator = match self.current_token {
|
|
|
|
Token::PlusEqual => MathOperator::Add,
|
|
|
|
Token::MinusEqual => MathOperator::Subtract,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let operator = Node::new(math_operator, self.current_position);
|
2024-08-12 19:02:04 +00:00
|
|
|
|
2024-08-09 22:14:46 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let value = self.parse_expression(operator_precedence)?;
|
|
|
|
let position = (left_start, value.position().1);
|
2024-08-12 01:37:44 +00:00
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
return Ok(Expression::operator(
|
2024-08-14 18:28:39 +00:00
|
|
|
OperatorExpression::CompoundAssignment {
|
|
|
|
assignee: left,
|
|
|
|
operator,
|
2024-08-15 04:20:36 +00:00
|
|
|
modifier: value,
|
2024-08-12 19:02:04 +00:00
|
|
|
},
|
2024-08-14 18:28:39 +00:00
|
|
|
position,
|
2024-08-09 22:14:46 +00:00
|
|
|
));
|
2024-08-10 04:01:50 +00:00
|
|
|
}
|
2024-08-09 18:01:01 +00:00
|
|
|
|
2024-08-15 01:15:37 +00:00
|
|
|
if let Token::DoubleDot = &self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
let end = self.parse_expression(operator_precedence)?;
|
|
|
|
let position = (left_start, end.position().1);
|
|
|
|
|
2024-08-16 15:21:20 +00:00
|
|
|
return Ok(Expression::exclusive_range(left, end, position));
|
2024-08-15 01:15:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Token::Minus | Token::Plus | Token::Star | Token::Slash | Token::Percent =
|
|
|
|
&self.current_token
|
|
|
|
{
|
|
|
|
let math_operator = match &self.current_token {
|
|
|
|
Token::Minus => Node::new(MathOperator::Subtract, self.current_position),
|
|
|
|
Token::Plus => Node::new(MathOperator::Add, self.current_position),
|
|
|
|
Token::Star => Node::new(MathOperator::Multiply, self.current_position),
|
|
|
|
Token::Slash => Node::new(MathOperator::Divide, self.current_position),
|
|
|
|
Token::Percent => Node::new(MathOperator::Modulo, self.current_position),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
let right = self.parse_expression(operator_precedence)?;
|
|
|
|
let position = (left_start, right.position().1);
|
|
|
|
|
|
|
|
return Ok(Expression::operator(
|
|
|
|
OperatorExpression::Math {
|
|
|
|
left,
|
|
|
|
operator: math_operator,
|
|
|
|
right,
|
|
|
|
},
|
|
|
|
position,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Token::DoubleEqual
|
|
|
|
| Token::BangEqual
|
|
|
|
| Token::Less
|
|
|
|
| Token::LessEqual
|
|
|
|
| Token::Greater
|
|
|
|
| Token::GreaterEqual = &self.current_token
|
|
|
|
{
|
|
|
|
let comparison_operator = match &self.current_token {
|
|
|
|
Token::DoubleEqual => Node::new(ComparisonOperator::Equal, self.current_position),
|
|
|
|
Token::BangEqual => Node::new(ComparisonOperator::NotEqual, self.current_position),
|
|
|
|
Token::Less => Node::new(ComparisonOperator::LessThan, self.current_position),
|
|
|
|
Token::LessEqual => {
|
|
|
|
Node::new(ComparisonOperator::LessThanOrEqual, self.current_position)
|
|
|
|
}
|
|
|
|
Token::Greater => Node::new(ComparisonOperator::GreaterThan, self.current_position),
|
|
|
|
Token::GreaterEqual => Node::new(
|
|
|
|
ComparisonOperator::GreaterThanOrEqual,
|
|
|
|
self.current_position,
|
|
|
|
),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
let right = self.parse_expression(operator_precedence)?;
|
|
|
|
let position = (left_start, right.position().1);
|
|
|
|
|
|
|
|
return Ok(Expression::operator(
|
|
|
|
OperatorExpression::Comparison {
|
|
|
|
left,
|
|
|
|
operator: comparison_operator,
|
|
|
|
right,
|
|
|
|
},
|
|
|
|
position,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let logic_operator = match &self.current_token {
|
|
|
|
Token::DoubleAmpersand => Node::new(LogicOperator::And, self.current_position),
|
|
|
|
Token::DoublePipe => Node::new(LogicOperator::Or, self.current_position),
|
2024-08-09 18:01:01 +00:00
|
|
|
_ => {
|
|
|
|
return Err(ParseError::UnexpectedToken {
|
2024-08-14 18:28:39 +00:00
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
2024-08-15 01:15:37 +00:00
|
|
|
})
|
2024-08-09 18:01:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let right = self.parse_expression(operator_precedence)?;
|
|
|
|
let position = (left_start, right.position().1);
|
2024-08-09 18:01:01 +00:00
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
Ok(Expression::operator(
|
2024-08-15 01:15:37 +00:00
|
|
|
OperatorExpression::Logic {
|
2024-08-14 18:28:39 +00:00
|
|
|
left,
|
2024-08-15 01:15:37 +00:00
|
|
|
operator: logic_operator,
|
2024-08-14 18:28:39 +00:00
|
|
|
right,
|
2024-08-09 18:01:01 +00:00
|
|
|
},
|
2024-08-14 18:28:39 +00:00
|
|
|
position,
|
2024-08-09 18:01:01 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
fn parse_postfix(&mut self, left: Expression) -> Result<Expression, ParseError> {
|
2024-08-14 18:28:39 +00:00
|
|
|
log::trace!("Parsing {} as postfix operator", self.current_token);
|
2024-08-14 07:53:15 +00:00
|
|
|
|
2024-08-15 01:15:37 +00:00
|
|
|
let expression = match &self.current_token {
|
2024-08-15 04:20:36 +00:00
|
|
|
Token::Dot => {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
if let Token::Integer(text) = &self.current_token {
|
|
|
|
let index = text.parse::<usize>().map_err(|error| ParseError::Integer {
|
|
|
|
error,
|
|
|
|
position: self.current_position,
|
|
|
|
})?;
|
|
|
|
let index_node = Node::new(index, self.current_position);
|
|
|
|
let position = (left.position().0, self.current_position.1);
|
|
|
|
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
Expression::tuple_access(left, index_node, position)
|
|
|
|
} else {
|
|
|
|
let field = self.parse_identifier()?;
|
2024-08-20 11:20:44 +00:00
|
|
|
let position = (left.position().0, field.position.1);
|
2024-08-15 04:20:36 +00:00
|
|
|
|
|
|
|
Expression::field_access(left, field, position)
|
|
|
|
}
|
|
|
|
}
|
2024-08-13 20:21:44 +00:00
|
|
|
Token::LeftParenthesis => {
|
|
|
|
self.next_token()?;
|
2024-08-09 22:14:46 +00:00
|
|
|
|
2024-08-13 20:21:44 +00:00
|
|
|
let mut arguments = Vec::new();
|
2024-08-12 20:57:10 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
while self.current_token != Token::RightParenthesis {
|
|
|
|
let argument = self.parse_expression(0)?;
|
2024-08-12 20:57:10 +00:00
|
|
|
|
2024-08-13 20:21:44 +00:00
|
|
|
arguments.push(argument);
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
if let Token::Comma = self.current_token {
|
2024-08-13 20:21:44 +00:00
|
|
|
self.next_token()?;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 20:57:10 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let position = (left.position().0, self.current_position.1);
|
2024-08-14 01:24:56 +00:00
|
|
|
|
2024-08-15 04:20:36 +00:00
|
|
|
Expression::call(left, arguments, position)
|
2024-08-13 20:21:44 +00:00
|
|
|
}
|
|
|
|
Token::LeftSquareBrace => {
|
|
|
|
self.next_token()?;
|
2024-08-12 23:06:57 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let index = self.parse_expression(0)?;
|
2024-08-12 23:06:57 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let operator_end = if let Token::RightSquareBrace = self.current_token {
|
|
|
|
let end = self.current_position.1;
|
2024-08-12 23:06:57 +00:00
|
|
|
|
2024-08-13 20:21:44 +00:00
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
end
|
|
|
|
} else {
|
|
|
|
return Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::RightSquareBrace,
|
2024-08-14 18:28:39 +00:00
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
2024-08-13 20:21:44 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
let position = (left.position().0, operator_end);
|
2024-08-13 20:21:44 +00:00
|
|
|
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::list_index(left, index, position)
|
2024-08-13 20:21:44 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2024-08-17 08:06:13 +00:00
|
|
|
return Err(ParseError::ExpectedTokenMultiple {
|
|
|
|
expected: vec![
|
|
|
|
TokenKind::Dot,
|
|
|
|
TokenKind::LeftParenthesis,
|
|
|
|
TokenKind::LeftSquareBrace,
|
|
|
|
],
|
2024-08-14 18:28:39 +00:00
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
2024-08-13 20:21:44 +00:00
|
|
|
});
|
|
|
|
}
|
2024-08-12 13:06:42 +00:00
|
|
|
};
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
if self.current_token.is_postfix() {
|
2024-08-15 01:15:37 +00:00
|
|
|
self.parse_postfix(expression)
|
2024-08-12 23:06:57 +00:00
|
|
|
} else {
|
2024-08-15 01:15:37 +00:00
|
|
|
Ok(expression)
|
2024-08-12 23:06:57 +00:00
|
|
|
}
|
2024-08-09 22:14:46 +00:00
|
|
|
}
|
2024-08-10 09:23:43 +00:00
|
|
|
|
2024-08-16 04:41:52 +00:00
|
|
|
fn parse_if(&mut self) -> Result<IfExpression, ParseError> {
|
2024-08-15 01:15:37 +00:00
|
|
|
// Assume that the "if" token has already been consumed
|
2024-08-14 22:30:36 +00:00
|
|
|
|
2024-08-16 01:34:47 +00:00
|
|
|
self.mode = ParserMode::Condition;
|
|
|
|
|
|
|
|
let condition = self.parse_expression(0)?;
|
|
|
|
|
|
|
|
self.mode = ParserMode::Normal;
|
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
let if_block = self.parse_block()?;
|
|
|
|
|
|
|
|
if let Token::Else = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-16 04:41:52 +00:00
|
|
|
let if_keyword_start = self.current_position.0;
|
|
|
|
|
2024-08-15 01:15:37 +00:00
|
|
|
if let Token::If = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-16 04:41:52 +00:00
|
|
|
let if_expression = self.parse_if()?;
|
|
|
|
let position = (if_keyword_start, self.current_position.1);
|
2024-08-15 01:15:37 +00:00
|
|
|
|
2024-08-16 04:41:52 +00:00
|
|
|
Ok(IfExpression::IfElse {
|
2024-08-14 22:30:36 +00:00
|
|
|
condition,
|
|
|
|
if_block,
|
2024-08-16 04:41:52 +00:00
|
|
|
r#else: ElseExpression::If(Node::new(Box::new(if_expression), position)),
|
2024-08-14 22:30:36 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let else_block = self.parse_block()?;
|
|
|
|
|
2024-08-16 04:41:52 +00:00
|
|
|
Ok(IfExpression::IfElse {
|
2024-08-14 22:30:36 +00:00
|
|
|
condition,
|
|
|
|
if_block,
|
|
|
|
r#else: ElseExpression::Block(else_block),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
2024-08-16 04:41:52 +00:00
|
|
|
Ok(IfExpression::If {
|
2024-08-14 22:30:36 +00:00
|
|
|
condition,
|
|
|
|
if_block,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
fn parse_identifier(&mut self) -> Result<Node<Identifier>, ParseError> {
|
|
|
|
if let Token::Identifier(text) = self.current_token {
|
2024-08-15 01:15:37 +00:00
|
|
|
let position = self.current_position;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
self.next_token()?;
|
2024-08-13 17:54:16 +00:00
|
|
|
|
2024-08-15 01:15:37 +00:00
|
|
|
Ok(Node::new(Identifier::new(text), position))
|
2024-08-14 18:28:39 +00:00
|
|
|
} else {
|
|
|
|
Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Identifier,
|
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
})
|
|
|
|
}
|
2024-08-13 17:54:16 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 09:14:00 +00:00
|
|
|
fn parse_block(&mut self) -> Result<Node<BlockExpression>, ParseError> {
|
2024-08-14 18:28:39 +00:00
|
|
|
let left_start = self.current_position.0;
|
|
|
|
let is_async = if let Token::Async = self.current_token {
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2024-08-10 09:23:43 +00:00
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
if let Token::LeftCurlyBrace = self.current_token {
|
2024-08-10 09:23:43 +00:00
|
|
|
self.next_token()?;
|
|
|
|
} else {
|
|
|
|
return Err(ParseError::ExpectedToken {
|
2024-08-12 14:08:34 +00:00
|
|
|
expected: TokenKind::LeftCurlyBrace,
|
2024-08-14 18:28:39 +00:00
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
2024-08-10 09:23:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-24 14:13:16 +00:00
|
|
|
let mut statements = VecDeque::new();
|
2024-08-10 09:23:43 +00:00
|
|
|
|
|
|
|
loop {
|
2024-08-14 18:28:39 +00:00
|
|
|
if let Token::RightCurlyBrace = self.current_token {
|
|
|
|
let position = (left_start, self.current_position.1);
|
2024-08-10 09:23:43 +00:00
|
|
|
|
|
|
|
self.next_token()?;
|
|
|
|
|
2024-08-30 22:06:58 +00:00
|
|
|
let ast = AbstractSyntaxTree {
|
|
|
|
statements,
|
|
|
|
context: self.context.create_child(),
|
|
|
|
};
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
return if is_async {
|
2024-08-30 22:06:58 +00:00
|
|
|
Ok(Node::new(BlockExpression::Async(ast), position))
|
2024-08-14 18:28:39 +00:00
|
|
|
} else {
|
2024-08-30 22:06:58 +00:00
|
|
|
Ok(Node::new(BlockExpression::Sync(ast), position))
|
2024-08-14 18:28:39 +00:00
|
|
|
};
|
2024-08-10 09:23:43 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 19:52:04 +00:00
|
|
|
let statement = self.parse_statement()?;
|
2024-08-10 09:23:43 +00:00
|
|
|
|
2024-08-24 14:13:16 +00:00
|
|
|
statements.push_back(statement);
|
2024-08-10 09:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-13 19:12:32 +00:00
|
|
|
|
|
|
|
fn parse_type(&mut self) -> Result<Node<Type>, ParseError> {
|
2024-08-14 18:28:39 +00:00
|
|
|
let r#type = match self.current_token {
|
2024-08-13 19:12:32 +00:00
|
|
|
Token::Bool => Type::Boolean,
|
|
|
|
Token::FloatKeyword => Type::Float,
|
|
|
|
Token::Int => Type::Integer,
|
|
|
|
_ => {
|
|
|
|
return Err(ParseError::ExpectedTokenMultiple {
|
|
|
|
expected: vec![TokenKind::Bool, TokenKind::FloatKeyword, TokenKind::Int],
|
2024-08-14 18:28:39 +00:00
|
|
|
actual: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
2024-08-13 19:12:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2024-08-14 18:28:39 +00:00
|
|
|
let position = self.current_position;
|
2024-08-13 19:12:32 +00:00
|
|
|
|
|
|
|
self.next_token()?;
|
|
|
|
|
|
|
|
Ok(Node::new(r#type, position))
|
|
|
|
}
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 01:34:47 +00:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum ParserMode {
|
|
|
|
Condition,
|
|
|
|
Normal,
|
|
|
|
}
|
|
|
|
|
2024-08-04 23:25:44 +00:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum ParseError {
|
2024-08-14 18:28:39 +00:00
|
|
|
Boolean {
|
2024-08-09 18:01:01 +00:00
|
|
|
error: ParseBoolError,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-08-14 18:28:39 +00:00
|
|
|
Lex(LexError),
|
2024-08-11 17:16:16 +00:00
|
|
|
ExpectedAssignment {
|
2024-08-14 18:28:39 +00:00
|
|
|
actual: Statement,
|
|
|
|
},
|
|
|
|
ExpectedExpression {
|
|
|
|
actual: Statement,
|
2024-08-11 17:16:16 +00:00
|
|
|
},
|
2024-08-14 22:30:36 +00:00
|
|
|
ExpectedIdentifierNode {
|
|
|
|
actual: Expression,
|
|
|
|
},
|
|
|
|
ExpectedIdentifierToken {
|
2024-08-09 10:09:59 +00:00
|
|
|
actual: TokenOwned,
|
2024-08-09 15:41:23 +00:00
|
|
|
position: Span,
|
2024-08-09 08:23:02 +00:00
|
|
|
},
|
2024-08-09 10:09:59 +00:00
|
|
|
ExpectedToken {
|
2024-08-12 14:08:34 +00:00
|
|
|
expected: TokenKind,
|
2024-08-09 08:23:02 +00:00
|
|
|
actual: TokenOwned,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-08-13 19:12:32 +00:00
|
|
|
ExpectedTokenMultiple {
|
|
|
|
expected: Vec<TokenKind>,
|
|
|
|
actual: TokenOwned,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-08-09 08:23:02 +00:00
|
|
|
UnexpectedToken {
|
|
|
|
actual: TokenOwned,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-08-14 18:28:39 +00:00
|
|
|
Float {
|
2024-08-09 18:01:01 +00:00
|
|
|
error: ParseFloatError,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-08-14 18:28:39 +00:00
|
|
|
Integer {
|
2024-08-09 18:01:01 +00:00
|
|
|
error: ParseIntError,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-08-09 04:49:17 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 04:01:50 +00:00
|
|
|
impl From<LexError> for ParseError {
|
|
|
|
fn from(v: LexError) -> Self {
|
2024-08-14 18:28:39 +00:00
|
|
|
Self::Lex(v)
|
2024-08-10 04:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 04:49:17 +00:00
|
|
|
impl ParseError {
|
|
|
|
pub fn position(&self) -> Span {
|
|
|
|
match self {
|
2024-08-14 18:28:39 +00:00
|
|
|
ParseError::Boolean { position, .. } => *position,
|
|
|
|
ParseError::ExpectedAssignment { actual } => actual.position(),
|
|
|
|
ParseError::ExpectedExpression { actual } => actual.position(),
|
2024-08-14 22:30:36 +00:00
|
|
|
ParseError::ExpectedIdentifierNode { actual } => actual.position(),
|
|
|
|
ParseError::ExpectedIdentifierToken { position, .. } => *position,
|
2024-08-09 18:01:01 +00:00
|
|
|
ParseError::ExpectedToken { position, .. } => *position,
|
2024-08-13 19:12:32 +00:00
|
|
|
ParseError::ExpectedTokenMultiple { position, .. } => *position,
|
2024-08-14 18:28:39 +00:00
|
|
|
ParseError::Float { position, .. } => *position,
|
|
|
|
ParseError::Integer { position, .. } => *position,
|
|
|
|
ParseError::Lex(error) => error.position(),
|
2024-08-09 18:01:01 +00:00
|
|
|
ParseError::UnexpectedToken { position, .. } => *position,
|
2024-08-09 04:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-04 23:25:44 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 00:58:56 +00:00
|
|
|
impl Display for ParseError {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2024-08-14 18:28:39 +00:00
|
|
|
Self::Boolean { error, .. } => write!(f, "{}", error),
|
2024-08-11 17:16:16 +00:00
|
|
|
Self::ExpectedAssignment { .. } => write!(f, "Expected assignment"),
|
2024-08-14 18:28:39 +00:00
|
|
|
Self::ExpectedExpression { .. } => write!(f, "Expected expression"),
|
2024-08-14 22:30:36 +00:00
|
|
|
Self::ExpectedIdentifierNode { actual } => {
|
|
|
|
write!(f, "Expected identifier, found {actual}")
|
|
|
|
}
|
|
|
|
Self::ExpectedIdentifierToken { actual, .. } => {
|
2024-08-09 08:23:02 +00:00
|
|
|
write!(f, "Expected identifier, found {actual}")
|
|
|
|
}
|
2024-08-09 10:09:59 +00:00
|
|
|
Self::ExpectedToken {
|
|
|
|
expected, actual, ..
|
|
|
|
} => write!(f, "Expected token {expected}, found {actual}"),
|
2024-08-13 19:12:32 +00:00
|
|
|
Self::ExpectedTokenMultiple {
|
|
|
|
expected, actual, ..
|
|
|
|
} => {
|
|
|
|
write!(f, "Expected one of")?;
|
|
|
|
|
|
|
|
for (i, token_kind) in expected.iter().enumerate() {
|
|
|
|
if i == 0 {
|
|
|
|
write!(f, " {token_kind}")?;
|
|
|
|
} else if i == expected.len() - 1 {
|
|
|
|
write!(f, " or {token_kind}")?;
|
|
|
|
} else {
|
|
|
|
write!(f, ", {token_kind}")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ", found {actual}")
|
|
|
|
}
|
2024-08-14 18:28:39 +00:00
|
|
|
Self::Float { error, .. } => write!(f, "{}", error),
|
|
|
|
Self::Integer { error, .. } => write!(f, "{}", error),
|
|
|
|
Self::Lex(error) => write!(f, "{}", error),
|
2024-08-09 04:49:17 +00:00
|
|
|
Self::UnexpectedToken { actual, .. } => write!(f, "Unexpected token {actual}"),
|
2024-08-09 00:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-04 00:23:52 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-08-14 19:52:04 +00:00
|
|
|
use crate::{Identifier, Type};
|
2024-08-04 00:23:52 +00:00
|
|
|
|
2024-08-04 23:25:44 +00:00
|
|
|
use super::*;
|
2024-08-04 00:23:52 +00:00
|
|
|
|
2024-08-28 15:31:47 +00:00
|
|
|
#[test]
|
|
|
|
fn dereference() {
|
|
|
|
let source = "*a";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::dereference(
|
|
|
|
Expression::identifier(Identifier::new("a"), (1, 2)),
|
|
|
|
(0, 2)
|
|
|
|
),)
|
|
|
|
]))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-23 09:54:58 +00:00
|
|
|
#[test]
|
|
|
|
fn character_literal() {
|
|
|
|
let source = "'a'";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::literal('a', (0, 3)))
|
|
|
|
]))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-20 18:45:43 +00:00
|
|
|
#[test]
|
|
|
|
fn break_loop() {
|
|
|
|
let source = "loop { break; }";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::infinite_loop(
|
|
|
|
Node::new(
|
2024-08-30 22:06:58 +00:00
|
|
|
BlockExpression::Sync(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::ExpressionNullified(Node::new(
|
|
|
|
Expression::r#break(None, (7, 12)),
|
|
|
|
(7, 13)
|
|
|
|
))
|
|
|
|
])),
|
2024-08-20 18:45:43 +00:00
|
|
|
(5, 15)
|
|
|
|
),
|
|
|
|
(0, 15)
|
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-20 11:20:44 +00:00
|
|
|
#[test]
|
|
|
|
fn built_in_function() {
|
|
|
|
let source = "42.to_string()";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::call(
|
|
|
|
Expression::field_access(
|
|
|
|
Expression::literal(42, (0, 2)),
|
|
|
|
Node::new(Identifier::new("to_string"), (3, 12)),
|
|
|
|
(0, 12)
|
|
|
|
),
|
|
|
|
vec![],
|
|
|
|
(0, 14)
|
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-17 08:06:13 +00:00
|
|
|
#[test]
|
2024-08-17 14:07:38 +00:00
|
|
|
fn map_expression() {
|
2024-08-23 09:24:48 +00:00
|
|
|
let source = "map { x = \"1\", y = 2, z = 3.0 }";
|
2024-08-17 14:07:38 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
2024-08-30 22:06:58 +00:00
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::map(
|
2024-08-17 14:07:38 +00:00
|
|
|
vec![
|
|
|
|
(
|
|
|
|
Node::new(Identifier::new("x"), (6, 7)),
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::literal("1".to_string(), (10, 13)),
|
2024-08-17 14:07:38 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
Node::new(Identifier::new("y"), (15, 16)),
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::literal(2, (19, 20)),
|
2024-08-17 14:07:38 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
Node::new(Identifier::new("z"), (22, 23)),
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::literal(3.0, (26, 29)),
|
2024-08-17 14:07:38 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
(0, 31),
|
2024-08-30 22:06:58 +00:00
|
|
|
))
|
|
|
|
]))
|
2024-08-17 14:07:38 +00:00
|
|
|
);
|
|
|
|
}
|
2024-08-17 08:06:13 +00:00
|
|
|
|
2024-08-17 14:07:38 +00:00
|
|
|
#[test]
|
|
|
|
fn let_mut_while_loop() {
|
2024-08-17 09:32:18 +00:00
|
|
|
let source = "let mut x = 0; while x < 10 { x += 1 }; x";
|
2024-08-17 08:06:13 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
2024-08-30 22:06:58 +00:00
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Let(Node::new(
|
|
|
|
LetStatement::LetMut {
|
|
|
|
identifier: Node::new(Identifier::new("x"), (8, 9)),
|
|
|
|
value: Expression::literal(0, (12, 13)),
|
|
|
|
},
|
|
|
|
(0, 14),
|
|
|
|
)),
|
|
|
|
Statement::ExpressionNullified(Node::new(
|
|
|
|
Expression::while_loop(
|
|
|
|
Expression::comparison(
|
|
|
|
Expression::identifier(Identifier::new("x"), (21, 22)),
|
|
|
|
Node::new(ComparisonOperator::LessThan, (23, 24)),
|
|
|
|
Expression::literal(10, (25, 27)),
|
|
|
|
(21, 27),
|
2024-08-17 08:06:13 +00:00
|
|
|
),
|
2024-08-30 22:06:58 +00:00
|
|
|
Node::new(
|
|
|
|
BlockExpression::Sync(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::compound_assignment(
|
|
|
|
Expression::identifier(Identifier::new("x"), (30, 31)),
|
|
|
|
Node::new(MathOperator::Add, (32, 34)),
|
|
|
|
Expression::literal(1, (35, 36)),
|
|
|
|
(30, 36),
|
|
|
|
),)
|
|
|
|
])),
|
|
|
|
(28, 38),
|
|
|
|
),
|
|
|
|
(15, 38),
|
|
|
|
),
|
|
|
|
(15, 39)
|
|
|
|
)),
|
|
|
|
Statement::Expression(Expression::identifier(Identifier::new("x"), (40, 41)),),
|
|
|
|
]))
|
2024-08-17 08:06:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-14 05:13:43 +00:00
|
|
|
#[test]
|
2024-08-16 11:09:46 +00:00
|
|
|
fn let_statement() {
|
2024-08-17 08:06:13 +00:00
|
|
|
let source = "let x = 42;";
|
2024-08-16 11:09:46 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
2024-08-23 20:47:57 +00:00
|
|
|
Ok(AbstractSyntaxTree::with_statements([Statement::Let(
|
|
|
|
Node::new(
|
2024-08-16 11:09:46 +00:00
|
|
|
LetStatement::Let {
|
|
|
|
identifier: Node::new(Identifier::new("x"), (4, 5)),
|
2024-08-17 16:15:47 +00:00
|
|
|
value: Expression::literal(42, (8, 10)),
|
2024-08-16 11:09:46 +00:00
|
|
|
},
|
2024-08-17 08:06:13 +00:00
|
|
|
(0, 11),
|
2024-08-23 20:47:57 +00:00
|
|
|
)
|
|
|
|
)]))
|
2024-08-16 11:09:46 +00:00
|
|
|
);
|
|
|
|
}
|
2024-08-14 05:13:43 +00:00
|
|
|
|
2024-08-16 11:09:46 +00:00
|
|
|
#[test]
|
|
|
|
fn let_mut_statement() {
|
2024-08-17 08:06:13 +00:00
|
|
|
let source = "let mut x = false;";
|
2024-08-16 11:09:46 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
2024-08-30 22:06:58 +00:00
|
|
|
Ok(AbstractSyntaxTree::with_statements([Statement::Let(
|
|
|
|
Node::new(
|
2024-08-16 11:09:46 +00:00
|
|
|
LetStatement::LetMut {
|
|
|
|
identifier: Node::new(Identifier::new("x"), (8, 9)),
|
2024-08-17 16:15:47 +00:00
|
|
|
value: Expression::literal(false, (12, 17)),
|
2024-08-16 11:09:46 +00:00
|
|
|
},
|
2024-08-17 08:06:13 +00:00
|
|
|
(0, 18),
|
2024-08-30 22:06:58 +00:00
|
|
|
)
|
|
|
|
)]))
|
2024-08-16 11:09:46 +00:00
|
|
|
);
|
2024-08-14 05:13:43 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 03:45:17 +00:00
|
|
|
#[test]
|
|
|
|
fn async_block() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "async { x = 42; y = 4.0 }";
|
2024-08-14 03:45:17 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2024-08-14 19:52:04 +00:00
|
|
|
parse(source),
|
2024-08-30 22:06:58 +00:00
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::block(
|
|
|
|
BlockExpression::Async(AbstractSyntaxTree::with_statements([
|
2024-08-20 04:15:19 +00:00
|
|
|
Statement::ExpressionNullified(Node::new(
|
|
|
|
Expression::operator(
|
2024-08-16 01:22:24 +00:00
|
|
|
OperatorExpression::Assignment {
|
2024-08-20 04:15:19 +00:00
|
|
|
assignee: Expression::identifier(Identifier::new("x"), (8, 9)),
|
|
|
|
value: Expression::literal(42, (12, 14)),
|
2024-08-16 01:22:24 +00:00
|
|
|
},
|
2024-08-20 04:15:19 +00:00
|
|
|
(8, 14)
|
|
|
|
),
|
|
|
|
(8, 15)
|
|
|
|
)),
|
|
|
|
Statement::Expression(Expression::operator(
|
|
|
|
OperatorExpression::Assignment {
|
|
|
|
assignee: Expression::identifier(Identifier::new("y"), (16, 17)),
|
|
|
|
value: Expression::literal(4.0, (20, 23)),
|
|
|
|
},
|
|
|
|
(16, 23)
|
|
|
|
))
|
2024-08-24 14:13:16 +00:00
|
|
|
])),
|
2024-08-16 01:22:24 +00:00
|
|
|
(0, 25)
|
2024-08-30 22:06:58 +00:00
|
|
|
))
|
|
|
|
]))
|
2024-08-14 03:45:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-14 01:24:56 +00:00
|
|
|
#[test]
|
|
|
|
fn tuple_struct_access() {
|
2024-08-23 09:24:48 +00:00
|
|
|
let source = "Foo(42, \"bar\").0";
|
2024-08-14 07:53:15 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-15 04:20:36 +00:00
|
|
|
Statement::Expression(Expression::tuple_access(
|
|
|
|
Expression::call(
|
|
|
|
Expression::identifier(Identifier::new("Foo"), (0, 3)),
|
|
|
|
vec![
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::literal(42, (4, 6)),
|
|
|
|
Expression::literal("bar".to_string(), (8, 13)),
|
2024-08-15 04:20:36 +00:00
|
|
|
],
|
|
|
|
(0, 15)
|
|
|
|
),
|
|
|
|
Node::new(0, (15, 16)),
|
|
|
|
(0, 16)
|
2024-08-14 23:43:12 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-14 01:24:56 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 23:41:36 +00:00
|
|
|
#[test]
|
|
|
|
fn fields_struct_instantiation() {
|
2024-08-15 04:20:36 +00:00
|
|
|
let source = "Foo { a: 42, b: 4.0 }";
|
2024-08-13 23:41:36 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::r#struct(
|
|
|
|
StructExpression::Fields {
|
2024-08-16 01:34:47 +00:00
|
|
|
name: Node::new(Identifier::new("Foo"), (0, 3)),
|
2024-08-14 23:43:12 +00:00
|
|
|
fields: vec![
|
|
|
|
(
|
2024-08-16 01:34:47 +00:00
|
|
|
Node::new(Identifier::new("a"), (6, 7)),
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::literal(42, (9, 11)),
|
2024-08-14 23:43:12 +00:00
|
|
|
),
|
|
|
|
(
|
2024-08-16 01:34:47 +00:00
|
|
|
Node::new(Identifier::new("b"), (13, 14)),
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::literal(4.0, (16, 19))
|
2024-08-14 23:43:12 +00:00
|
|
|
)
|
|
|
|
]
|
|
|
|
},
|
2024-08-16 01:34:47 +00:00
|
|
|
(0, 21)
|
2024-08-14 23:43:12 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-13 23:41:36 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 22:48:02 +00:00
|
|
|
#[test]
|
|
|
|
fn fields_struct() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "struct Foo { a: int, b: float }";
|
2024-08-13 22:48:02 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::struct_definition(
|
|
|
|
StructDefinition::Fields {
|
2024-08-15 04:20:36 +00:00
|
|
|
name: Node::new(Identifier::new("Foo"), (7, 10)),
|
2024-08-14 23:43:12 +00:00
|
|
|
fields: vec![
|
|
|
|
(
|
2024-08-15 04:20:36 +00:00
|
|
|
Node::new(Identifier::new("a"), (13, 14)),
|
|
|
|
Node::new(Type::Integer, (16, 19))
|
2024-08-14 23:43:12 +00:00
|
|
|
),
|
|
|
|
(
|
2024-08-15 04:20:36 +00:00
|
|
|
Node::new(Identifier::new("b"), (21, 22)),
|
|
|
|
Node::new(Type::Float, (24, 29))
|
2024-08-14 23:43:12 +00:00
|
|
|
)
|
|
|
|
]
|
|
|
|
},
|
2024-08-15 04:20:36 +00:00
|
|
|
(0, 31)
|
2024-08-14 23:43:12 +00:00
|
|
|
)
|
|
|
|
]))
|
|
|
|
);
|
2024-08-13 22:48:02 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 20:21:44 +00:00
|
|
|
#[test]
|
|
|
|
fn tuple_struct_instantiation() {
|
2024-08-17 14:07:38 +00:00
|
|
|
let source = "Foo(1, 2.0)";
|
2024-08-13 20:21:44 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-15 01:15:37 +00:00
|
|
|
Statement::Expression(Expression::call(
|
2024-08-17 14:07:38 +00:00
|
|
|
Expression::identifier(Identifier::new("Foo"), (0, 3)),
|
2024-08-15 04:20:36 +00:00
|
|
|
vec![
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::literal(1, (4, 5)),
|
|
|
|
Expression::literal(2.0, (7, 10))
|
2024-08-15 04:20:36 +00:00
|
|
|
],
|
2024-08-17 14:07:38 +00:00
|
|
|
(0, 11)
|
2024-08-14 23:43:12 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-13 20:21:44 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 19:12:32 +00:00
|
|
|
#[test]
|
2024-08-17 14:07:38 +00:00
|
|
|
fn tuple_struct_definition() {
|
|
|
|
let source = "struct Foo(int, float);";
|
2024-08-13 19:12:32 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::StructDefinition(Node::new(
|
|
|
|
StructDefinition::Tuple {
|
2024-08-15 01:15:37 +00:00
|
|
|
name: Node::new(Identifier::new("Foo"), (7, 10)),
|
2024-08-14 23:43:12 +00:00
|
|
|
items: vec![
|
2024-08-15 01:15:37 +00:00
|
|
|
Node::new(Type::Integer, (11, 14)),
|
|
|
|
Node::new(Type::Float, (16, 21)),
|
2024-08-14 23:43:12 +00:00
|
|
|
],
|
|
|
|
},
|
2024-08-17 14:07:38 +00:00
|
|
|
(0, 23)
|
2024-08-14 23:43:12 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-13 19:12:32 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 17:54:16 +00:00
|
|
|
#[test]
|
|
|
|
fn unit_struct() {
|
2024-08-17 14:07:38 +00:00
|
|
|
let source = "struct Foo;";
|
2024-08-13 17:54:16 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::StructDefinition(Node::new(
|
|
|
|
StructDefinition::Unit {
|
2024-08-15 01:15:37 +00:00
|
|
|
name: Node::new(Identifier::new("Foo"), (7, 10)),
|
2024-08-14 23:43:12 +00:00
|
|
|
},
|
2024-08-17 14:07:38 +00:00
|
|
|
(0, 11)
|
2024-08-14 23:43:12 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-13 17:54:16 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 20:57:10 +00:00
|
|
|
#[test]
|
|
|
|
fn list_index_nested() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "[1, [2], 3][1][0]";
|
2024-08-12 20:57:10 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::list_index(
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::list_index(
|
|
|
|
Expression::list(
|
|
|
|
[
|
|
|
|
Expression::literal(1, (1, 2)),
|
|
|
|
Expression::list([Expression::literal(2, (5, 6))], (4, 7)),
|
|
|
|
Expression::literal(3, (9, 10)),
|
|
|
|
],
|
|
|
|
(0, 11)
|
2024-08-14 23:43:12 +00:00
|
|
|
),
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::literal(1, (12, 13)),
|
|
|
|
(0, 14)
|
|
|
|
),
|
|
|
|
Expression::literal(0, (15, 16)),
|
2024-08-15 04:20:36 +00:00
|
|
|
(0, 17)
|
2024-08-17 16:15:47 +00:00
|
|
|
),)
|
2024-08-14 23:43:12 +00:00
|
|
|
]))
|
|
|
|
);
|
2024-08-12 20:57:10 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 14:08:34 +00:00
|
|
|
#[test]
|
|
|
|
fn range() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "0..42";
|
2024-08-12 14:08:34 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-16 15:21:20 +00:00
|
|
|
Statement::Expression(Expression::exclusive_range(
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::literal(0, (0, 1)),
|
|
|
|
Expression::literal(42, (3, 5)),
|
2024-08-15 01:15:37 +00:00
|
|
|
(0, 5)
|
2024-08-14 23:43:12 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-12 14:08:34 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:44:05 +00:00
|
|
|
#[test]
|
|
|
|
fn negate_variable() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "a = 1; -a";
|
2024-08-12 09:44:05 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-15 01:15:37 +00:00
|
|
|
Statement::ExpressionNullified(Node::new(
|
|
|
|
Expression::operator(
|
|
|
|
OperatorExpression::Assignment {
|
|
|
|
assignee: Expression::identifier(Identifier::new("a"), (0, 1)),
|
2024-08-17 16:15:47 +00:00
|
|
|
value: Expression::literal(1, (4, 5)),
|
2024-08-15 01:15:37 +00:00
|
|
|
},
|
|
|
|
(0, 5)
|
|
|
|
),
|
|
|
|
(0, 6)
|
2024-08-14 23:43:12 +00:00
|
|
|
)),
|
|
|
|
Statement::Expression(Expression::operator(
|
|
|
|
OperatorExpression::Negation(Expression::identifier(
|
|
|
|
Identifier::new("a"),
|
2024-08-15 01:15:37 +00:00
|
|
|
(8, 9)
|
2024-08-14 23:43:12 +00:00
|
|
|
)),
|
2024-08-15 01:15:37 +00:00
|
|
|
(7, 9)
|
2024-08-14 23:43:12 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-12 09:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn negate_expression() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "-(1 + 1)";
|
2024-08-12 09:44:05 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::operator(
|
|
|
|
OperatorExpression::Negation(Expression::grouped(
|
|
|
|
Expression::operator(
|
|
|
|
OperatorExpression::Math {
|
2024-08-17 16:15:47 +00:00
|
|
|
left: Expression::literal(1, (2, 3)),
|
2024-08-15 01:15:37 +00:00
|
|
|
operator: Node::new(MathOperator::Add, (4, 5)),
|
2024-08-17 16:15:47 +00:00
|
|
|
right: Expression::literal(1, (6, 7)),
|
2024-08-14 23:43:12 +00:00
|
|
|
},
|
2024-08-15 01:15:37 +00:00
|
|
|
(2, 7)
|
2024-08-14 23:43:12 +00:00
|
|
|
),
|
2024-08-15 01:15:37 +00:00
|
|
|
(1, 8)
|
2024-08-14 23:43:12 +00:00
|
|
|
)),
|
2024-08-15 01:15:37 +00:00
|
|
|
(0, 8)
|
2024-08-14 23:43:12 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-12 09:44:05 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 10:00:14 +00:00
|
|
|
#[test]
|
|
|
|
fn not_expression() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "!(1 > 42)";
|
2024-08-12 10:00:14 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::operator(
|
|
|
|
OperatorExpression::Not(Expression::grouped(
|
|
|
|
Expression::operator(
|
|
|
|
OperatorExpression::Comparison {
|
2024-08-17 16:15:47 +00:00
|
|
|
left: Expression::literal(1, (2, 3)),
|
2024-08-15 01:15:37 +00:00
|
|
|
operator: Node::new(ComparisonOperator::GreaterThan, (4, 5)),
|
2024-08-17 16:15:47 +00:00
|
|
|
right: Expression::literal(42, (6, 8)),
|
2024-08-14 23:43:12 +00:00
|
|
|
},
|
2024-08-15 01:15:37 +00:00
|
|
|
(2, 8)
|
2024-08-14 23:43:12 +00:00
|
|
|
),
|
2024-08-15 01:15:37 +00:00
|
|
|
(1, 9)
|
2024-08-14 23:43:12 +00:00
|
|
|
)),
|
2024-08-15 01:15:37 +00:00
|
|
|
(0, 9)
|
2024-08-14 23:43:12 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-12 10:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_variable() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "a = false; !a";
|
2024-08-12 10:00:14 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-15 01:15:37 +00:00
|
|
|
Statement::ExpressionNullified(Node::new(
|
|
|
|
Expression::operator(
|
|
|
|
OperatorExpression::Assignment {
|
|
|
|
assignee: Expression::identifier(Identifier::new("a"), (0, 1)),
|
2024-08-17 16:15:47 +00:00
|
|
|
value: Expression::literal(false, (4, 9)),
|
2024-08-15 01:15:37 +00:00
|
|
|
},
|
|
|
|
(0, 9)
|
|
|
|
),
|
|
|
|
(0, 10)
|
2024-08-14 23:43:12 +00:00
|
|
|
)),
|
|
|
|
Statement::Expression(Expression::operator(
|
2024-08-15 01:15:37 +00:00
|
|
|
OperatorExpression::Not(Expression::identifier(Identifier::new("a"), (12, 13))),
|
|
|
|
(11, 13)
|
2024-08-14 23:43:12 +00:00
|
|
|
)),
|
|
|
|
]))
|
|
|
|
);
|
2024-08-12 10:00:14 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 18:35:33 +00:00
|
|
|
#[test]
|
|
|
|
fn r#if() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "if x { y }";
|
2024-08-11 18:35:33 +00:00
|
|
|
|
2024-08-14 23:43:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-20 04:15:19 +00:00
|
|
|
Statement::Expression(Expression::r#if(
|
|
|
|
IfExpression::If {
|
|
|
|
condition: Expression::identifier(Identifier::new("x"), (3, 4)),
|
|
|
|
if_block: Node::new(
|
2024-08-30 22:06:58 +00:00
|
|
|
BlockExpression::Sync(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::identifier(
|
|
|
|
Identifier::new("y"),
|
|
|
|
(7, 8)
|
|
|
|
))
|
|
|
|
])),
|
2024-08-20 04:15:19 +00:00
|
|
|
(5, 10)
|
|
|
|
)
|
|
|
|
},
|
2024-08-14 23:43:12 +00:00
|
|
|
(0, 10)
|
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-11 18:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_else() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "if x { y } else { z }";
|
2024-08-11 18:35:33 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-20 04:15:19 +00:00
|
|
|
Statement::Expression(Expression::r#if(
|
|
|
|
IfExpression::IfElse {
|
|
|
|
condition: Expression::identifier(Identifier::new("x"), (3, 4)),
|
|
|
|
if_block: Node::new(
|
2024-08-30 22:06:58 +00:00
|
|
|
BlockExpression::Sync(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::identifier(
|
|
|
|
Identifier::new("y"),
|
|
|
|
(7, 8)
|
|
|
|
))
|
|
|
|
])),
|
2024-08-20 04:15:19 +00:00
|
|
|
(5, 10)
|
|
|
|
),
|
|
|
|
r#else: ElseExpression::Block(Node::new(
|
2024-08-30 22:06:58 +00:00
|
|
|
BlockExpression::Sync(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::identifier(
|
|
|
|
Identifier::new("z"),
|
|
|
|
(18, 19)
|
|
|
|
))
|
|
|
|
])),
|
2024-08-20 04:15:19 +00:00
|
|
|
(16, 21)
|
|
|
|
))
|
|
|
|
},
|
2024-08-14 22:30:36 +00:00
|
|
|
(0, 21)
|
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-11 18:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_else_if_else() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "if x { y } else if z { a } else { b }";
|
2024-08-11 18:35:33 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-20 04:15:19 +00:00
|
|
|
Statement::Expression(Expression::r#if(
|
|
|
|
IfExpression::IfElse {
|
|
|
|
condition: Expression::identifier(Identifier::new("x"), (3, 4)),
|
|
|
|
if_block: Node::new(
|
2024-08-30 22:06:58 +00:00
|
|
|
BlockExpression::Sync(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::identifier(
|
|
|
|
Identifier::new("y"),
|
|
|
|
(7, 8)
|
|
|
|
))
|
|
|
|
])),
|
2024-08-20 04:15:19 +00:00
|
|
|
(5, 10)
|
|
|
|
),
|
|
|
|
r#else: ElseExpression::If(Node::new(
|
|
|
|
Box::new(IfExpression::IfElse {
|
|
|
|
condition: Expression::identifier(Identifier::new("z"), (19, 20)),
|
|
|
|
if_block: Node::new(
|
2024-08-30 22:06:58 +00:00
|
|
|
BlockExpression::Sync(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::identifier(
|
|
|
|
Identifier::new("a"),
|
|
|
|
(23, 24)
|
|
|
|
))
|
|
|
|
])),
|
2024-08-20 04:15:19 +00:00
|
|
|
(21, 26)
|
|
|
|
),
|
|
|
|
r#else: ElseExpression::Block(Node::new(
|
2024-08-30 22:06:58 +00:00
|
|
|
BlockExpression::Sync(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::identifier(
|
|
|
|
Identifier::new("b"),
|
|
|
|
(34, 35)
|
|
|
|
))
|
|
|
|
])),
|
2024-08-20 04:15:19 +00:00
|
|
|
(32, 37)
|
|
|
|
)),
|
|
|
|
}),
|
|
|
|
(16, 37)
|
|
|
|
)),
|
|
|
|
},
|
2024-08-14 22:30:36 +00:00
|
|
|
(0, 37)
|
|
|
|
))
|
|
|
|
]))
|
|
|
|
)
|
2024-08-11 18:35:33 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 09:23:43 +00:00
|
|
|
#[test]
|
|
|
|
fn while_loop() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "while x < 10 { x += 1 }";
|
2024-08-10 09:23:43 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-20 04:15:19 +00:00
|
|
|
Statement::Expression(Expression::while_loop(
|
|
|
|
Expression::operator(
|
|
|
|
OperatorExpression::Comparison {
|
|
|
|
left: Expression::identifier(Identifier::new("x"), (6, 7)),
|
|
|
|
operator: Node::new(ComparisonOperator::LessThan, (8, 9)),
|
|
|
|
right: Expression::literal(10, (10, 12)),
|
|
|
|
},
|
|
|
|
(6, 12)
|
|
|
|
),
|
|
|
|
Node::new(
|
2024-08-30 22:06:58 +00:00
|
|
|
BlockExpression::Sync(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::operator(
|
2024-08-24 14:13:16 +00:00
|
|
|
OperatorExpression::CompoundAssignment {
|
|
|
|
assignee: Expression::identifier(
|
|
|
|
Identifier::new("x"),
|
|
|
|
(15, 16)
|
|
|
|
),
|
|
|
|
operator: Node::new(MathOperator::Add, (17, 19)),
|
|
|
|
modifier: Expression::literal(1, (20, 21)),
|
|
|
|
},
|
|
|
|
(15, 21)
|
2024-08-30 22:06:58 +00:00
|
|
|
))
|
|
|
|
])),
|
2024-08-20 04:15:19 +00:00
|
|
|
(13, 23)
|
2024-08-15 04:20:36 +00:00
|
|
|
),
|
2024-08-15 01:15:37 +00:00
|
|
|
(0, 23)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
)
|
2024-08-10 09:23:43 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:14:46 +00:00
|
|
|
#[test]
|
|
|
|
fn add_assign() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "a += 1";
|
2024-08-09 22:14:46 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::operator(
|
2024-08-15 04:20:36 +00:00
|
|
|
OperatorExpression::CompoundAssignment {
|
|
|
|
assignee: Expression::identifier(Identifier::new("a"), (0, 1)),
|
|
|
|
operator: Node::new(MathOperator::Add, (2, 4)),
|
2024-08-17 16:15:47 +00:00
|
|
|
modifier: Expression::literal(1, (5, 6)),
|
2024-08-14 22:30:36 +00:00
|
|
|
},
|
2024-08-15 04:20:36 +00:00
|
|
|
(0, 6)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
)
|
2024-08-09 22:14:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 18:01:01 +00:00
|
|
|
#[test]
|
|
|
|
fn or() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "true || false";
|
2024-08-09 18:01:01 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::operator(
|
|
|
|
OperatorExpression::Logic {
|
2024-08-17 16:15:47 +00:00
|
|
|
left: Expression::literal(true, (0, 4)),
|
2024-08-15 01:15:37 +00:00
|
|
|
operator: Node::new(LogicOperator::Or, (5, 7)),
|
2024-08-17 16:15:47 +00:00
|
|
|
right: Expression::literal(false, (8, 13)),
|
2024-08-14 22:30:36 +00:00
|
|
|
},
|
2024-08-15 01:15:37 +00:00
|
|
|
(0, 13)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
)
|
2024-08-09 15:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_with_one_statement() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "{ 40 + 2 }";
|
2024-08-09 15:41:23 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-20 04:15:19 +00:00
|
|
|
Statement::Expression(Expression::block(
|
2024-08-30 22:06:58 +00:00
|
|
|
BlockExpression::Sync(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::operator(
|
2024-08-24 14:13:16 +00:00
|
|
|
OperatorExpression::Math {
|
|
|
|
left: Expression::literal(40, (2, 4)),
|
|
|
|
operator: Node::new(MathOperator::Add, (5, 6)),
|
|
|
|
right: Expression::literal(2, (7, 8)),
|
|
|
|
},
|
|
|
|
(2, 8)
|
2024-08-30 22:06:58 +00:00
|
|
|
))
|
|
|
|
])),
|
2024-08-15 04:20:36 +00:00
|
|
|
(0, 10)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
)
|
2024-08-09 15:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_with_assignment() {
|
2024-08-23 09:24:48 +00:00
|
|
|
let source = "{ foo = 42; bar = 42; baz = \"42\" }";
|
2024-08-09 15:41:23 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-20 04:15:19 +00:00
|
|
|
Statement::Expression(Expression::block(
|
2024-08-30 22:06:58 +00:00
|
|
|
BlockExpression::Sync(AbstractSyntaxTree::with_statements([
|
2024-08-20 04:15:19 +00:00
|
|
|
Statement::ExpressionNullified(Node::new(
|
|
|
|
Expression::assignment(
|
|
|
|
Expression::identifier("foo", (2, 5)),
|
|
|
|
Expression::literal(42, (8, 10)),
|
|
|
|
(2, 10)
|
|
|
|
),
|
|
|
|
(2, 11)
|
|
|
|
)),
|
|
|
|
Statement::ExpressionNullified(Node::new(
|
|
|
|
Expression::assignment(
|
|
|
|
Expression::identifier("bar", (12, 15)),
|
|
|
|
Expression::literal(42, (18, 20)),
|
|
|
|
(12, 20)
|
|
|
|
),
|
|
|
|
(12, 21)
|
|
|
|
)),
|
|
|
|
Statement::Expression(Expression::assignment(
|
|
|
|
Expression::identifier("baz", (22, 25)),
|
|
|
|
Expression::literal("42", (28, 32)),
|
|
|
|
(22, 32)
|
|
|
|
))
|
2024-08-24 14:13:16 +00:00
|
|
|
])),
|
2024-08-16 01:22:24 +00:00
|
|
|
(0, 34)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
)
|
2024-08-09 15:41:23 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 11:15:09 +00:00
|
|
|
#[test]
|
|
|
|
fn equal() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "42 == 42";
|
2024-08-09 11:15:09 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::comparison(
|
|
|
|
Expression::literal(42, (0, 2)),
|
|
|
|
Node::new(ComparisonOperator::Equal, (3, 5)),
|
|
|
|
Expression::literal(42, (6, 8)),
|
|
|
|
(0, 8)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-09 10:46:24 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 07:00:48 +00:00
|
|
|
#[test]
|
|
|
|
fn less_than() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "1 < 2";
|
2024-08-09 07:00:48 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::comparison(
|
|
|
|
Expression::literal(1, (0, 1)),
|
|
|
|
Node::new(ComparisonOperator::LessThan, (2, 3)),
|
|
|
|
Expression::literal(2, (4, 5)),
|
|
|
|
(0, 5)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn less_than_or_equal() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "1 <= 2";
|
2024-08-09 07:00:48 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::comparison(
|
|
|
|
Expression::literal(1, (0, 1)),
|
|
|
|
Node::new(ComparisonOperator::LessThanOrEqual, (2, 4)),
|
|
|
|
Expression::literal(2, (5, 6)),
|
|
|
|
(0, 6)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn greater_than_or_equal() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "1 >= 2";
|
2024-08-09 07:00:48 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::comparison(
|
|
|
|
Expression::literal(1, (0, 1)),
|
|
|
|
Node::new(ComparisonOperator::GreaterThanOrEqual, (2, 4)),
|
|
|
|
Expression::literal(2, (5, 6)),
|
|
|
|
(0, 6)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn greater_than() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "1 > 2";
|
2024-08-09 07:00:48 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::comparison(
|
|
|
|
Expression::literal(1, (0, 1)),
|
|
|
|
Node::new(ComparisonOperator::GreaterThan, (2, 3)),
|
|
|
|
Expression::literal(2, (4, 5)),
|
|
|
|
(0, 5)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 17:11:32 +00:00
|
|
|
#[test]
|
2024-08-09 03:28:47 +00:00
|
|
|
fn subtract_negative_integers() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "-1 - -2";
|
2024-08-09 03:28:47 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::math(
|
|
|
|
Expression::literal(-1, (0, 2)),
|
|
|
|
MathOperator::subtract((3, 4)),
|
|
|
|
Expression::literal(-2, (5, 7)),
|
|
|
|
(0, 7)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn modulo() {
|
|
|
|
let source = "42 % 2";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::math(
|
|
|
|
Expression::literal(42, (0, 2)),
|
|
|
|
MathOperator::modulo((3, 4)),
|
|
|
|
Expression::literal(2, (5, 6)),
|
|
|
|
(0, 6)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn divide() {
|
|
|
|
let source = "42 / 2";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::math(
|
|
|
|
Expression::literal(42, (0, 2)),
|
|
|
|
MathOperator::divide((3, 4)),
|
|
|
|
Expression::literal(2, (5, 6)),
|
|
|
|
(0, 6)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-09 03:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-08-08 17:11:32 +00:00
|
|
|
fn string_concatenation() {
|
2024-08-23 09:24:48 +00:00
|
|
|
let source = "\"Hello, \" + \"World!\"";
|
2024-08-08 17:11:32 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::math(
|
|
|
|
Expression::literal("Hello, ", (0, 9)),
|
|
|
|
MathOperator::add((10, 11)),
|
|
|
|
Expression::literal("World!", (12, 20)),
|
2024-08-15 01:15:37 +00:00
|
|
|
(0, 20)
|
2024-08-14 22:30:36 +00:00
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-08 17:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "\"Hello, World!\"";
|
2024-08-08 17:11:32 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::literal("Hello, World!", (0, 15)))
|
2024-08-14 22:30:36 +00:00
|
|
|
]))
|
|
|
|
);
|
2024-08-08 17:11:32 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 14:50:19 +00:00
|
|
|
#[test]
|
|
|
|
fn boolean() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "true";
|
2024-08-07 14:50:19 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::literal(true, (0, 4)))
|
2024-08-14 22:30:36 +00:00
|
|
|
]))
|
|
|
|
);
|
2024-08-07 22:24:25 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 18:58:58 +00:00
|
|
|
#[test]
|
2024-08-12 20:57:10 +00:00
|
|
|
fn list_index() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "[1, 2, 3][0]";
|
2024-08-05 18:58:58 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::list_index(
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::list(
|
|
|
|
[
|
|
|
|
Expression::literal(1, (1, 2)),
|
|
|
|
Expression::literal(2, (4, 5)),
|
|
|
|
Expression::literal(3, (7, 8)),
|
|
|
|
],
|
|
|
|
(0, 9)
|
|
|
|
),
|
|
|
|
Expression::literal(0, (10, 11)),
|
2024-08-14 22:30:36 +00:00
|
|
|
(0, 12)
|
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-05 18:58:58 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 18:31:08 +00:00
|
|
|
#[test]
|
|
|
|
fn property_access() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "a.b";
|
2024-08-05 18:31:08 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::field_access(
|
2024-08-15 04:20:36 +00:00
|
|
|
Expression::identifier(Identifier::new("a"), (0, 1)),
|
|
|
|
Node::new(Identifier::new("b"), (2, 3)),
|
2024-08-14 22:30:36 +00:00
|
|
|
(0, 3)
|
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-05 18:31:08 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 01:39:57 +00:00
|
|
|
#[test]
|
|
|
|
fn complex_list() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "[1, 1 + 1, 2 + (4 * 10)]";
|
2024-08-05 01:39:57 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::list(
|
2024-08-17 16:15:47 +00:00
|
|
|
[
|
|
|
|
Expression::literal(1, (1, 2)),
|
|
|
|
Expression::math(
|
|
|
|
Expression::literal(1, (4, 5)),
|
|
|
|
MathOperator::add((6, 7)),
|
|
|
|
Expression::literal(1, (8, 9)),
|
2024-08-14 22:30:36 +00:00
|
|
|
(4, 9)
|
|
|
|
),
|
2024-08-17 16:15:47 +00:00
|
|
|
Expression::math(
|
|
|
|
Expression::literal(2, (11, 12)),
|
|
|
|
Node::new(MathOperator::Add, (13, 14)),
|
|
|
|
Expression::grouped(
|
|
|
|
Expression::math(
|
|
|
|
Expression::literal(4, (16, 17)),
|
|
|
|
MathOperator::multiply((18, 19)),
|
|
|
|
Expression::literal(10, (20, 22)),
|
|
|
|
(16, 22)
|
2024-08-14 22:30:36 +00:00
|
|
|
),
|
2024-08-17 16:15:47 +00:00
|
|
|
(15, 23)
|
|
|
|
),
|
2024-08-14 22:30:36 +00:00
|
|
|
(11, 23)
|
2024-08-17 16:15:47 +00:00
|
|
|
),
|
|
|
|
],
|
2024-08-14 22:30:36 +00:00
|
|
|
(0, 24)
|
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-05 01:39:57 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 01:31:18 +00:00
|
|
|
#[test]
|
|
|
|
fn list() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "[1, 2]";
|
2024-08-05 01:31:18 +00:00
|
|
|
|
2024-08-14 22:30:36 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
|
|
|
Statement::Expression(Expression::list(
|
2024-08-17 16:15:47 +00:00
|
|
|
[
|
|
|
|
Expression::literal(1, (1, 2)),
|
|
|
|
Expression::literal(2, (4, 5))
|
|
|
|
],
|
2024-08-14 22:30:36 +00:00
|
|
|
(0, 6)
|
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-05 01:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_list() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "[]";
|
2024-08-05 01:31:18 +00:00
|
|
|
|
2024-08-14 20:07:32 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::list(vec![], (0, 2)))
|
2024-08-14 20:07:32 +00:00
|
|
|
]))
|
|
|
|
);
|
2024-08-05 01:31:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 00:08:43 +00:00
|
|
|
#[test]
|
|
|
|
fn float() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "42.0";
|
2024-08-05 00:08:43 +00:00
|
|
|
|
2024-08-14 20:07:32 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::literal(42.0, (0, 4)))
|
2024-08-14 20:07:32 +00:00
|
|
|
]))
|
|
|
|
);
|
2024-08-05 00:08:43 +00:00
|
|
|
}
|
|
|
|
|
2024-08-04 00:23:52 +00:00
|
|
|
#[test]
|
|
|
|
fn add() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "1 + 2";
|
2024-08-04 00:23:52 +00:00
|
|
|
|
2024-08-14 20:07:32 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::math(
|
|
|
|
Expression::literal(1, (0, 1)),
|
|
|
|
MathOperator::add((2, 3)),
|
|
|
|
Expression::literal(2, (4, 5)),
|
2024-08-14 20:07:32 +00:00
|
|
|
(0, 5)
|
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiply() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "1 * 2";
|
2024-08-04 00:23:52 +00:00
|
|
|
|
2024-08-14 20:07:32 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::math(
|
|
|
|
Expression::literal(1, (0, 1)),
|
|
|
|
MathOperator::multiply((2, 3)),
|
|
|
|
Expression::literal(2, (4, 5)),
|
2024-08-14 20:07:32 +00:00
|
|
|
(0, 5)
|
2024-08-17 16:15:47 +00:00
|
|
|
),)
|
2024-08-14 20:07:32 +00:00
|
|
|
]))
|
|
|
|
);
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_and_multiply() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "1 + 2 * 3";
|
2024-08-04 00:23:52 +00:00
|
|
|
|
2024-08-14 20:07:32 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::math(
|
|
|
|
Expression::literal(1, (0, 1)),
|
|
|
|
MathOperator::add((2, 3)),
|
|
|
|
Expression::math(
|
|
|
|
Expression::literal(2, (4, 5)),
|
|
|
|
MathOperator::multiply((6, 7)),
|
|
|
|
Expression::literal(3, (8, 9)),
|
|
|
|
(4, 9)
|
|
|
|
),
|
2024-08-15 01:15:37 +00:00
|
|
|
(0, 9)
|
2024-08-17 16:15:47 +00:00
|
|
|
)),
|
2024-08-14 20:07:32 +00:00
|
|
|
]))
|
|
|
|
);
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn assignment() {
|
2024-08-14 19:52:04 +00:00
|
|
|
let source = "a = 1 + 2 * 3";
|
2024-08-04 00:23:52 +00:00
|
|
|
|
2024-08-14 20:07:32 +00:00
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(AbstractSyntaxTree::with_statements([
|
2024-08-17 16:15:47 +00:00
|
|
|
Statement::Expression(Expression::assignment(
|
|
|
|
Expression::identifier("a", (0, 1)),
|
|
|
|
Expression::math(
|
|
|
|
Expression::literal(1, (4, 5)),
|
|
|
|
MathOperator::add((6, 7)),
|
|
|
|
Expression::math(
|
|
|
|
Expression::literal(2, (8, 9)),
|
|
|
|
MathOperator::multiply((10, 11)),
|
|
|
|
Expression::literal(3, (12, 13)),
|
|
|
|
(8, 13)
|
|
|
|
),
|
|
|
|
(4, 13)
|
|
|
|
),
|
2024-08-14 20:07:32 +00:00
|
|
|
(0, 13)
|
|
|
|
))
|
|
|
|
]))
|
|
|
|
);
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
|
|
|
}
|