Continue passing tests
This commit is contained in:
parent
f4ee3ffcf8
commit
81b7888920
@ -285,7 +285,6 @@ pub enum LiteralExpression {
|
|||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
Float(f64),
|
Float(f64),
|
||||||
Integer(i64),
|
Integer(i64),
|
||||||
Range(i64, i64),
|
|
||||||
String(String),
|
String(String),
|
||||||
Value(Value),
|
Value(Value),
|
||||||
}
|
}
|
||||||
@ -296,7 +295,6 @@ impl Display for LiteralExpression {
|
|||||||
LiteralExpression::Boolean(boolean) => write!(f, "{}", boolean),
|
LiteralExpression::Boolean(boolean) => write!(f, "{}", boolean),
|
||||||
LiteralExpression::Float(float) => write!(f, "{}", float),
|
LiteralExpression::Float(float) => write!(f, "{}", float),
|
||||||
LiteralExpression::Integer(integer) => write!(f, "{}", integer),
|
LiteralExpression::Integer(integer) => write!(f, "{}", integer),
|
||||||
LiteralExpression::Range(start, end) => write!(f, "{}..{}", start, end),
|
|
||||||
LiteralExpression::String(string) => write!(f, "{}", string),
|
LiteralExpression::String(string) => write!(f, "{}", string),
|
||||||
LiteralExpression::Value(value) => write!(f, "{}", value),
|
LiteralExpression::Value(value) => write!(f, "{}", value),
|
||||||
}
|
}
|
||||||
@ -582,13 +580,10 @@ impl Display for Loop {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
||||||
pub enum StructExpression {
|
pub enum StructExpression {
|
||||||
|
// The tuple struct expression is omitted because it is redundant with call expression
|
||||||
Unit {
|
Unit {
|
||||||
name: Node<Identifier>,
|
name: Node<Identifier>,
|
||||||
},
|
},
|
||||||
Tuple {
|
|
||||||
name: Node<Identifier>,
|
|
||||||
items: Vec<Expression>,
|
|
||||||
},
|
|
||||||
Fields {
|
Fields {
|
||||||
name: Node<Identifier>,
|
name: Node<Identifier>,
|
||||||
fields: Vec<(Node<Identifier>, Expression)>,
|
fields: Vec<(Node<Identifier>, Expression)>,
|
||||||
@ -599,19 +594,6 @@ impl Display for StructExpression {
|
|||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
StructExpression::Unit { name } => write!(f, "{}", name),
|
StructExpression::Unit { name } => write!(f, "{}", name),
|
||||||
StructExpression::Tuple { name, items } => {
|
|
||||||
write!(f, "{}(", name)?;
|
|
||||||
|
|
||||||
for (index, item) in items.iter().enumerate() {
|
|
||||||
write!(f, "{}", item)?;
|
|
||||||
|
|
||||||
if index < items.len() - 1 {
|
|
||||||
write!(f, ", ")?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
write!(f, ")")
|
|
||||||
}
|
|
||||||
StructExpression::Fields { name, fields } => {
|
StructExpression::Fields { name, fields } => {
|
||||||
write!(f, "{} {{", name)?;
|
write!(f, "{} {{", name)?;
|
||||||
|
|
||||||
|
@ -237,7 +237,17 @@ impl<'src> Parser<'src> {
|
|||||||
|
|
||||||
let expression = self.parse_expression(0)?;
|
let expression = self.parse_expression(0)?;
|
||||||
|
|
||||||
Ok(Statement::Expression(expression))
|
let statement = if let Token::Semicolon = self.current_token {
|
||||||
|
let position = (start_position.0, self.current_position.1);
|
||||||
|
|
||||||
|
self.next_token()?;
|
||||||
|
|
||||||
|
Statement::ExpressionNullified(Node::new(expression, position))
|
||||||
|
} else {
|
||||||
|
Statement::Expression(expression)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(statement)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_token(&mut self) -> Result<(), ParseError> {
|
fn next_token(&mut self) -> Result<(), ParseError> {
|
||||||
@ -321,7 +331,7 @@ impl<'src> Parser<'src> {
|
|||||||
let block = self.parse_block()?;
|
let block = self.parse_block()?;
|
||||||
let position = (start_position.0, self.current_position.1);
|
let position = (start_position.0, self.current_position.1);
|
||||||
|
|
||||||
return Ok(Expression::block(block.inner, position));
|
Ok(Expression::block(block.inner, position))
|
||||||
}
|
}
|
||||||
Token::Boolean(text) => {
|
Token::Boolean(text) => {
|
||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
@ -330,13 +340,10 @@ impl<'src> Parser<'src> {
|
|||||||
error,
|
error,
|
||||||
position: start_position,
|
position: start_position,
|
||||||
})?;
|
})?;
|
||||||
let right_end = self.current_position.1;
|
let statement =
|
||||||
let statement = Expression::literal(
|
Expression::literal(LiteralExpression::Boolean(boolean), start_position);
|
||||||
LiteralExpression::Boolean(boolean),
|
|
||||||
(start_position.0, right_end),
|
|
||||||
);
|
|
||||||
|
|
||||||
return Ok(statement);
|
Ok(statement)
|
||||||
}
|
}
|
||||||
Token::Float(text) => {
|
Token::Float(text) => {
|
||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
@ -345,20 +352,18 @@ impl<'src> Parser<'src> {
|
|||||||
error,
|
error,
|
||||||
position: start_position,
|
position: start_position,
|
||||||
})?;
|
})?;
|
||||||
let position = (start_position.0, self.current_position.1);
|
|
||||||
|
|
||||||
Ok(Expression::literal(
|
Ok(Expression::literal(
|
||||||
LiteralExpression::Float(float),
|
LiteralExpression::Float(float),
|
||||||
position,
|
start_position,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
Token::Identifier(text) => {
|
Token::Identifier(text) => {
|
||||||
let identifier = Identifier::new(text);
|
|
||||||
let identifier_position = self.current_position;
|
|
||||||
|
|
||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
|
|
||||||
Ok(Expression::identifier(identifier, identifier_position))
|
let identifier = Identifier::new(text);
|
||||||
|
|
||||||
|
Ok(Expression::identifier(identifier, start_position))
|
||||||
}
|
}
|
||||||
Token::Integer(text) => {
|
Token::Integer(text) => {
|
||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
@ -368,42 +373,16 @@ impl<'src> Parser<'src> {
|
|||||||
position: start_position,
|
position: start_position,
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
if let Token::DoubleDot = self.current_token {
|
Ok(Expression::literal(
|
||||||
self.next_token()?;
|
LiteralExpression::Integer(integer),
|
||||||
|
start_position,
|
||||||
if let Token::Integer(range_end) = self.current_token {
|
))
|
||||||
let end_position = self.current_position;
|
|
||||||
|
|
||||||
self.next_token()?;
|
|
||||||
|
|
||||||
let range_end =
|
|
||||||
range_end
|
|
||||||
.parse::<i64>()
|
|
||||||
.map_err(|error| ParseError::Integer {
|
|
||||||
error,
|
|
||||||
position: end_position,
|
|
||||||
})?;
|
|
||||||
|
|
||||||
Ok(Expression::literal(
|
|
||||||
LiteralExpression::Range(integer, range_end),
|
|
||||||
(start_position.0, end_position.1),
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
Err(ParseError::ExpectedToken {
|
|
||||||
expected: TokenKind::Integer,
|
|
||||||
actual: self.current_token.to_owned(),
|
|
||||||
position: (start_position.0, self.current_position.1),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Ok(Expression::literal(
|
|
||||||
LiteralExpression::Integer(integer),
|
|
||||||
start_position,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Token::If => {
|
Token::If => {
|
||||||
let start = self.current_position.0;
|
let start = self.current_position.0;
|
||||||
|
|
||||||
|
self.next_token()?;
|
||||||
|
|
||||||
let r#if = self.parse_if()?;
|
let r#if = self.parse_if()?;
|
||||||
let position = (start, self.current_position.1);
|
let position = (start, self.current_position.1);
|
||||||
|
|
||||||
@ -591,17 +570,87 @@ impl<'src> Parser<'src> {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let math_operator = match &self.current_token {
|
if let Token::DoubleDot = &self.current_token {
|
||||||
Token::Minus => Node::new(MathOperator::Subtract, self.current_position),
|
self.next_token()?;
|
||||||
Token::Plus => Node::new(MathOperator::Add, self.current_position),
|
|
||||||
Token::Star => Node::new(MathOperator::Multiply, self.current_position),
|
let end = self.parse_expression(operator_precedence)?;
|
||||||
Token::Slash => Node::new(MathOperator::Divide, self.current_position),
|
let position = (left_start, end.position().1);
|
||||||
Token::Percent => Node::new(MathOperator::Modulo, self.current_position),
|
|
||||||
|
return Ok(Expression::range(Range { start: left, end }, position));
|
||||||
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
_ => {
|
_ => {
|
||||||
return Err(ParseError::UnexpectedToken {
|
return Err(ParseError::UnexpectedToken {
|
||||||
actual: self.current_token.to_owned(),
|
actual: self.current_token.to_owned(),
|
||||||
position: self.current_position,
|
position: self.current_position,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -611,9 +660,9 @@ impl<'src> Parser<'src> {
|
|||||||
let position = (left_start, right.position().1);
|
let position = (left_start, right.position().1);
|
||||||
|
|
||||||
Ok(Expression::operator(
|
Ok(Expression::operator(
|
||||||
OperatorExpression::Math {
|
OperatorExpression::Logic {
|
||||||
left,
|
left,
|
||||||
operator: math_operator,
|
operator: logic_operator,
|
||||||
right,
|
right,
|
||||||
},
|
},
|
||||||
position,
|
position,
|
||||||
@ -623,7 +672,7 @@ impl<'src> Parser<'src> {
|
|||||||
fn parse_postfix(&mut self, left: Expression) -> Result<Expression, ParseError> {
|
fn parse_postfix(&mut self, left: Expression) -> Result<Expression, ParseError> {
|
||||||
log::trace!("Parsing {} as postfix operator", self.current_token);
|
log::trace!("Parsing {} as postfix operator", self.current_token);
|
||||||
|
|
||||||
let statement = match &self.current_token {
|
let expression = match &self.current_token {
|
||||||
Token::LeftCurlyBrace => {
|
Token::LeftCurlyBrace => {
|
||||||
let identifier = if let Some(identifier) = left.as_identifier() {
|
let identifier = if let Some(identifier) = left.as_identifier() {
|
||||||
identifier
|
identifier
|
||||||
@ -640,14 +689,9 @@ impl<'src> Parser<'src> {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Token::RightCurlyBrace = self.current_token {
|
if let Token::RightCurlyBrace = self.current_token {
|
||||||
let position = (start_left, self.current_position.1);
|
|
||||||
|
|
||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
|
|
||||||
return Ok(Expression::r#struct(
|
break;
|
||||||
StructExpression::Fields { name, fields },
|
|
||||||
position,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let field_name = self.parse_identifier()?;
|
let field_name = self.parse_identifier()?;
|
||||||
@ -670,6 +714,10 @@ impl<'src> Parser<'src> {
|
|||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let position = (start_left, self.current_position.1);
|
||||||
|
|
||||||
|
Expression::r#struct(StructExpression::Fields { name, fields }, position)
|
||||||
}
|
}
|
||||||
Token::LeftParenthesis => {
|
Token::LeftParenthesis => {
|
||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
@ -734,22 +782,14 @@ impl<'src> Parser<'src> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if self.current_token.is_postfix() {
|
if self.current_token.is_postfix() {
|
||||||
self.parse_postfix(statement)
|
self.parse_postfix(expression)
|
||||||
} else {
|
} else {
|
||||||
Ok(statement)
|
Ok(expression)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_if(&mut self) -> Result<If, ParseError> {
|
fn parse_if(&mut self) -> Result<If, ParseError> {
|
||||||
if let Token::If = self.current_token {
|
// Assume that the "if" token has already been consumed
|
||||||
self.next_token()?;
|
|
||||||
} else {
|
|
||||||
return Err(ParseError::ExpectedToken {
|
|
||||||
expected: TokenKind::If,
|
|
||||||
actual: self.current_token.to_owned(),
|
|
||||||
position: self.current_position,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let condition = self.parse_expression(0)?;
|
let condition = self.parse_expression(0)?;
|
||||||
let if_block = self.parse_block()?;
|
let if_block = self.parse_block()?;
|
||||||
@ -757,7 +797,11 @@ impl<'src> Parser<'src> {
|
|||||||
if let Token::Else = self.current_token {
|
if let Token::Else = self.current_token {
|
||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
|
|
||||||
if let Ok(else_if) = self.parse_if() {
|
if let Token::If = self.current_token {
|
||||||
|
self.next_token()?;
|
||||||
|
|
||||||
|
let else_if = self.parse_if()?;
|
||||||
|
|
||||||
Ok(If::IfElse {
|
Ok(If::IfElse {
|
||||||
condition,
|
condition,
|
||||||
if_block,
|
if_block,
|
||||||
@ -782,9 +826,11 @@ impl<'src> Parser<'src> {
|
|||||||
|
|
||||||
fn parse_identifier(&mut self) -> Result<Node<Identifier>, ParseError> {
|
fn parse_identifier(&mut self) -> Result<Node<Identifier>, ParseError> {
|
||||||
if let Token::Identifier(text) = self.current_token {
|
if let Token::Identifier(text) = self.current_token {
|
||||||
|
let position = self.current_position;
|
||||||
|
|
||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
|
|
||||||
Ok(Node::new(Identifier::new(text), self.current_position))
|
Ok(Node::new(Identifier::new(text), position))
|
||||||
} else {
|
} else {
|
||||||
Err(ParseError::ExpectedToken {
|
Err(ParseError::ExpectedToken {
|
||||||
expected: TokenKind::Identifier,
|
expected: TokenKind::Identifier,
|
||||||
@ -975,7 +1021,6 @@ impl Display for ParseError {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use fmt::Write;
|
|
||||||
|
|
||||||
use crate::{Identifier, Type};
|
use crate::{Identifier, Type};
|
||||||
|
|
||||||
@ -1021,17 +1066,17 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn tuple_struct_access() {
|
fn tuple_struct_access() {
|
||||||
let source = "(Foo(42, 'bar')).0";
|
let source = "Foo(42, 'bar').0";
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(source),
|
parse(source),
|
||||||
Ok(AbstractSyntaxTree::with_statements([
|
Ok(AbstractSyntaxTree::with_statements([
|
||||||
Statement::Expression(Expression::field_access(
|
Statement::Expression(Expression::field_access(
|
||||||
FieldAccess {
|
FieldAccess {
|
||||||
container: Expression::r#struct(
|
container: Expression::call(
|
||||||
StructExpression::Tuple {
|
CallExpression {
|
||||||
name: Node::new(Identifier::new("Foo"), (0, 0)),
|
function: Expression::identifier(Identifier::new("Foo"), (0, 0)),
|
||||||
items: vec![
|
arguments: vec![
|
||||||
Expression::literal(LiteralExpression::Integer(42), (0, 0)),
|
Expression::literal(LiteralExpression::Integer(42), (0, 0)),
|
||||||
Expression::literal(
|
Expression::literal(
|
||||||
LiteralExpression::String("bar".to_string()),
|
LiteralExpression::String("bar".to_string()),
|
||||||
@ -1112,23 +1157,23 @@ mod tests {
|
|||||||
Ok(AbstractSyntaxTree::with_statements([
|
Ok(AbstractSyntaxTree::with_statements([
|
||||||
Statement::struct_definition(
|
Statement::struct_definition(
|
||||||
StructDefinition::Tuple {
|
StructDefinition::Tuple {
|
||||||
name: Node::new(Identifier::new("Foo"), (0, 0)),
|
name: Node::new(Identifier::new("Foo"), (7, 10)),
|
||||||
items: vec![
|
items: vec![
|
||||||
Node::new(Type::Integer, (0, 0)),
|
Node::new(Type::Integer, (11, 14)),
|
||||||
Node::new(Type::Float, (0, 0)),
|
Node::new(Type::Float, (16, 21)),
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
(0, 0)
|
(0, 22)
|
||||||
),
|
),
|
||||||
Statement::Expression(Expression::r#struct(
|
Statement::Expression(Expression::call(
|
||||||
StructExpression::Tuple {
|
CallExpression {
|
||||||
name: Node::new(Identifier::new("Foo"), (0, 0)),
|
function: Expression::identifier(Identifier::new("Foo"), (23, 26)),
|
||||||
items: vec![
|
arguments: vec![
|
||||||
Expression::literal(LiteralExpression::Integer(1), (0, 0)),
|
Expression::literal(LiteralExpression::Integer(1), (27, 28)),
|
||||||
Expression::literal(LiteralExpression::Float(2.0), (0, 0))
|
Expression::literal(LiteralExpression::Float(2.0), (30, 33))
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
(0, 0)
|
(23, 34)
|
||||||
))
|
))
|
||||||
]))
|
]))
|
||||||
);
|
);
|
||||||
@ -1143,13 +1188,13 @@ mod tests {
|
|||||||
Ok(AbstractSyntaxTree::with_statements([
|
Ok(AbstractSyntaxTree::with_statements([
|
||||||
Statement::StructDefinition(Node::new(
|
Statement::StructDefinition(Node::new(
|
||||||
StructDefinition::Tuple {
|
StructDefinition::Tuple {
|
||||||
name: Node::new(Identifier::new("Foo"), (0, 0)),
|
name: Node::new(Identifier::new("Foo"), (7, 10)),
|
||||||
items: vec![
|
items: vec![
|
||||||
Node::new(Type::Integer, (0, 0)),
|
Node::new(Type::Integer, (11, 14)),
|
||||||
Node::new(Type::Float, (0, 0)),
|
Node::new(Type::Float, (16, 21)),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
(0, 0)
|
(0, 22)
|
||||||
))
|
))
|
||||||
]))
|
]))
|
||||||
);
|
);
|
||||||
@ -1164,9 +1209,9 @@ mod tests {
|
|||||||
Ok(AbstractSyntaxTree::with_statements([
|
Ok(AbstractSyntaxTree::with_statements([
|
||||||
Statement::StructDefinition(Node::new(
|
Statement::StructDefinition(Node::new(
|
||||||
StructDefinition::Unit {
|
StructDefinition::Unit {
|
||||||
name: Node::new(Identifier::new("Foo"), (0, 0)),
|
name: Node::new(Identifier::new("Foo"), (7, 10)),
|
||||||
},
|
},
|
||||||
(0, 0)
|
(0, 10)
|
||||||
))
|
))
|
||||||
]))
|
]))
|
||||||
);
|
);
|
||||||
@ -1209,13 +1254,6 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn map_property_nested() {
|
|
||||||
let source = "{ x = { y = 42 } }.x.y";
|
|
||||||
|
|
||||||
assert_eq!(parse(source), todo!());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn range() {
|
fn range() {
|
||||||
let source = "0..42";
|
let source = "0..42";
|
||||||
@ -1225,10 +1263,10 @@ mod tests {
|
|||||||
Ok(AbstractSyntaxTree::with_statements([
|
Ok(AbstractSyntaxTree::with_statements([
|
||||||
Statement::Expression(Expression::range(
|
Statement::Expression(Expression::range(
|
||||||
Range {
|
Range {
|
||||||
start: Expression::literal(LiteralExpression::Integer(0), (0, 0)),
|
start: Expression::literal(LiteralExpression::Integer(0), (0, 1)),
|
||||||
end: Expression::literal(LiteralExpression::Integer(42), (0, 0)),
|
end: Expression::literal(LiteralExpression::Integer(42), (3, 5)),
|
||||||
},
|
},
|
||||||
(0, 0)
|
(0, 5)
|
||||||
))
|
))
|
||||||
]))
|
]))
|
||||||
);
|
);
|
||||||
@ -1241,19 +1279,22 @@ mod tests {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(source),
|
parse(source),
|
||||||
Ok(AbstractSyntaxTree::with_statements([
|
Ok(AbstractSyntaxTree::with_statements([
|
||||||
Statement::Expression(Expression::operator(
|
Statement::ExpressionNullified(Node::new(
|
||||||
OperatorExpression::Assignment {
|
Expression::operator(
|
||||||
assignee: Expression::identifier(Identifier::new("a"), (0, 0)),
|
OperatorExpression::Assignment {
|
||||||
value: Expression::literal(LiteralExpression::Integer(1), (0, 0)),
|
assignee: Expression::identifier(Identifier::new("a"), (0, 1)),
|
||||||
},
|
value: Expression::literal(LiteralExpression::Integer(1), (4, 5)),
|
||||||
(0, 0)
|
},
|
||||||
|
(0, 5)
|
||||||
|
),
|
||||||
|
(0, 6)
|
||||||
)),
|
)),
|
||||||
Statement::Expression(Expression::operator(
|
Statement::Expression(Expression::operator(
|
||||||
OperatorExpression::Negation(Expression::identifier(
|
OperatorExpression::Negation(Expression::identifier(
|
||||||
Identifier::new("a"),
|
Identifier::new("a"),
|
||||||
(0, 0)
|
(8, 9)
|
||||||
)),
|
)),
|
||||||
(0, 0)
|
(7, 9)
|
||||||
))
|
))
|
||||||
]))
|
]))
|
||||||
);
|
);
|
||||||
@ -1270,15 +1311,15 @@ mod tests {
|
|||||||
OperatorExpression::Negation(Expression::grouped(
|
OperatorExpression::Negation(Expression::grouped(
|
||||||
Expression::operator(
|
Expression::operator(
|
||||||
OperatorExpression::Math {
|
OperatorExpression::Math {
|
||||||
left: Expression::literal(LiteralExpression::Integer(1), (0, 0)),
|
left: Expression::literal(LiteralExpression::Integer(1), (2, 3)),
|
||||||
operator: Node::new(MathOperator::Add, (0, 0)),
|
operator: Node::new(MathOperator::Add, (4, 5)),
|
||||||
right: Expression::literal(LiteralExpression::Integer(1), (0, 0)),
|
right: Expression::literal(LiteralExpression::Integer(1), (6, 7)),
|
||||||
},
|
},
|
||||||
(0, 0)
|
(2, 7)
|
||||||
),
|
),
|
||||||
(0, 0)
|
(1, 8)
|
||||||
)),
|
)),
|
||||||
(0, 0)
|
(0, 8)
|
||||||
))
|
))
|
||||||
]))
|
]))
|
||||||
);
|
);
|
||||||
@ -1295,15 +1336,15 @@ mod tests {
|
|||||||
OperatorExpression::Not(Expression::grouped(
|
OperatorExpression::Not(Expression::grouped(
|
||||||
Expression::operator(
|
Expression::operator(
|
||||||
OperatorExpression::Comparison {
|
OperatorExpression::Comparison {
|
||||||
left: Expression::literal(LiteralExpression::Integer(1), (0, 0)),
|
left: Expression::literal(LiteralExpression::Integer(1), (2, 3)),
|
||||||
operator: Node::new(ComparisonOperator::GreaterThan, (0, 0)),
|
operator: Node::new(ComparisonOperator::GreaterThan, (4, 5)),
|
||||||
right: Expression::literal(LiteralExpression::Integer(42), (0, 0)),
|
right: Expression::literal(LiteralExpression::Integer(42), (6, 8)),
|
||||||
},
|
},
|
||||||
(0, 0)
|
(2, 8)
|
||||||
),
|
),
|
||||||
(0, 0)
|
(1, 9)
|
||||||
)),
|
)),
|
||||||
(0, 0)
|
(0, 9)
|
||||||
))
|
))
|
||||||
]))
|
]))
|
||||||
);
|
);
|
||||||
@ -1316,16 +1357,19 @@ mod tests {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(source),
|
parse(source),
|
||||||
Ok(AbstractSyntaxTree::with_statements([
|
Ok(AbstractSyntaxTree::with_statements([
|
||||||
Statement::Expression(Expression::operator(
|
Statement::ExpressionNullified(Node::new(
|
||||||
OperatorExpression::Assignment {
|
Expression::operator(
|
||||||
assignee: Expression::identifier(Identifier::new("a"), (0, 0)),
|
OperatorExpression::Assignment {
|
||||||
value: Expression::literal(LiteralExpression::Boolean(false), (0, 0)),
|
assignee: Expression::identifier(Identifier::new("a"), (0, 1)),
|
||||||
},
|
value: Expression::literal(LiteralExpression::Boolean(false), (4, 9)),
|
||||||
(0, 0)
|
},
|
||||||
|
(0, 9)
|
||||||
|
),
|
||||||
|
(0, 10)
|
||||||
)),
|
)),
|
||||||
Statement::Expression(Expression::operator(
|
Statement::Expression(Expression::operator(
|
||||||
OperatorExpression::Not(Expression::identifier(Identifier::new("a"), (0, 0))),
|
OperatorExpression::Not(Expression::identifier(Identifier::new("a"), (12, 13))),
|
||||||
(0, 0)
|
(11, 13)
|
||||||
)),
|
)),
|
||||||
]))
|
]))
|
||||||
);
|
);
|
||||||
@ -1427,13 +1471,6 @@ mod tests {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn malformed_map() {
|
|
||||||
let source = "{ x = 1, y = 2, z = 3; }";
|
|
||||||
|
|
||||||
assert_eq!(parse(source), todo!())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn while_loop() {
|
fn while_loop() {
|
||||||
let source = "while x < 10 { x += 1 }";
|
let source = "while x < 10 { x += 1 }";
|
||||||
@ -1445,28 +1482,34 @@ mod tests {
|
|||||||
Loop::While {
|
Loop::While {
|
||||||
condition: Expression::operator(
|
condition: Expression::operator(
|
||||||
OperatorExpression::Comparison {
|
OperatorExpression::Comparison {
|
||||||
left: Expression::identifier(Identifier::new("x"), (0, 0)),
|
left: Expression::identifier(Identifier::new("x"), (6, 7)),
|
||||||
operator: Node::new(ComparisonOperator::LessThan, (0, 0)),
|
operator: Node::new(ComparisonOperator::LessThan, (8, 9)),
|
||||||
right: Expression::literal(LiteralExpression::Integer(10), (0, 0)),
|
right: Expression::literal(
|
||||||
|
LiteralExpression::Integer(10),
|
||||||
|
(10, 12)
|
||||||
|
),
|
||||||
},
|
},
|
||||||
(0, 0)
|
(6, 12)
|
||||||
),
|
),
|
||||||
block: Node::new(
|
block: Node::new(
|
||||||
Block::Sync(vec![Statement::Expression(Expression::operator(
|
Block::Sync(vec![Statement::Expression(Expression::operator(
|
||||||
OperatorExpression::CompoundAssignment {
|
OperatorExpression::CompoundAssignment {
|
||||||
assignee: Expression::identifier(Identifier::new("x"), (0, 0)),
|
assignee: Expression::identifier(
|
||||||
operator: Node::new(MathOperator::Add, (0, 0)),
|
Identifier::new("x"),
|
||||||
|
(15, 16)
|
||||||
|
),
|
||||||
|
operator: Node::new(MathOperator::Add, (17, 19)),
|
||||||
value: Expression::literal(
|
value: Expression::literal(
|
||||||
LiteralExpression::Integer(1),
|
LiteralExpression::Integer(1),
|
||||||
(0, 0)
|
(20, 21)
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
(0, 0)
|
(15, 21)
|
||||||
))]),
|
))]),
|
||||||
(0, 0)
|
(13, 23)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
(0, 0)
|
(0, 23)
|
||||||
))
|
))
|
||||||
]))
|
]))
|
||||||
)
|
)
|
||||||
@ -1506,11 +1549,11 @@ mod tests {
|
|||||||
Ok(AbstractSyntaxTree::with_statements([
|
Ok(AbstractSyntaxTree::with_statements([
|
||||||
Statement::Expression(Expression::operator(
|
Statement::Expression(Expression::operator(
|
||||||
OperatorExpression::Logic {
|
OperatorExpression::Logic {
|
||||||
left: Expression::literal(LiteralExpression::Boolean(true), (0, 0)),
|
left: Expression::literal(LiteralExpression::Boolean(true), (0, 4)),
|
||||||
operator: Node::new(LogicOperator::Or, (0, 0)),
|
operator: Node::new(LogicOperator::Or, (5, 7)),
|
||||||
right: Expression::literal(LiteralExpression::Boolean(false), (0, 0)),
|
right: Expression::literal(LiteralExpression::Boolean(false), (8, 13)),
|
||||||
},
|
},
|
||||||
(0, 0)
|
(0, 13)
|
||||||
))
|
))
|
||||||
]))
|
]))
|
||||||
)
|
)
|
||||||
@ -1562,7 +1605,7 @@ mod tests {
|
|||||||
(0, 0)
|
(0, 0)
|
||||||
),
|
),
|
||||||
(0, 0)
|
(0, 0)
|
||||||
)),
|
),),
|
||||||
Statement::ExpressionNullified(Node::new(
|
Statement::ExpressionNullified(Node::new(
|
||||||
Expression::operator(
|
Expression::operator(
|
||||||
OperatorExpression::Assignment {
|
OperatorExpression::Assignment {
|
||||||
@ -1578,7 +1621,7 @@ mod tests {
|
|||||||
(0, 0)
|
(0, 0)
|
||||||
),
|
),
|
||||||
(0, 0)
|
(0, 0)
|
||||||
)),
|
),),
|
||||||
Statement::Expression(Expression::operator(
|
Statement::Expression(Expression::operator(
|
||||||
OperatorExpression::Assignment {
|
OperatorExpression::Assignment {
|
||||||
assignee: Expression::identifier(Identifier::new("baz"), (0, 0)),
|
assignee: Expression::identifier(Identifier::new("baz"), (0, 0)),
|
||||||
@ -1596,34 +1639,6 @@ mod tests {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn empty_map() {
|
|
||||||
let source = "{}";
|
|
||||||
|
|
||||||
assert_eq!(parse(source), todo!());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn map_with_trailing_comma() {
|
|
||||||
let source = "{ foo = 42, bar = 42, baz = '42', }";
|
|
||||||
|
|
||||||
assert_eq!(parse(source), todo!());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn map_with_two_fields() {
|
|
||||||
let source = "{ x = 42, y = 'foobar' }";
|
|
||||||
|
|
||||||
assert_eq!(parse(source), todo!());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn map_with_one_field() {
|
|
||||||
let source = "{ x = 42 }";
|
|
||||||
|
|
||||||
assert_eq!(parse(source), todo!());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn equal() {
|
fn equal() {
|
||||||
let source = "42 == 42";
|
let source = "42 == 42";
|
||||||
@ -1729,7 +1744,7 @@ mod tests {
|
|||||||
Statement::Expression(Expression::operator(
|
Statement::Expression(Expression::operator(
|
||||||
OperatorExpression::Math {
|
OperatorExpression::Math {
|
||||||
left: Expression::literal(LiteralExpression::Integer(-1), (0, 2)),
|
left: Expression::literal(LiteralExpression::Integer(-1), (0, 2)),
|
||||||
operator: Node::new(MathOperator::Subtract, (2, 3)),
|
operator: Node::new(MathOperator::Subtract, (3, 4)),
|
||||||
right: Expression::literal(LiteralExpression::Integer(-2), (5, 7)),
|
right: Expression::literal(LiteralExpression::Integer(-2), (5, 7)),
|
||||||
},
|
},
|
||||||
(0, 7),
|
(0, 7),
|
||||||
@ -1778,7 +1793,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn string_concatenation() {
|
fn string_concatenation() {
|
||||||
let source = "\"Hello, \" + \"World!\"";
|
let source = "'Hello, ' + 'World!'";
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse(source),
|
parse(source),
|
||||||
@ -1789,13 +1804,13 @@ mod tests {
|
|||||||
LiteralExpression::String("Hello, ".to_string()),
|
LiteralExpression::String("Hello, ".to_string()),
|
||||||
(0, 9)
|
(0, 9)
|
||||||
),
|
),
|
||||||
operator: Node::new(MathOperator::Add, (9, 10)),
|
operator: Node::new(MathOperator::Add, (10, 11)),
|
||||||
right: Expression::literal(
|
right: Expression::literal(
|
||||||
LiteralExpression::String("World!".to_string()),
|
LiteralExpression::String("World!".to_string()),
|
||||||
(10, 18)
|
(12, 20)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
(0, 18)
|
(0, 20)
|
||||||
))
|
))
|
||||||
]))
|
]))
|
||||||
);
|
);
|
||||||
@ -2023,7 +2038,7 @@ mod tests {
|
|||||||
(4, 9)
|
(4, 9)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
(0, 5)
|
(0, 9)
|
||||||
))
|
))
|
||||||
]))
|
]))
|
||||||
);
|
);
|
||||||
|
@ -35,6 +35,7 @@ pub enum Token<'src> {
|
|||||||
WriteLine,
|
WriteLine,
|
||||||
|
|
||||||
// Symbols
|
// Symbols
|
||||||
|
BangEqual,
|
||||||
Bang,
|
Bang,
|
||||||
Colon,
|
Colon,
|
||||||
Comma,
|
Comma,
|
||||||
@ -68,6 +69,7 @@ impl<'src> Token<'src> {
|
|||||||
pub fn to_owned(&self) -> TokenOwned {
|
pub fn to_owned(&self) -> TokenOwned {
|
||||||
match self {
|
match self {
|
||||||
Token::Async => TokenOwned::Async,
|
Token::Async => TokenOwned::Async,
|
||||||
|
Token::BangEqual => TokenOwned::BangEqual,
|
||||||
Token::Bang => TokenOwned::Bang,
|
Token::Bang => TokenOwned::Bang,
|
||||||
Token::Bool => TokenOwned::Bool,
|
Token::Bool => TokenOwned::Bool,
|
||||||
Token::Boolean(boolean) => TokenOwned::Boolean(boolean.to_string()),
|
Token::Boolean(boolean) => TokenOwned::Boolean(boolean.to_string()),
|
||||||
@ -128,6 +130,7 @@ impl<'src> Token<'src> {
|
|||||||
Token::String(text) => text,
|
Token::String(text) => text,
|
||||||
|
|
||||||
Token::Async => "async",
|
Token::Async => "async",
|
||||||
|
Token::BangEqual => "!=",
|
||||||
Token::Bang => "!",
|
Token::Bang => "!",
|
||||||
Token::Bool => "bool",
|
Token::Bool => "bool",
|
||||||
Token::Colon => ":",
|
Token::Colon => ":",
|
||||||
@ -177,6 +180,7 @@ impl<'src> Token<'src> {
|
|||||||
pub fn kind(&self) -> TokenKind {
|
pub fn kind(&self) -> TokenKind {
|
||||||
match self {
|
match self {
|
||||||
Token::Async => TokenKind::Async,
|
Token::Async => TokenKind::Async,
|
||||||
|
Token::BangEqual => TokenKind::BangEqual,
|
||||||
Token::Bang => TokenKind::Bang,
|
Token::Bang => TokenKind::Bang,
|
||||||
Token::Bool => TokenKind::Bool,
|
Token::Bool => TokenKind::Bool,
|
||||||
Token::Boolean(_) => TokenKind::Boolean,
|
Token::Boolean(_) => TokenKind::Boolean,
|
||||||
@ -246,7 +250,7 @@ impl<'src> Token<'src> {
|
|||||||
Token::DoubleAmpersand => 4,
|
Token::DoubleAmpersand => 4,
|
||||||
Token::DoublePipe => 3,
|
Token::DoublePipe => 3,
|
||||||
Token::Equal | Token::MinusEqual | Token::PlusEqual => 2,
|
Token::Equal | Token::MinusEqual | Token::PlusEqual => 2,
|
||||||
Token::DoubleDot | Token::Semicolon => 1,
|
Token::DoubleDot => 1,
|
||||||
_ => 0,
|
_ => 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -273,10 +277,7 @@ impl<'src> Token<'src> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_postfix(&self) -> bool {
|
pub fn is_postfix(&self) -> bool {
|
||||||
matches!(
|
matches!(self, Token::LeftParenthesis | Token::LeftSquareBrace)
|
||||||
self,
|
|
||||||
Token::LeftParenthesis | Token::LeftSquareBrace | Token::Semicolon
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,6 +321,7 @@ pub enum TokenOwned {
|
|||||||
// Symbols
|
// Symbols
|
||||||
Async,
|
Async,
|
||||||
Bang,
|
Bang,
|
||||||
|
BangEqual,
|
||||||
Colon,
|
Colon,
|
||||||
Comma,
|
Comma,
|
||||||
Dot,
|
Dot,
|
||||||
@ -354,6 +356,7 @@ impl Display for TokenOwned {
|
|||||||
match self {
|
match self {
|
||||||
TokenOwned::Async => Token::Async.fmt(f),
|
TokenOwned::Async => Token::Async.fmt(f),
|
||||||
TokenOwned::Bang => Token::Bang.fmt(f),
|
TokenOwned::Bang => Token::Bang.fmt(f),
|
||||||
|
TokenOwned::BangEqual => Token::BangEqual.fmt(f),
|
||||||
TokenOwned::Bool => write!(f, "bool"),
|
TokenOwned::Bool => write!(f, "bool"),
|
||||||
TokenOwned::Boolean(boolean) => write!(f, "{boolean}"),
|
TokenOwned::Boolean(boolean) => write!(f, "{boolean}"),
|
||||||
TokenOwned::Colon => Token::Colon.fmt(f),
|
TokenOwned::Colon => Token::Colon.fmt(f),
|
||||||
@ -435,6 +438,7 @@ pub enum TokenKind {
|
|||||||
WriteLine,
|
WriteLine,
|
||||||
|
|
||||||
// Symbols
|
// Symbols
|
||||||
|
BangEqual,
|
||||||
Bang,
|
Bang,
|
||||||
Colon,
|
Colon,
|
||||||
Comma,
|
Comma,
|
||||||
@ -471,6 +475,7 @@ impl Display for TokenKind {
|
|||||||
match self {
|
match self {
|
||||||
TokenKind::Async => Token::Async.fmt(f),
|
TokenKind::Async => Token::Async.fmt(f),
|
||||||
TokenKind::Bang => Token::Bang.fmt(f),
|
TokenKind::Bang => Token::Bang.fmt(f),
|
||||||
|
TokenKind::BangEqual => Token::BangEqual.fmt(f),
|
||||||
TokenKind::Bool => Token::Bool.fmt(f),
|
TokenKind::Bool => Token::Bool.fmt(f),
|
||||||
TokenKind::Boolean => write!(f, "boolean value"),
|
TokenKind::Boolean => write!(f, "boolean value"),
|
||||||
TokenKind::Colon => Token::Colon.fmt(f),
|
TokenKind::Colon => Token::Colon.fmt(f),
|
||||||
@ -526,10 +531,11 @@ impl Display for TokenKind {
|
|||||||
pub(crate) mod tests {
|
pub(crate) mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
pub fn all_tokens<'src>() -> [Token<'src>; 48] {
|
pub fn all_tokens<'src>() -> [Token<'src>; 49] {
|
||||||
[
|
[
|
||||||
Token::Async,
|
Token::Async,
|
||||||
Token::Bang,
|
Token::Bang,
|
||||||
|
Token::BangEqual,
|
||||||
Token::Bool,
|
Token::Bool,
|
||||||
Token::Boolean("true"),
|
Token::Boolean("true"),
|
||||||
Token::Colon,
|
Token::Colon,
|
||||||
|
Loading…
Reference in New Issue
Block a user