From 8a72b9308d01e7403ed67b98be63de231b1c11a9 Mon Sep 17 00:00:00 2001 From: Araozu Date: Tue, 28 Mar 2023 09:53:26 -0500 Subject: [PATCH] [Compiler] Fix error in val/var error messages when the offending token is a string --- compiler/src/syntax/binding.rs | 43 +++++++++---- compiler/src/token.rs | 13 ++++ doc-generator/static/index.html | 85 +++++++++++++++---------- doc-generator/static/styles/global.css | 6 ++ doc-generator/static/tailwind.config.js | 10 +++ 5 files changed, 113 insertions(+), 44 deletions(-) diff --git a/compiler/src/syntax/binding.rs b/compiler/src/syntax/binding.rs index 3232d6f..028e708 100644 --- a/compiler/src/syntax/binding.rs +++ b/compiler/src/syntax/binding.rs @@ -25,7 +25,9 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> Option } }; - // var/val keyword + /* + * val/var keyword + */ let (is_val, binding_token) = { let res1 = try_token_type(tokens, pos, TokenType::VAL); match res1 { @@ -42,6 +44,9 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> Option } }; + /* + * identifier + */ let identifier = match try_token_type(tokens, pos + 1, TokenType::Identifier) { Result3::Ok(t) => t, Result3::Err(t) => { @@ -52,11 +57,10 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> Option if is_val { "val" } else { "var" } ), error_start: t.position, - error_end: t.position + t.value.len(), + error_end: t.get_end_position(), })); } Result3::None => { - // TODO: Differentiate between no token found and incorrect token found. // The parser didn't find an Identifier after VAL/VAR return Some(SyntaxResult::Err(SyntaxError { reason: format!( @@ -64,29 +68,30 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> Option if is_val { "val" } else { "var" } ), error_start: binding_token.position, - error_end: binding_token.position + binding_token.value.len(), + error_end: binding_token.get_end_position(), })); } }; + /* + * Equal (=) operator + */ let _equal_operator: &Token = match try_operator(tokens, pos + 2, String::from("=")) { Result3::Ok(t) => t, Result3::Err(t) => { - // TODO: Differentiate between no token found and incorrect token found. - // The parser didn't find the `=` operator after the identifier + // The parser found a token, but it's not the `=` operator return Some(SyntaxResult::Err(SyntaxError { - reason: format!("There should be an equal sign `=` after the identifier",), - error_start: identifier.position, - error_end: identifier.position + identifier.value.len(), + reason: format!("There should be an equal sign `=` after the identifier"), + error_start: t.position, + error_end: t.get_end_position(), })); } Result3::None => { - // TODO: Differentiate between no token found and incorrect token found. // 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, - error_end: identifier.position + identifier.value.len(), + error_end: identifier.get_end_position(), })); } }; @@ -128,6 +133,7 @@ fn try_token_type(tokens: &Vec, pos: usize, token_type: TokenType) -> Res fn try_operator(tokens: &Vec, pos: usize, operator: String) -> Result3<&Token> { match tokens.get(pos) { Some(t) if t.token_type == TokenType::Operator && t.value == operator => Result3::Ok(t), + Some(t) if t.token_type == TokenType::Semicolon || t.token_type == TokenType::EOF => Result3::None, Some(t) => Result3::Err(t), None => Result3::None, } @@ -233,7 +239,6 @@ mod tests { } - // ERROR: when computing the length of the token "hello" the quotes are not considered let tokens = get_tokens(&String::from("val \"hello\"")).unwrap(); let binding = try_parse(&tokens, 0).unwrap(); @@ -245,4 +250,18 @@ mod tests { _ => panic!("Error expected") } } + + #[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); + } + _ => panic!("Error expected") + } + } } diff --git a/compiler/src/token.rs b/compiler/src/token.rs index 50e7a62..ee35d04 100755 --- a/compiler/src/token.rs +++ b/compiler/src/token.rs @@ -26,6 +26,19 @@ pub struct Token { pub position: usize, } +impl Token { + pub fn get_end_position(&self) -> usize { + match self.token_type { + TokenType::String => { + self.position + self.value.len() + 2 + } + _ => { + self.position + self.value.len() + } + } + } +} + pub fn new_eof(position: usize) -> Token { Token { token_type: TokenType::EOF, diff --git a/doc-generator/static/index.html b/doc-generator/static/index.html index b163a99..f9f053a 100644 --- a/doc-generator/static/index.html +++ b/doc-generator/static/index.html @@ -12,53 +12,74 @@ - + - -
-
- Misti logo -
- -

- A type-safe, consistent, compiled language for JS + + + +
+ +

+ Typed. +
+ Expressive. +
+ Consistent. +
+ Friendly.

+

+ Misti is yet another programming language that compiles to JavaScript. +

+ - + +
+ + diff --git a/doc-generator/static/styles/global.css b/doc-generator/static/styles/global.css index 8b3cca6..40a9521 100644 --- a/doc-generator/static/styles/global.css +++ b/doc-generator/static/styles/global.css @@ -29,6 +29,9 @@ body ::selection { /* C2 Material Colors */ --c2-primary: #90ccff; + --c2-on-primary: #003351; + --c2-primary-container: #004b72; + --c2-on-primary-container: #cce5ff; /* C3 Material Colors */ --c3-primary: #ffaed7; @@ -64,6 +67,9 @@ body ::selection { /* C2 Material Colors */ --c2-primary: #006397; + --c2-on-primary: #ffffff; + --c2-primary-container: #cce5ff; + --c2-on-primary-container: #001e31; /* C3 Material Colors */ --c3-primary: #954170; diff --git a/doc-generator/static/tailwind.config.js b/doc-generator/static/tailwind.config.js index 1f71515..3cc5810 100644 --- a/doc-generator/static/tailwind.config.js +++ b/doc-generator/static/tailwind.config.js @@ -5,6 +5,7 @@ module.exports = { colors: { transparent: 'transparent', current: 'currentColor', + white: "#ffffff", "js-color": "var(--js-color)", "dark-color": "var(--dark-color)", @@ -22,8 +23,14 @@ module.exports = { "code-bg-color": "var(--code-bg-color)", "code-color": "var(--code-color)", "border-color": "var(--border-color)", + "c1-primary": "var(--c1-primary)", + "c2-primary": "var(--c2-primary)", + "c2-on-primary": "var(--c2-on-primary)", + "c2-primary-container": "var(--c2-primary-container)", + "c2-on-primary-container": "var(--c2-on-primary-container)", + "c3-primary": "var(--c3-primary)", "c3-on-primary": "var(--c3-on-primary)", "c3-primary-container": "var(--c3-primary-container)", @@ -35,6 +42,9 @@ module.exports = { "c5-on-primary-container": "var(--c5-on-primary-container)", "bg-color": "var(--bg-color)", }, + borderWidth: { + '1': '1px' + }, extend: {}, }, plugins: [],