2023-09-08 01:50:51 +00:00
|
|
|
use super::ast::{Binding, ValBinding, VarBinding};
|
2023-03-14 21:10:43 +00:00
|
|
|
use super::{expression, SyntaxResult};
|
2023-03-15 21:33:00 +00:00
|
|
|
use crate::error_handling::SyntaxError;
|
2023-09-08 01:46:11 +00:00
|
|
|
use crate::lexic::token::{Token, TokenType};
|
2023-03-16 18:31:24 +00:00
|
|
|
use crate::utils::Result3;
|
2023-01-08 23:09:06 +00:00
|
|
|
|
2023-02-15 21:17:50 +00:00
|
|
|
// TODO: Should return a 3 state value:
|
2023-01-08 23:09:06 +00:00
|
|
|
// - Success: binding parsed successfully
|
|
|
|
// - NotFound: the first token (var | val) was not found, so the parser should try other options
|
|
|
|
// - Error: token (var | val) was found, but then other expected tokens were not found
|
2023-03-14 21:10:43 +00:00
|
|
|
pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> Option<SyntaxResult> {
|
2023-02-15 21:17:50 +00:00
|
|
|
let mut pos = pos;
|
2023-03-14 21:10:43 +00:00
|
|
|
|
2023-02-15 21:17:50 +00:00
|
|
|
// Optional datatype annotation
|
|
|
|
let datatype_annotation = {
|
2023-03-16 18:31:24 +00:00
|
|
|
match try_token_type(tokens, pos, TokenType::Datatype) {
|
|
|
|
Result3::Ok(t) => {
|
2023-02-15 21:17:50 +00:00
|
|
|
pos += 1;
|
|
|
|
Some(String::from(&t.value))
|
|
|
|
}
|
2023-03-16 18:31:24 +00:00
|
|
|
Result3::Err(_) => None,
|
|
|
|
Result3::None => panic!(
|
2023-03-15 21:33:00 +00:00
|
|
|
"Internal compiler error: Illegal token stream at src/syntax/binding.rs#try_parse"
|
|
|
|
),
|
2023-02-15 21:17:50 +00:00
|
|
|
}
|
|
|
|
};
|
2023-03-14 21:10:43 +00:00
|
|
|
|
2023-03-28 14:53:26 +00:00
|
|
|
/*
|
|
|
|
* val/var keyword
|
|
|
|
*/
|
2023-03-15 21:33:00 +00:00
|
|
|
let (is_val, binding_token) = {
|
2023-02-09 23:44:31 +00:00
|
|
|
let res1 = try_token_type(tokens, pos, TokenType::VAL);
|
|
|
|
match res1 {
|
2023-03-16 18:31:24 +00:00
|
|
|
Result3::Ok(val_token) => (true, val_token),
|
|
|
|
_ => {
|
2023-02-09 23:44:31 +00:00
|
|
|
let res2 = try_token_type(tokens, pos, TokenType::VAR);
|
|
|
|
match res2 {
|
2023-03-16 18:31:24 +00:00
|
|
|
Result3::Ok(var_token) => (false, var_token),
|
2023-03-15 21:33:00 +00:00
|
|
|
// Neither VAL nor VAR were matched, the parser should try
|
|
|
|
// other constructs
|
2023-03-16 18:31:24 +00:00
|
|
|
_ => return None,
|
2023-02-09 23:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-01-08 23:09:06 +00:00
|
|
|
|
2023-03-28 14:53:26 +00:00
|
|
|
/*
|
|
|
|
* identifier
|
|
|
|
*/
|
2023-03-16 18:31:24 +00:00
|
|
|
let identifier = match try_token_type(tokens, pos + 1, TokenType::Identifier) {
|
|
|
|
Result3::Ok(t) => t,
|
|
|
|
Result3::Err(t) => {
|
|
|
|
// The parser found a token, but it's not an identifier
|
|
|
|
return Some(SyntaxResult::Err(SyntaxError {
|
|
|
|
reason: format!(
|
|
|
|
"There should be an identifier after a `{}` token",
|
|
|
|
if is_val { "val" } else { "var" }
|
|
|
|
),
|
2023-03-27 14:41:16 +00:00
|
|
|
error_start: t.position,
|
2023-03-28 14:53:26 +00:00
|
|
|
error_end: t.get_end_position(),
|
2023-03-16 18:31:24 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
Result3::None => {
|
|
|
|
// The parser didn't find an Identifier after VAL/VAR
|
|
|
|
return Some(SyntaxResult::Err(SyntaxError {
|
|
|
|
reason: format!(
|
|
|
|
"There should be an identifier after a `{}` token",
|
|
|
|
if is_val { "val" } else { "var" }
|
|
|
|
),
|
|
|
|
error_start: binding_token.position,
|
2023-03-28 14:53:26 +00:00
|
|
|
error_end: binding_token.get_end_position(),
|
2023-03-16 18:31:24 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
};
|
2023-01-08 23:09:06 +00:00
|
|
|
|
2023-03-28 14:53:26 +00:00
|
|
|
/*
|
|
|
|
* Equal (=) operator
|
|
|
|
*/
|
2023-03-28 15:06:23 +00:00
|
|
|
let equal_operator: &Token = match try_operator(tokens, pos + 2, String::from("=")) {
|
2023-03-16 18:31:24 +00:00
|
|
|
Result3::Ok(t) => t,
|
|
|
|
Result3::Err(t) => {
|
2023-03-28 14:53:26 +00:00
|
|
|
// The parser found a token, but it's not the `=` operator
|
2023-03-16 18:31:24 +00:00
|
|
|
return Some(SyntaxResult::Err(SyntaxError {
|
2023-03-28 14:53:26 +00:00
|
|
|
reason: format!("There should be an equal sign `=` after the identifier"),
|
|
|
|
error_start: t.position,
|
|
|
|
error_end: t.get_end_position(),
|
2023-03-16 18:31:24 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
Result3::None => {
|
|
|
|
// The parser didn't find the `=` operator after the identifier
|
|
|
|
return Some(SyntaxResult::Err(SyntaxError {
|
|
|
|
reason: format!("There should be an equal sign `=` after the identifier",),
|
|
|
|
error_start: identifier.position,
|
2023-03-28 14:53:26 +00:00
|
|
|
error_end: identifier.get_end_position(),
|
2023-03-16 18:31:24 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
};
|
2023-01-08 23:09:06 +00:00
|
|
|
|
|
|
|
let expression = expression::try_parse(tokens, pos + 3);
|
2023-03-14 21:10:43 +00:00
|
|
|
if expression.is_none() {
|
2023-03-28 15:06:23 +00:00
|
|
|
return Some(SyntaxResult::Err(SyntaxError {
|
|
|
|
reason: String::from("Expected an expression after the equal `=` operator"),
|
|
|
|
error_start: equal_operator.position,
|
|
|
|
error_end: equal_operator.get_end_position(),
|
|
|
|
}));
|
2023-03-14 21:10:43 +00:00
|
|
|
}
|
2023-01-08 23:09:06 +00:00
|
|
|
let expression = expression.unwrap();
|
|
|
|
|
2023-03-14 21:10:43 +00:00
|
|
|
let binding = if is_val {
|
|
|
|
Binding::Val(ValBinding {
|
2023-02-15 21:17:50 +00:00
|
|
|
datatype: datatype_annotation,
|
2023-09-08 01:32:59 +00:00
|
|
|
identifier: Box::new(identifier.value.clone()),
|
2023-02-09 23:44:31 +00:00
|
|
|
expression,
|
2023-03-14 21:10:43 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Binding::Var(VarBinding {
|
2023-02-15 21:17:50 +00:00
|
|
|
datatype: datatype_annotation,
|
2023-09-08 01:32:59 +00:00
|
|
|
identifier: Box::new(identifier.value.clone()),
|
2023-02-09 23:44:31 +00:00
|
|
|
expression,
|
2023-03-14 21:10:43 +00:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2023-09-09 01:28:53 +00:00
|
|
|
Some(SyntaxResult::Ok(
|
|
|
|
super::ast::TopLevelConstruct::Binding(binding)
|
|
|
|
))
|
2023-01-08 23:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 18:31:24 +00:00
|
|
|
/// Expects the token at `pos` to be of type `token_type`
|
|
|
|
fn try_token_type(tokens: &Vec<Token>, pos: usize, token_type: TokenType) -> Result3<&Token> {
|
|
|
|
match tokens.get(pos) {
|
|
|
|
Some(t) if t.token_type == token_type => Result3::Ok(t),
|
2023-09-10 16:16:34 +00:00
|
|
|
Some(t) if t.token_type == TokenType::NewLine || t.token_type == TokenType::EOF => {
|
2023-04-05 15:31:12 +00:00
|
|
|
Result3::None
|
|
|
|
}
|
2023-03-16 18:31:24 +00:00
|
|
|
Some(t) => Result3::Err(t),
|
|
|
|
None => Result3::None,
|
|
|
|
}
|
2023-01-08 23:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 18:31:24 +00:00
|
|
|
fn try_operator(tokens: &Vec<Token>, pos: usize, operator: String) -> Result3<&Token> {
|
|
|
|
match tokens.get(pos) {
|
|
|
|
Some(t) if t.token_type == TokenType::Operator && t.value == operator => Result3::Ok(t),
|
2023-09-10 16:16:34 +00:00
|
|
|
Some(t) if t.token_type == TokenType::NewLine || t.token_type == TokenType::EOF => {
|
2023-04-05 15:31:12 +00:00
|
|
|
Result3::None
|
|
|
|
}
|
2023-03-16 18:31:24 +00:00
|
|
|
Some(t) => Result3::Err(t),
|
|
|
|
None => Result3::None,
|
|
|
|
}
|
2023-01-08 23:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2023-09-09 01:17:46 +00:00
|
|
|
use crate::{lexic::get_tokens, syntax::ast::TopLevelConstruct};
|
2023-01-08 23:09:06 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_parse_val_binding() {
|
|
|
|
let tokens = get_tokens(&String::from("val identifier = 20")).unwrap();
|
|
|
|
let binding = try_parse(&tokens, 0).unwrap();
|
|
|
|
|
|
|
|
match binding {
|
2023-09-09 01:17:46 +00:00
|
|
|
SyntaxResult::Ok(TopLevelConstruct::Binding(Binding::Val(binding))) => {
|
2023-09-08 01:32:59 +00:00
|
|
|
assert_eq!("identifier", format!("{}", binding.identifier));
|
2023-01-08 23:09:06 +00:00
|
|
|
}
|
2023-03-14 21:10:43 +00:00
|
|
|
_ => panic!(),
|
2023-01-08 23:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_parse_val() {
|
|
|
|
let tokens = get_tokens(&String::from("val")).unwrap();
|
2023-03-16 18:31:24 +00:00
|
|
|
let token = *try_token_type(&tokens, 0, TokenType::VAL).unwrap();
|
2023-01-08 23:09:06 +00:00
|
|
|
|
|
|
|
assert_eq!(TokenType::VAL, token.token_type);
|
|
|
|
assert_eq!("val", token.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_parse_identifier() {
|
|
|
|
let tokens = get_tokens(&String::from("identifier")).unwrap();
|
2023-03-16 18:31:24 +00:00
|
|
|
let token = *try_token_type(&tokens, 0, TokenType::Identifier).unwrap();
|
2023-01-08 23:09:06 +00:00
|
|
|
|
|
|
|
assert_eq!("identifier", token.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_parse_operator() {
|
|
|
|
let tokens = get_tokens(&String::from("=")).unwrap();
|
2023-03-16 18:31:24 +00:00
|
|
|
let token = *try_operator(&tokens, 0, String::from("=")).unwrap();
|
2023-01-08 23:09:06 +00:00
|
|
|
|
|
|
|
assert_eq!("=", token.value);
|
|
|
|
}
|
2023-03-14 21:10:43 +00:00
|
|
|
|
2023-02-15 21:17:50 +00:00
|
|
|
#[test]
|
|
|
|
fn should_parse_binding_with_datatype() {
|
|
|
|
let tokens = get_tokens(&String::from("Num val identifier = 20")).unwrap();
|
|
|
|
let binding = try_parse(&tokens, 0).unwrap();
|
|
|
|
|
|
|
|
match binding {
|
2023-09-09 01:17:46 +00:00
|
|
|
SyntaxResult::Ok(TopLevelConstruct::Binding(Binding::Val(binding))) => {
|
2023-02-15 21:17:50 +00:00
|
|
|
assert_eq!(Some(String::from("Num")), binding.datatype);
|
2023-09-08 01:32:59 +00:00
|
|
|
assert_eq!("identifier", format!("{}", binding.identifier));
|
2023-02-15 21:17:50 +00:00
|
|
|
}
|
2023-03-14 21:10:43 +00:00
|
|
|
_ => panic!(),
|
2023-02-15 21:17:50 +00:00
|
|
|
}
|
2023-03-14 21:10:43 +00:00
|
|
|
|
2023-02-15 21:17:50 +00:00
|
|
|
let tokens = get_tokens(&String::from("Bool var identifier = true")).unwrap();
|
|
|
|
let binding = try_parse(&tokens, 0).unwrap();
|
|
|
|
|
|
|
|
match binding {
|
2023-09-09 01:17:46 +00:00
|
|
|
SyntaxResult::Ok(TopLevelConstruct::Binding(Binding::Var(binding))) => {
|
2023-02-15 21:17:50 +00:00
|
|
|
assert_eq!(Some(String::from("Bool")), binding.datatype);
|
2023-09-08 01:32:59 +00:00
|
|
|
assert_eq!("identifier", format!("{}", binding.identifier));
|
2023-02-15 21:17:50 +00:00
|
|
|
}
|
2023-09-09 01:17:46 +00:00
|
|
|
_ => panic!("D: {:?}", binding),
|
2023-02-15 21:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-15 21:33:00 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_return_correct_error() {
|
|
|
|
let tokens = get_tokens(&String::from("val")).unwrap();
|
|
|
|
assert_eq!(TokenType::VAL, tokens[0].token_type);
|
|
|
|
assert_eq!(0, tokens[0].position);
|
|
|
|
let binding = try_parse(&tokens, 0).unwrap();
|
|
|
|
|
|
|
|
match binding {
|
|
|
|
SyntaxResult::Err(error) => {
|
|
|
|
assert_eq!(0, error.error_start);
|
|
|
|
assert_eq!(3, error.error_end);
|
|
|
|
}
|
2023-03-16 18:31:24 +00:00
|
|
|
_ => panic!("Error expected"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_return_error_when_identifier_is_wrong() {
|
|
|
|
let tokens = get_tokens(&String::from("val 322")).unwrap();
|
|
|
|
assert_eq!(TokenType::VAL, tokens[0].token_type);
|
|
|
|
assert_eq!(0, tokens[0].position);
|
|
|
|
let binding = try_parse(&tokens, 0).unwrap();
|
|
|
|
|
|
|
|
match binding {
|
|
|
|
SyntaxResult::Err(error) => {
|
2023-03-27 14:41:16 +00:00
|
|
|
assert_eq!(4, error.error_start);
|
|
|
|
assert_eq!(7, error.error_end);
|
|
|
|
}
|
2023-04-05 15:31:12 +00:00
|
|
|
_ => panic!("Error expected"),
|
2023-03-27 14:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let tokens = get_tokens(&String::from("val \"hello\"")).unwrap();
|
|
|
|
let binding = try_parse(&tokens, 0).unwrap();
|
|
|
|
|
|
|
|
match binding {
|
|
|
|
SyntaxResult::Err(error) => {
|
|
|
|
assert_eq!(4, error.error_start);
|
|
|
|
assert_eq!(11, error.error_end);
|
2023-03-16 18:31:24 +00:00
|
|
|
}
|
2023-04-05 15:31:12 +00:00
|
|
|
_ => panic!("Error expected"),
|
2023-03-15 21:33:00 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-28 14:53:26 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_return_error_when_equal_op_is_wrong() {
|
|
|
|
let tokens = get_tokens(&String::from("val id \"error\"")).unwrap();
|
|
|
|
let binding = try_parse(&tokens, 0).unwrap();
|
|
|
|
|
|
|
|
match binding {
|
|
|
|
SyntaxResult::Err(error) => {
|
|
|
|
assert_eq!(7, error.error_start);
|
|
|
|
assert_eq!(14, error.error_end);
|
|
|
|
}
|
2023-04-05 15:31:12 +00:00
|
|
|
_ => panic!("Error expected"),
|
2023-03-28 14:53:26 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-08 23:09:06 +00:00
|
|
|
}
|