1
0
dust/dust-lang/src/parser.rs

1694 lines
61 KiB
Rust
Raw Normal View History

2024-03-20 12:36:18 +00:00
use std::{cell::RefCell, collections::HashMap, rc::Rc};
2024-02-25 18:49:26 +00:00
use chumsky::{input::SpannedInput, pratt::*, prelude::*};
2024-03-07 10:37:26 +00:00
use crate::{
abstract_tree::*,
error::Error,
2024-03-25 04:16:55 +00:00
identifier::Identifier,
2024-03-24 19:35:19 +00:00
lexer::{Control, Keyword, Operator, Token},
2024-03-07 10:37:26 +00:00
};
2024-02-25 18:49:26 +00:00
2024-03-06 20:36:58 +00:00
pub type ParserInput<'src> =
SpannedInput<Token<'src>, SimpleSpan, &'src [(Token<'src>, SimpleSpan)]>;
2024-02-25 18:49:26 +00:00
2024-04-27 07:40:05 +00:00
pub type ParserExtra<'src> = extra::Err<Rich<'src, Token<'src>, SimpleSpan>>;
pub type Comment = String;
pub fn parse<'src>(tokens: &'src [(Token<'src>, SimpleSpan)]) -> Result<AbstractTree, Vec<Error>> {
2024-04-22 01:33:21 +00:00
let statements = parser(false)
2024-03-06 20:36:58 +00:00
.parse(tokens.spanned((tokens.len()..tokens.len()).into()))
2024-03-02 01:17:55 +00:00
.into_result()
.map_err(|errors| {
errors
.into_iter()
.map(|error| Error::from(error))
.collect::<Vec<Error>>()
})?;
Ok(AbstractTree::new(statements))
2024-03-02 01:17:55 +00:00
}
2024-03-25 04:16:55 +00:00
pub fn parser<'src>(
2024-04-22 01:33:21 +00:00
allow_built_ins: bool,
2024-04-27 07:40:05 +00:00
) -> impl Parser<'src, ParserInput<'src>, Vec<Statement>, ParserExtra<'src>> {
let identifiers: RefCell<HashMap<&str, Identifier>> = RefCell::new(HashMap::new());
2024-03-20 12:36:18 +00:00
let _custom_types: Rc<RefCell<HashMap<Identifier, Type>>> =
Rc::new(RefCell::new(HashMap::new()));
2024-03-20 12:53:51 +00:00
let custom_types = (_custom_types.clone(), _custom_types.clone());
2024-02-26 21:27:01 +00:00
let identifier = select! {
Token::Identifier(text) => {
let mut identifiers = identifiers.borrow_mut();
if let Some(identifier) = identifiers.get(&text) {
identifier.clone()
} else {
let new = Identifier::new(text);
identifiers.insert(text, new.clone());
new
}
}
2024-02-26 21:27:01 +00:00
};
2024-02-25 18:49:26 +00:00
2024-03-18 07:24:41 +00:00
let positioned_identifier = identifier
.clone()
.map_with(|identifier, state| identifier.with_position(state.span()));
2024-03-09 17:58:29 +00:00
let basic_value = select! {
Token::Boolean(boolean) => ValueNode::Boolean(boolean),
Token::Float(float) => ValueNode::Float(float),
2024-03-18 01:07:03 +00:00
Token::Integer(integer) => ValueNode::Integer(integer),
Token::String(text) => ValueNode::String(text.to_string()),
2024-03-09 17:58:29 +00:00
}
2024-03-25 04:16:55 +00:00
.map_with(|value, state| Expression::Value(value.with_position(state.span())));
2024-03-09 17:58:29 +00:00
2024-03-17 10:26:12 +00:00
let r#type = recursive(|r#type| {
2024-03-24 13:10:49 +00:00
let function_type = just(Token::Keyword(Keyword::Fn))
.ignore_then(
r#type
.clone()
.separated_by(just(Token::Control(Control::Comma)))
.collect()
.delimited_by(
just(Token::Control(Control::ParenOpen)),
just(Token::Control(Control::ParenClose)),
),
2024-03-17 10:52:11 +00:00
)
2024-03-24 13:10:49 +00:00
.then_ignore(just(Token::Control(Control::SkinnyArrow)))
2024-03-17 10:52:11 +00:00
.then(r#type.clone())
.map(|(parameter_types, return_type)| Type::Function {
parameter_types,
return_type: Box::new(return_type),
});
2024-03-10 01:57:46 +00:00
2024-03-20 15:43:47 +00:00
let list_of = just(Token::Keyword(Keyword::List))
2024-03-17 10:52:11 +00:00
.ignore_then(r#type.clone().delimited_by(
just(Token::Control(Control::ParenOpen)),
just(Token::Control(Control::ParenClose)),
2024-03-09 20:17:19 +00:00
))
2024-03-17 10:52:11 +00:00
.map(|item_type| Type::ListOf(Box::new(item_type)));
let list_exact = r#type
.clone()
.separated_by(just(Token::Control(Control::Comma)))
.collect()
.delimited_by(
just(Token::Control(Control::SquareOpen)),
just(Token::Control(Control::SquareClose)),
)
.map(|types| Type::ListExact(types));
2024-03-09 20:17:19 +00:00
2024-03-17 10:52:11 +00:00
choice((
function_type,
list_of,
list_exact,
2024-03-20 15:43:47 +00:00
just(Token::Keyword(Keyword::Any)).to(Type::Any),
just(Token::Keyword(Keyword::Bool)).to(Type::Boolean),
just(Token::Keyword(Keyword::Float)).to(Type::Float),
just(Token::Keyword(Keyword::Int)).to(Type::Integer),
just(Token::Keyword(Keyword::None)).to(Type::None),
just(Token::Keyword(Keyword::Range)).to(Type::Range),
just(Token::Keyword(Keyword::Str)).to(Type::String),
just(Token::Keyword(Keyword::List)).to(Type::List),
2024-03-24 13:10:49 +00:00
identifier.clone().map(move |identifier| {
if let Some(r#type) = custom_types.0.borrow().get(&identifier) {
r#type.clone()
} else {
Type::Argument(identifier)
}
2024-03-20 12:36:18 +00:00
}),
2024-03-17 10:52:11 +00:00
))
2024-03-24 16:21:08 +00:00
.map_with(|r#type, state| r#type.with_position(state.span()))
});
2024-03-17 06:51:33 +00:00
2024-03-24 13:10:49 +00:00
let type_argument = identifier
.clone()
.map_with(|identifier, state| Type::Argument(identifier).with_position(state.span()));
2024-03-17 10:52:11 +00:00
let type_specification = just(Token::Control(Control::Colon)).ignore_then(r#type.clone());
let structure_field_definition = identifier.clone().then(type_specification.clone());
let structure_definition = just(Token::Keyword(Keyword::Struct))
.ignore_then(identifier.clone())
.then(
structure_field_definition
.separated_by(just(Token::Control(Control::Comma)))
.allow_trailing()
.collect::<Vec<(Identifier, WithPosition<Type>)>>()
.delimited_by(
just(Token::Control(Control::CurlyOpen)),
just(Token::Control(Control::CurlyClose)),
),
)
.map_with(move |(name, fields), state| {
let definition = StructureDefinition::new(name.clone(), fields.clone());
let r#type = Type::Structure {
name: name.clone(),
fields,
};
custom_types.1.borrow_mut().insert(name, r#type);
2024-03-25 04:16:55 +00:00
Statement::StructureDefinition(definition.with_position(state.span()))
});
2024-03-25 04:16:55 +00:00
let statement = recursive(|statement| {
2024-05-20 19:22:50 +00:00
let allow_built_ins = allow_built_ins.clone();
2024-03-25 04:16:55 +00:00
let block = statement
2024-03-07 21:13:15 +00:00
.clone()
2024-03-09 17:58:29 +00:00
.repeated()
2024-03-07 21:13:15 +00:00
.collect()
.delimited_by(
just(Token::Control(Control::CurlyOpen)),
just(Token::Control(Control::CurlyClose)),
)
.map_with(|statements, state| Block::new(statements).with_position(state.span()));
2024-03-07 21:13:15 +00:00
2024-03-25 04:16:55 +00:00
let expression = recursive(|expression| {
2024-05-20 19:22:50 +00:00
let allow_built_ins = allow_built_ins.clone();
2024-03-17 10:26:12 +00:00
let identifier_expression = identifier.clone().map_with(|identifier, state| {
2024-03-25 04:16:55 +00:00
Expression::Identifier(identifier.with_position(state.span()))
2024-03-17 10:26:12 +00:00
});
2024-02-26 21:27:01 +00:00
2024-03-18 01:07:03 +00:00
let raw_integer = select! {
Token::Integer(integer) => integer
2024-03-09 17:58:29 +00:00
};
2024-03-18 01:07:03 +00:00
let range = raw_integer
.clone()
.then_ignore(just(Token::Control(Control::DoubleDot)))
.then(raw_integer)
.map_with(|(start, end), state| {
2024-03-25 04:16:55 +00:00
Expression::Value(ValueNode::Range(start..end).with_position(state.span()))
2024-03-18 01:07:03 +00:00
});
2024-03-25 04:16:55 +00:00
let list = expression
2024-03-09 17:58:29 +00:00
.clone()
.separated_by(just(Token::Control(Control::Comma)))
.allow_trailing()
.collect()
.delimited_by(
just(Token::Control(Control::SquareOpen)),
just(Token::Control(Control::SquareClose)),
)
2024-03-17 10:26:12 +00:00
.map_with(|list, state| {
2024-03-25 04:16:55 +00:00
Expression::Value(ValueNode::List(list).with_position(state.span()))
2024-03-17 10:26:12 +00:00
});
2024-03-09 17:58:29 +00:00
let map_assignment = identifier
.clone()
2024-03-17 10:52:11 +00:00
.then(type_specification.clone().or_not())
2024-03-09 17:58:29 +00:00
.then_ignore(just(Token::Operator(Operator::Assign)))
2024-03-25 04:16:55 +00:00
.then(expression.clone())
2024-03-17 10:26:12 +00:00
.map(|((identifier, r#type), expression)| (identifier, r#type, expression));
2024-03-09 17:58:29 +00:00
let map = map_assignment
.separated_by(just(Token::Control(Control::Comma)).or_not())
.allow_trailing()
.collect()
.delimited_by(
just(Token::Control(Control::CurlyOpen)),
just(Token::Control(Control::CurlyClose)),
)
2024-03-17 10:26:12 +00:00
.map_with(|map_assigment_list, state| {
2024-03-25 04:16:55 +00:00
Expression::Value(
ValueNode::Map(map_assigment_list).with_position(state.span()),
)
2024-03-17 10:26:12 +00:00
});
2024-03-09 17:58:29 +00:00
2024-03-24 13:10:49 +00:00
let type_arguments = type_argument
2024-03-09 17:58:29 +00:00
.clone()
.separated_by(just(Token::Control(Control::Comma)))
2024-03-24 13:10:49 +00:00
.at_least(1)
2024-03-17 10:26:12 +00:00
.collect()
2024-03-09 17:58:29 +00:00
.delimited_by(
just(Token::Control(Control::ParenOpen)),
just(Token::Control(Control::ParenClose)),
2024-03-24 13:10:49 +00:00
);
let parsed_function = just(Token::Keyword(Keyword::Fn))
.ignore_then(
type_arguments.or_not().then(
identifier
.clone()
.then(type_specification.clone())
.separated_by(just(Token::Control(Control::Comma)))
.collect()
.delimited_by(
just(Token::Control(Control::ParenOpen)),
just(Token::Control(Control::ParenClose)),
)
.then(r#type.clone())
.then(block.clone()),
),
2024-03-09 17:58:29 +00:00
)
2024-03-24 13:10:49 +00:00
.map_with(
|(type_arguments, ((parameters, return_type), body)), state| {
2024-03-25 04:16:55 +00:00
Expression::Value(
ValueNode::ParsedFunction {
type_arguments: type_arguments
.unwrap_or_else(|| Vec::with_capacity(0)),
parameters,
return_type,
body,
2024-03-25 04:16:55 +00:00
}
.with_position(state.span()),
)
2024-03-24 13:10:49 +00:00
},
);
2024-03-09 17:58:29 +00:00
2024-04-21 22:06:26 +00:00
let built_in_function_call = choice((
2024-05-20 19:22:50 +00:00
just(Token::Keyword(Keyword::ReadLine)).map_with(|_, state| {
Expression::BuiltInFunctionCall(
Box::new(BuiltInFunctionCall::ReadLine).with_position(state.span()),
2024-05-18 21:55:58 +00:00
)
2024-05-20 19:22:50 +00:00
}),
just(Token::Keyword(Keyword::Sleep))
.ignore_then(expression.clone())
.map_with(|argument, state| {
Expression::BuiltInFunctionCall(
Box::new(BuiltInFunctionCall::Sleep(argument))
.with_position(state.span()),
)
}),
just(Token::Keyword(Keyword::WriteLine))
.ignore_then(expression.clone())
.map_with(|argument, state| {
Expression::BuiltInFunctionCall(
Box::new(BuiltInFunctionCall::WriteLine(argument))
.with_position(state.span()),
)
}),
))
.try_map_with(move |expression, state| {
if allow_built_ins {
Ok(expression)
} else {
Err(Rich::custom(
2024-04-22 01:33:21 +00:00
state.span(),
"Built-in function calls can only be used by the standard library.",
2024-05-20 19:22:50 +00:00
))
2024-04-22 01:33:21 +00:00
}
2024-04-21 22:06:26 +00:00
});
2024-03-23 13:35:24 +00:00
2024-03-24 13:10:49 +00:00
let structure_field = identifier
.clone()
.then_ignore(just(Token::Operator(Operator::Assign)))
2024-03-25 04:16:55 +00:00
.then(expression.clone());
2024-03-24 13:10:49 +00:00
2024-03-25 04:16:55 +00:00
let structure_instance = positioned_identifier
2024-03-24 13:10:49 +00:00
.clone()
.then(
structure_field
.separated_by(just(Token::Control(Control::Comma)))
.allow_trailing()
.collect()
.delimited_by(
just(Token::Control(Control::CurlyOpen)),
just(Token::Control(Control::CurlyClose)),
),
)
.map_with(|(name, fields), state| {
2024-03-25 04:16:55 +00:00
Expression::Value(
ValueNode::Structure { name, fields }.with_position(state.span()),
)
2024-03-24 13:10:49 +00:00
});
2024-03-24 14:58:09 +00:00
let turbofish = r#type
.clone()
.separated_by(just(Token::Control(Control::Comma)))
.at_least(1)
.collect()
.delimited_by(
just(Token::Control(Control::ParenOpen)),
just(Token::Control(Control::ParenClose)),
)
.delimited_by(
just(Token::Control(Control::DoubleColon)),
just(Token::Control(Control::DoubleColon)),
);
2024-03-09 17:58:29 +00:00
let atom = choice((
2024-03-24 13:10:49 +00:00
range.clone(),
2024-03-23 13:35:24 +00:00
parsed_function.clone(),
2024-03-09 17:58:29 +00:00
list.clone(),
2024-03-24 13:10:49 +00:00
basic_value.clone(),
map.clone(),
structure_instance.clone(),
2024-03-24 13:10:49 +00:00
identifier_expression.clone(),
2024-03-25 04:16:55 +00:00
expression.clone().delimited_by(
2024-03-09 17:58:29 +00:00
just(Token::Control(Control::ParenOpen)),
just(Token::Control(Control::ParenClose)),
2024-03-07 17:29:07 +00:00
),
2024-03-17 10:26:12 +00:00
));
2024-03-09 17:58:29 +00:00
let logic_math_indexes_and_function_calls = atom.pratt((
2024-03-29 19:03:38 +00:00
prefix(
2,
just(Token::Operator(Operator::Not)),
|_, expression, span| {
Expression::Logic(Box::new(Logic::Not(expression)).with_position(span))
},
),
postfix(
3,
expression.clone().delimited_by(
just(Token::Control(Control::SquareOpen)),
just(Token::Control(Control::SquareClose)),
),
|left, right, span| {
Expression::ListIndex(
Box::new(ListIndex::new(left, right)).with_position(span),
)
},
),
infix(
left(4),
just(Token::Control(Control::Dot)),
|left: Expression, _: Token, right: Expression, span| {
Expression::MapIndex(
Box::new(MapIndex::new(left, right)).with_position(span),
)
},
),
2024-03-18 01:42:53 +00:00
postfix(
3,
2024-03-24 14:58:09 +00:00
turbofish.clone().or_not().then(
2024-03-25 04:16:55 +00:00
expression
2024-03-24 14:58:09 +00:00
.clone()
.separated_by(just(Token::Control(Control::Comma)))
.collect()
.delimited_by(
just(Token::Control(Control::ParenOpen)),
just(Token::Control(Control::ParenClose)),
),
),
|function_expression,
(type_arguments, arguments): (
Option<Vec<WithPosition<Type>>>,
2024-03-25 04:16:55 +00:00
Vec<Expression>,
2024-03-24 14:58:09 +00:00
),
span| {
2024-03-25 04:16:55 +00:00
Expression::FunctionCall(
FunctionCall::new(
function_expression,
type_arguments.unwrap_or_else(|| Vec::with_capacity(0)),
arguments,
)
.with_position(span),
)
2024-03-18 01:07:03 +00:00
},
),
2024-03-17 10:26:12 +00:00
infix(
left(1),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::Equal)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Logic(Box::new(Logic::Equal(left, right)).with_position(span))
2024-03-17 10:26:12 +00:00
},
),
infix(
left(1),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::NotEqual)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Logic(
Box::new(Logic::NotEqual(left, right)).with_position(span),
)
2024-03-17 10:26:12 +00:00
},
),
infix(
left(1),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::Greater)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Logic(Box::new(Logic::Greater(left, right)).with_position(span))
2024-03-17 10:26:12 +00:00
},
),
infix(
left(1),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::Less)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Logic(Box::new(Logic::Less(left, right)).with_position(span))
2024-03-17 10:26:12 +00:00
},
),
infix(
left(1),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::GreaterOrEqual)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Logic(
Box::new(Logic::GreaterOrEqual(left, right)).with_position(span),
)
2024-03-17 10:26:12 +00:00
},
),
infix(
left(1),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::LessOrEqual)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Logic(
Box::new(Logic::LessOrEqual(left, right)).with_position(span),
)
2024-03-17 10:26:12 +00:00
},
),
infix(
left(1),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::And)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Logic(Box::new(Logic::And(left, right)).with_position(span))
2024-03-17 10:26:12 +00:00
},
),
infix(
left(1),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::Or)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Logic(Box::new(Logic::Or(left, right)).with_position(span))
2024-03-17 10:26:12 +00:00
},
),
infix(
left(1),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::Add)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Math(Box::new(Math::Add(left, right)).with_position(span))
2024-03-17 10:26:12 +00:00
},
),
infix(
left(1),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::Subtract)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Math(Box::new(Math::Subtract(left, right)).with_position(span))
2024-03-17 10:26:12 +00:00
},
),
infix(
left(2),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::Multiply)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Math(Box::new(Math::Multiply(left, right)).with_position(span))
2024-03-17 10:26:12 +00:00
},
),
infix(
left(2),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::Divide)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Math(Box::new(Math::Divide(left, right)).with_position(span))
2024-03-17 10:26:12 +00:00
},
),
infix(
left(1),
2024-03-29 19:03:38 +00:00
just(Token::Operator(Operator::Modulo)),
2024-03-17 10:26:12 +00:00
|left, _, right, span| {
2024-03-25 04:16:55 +00:00
Expression::Math(Box::new(Math::Modulo(left, right)).with_position(span))
2024-03-17 10:26:12 +00:00
},
),
));
2024-05-20 19:22:50 +00:00
2024-05-21 23:27:33 +00:00
let r#as = choice((
basic_value.clone(),
identifier_expression.clone(),
logic_math_indexes_and_function_calls.clone(),
))
.then_ignore(just(Token::Keyword(Keyword::As)))
.then(r#type.clone())
.map_with(|(expression, r#type), state| {
Expression::As(Box::new(As::new(expression, r#type)).with_position(state.span()))
});
2024-03-09 17:58:29 +00:00
choice((
2024-05-21 21:07:12 +00:00
r#as,
2024-05-21 23:27:33 +00:00
logic_math_indexes_and_function_calls,
built_in_function_call,
2024-03-24 13:10:49 +00:00
range,
structure_instance,
2024-03-23 13:35:24 +00:00
parsed_function,
2024-03-09 17:58:29 +00:00
list,
map,
basic_value,
2024-03-18 01:07:03 +00:00
identifier_expression,
2024-02-26 21:27:01 +00:00
))
2024-03-09 17:58:29 +00:00
});
2024-02-26 21:27:01 +00:00
2024-03-25 04:16:55 +00:00
let expression_statement = expression
.clone()
.map(|expression| Statement::Expression(expression));
2024-02-26 21:27:01 +00:00
2024-03-20 21:05:37 +00:00
let async_block = just(Token::Keyword(Keyword::Async))
2024-03-25 04:16:55 +00:00
.ignore_then(statement.clone().repeated().collect().delimited_by(
just(Token::Control(Control::CurlyOpen)),
just(Token::Control(Control::CurlyClose)),
))
2024-03-20 21:05:37 +00:00
.map_with(|statements, state| {
2024-03-25 04:16:55 +00:00
Statement::AsyncBlock(AsyncBlock::new(statements).with_position(state.span()))
2024-03-20 21:05:37 +00:00
});
2024-03-20 15:43:47 +00:00
let r#break = just(Token::Keyword(Keyword::Break))
2024-03-25 04:16:55 +00:00
.map_with(|_, state| Statement::Break(().with_position(state.span())));
2024-03-08 17:24:11 +00:00
2024-03-18 07:24:41 +00:00
let assignment = positioned_identifier
2024-03-08 21:14:47 +00:00
.clone()
2024-03-17 10:52:11 +00:00
.then(type_specification.clone().or_not())
.then(choice((
just(Token::Operator(Operator::Assign)).to(AssignmentOperator::Assign),
just(Token::Operator(Operator::AddAssign)).to(AssignmentOperator::AddAssign),
just(Token::Operator(Operator::SubAssign)).to(AssignmentOperator::SubAssign),
)))
2024-03-25 04:16:55 +00:00
.then(statement.clone())
2024-03-16 19:01:45 +00:00
.map_with(|(((identifier, r#type), operator), statement), state| {
2024-03-25 04:16:55 +00:00
Statement::Assignment(
Assignment::new(identifier, r#type, operator, statement)
.with_position(state.span()),
)
2024-03-17 10:26:12 +00:00
});
2024-02-25 18:49:26 +00:00
let block_statement = block.clone().map(|block| Statement::Block(block));
2024-02-28 23:16:25 +00:00
2024-03-25 04:16:55 +00:00
let r#loop = statement
2024-03-02 01:17:55 +00:00
.clone()
2024-03-07 00:45:41 +00:00
.repeated()
2024-03-08 17:24:11 +00:00
.at_least(1)
2024-03-02 01:17:55 +00:00
.collect()
.delimited_by(
2024-03-20 15:43:47 +00:00
just(Token::Keyword(Keyword::Loop)).then(just(Token::Control(Control::CurlyOpen))),
2024-03-07 11:57:33 +00:00
just(Token::Control(Control::CurlyClose)),
2024-03-02 01:17:55 +00:00
)
2024-03-17 10:26:12 +00:00
.map_with(|statements, state| {
2024-03-25 04:16:55 +00:00
Statement::Loop(Loop::new(statements).with_position(state.span()))
2024-03-17 10:26:12 +00:00
});
2024-03-02 01:17:55 +00:00
2024-03-20 15:43:47 +00:00
let r#while = just(Token::Keyword(Keyword::While))
2024-03-25 04:16:55 +00:00
.ignore_then(expression.clone())
.then(statement.clone().repeated().collect().delimited_by(
just(Token::Control(Control::CurlyOpen)),
just(Token::Control(Control::CurlyClose)),
))
2024-03-17 20:59:52 +00:00
.map_with(|(expression, statements), state| {
2024-03-25 04:16:55 +00:00
Statement::While(While::new(expression, statements).with_position(state.span()))
2024-03-17 10:26:12 +00:00
});
2024-03-11 21:58:26 +00:00
2024-03-20 15:43:47 +00:00
let if_else = just(Token::Keyword(Keyword::If))
2024-03-25 04:16:55 +00:00
.ignore_then(expression.clone())
2024-03-17 14:19:22 +00:00
.then(block.clone())
2024-03-25 04:16:55 +00:00
.then(
just(Token::Keyword(Keyword::Else))
.ignore_then(just(Token::Keyword(Keyword::If)))
.ignore_then(expression.clone())
.then(block.clone())
.repeated()
.collect(),
2024-03-25 04:16:55 +00:00
)
2024-03-08 19:29:53 +00:00
.then(
2024-03-20 15:43:47 +00:00
just(Token::Keyword(Keyword::Else))
2024-03-17 14:19:22 +00:00
.ignore_then(block.clone())
2024-03-08 19:29:53 +00:00
.or_not(),
)
2024-03-25 04:16:55 +00:00
.map_with(
|(((if_expression, if_block), else_ifs), else_block), state| {
Statement::IfElse(
IfElse::new(if_expression, if_block, else_ifs, else_block)
.with_position(state.span()),
)
},
);
2024-03-19 21:49:24 +00:00
2024-03-08 19:01:05 +00:00
choice((
2024-03-20 21:05:37 +00:00
async_block,
2024-03-19 21:49:24 +00:00
structure_definition,
2024-03-10 01:57:46 +00:00
if_else,
2024-03-08 19:01:05 +00:00
assignment,
expression_statement,
r#break,
2024-03-09 17:58:29 +00:00
block_statement,
2024-03-08 19:01:05 +00:00
r#loop,
2024-03-11 21:58:26 +00:00
r#while,
2024-03-08 19:01:05 +00:00
))
.then_ignore(just(Token::Control(Control::Semicolon)).or_not())
2024-02-26 21:27:01 +00:00
});
2024-02-25 18:49:26 +00:00
2024-03-25 04:16:55 +00:00
statement.clone().repeated().collect()
2024-02-25 18:49:26 +00:00
}
#[cfg(test)]
mod tests {
2024-04-21 21:00:08 +00:00
use crate::lexer::lex;
2024-02-25 18:49:26 +00:00
use super::*;
2024-05-21 23:27:33 +00:00
#[test]
fn r#as() {
assert_eq!(
parse(&lex("1 as str").unwrap()).unwrap()[0],
Statement::Expression(Expression::As(
Box::new(As::new(
Expression::Value(ValueNode::Integer(1).with_position((0, 1))),
Type::String.with_position((5, 8))
))
.with_position((0, 8))
))
)
}
2024-03-23 13:35:24 +00:00
#[test]
fn built_in_function() {
2024-05-20 19:22:50 +00:00
let tokens = lex("READ_LINE").unwrap();
2024-04-22 01:33:21 +00:00
let statements = parser(true)
.parse(tokens.spanned((tokens.len()..tokens.len()).into()))
.into_result()
.map_err(|errors| {
errors
.into_iter()
.map(|error| Error::from(error))
.collect::<Vec<Error>>()
})
.unwrap();
2024-03-23 13:35:24 +00:00
assert_eq!(
2024-04-22 01:33:21 +00:00
statements[0],
2024-04-21 21:00:08 +00:00
Statement::Expression(Expression::BuiltInFunctionCall(
2024-05-20 19:22:50 +00:00
Box::new(BuiltInFunctionCall::ReadLine).with_position((0, 9))
2024-03-25 04:16:55 +00:00
))
2024-04-21 21:00:08 +00:00
);
2024-05-20 19:22:50 +00:00
let tokens = lex("WRITE_LINE 'hiya'").unwrap();
2024-04-22 01:33:21 +00:00
let statements = parser(true)
.parse(tokens.spanned((tokens.len()..tokens.len()).into()))
.into_result()
.map_err(|errors| {
errors
.into_iter()
.map(|error| Error::from(error))
.collect::<Vec<Error>>()
})
.unwrap();
2024-04-21 21:00:08 +00:00
assert_eq!(
2024-04-22 01:33:21 +00:00
statements[0],
2024-04-21 21:00:08 +00:00
Statement::Expression(Expression::BuiltInFunctionCall(
Box::new(BuiltInFunctionCall::WriteLine(Expression::Value(
2024-04-21 22:06:26 +00:00
ValueNode::String("hiya".to_string()).with_position((11, 17))
2024-04-21 21:00:08 +00:00
)))
2024-05-20 19:22:50 +00:00
.with_position((0, 17))
2024-04-21 21:00:08 +00:00
))
);
2024-03-23 13:35:24 +00:00
}
2024-03-20 21:05:37 +00:00
#[test]
fn async_block() {
assert_eq!(
parse(
&lex("
async {
1
2
3
}
")
.unwrap()
)
2024-03-25 04:16:55 +00:00
.unwrap()[0],
Statement::AsyncBlock(
AsyncBlock::new(vec![
Statement::Expression(Expression::Value(
ValueNode::Integer(1).with_position((53, 54))
2024-03-25 04:16:55 +00:00
)),
Statement::Expression(Expression::Value(
ValueNode::Integer(2).with_position((79, 80))
2024-03-25 04:16:55 +00:00
)),
Statement::Expression(Expression::Value(
ValueNode::Integer(3).with_position((105, 106))
2024-03-25 04:16:55 +00:00
)),
])
.with_position((21, 128))
2024-03-25 04:16:55 +00:00
)
2024-03-20 21:05:37 +00:00
)
}
2024-03-19 22:31:52 +00:00
#[test]
fn structure_instance() {
assert_eq!(
parse(
&lex("
Foo {
bar = 42,
baz = 'hiya',
}
")
.unwrap()
)
2024-03-25 04:16:55 +00:00
.unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Structure {
name: Identifier::new("Foo").with_position((21, 24)),
2024-03-25 04:16:55 +00:00
fields: vec![
(
Identifier::new("bar"),
Expression::Value(ValueNode::Integer(42).with_position((57, 59)))
),
(
Identifier::new("baz"),
Expression::Value(
ValueNode::String("hiya".to_string()).with_position((91, 97))
)
),
]
}
.with_position((21, 120))
2024-03-25 04:16:55 +00:00
))
2024-03-19 22:31:52 +00:00
)
}
2024-03-19 21:49:24 +00:00
#[test]
fn structure_definition() {
assert_eq!(
parse(
&lex("
struct Foo {
bar : int,
baz : str,
}
")
.unwrap()
)
2024-03-25 04:16:55 +00:00
.unwrap()[0],
Statement::StructureDefinition(
StructureDefinition::new(
Identifier::new("Foo"),
vec![
(
Identifier::new("bar"),
Type::Integer.with_position((64, 67))
),
(
Identifier::new("baz"),
Type::String.with_position((99, 102))
),
]
)
.with_position((21, 125))
2024-03-25 04:16:55 +00:00
)
2024-03-19 21:49:24 +00:00
)
}
2024-03-18 01:07:03 +00:00
#[test]
fn map_index() {
assert_eq!(
2024-03-27 17:53:55 +00:00
parse(&lex("{ x = 42 }.x").unwrap()).unwrap()[0],
2024-03-25 04:16:55 +00:00
Statement::Expression(Expression::MapIndex(
Box::new(MapIndex::new(
Expression::Value(
ValueNode::Map(vec![(
Identifier::new("x"),
None,
Expression::Value(ValueNode::Integer(42).with_position((6, 8)))
)])
2024-03-27 18:11:18 +00:00
.with_position((0, 10))
2024-03-25 04:16:55 +00:00
),
Expression::Identifier(Identifier::new("x").with_position((11, 12)))
2024-03-25 04:16:55 +00:00
))
2024-03-27 18:11:18 +00:00
.with_position((0, 12))
2024-03-25 04:16:55 +00:00
))
2024-03-18 01:07:03 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("foo.x").unwrap()).unwrap()[0],
Statement::Expression(Expression::MapIndex(
Box::new(MapIndex::new(
Expression::Identifier(Identifier::new("foo").with_position((0, 3))),
Expression::Identifier(Identifier::new("x").with_position((4, 5)))
2024-03-25 04:16:55 +00:00
))
.with_position((0, 5))
2024-03-25 04:16:55 +00:00
))
2024-03-18 01:07:03 +00:00
);
}
2024-03-11 21:58:26 +00:00
#[test]
fn r#while() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("while true { output('hi') }").unwrap()).unwrap()[0],
Statement::While(
While::new(
Expression::Value(ValueNode::Boolean(true).with_position((6, 10))),
vec![Statement::Expression(Expression::FunctionCall(
FunctionCall::new(
Expression::Identifier(
Identifier::new("output").with_position((13, 19))
),
Vec::with_capacity(0),
vec![Expression::Value(
ValueNode::String("hi".to_string()).with_position((20, 24))
)]
)
.with_position((13, 25))
))]
)
.with_position((0, 27))
2024-03-25 04:16:55 +00:00
)
2024-03-11 21:58:26 +00:00
)
}
2024-03-10 01:57:46 +00:00
#[test]
2024-03-17 10:52:11 +00:00
fn boolean_type() {
2024-03-10 01:57:46 +00:00
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("foobar : bool = true").unwrap()).unwrap()[0],
Statement::Assignment(
Assignment::new(
Identifier::new("foobar").with_position((0, 6)),
Some(Type::Boolean.with_position((9, 13))),
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(
ValueNode::Boolean(true).with_position((16, 20))
))
)
.with_position((0, 20))
2024-03-25 04:16:55 +00:00
)
2024-03-17 13:54:15 +00:00
);
}
#[test]
fn list_type() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("foobar: list = []").unwrap()).unwrap()[0],
Statement::Assignment(
Assignment::new(
Identifier::new("foobar").with_position((0, 6)),
Some(Type::List.with_position((8, 12))),
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(
ValueNode::List(vec![]).with_position((15, 17))
))
)
.with_position((0, 17))
2024-03-25 04:16:55 +00:00
)
2024-03-10 01:57:46 +00:00
);
2024-03-17 10:52:11 +00:00
}
#[test]
fn list_of_type() {
2024-03-10 01:57:46 +00:00
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("foobar : list(bool) = [true]").unwrap()).unwrap()[0],
Statement::Assignment(
Assignment::new(
Identifier::new("foobar").with_position((0, 6)),
Some(
Type::ListOf(Box::new(Type::Boolean.with_position((14, 18))))
.with_position((9, 19))
),
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(
ValueNode::List(vec![Expression::Value(
ValueNode::Boolean(true).with_position((23, 27))
)])
.with_position((22, 28))
2024-03-25 04:16:55 +00:00
))
2024-03-17 10:26:12 +00:00
)
.with_position((0, 28))
2024-03-25 04:16:55 +00:00
)
2024-03-10 01:57:46 +00:00
);
2024-03-08 19:29:53 +00:00
}
#[test]
2024-03-17 10:52:11 +00:00
fn list_exact_type() {
2024-03-08 19:01:05 +00:00
assert_eq!(
2024-03-17 10:52:11 +00:00
parse(&lex("foobar : [bool, str] = [true, '42']").unwrap()).unwrap()[0],
2024-03-25 04:16:55 +00:00
Statement::Assignment(
Assignment::new(
Identifier::new("foobar").with_position((0, 6)),
Some(
Type::ListExact(vec![
Type::Boolean.with_position((10, 14)),
Type::String.with_position((16, 19))
])
.with_position((9, 20))
),
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(
ValueNode::List(vec![
Expression::Value(ValueNode::Boolean(true).with_position((24, 28))),
Expression::Value(
ValueNode::String("42".to_string()).with_position((30, 34))
)
])
.with_position((23, 35))
2024-03-25 04:16:55 +00:00
))
)
.with_position((0, 35))
)
2024-02-25 18:49:26 +00:00
);
}
#[test]
2024-03-17 10:52:11 +00:00
fn function_type() {
2024-02-25 18:49:26 +00:00
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("foobar : fn() -> any = some_function").unwrap()).unwrap()[0],
Statement::Assignment(
Assignment::new(
Identifier::new("foobar").with_position((0, 6)),
Some(
Type::Function {
parameter_types: vec![],
return_type: Box::new(Type::Any.with_position((17, 20)))
}
.with_position((9, 20))
),
AssignmentOperator::Assign,
Statement::Expression(Expression::Identifier(
Identifier::new("some_function").with_position((23, 36))
2024-03-25 04:16:55 +00:00
))
)
.with_position((0, 36))
2024-03-25 04:16:55 +00:00
)
2024-02-25 18:49:26 +00:00
);
}
2024-03-17 11:31:45 +00:00
#[test]
fn function_call() {
assert_eq!(
parse(&lex("foobar()").unwrap()).unwrap()[0],
2024-03-25 04:16:55 +00:00
Statement::Expression(Expression::FunctionCall(
FunctionCall::new(
Expression::Identifier(Identifier::new("foobar").with_position((0, 6))),
2024-03-25 04:16:55 +00:00
Vec::with_capacity(0),
Vec::with_capacity(0),
)
.with_position((0, 8))
2024-03-25 04:16:55 +00:00
))
2024-03-24 14:58:09 +00:00
)
}
#[test]
fn function_call_with_type_arguments() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("foobar::(str)::('hi')").unwrap()).unwrap()[0],
Statement::Expression(Expression::FunctionCall(
FunctionCall::new(
Expression::Identifier(Identifier::new("foobar").with_position((0, 6))),
vec![Type::String.with_position((9, 12))],
vec![Expression::Value(
ValueNode::String("hi".to_string()).with_position((16, 20))
2024-03-25 04:16:55 +00:00
)],
)
.with_position((0, 21))
2024-03-25 04:16:55 +00:00
))
2024-03-17 11:31:45 +00:00
)
}
2024-03-17 10:52:11 +00:00
2024-03-17 11:31:45 +00:00
#[test]
fn range() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("1..10").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Range(1..10).with_position((0, 5))
2024-03-25 04:16:55 +00:00
))
2024-03-17 11:31:45 +00:00
)
}
2024-03-17 10:52:11 +00:00
2024-03-17 12:30:46 +00:00
#[test]
fn function() {
assert_eq!(
parse(&lex("fn (x: int) int { x }").unwrap()).unwrap()[0],
2024-03-25 04:16:55 +00:00
Statement::Expression(Expression::Value(
ValueNode::ParsedFunction {
type_arguments: Vec::with_capacity(0),
parameters: vec![(Identifier::new("x"), Type::Integer.with_position((7, 10)))],
return_type: Type::Integer.with_position((12, 15)),
2024-03-25 04:16:55 +00:00
body: Block::new(vec![Statement::Expression(Expression::Identifier(
Identifier::new("x").with_position((18, 19))
2024-03-25 04:16:55 +00:00
))])
.with_position((16, 21)),
2024-03-25 04:16:55 +00:00
}
.with_position((0, 21))
2024-03-25 04:16:55 +00:00
),)
2024-03-24 13:10:49 +00:00
)
}
#[test]
fn function_with_type_arguments() {
assert_eq!(
parse(&lex("fn (T, U)(x: T, y: U) T { x }").unwrap()).unwrap()[0],
2024-03-25 04:16:55 +00:00
Statement::Expression(Expression::Value(
ValueNode::ParsedFunction {
type_arguments: vec![
Type::Argument(Identifier::new("T")).with_position((4, 5)),
Type::Argument(Identifier::new("U")).with_position((7, 8)),
2024-03-25 04:16:55 +00:00
],
parameters: vec![
(
Identifier::new("x"),
Type::Argument(Identifier::new("T")).with_position((13, 14))
2024-03-25 04:16:55 +00:00
),
(
Identifier::new("y"),
Type::Argument(Identifier::new("U")).with_position((19, 20))
2024-03-25 04:16:55 +00:00
)
],
return_type: Type::Argument(Identifier::new("T")).with_position((22, 23)),
2024-03-25 04:16:55 +00:00
body: Block::new(vec![Statement::Expression(Expression::Identifier(
Identifier::new("x").with_position((26, 27))
2024-03-25 04:16:55 +00:00
))])
.with_position((24, 29)),
2024-03-25 04:16:55 +00:00
}
.with_position((0, 29))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
)
}
#[test]
fn r#if() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("if true { 'foo' }").unwrap()).unwrap()[0],
Statement::IfElse(
IfElse::new(
Expression::Value(ValueNode::Boolean(true).with_position((3, 7))),
Block::new(vec![Statement::Expression(Expression::Value(
ValueNode::String("foo".to_string()).with_position((10, 15))
))])
.with_position((8, 17)),
Vec::with_capacity(0),
2024-03-25 04:16:55 +00:00
None
)
.with_position((0, 17))
2024-03-25 04:16:55 +00:00
)
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn if_else() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("if true {'foo' } else { 'bar' }").unwrap()).unwrap()[0],
Statement::IfElse(
IfElse::new(
Expression::Value(ValueNode::Boolean(true).with_position((3, 7))),
Block::new(vec![Statement::Expression(Expression::Value(
ValueNode::String("foo".to_string()).with_position((9, 14))
))])
.with_position((8, 16)),
Vec::with_capacity(0),
Some(
Block::new(vec![Statement::Expression(Expression::Value(
ValueNode::String("bar".to_string()).with_position((24, 29))
))])
.with_position((22, 31))
)
2024-03-25 04:16:55 +00:00
)
.with_position((0, 31)),
2024-03-25 04:16:55 +00:00
)
2024-03-17 12:30:46 +00:00
)
}
#[test]
fn map() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("{ foo = 'bar' }").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Map(vec![(
Identifier::new("foo"),
None,
Expression::Value(ValueNode::String("bar".to_string()).with_position((8, 13)))
)])
.with_position((0, 15))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("{ x = 1, y = 2, }").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Map(vec![
(
Identifier::new("x"),
None,
Expression::Value(ValueNode::Integer(1).with_position((6, 7)))
),
(
Identifier::new("y"),
None,
Expression::Value(ValueNode::Integer(2).with_position((13, 14)))
),
])
.with_position((0, 17))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("{ x = 1 y = 2 }").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Map(vec![
(
Identifier::new("x"),
None,
Expression::Value(ValueNode::Integer(1).with_position((6, 7)))
),
(
Identifier::new("y"),
None,
Expression::Value(ValueNode::Integer(2).with_position((12, 13)))
),
])
.with_position((0, 15))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn math() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("1 + 1").unwrap()).unwrap()[0],
Statement::Expression(Expression::Math(
Box::new(Math::Add(
Expression::Value(ValueNode::Integer(1).with_position((0, 1))),
Expression::Value(ValueNode::Integer(1).with_position((4, 5)))
))
.with_position((0, 5))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn r#loop() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("loop { 42 }").unwrap()).unwrap()[0],
Statement::Loop(
Loop::new(vec![Statement::Expression(Expression::Value(
ValueNode::Integer(42).with_position((7, 9))
2024-03-25 04:16:55 +00:00
))])
.with_position((0, 11))
2024-03-25 04:16:55 +00:00
)
2024-03-17 12:30:46 +00:00
);
2024-03-17 17:36:31 +00:00
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("loop { if i > 2 { break } else { i += 1 } }").unwrap()).unwrap()[0],
Statement::Loop(
Loop::new(vec![Statement::IfElse(
IfElse::new(
Expression::Logic(
Box::new(Logic::Greater(
Expression::Identifier(
Identifier::new("i").with_position((10, 11))
),
Expression::Value(ValueNode::Integer(2).with_position((14, 15)))
))
.with_position((10, 15))
2024-03-25 04:16:55 +00:00
),
Block::new(vec![Statement::Break(().with_position((18, 23)))])
.with_position((16, 25)),
Vec::with_capacity(0),
Some(
Block::new(vec![Statement::Assignment(
Assignment::new(
Identifier::new("i").with_position((33, 34)),
None,
AssignmentOperator::AddAssign,
Statement::Expression(Expression::Value(
ValueNode::Integer(1).with_position((38, 39))
))
)
.with_position((33, 39))
)])
.with_position((31, 41))
)
2024-03-25 04:16:55 +00:00
)
.with_position((7, 41))
2024-03-25 04:16:55 +00:00
)])
.with_position((0, 43))
2024-03-25 04:16:55 +00:00
)
2024-03-17 17:36:31 +00:00
);
2024-03-17 12:30:46 +00:00
}
#[test]
fn block() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("{ x }").unwrap()).unwrap()[0],
Statement::Block(
Block::new(vec![Statement::Expression(Expression::Identifier(
Identifier::new("x").with_position((2, 3))
2024-03-25 04:16:55 +00:00
),)])
.with_position((0, 5))
2024-03-17 12:30:46 +00:00
)
);
assert_eq!(
parse(
&lex("
{
x;
y;
z
}
")
.unwrap()
)
2024-03-25 04:16:55 +00:00
.unwrap()[0],
Statement::Block(
Block::new(vec![
Statement::Expression(Expression::Identifier(
Identifier::new("x").with_position((39, 40))
2024-03-25 04:16:55 +00:00
)),
Statement::Expression(Expression::Identifier(
Identifier::new("y").with_position((62, 63))
2024-03-25 04:16:55 +00:00
)),
Statement::Expression(Expression::Identifier(
Identifier::new("z").with_position((85, 86))
2024-03-25 04:16:55 +00:00
)),
])
.with_position((17, 104)),
2024-03-25 04:16:55 +00:00
)
2024-03-17 12:30:46 +00:00
);
assert_eq!(
parse(
&lex("
{
1 == 1
z
}
")
.unwrap()
)
2024-03-25 04:16:55 +00:00
.unwrap()[0],
Statement::Block(
Block::new(vec![
Statement::Expression(Expression::Logic(
Box::new(Logic::Equal(
Expression::Value(ValueNode::Integer(1).with_position((39, 40))),
Expression::Value(ValueNode::Integer(1).with_position((44, 45)))
))
.with_position((39, 45))
2024-03-25 04:16:55 +00:00
)),
Statement::Expression(Expression::Identifier(
Identifier::new("z").with_position((66, 67))
)),
])
.with_position((17, 85)),
2024-03-25 04:16:55 +00:00
)
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn identifier() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("x").unwrap()).unwrap()[0],
Statement::Expression(Expression::Identifier(
Identifier::new("x").with_position((0, 1))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("foobar").unwrap()).unwrap()[0],
Statement::Expression(Expression::Identifier(
Identifier::new("foobar").with_position((0, 6))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("HELLO").unwrap()).unwrap()[0],
Statement::Expression(Expression::Identifier(
Identifier::new("HELLO").with_position((0, 5))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn assignment() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("foobar = 1").unwrap()).unwrap()[0],
Statement::Assignment(
Assignment::new(
Identifier::new("foobar").with_position((0, 6)),
None,
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(
ValueNode::Integer(1).with_position((9, 10))
))
)
.with_position((0, 10)),
2024-03-25 04:16:55 +00:00
)
2024-03-17 12:30:46 +00:00
);
}
#[test]
2024-03-17 13:54:15 +00:00
fn assignment_with_type() {
2024-03-17 12:30:46 +00:00
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("foobar: int = 1").unwrap()).unwrap()[0],
Statement::Assignment(
Assignment::new(
Identifier::new("foobar").with_position((0, 6)),
Some(Type::Integer.with_position((8, 11))),
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(
ValueNode::Integer(1).with_position((14, 15))
))
)
.with_position((0, 15)),
2024-03-25 04:16:55 +00:00
)
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn logic() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("x == 1").unwrap()).unwrap()[0],
Statement::Expression(Expression::Logic(
Box::new(Logic::Equal(
Expression::Identifier(Identifier::new("x").with_position((0, 1))),
Expression::Value(ValueNode::Integer(1).with_position((5, 6))),
))
.with_position((0, 6))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("(x == 1) && (y == 2)").unwrap()).unwrap()[0],
Statement::Expression(Expression::Logic(
Box::new(Logic::And(
Expression::Logic(
Box::new(Logic::Equal(
Expression::Identifier(Identifier::new("x").with_position((1, 2))),
Expression::Value(ValueNode::Integer(1).with_position((6, 7))),
))
.with_position((1, 7))
2024-03-25 04:16:55 +00:00
),
Expression::Logic(
Box::new(Logic::Equal(
Expression::Identifier(Identifier::new("y").with_position((13, 14))),
Expression::Value(ValueNode::Integer(2).with_position((18, 19))),
))
.with_position((13, 19))
2024-03-25 04:16:55 +00:00
)
))
.with_position((0, 20))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("(x == 1) && (y == 2) && true").unwrap()).unwrap()[0],
Statement::Expression(Expression::Logic(
Box::new(Logic::And(
Expression::Logic(
Box::new(Logic::And(
Expression::Logic(
Box::new(Logic::Equal(
Expression::Identifier(
Identifier::new("x").with_position((1, 2))
),
Expression::Value(ValueNode::Integer(1).with_position((6, 7)))
))
.with_position((1, 7))
),
Expression::Logic(
Box::new(Logic::Equal(
Expression::Identifier(
Identifier::new("y").with_position((13, 14))
),
Expression::Value(
ValueNode::Integer(2).with_position((18, 19))
)
))
.with_position((13, 19))
),
))
.with_position((0, 20))
),
Expression::Value(ValueNode::Boolean(true).with_position((24, 28)))
))
.with_position((0, 28))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn list() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("[]").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::List(Vec::with_capacity(0)).with_position((0, 2))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("[42]").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::List(vec![Expression::Value(
ValueNode::Integer(42).with_position((1, 3))
2024-03-25 04:16:55 +00:00
)])
.with_position((0, 4))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("[42, 'foo', 'bar', [1, 2, 3,]]").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::List(vec![
Expression::Value(ValueNode::Integer(42).with_position((1, 3))),
Expression::Value(ValueNode::String("foo".to_string()).with_position((5, 10))),
Expression::Value(ValueNode::String("bar".to_string()).with_position((12, 17))),
Expression::Value(
ValueNode::List(vec![
Expression::Value(ValueNode::Integer(1).with_position((20, 21))),
Expression::Value(ValueNode::Integer(2).with_position((23, 24))),
Expression::Value(ValueNode::Integer(3).with_position((26, 27))),
])
.with_position((19, 29))
2024-03-25 04:16:55 +00:00
)
])
.with_position((0, 30))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn r#true() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("true").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Boolean(true).with_position((0, 4))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn r#false() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("false").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Boolean(false).with_position((0, 5))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn positive_float() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("0.0").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Float(0.0).with_position((0, 3))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("42.0").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Float(42.0).with_position((0, 4))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
let max_float = f64::MAX.to_string() + ".0";
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex(&max_float).unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Float(f64::MAX).with_position((0, 311))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
let min_positive_float = f64::MIN_POSITIVE.to_string();
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex(&min_positive_float).unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Float(f64::MIN_POSITIVE).with_position((0, 326))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn negative_float() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("-0.0").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Float(-0.0).with_position((0, 4))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("-42.0").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Float(-42.0).with_position((0, 5))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
let min_float = f64::MIN.to_string() + ".0";
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex(&min_float).unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Float(f64::MIN).with_position((0, 312))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
let max_negative_float = format!("-{}", f64::MIN_POSITIVE);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex(&max_negative_float).unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Float(-f64::MIN_POSITIVE).with_position((0, 327))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn other_float() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("Infinity").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Float(f64::INFINITY).with_position((0, 8))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("-Infinity").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Float(f64::NEG_INFINITY).with_position((0, 9))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
2024-03-25 04:16:55 +00:00
if let Statement::Expression(Expression::Value(WithPosition {
2024-04-22 07:41:21 +00:00
item: ValueNode::Float(float),
2024-03-25 04:16:55 +00:00
..
})) = &parse(&lex("NaN").unwrap()).unwrap()[0]
2024-03-17 12:30:46 +00:00
{
assert!(float.is_nan());
} else {
panic!("Expected a float.");
}
}
#[test]
fn positive_integer() {
for i in 0..10 {
let source = i.to_string();
let tokens = lex(&source).unwrap();
let statements = parse(&tokens).unwrap();
assert_eq!(
2024-03-25 04:16:55 +00:00
statements[0],
Statement::Expression(Expression::Value(
ValueNode::Integer(i).with_position((0, 1))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
)
}
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("42").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Integer(42).with_position((0, 2))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
let maximum_integer = i64::MAX.to_string();
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex(&maximum_integer).unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Integer(i64::MAX).with_position((0, 19))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn negative_integer() {
for i in -9..0 {
2024-03-17 12:30:46 +00:00
let source = i.to_string();
let tokens = lex(&source).unwrap();
let statements = parse(&tokens).unwrap();
assert_eq!(
2024-03-25 04:16:55 +00:00
statements[0],
Statement::Expression(Expression::Value(
ValueNode::Integer(i).with_position((0, 2))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
)
}
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("-42").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Integer(-42).with_position((0, 3))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
let minimum_integer = i64::MIN.to_string();
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex(&minimum_integer).unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::Integer(i64::MIN).with_position((0, 20))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn double_quoted_string() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("\"\"").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::String("".to_string()).with_position((0, 2))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("\"42\"").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::String("42".to_string()).with_position((0, 4))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("\"foobar\"").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::String("foobar".to_string()).with_position((0, 8))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn single_quoted_string() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("''").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::String("".to_string()).with_position((0, 2))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("'42'").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::String("42".to_string()).with_position((0, 4))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("'foobar'").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::String("foobar".to_string()).with_position((0, 8))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
}
#[test]
fn grave_quoted_string() {
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("``").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::String("".to_string()).with_position((0, 2))
2024-03-25 04:16:55 +00:00
))
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("`42`").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::String("42".to_string()).with_position((0, 4))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
assert_eq!(
2024-03-25 04:16:55 +00:00
parse(&lex("`foobar`").unwrap()).unwrap()[0],
Statement::Expression(Expression::Value(
ValueNode::String("foobar".to_string()).with_position((0, 8))
2024-03-25 04:16:55 +00:00
),)
2024-03-17 12:30:46 +00:00
);
}
2024-02-25 18:49:26 +00:00
}