Skip to content

Commit

Permalink
Merge pull request #6 from Jkutkut/3-empty-line-with-content-line
Browse files Browse the repository at this point in the history
bugfix: lexer cleaning logic refactored
  • Loading branch information
Jkutkut authored Jun 5, 2024
2 parents be74d77 + 78a8da4 commit 3ba58ea
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "osmia"
version = "1.10.0"
version = "1.10.1"
edition = "2021"
authors = ["Jkutkut"]

Expand Down
64 changes: 37 additions & 27 deletions src/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,39 +124,49 @@ impl<'a> Lexer<'a> {
}

fn clean_tokens(tokens: &mut Vec<Token>) {
let mut i = 0;
while i < tokens.len() - 1 {
i += 1;
if !Self::is_new_line_token(&tokens[i - 1]) {
continue;
let mut i: usize = tokens.len() - 1;
let mut end: usize;
while i > 0 {
end = i;
while i > 0 {
i -= 1;
if Self::is_new_line_token(&tokens[i]) {
break;
}
}
if !Self::is_whitespace_token(&tokens[i]) {
continue;
if Self::is_new_line_token(&tokens[i]) {
i += 1;
}
let mut j = 1;
let mut is_printable_token_in_row = false;
while i + j < tokens.len() - 1 {
if Self::is_new_line_token(&tokens[i + j]) {
break;
let line = &tokens[i..end + 1];
let mut row_has_printable_token = false;
let mut k = 0;
while k < line.len() - 1 && !row_has_printable_token {
match &line[k] {
Token::Raw(_) => row_has_printable_token = !Self::is_whitespace_token(&line[k]),
Token::DelimiterStart => {
match &line[k + 1] {
Token::Value(_) => row_has_printable_token = true,
_ => ()
}
},
_ => ()
}
else if let Token::Raw(_) = &tokens[i + j] {
is_printable_token_in_row = true;
break;
k += 1;
}
if !row_has_printable_token {
let mut ts: Vec<Token> = Vec::new();
let mut line = line.to_vec();
if Self::is_new_line_token(&tokens[end]) {
ts.push(tokens.remove(end));
line.remove(line.len() - 1);
}
else if let Token::DelimiterStart = &tokens[i + j] {
match &tokens[i + j + 1] {
Token::Value(_) => {
is_printable_token_in_row = true;
break;
},
_ => ()
}
if Self::is_whitespace_token(&tokens[i]) {
ts.push(tokens.remove(i));
line.remove(0);
}
j += 1;
}
if !is_printable_token_in_row {
tokens.drain(i - 1..i + 1);
i = i + j - 2;
if i > 0 {
i -= 1;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ macro_tests!(
{{continue}}{{break}}
"#,
vec![
Token::Raw("\n".to_string()),
Token::DelimiterStart,
Token::Print,
Token::DelimiterEnd,
Expand Down
65 changes: 62 additions & 3 deletions src/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ macro_tests!(
"#),
"Hello, world!
<a href=\"https://example01.com\">Example01</a>
<a href=\"https://example02.org\">Example02</a>"
<a href=\"https://example02.org\">Example02</a>\n"
),
(
foreach02,
Expand All @@ -155,7 +155,7 @@ macro_tests!(
"Hello, world!
<li>Element 1</li>
<li>Element 2</li>
<li>Element 3</li>"
<li>Element 3</li>\n"
),
(
foreach03,
Expand Down Expand Up @@ -229,7 +229,7 @@ macro_tests!(
"#),
"Hey!
True!
var: value"
var: value\n"
),
(
spacing01,
Expand Down Expand Up @@ -416,5 +416,64 @@ Jack is an adult.
}
"#),
"\n\n\n\t\t7\t\t\t\t\n\t\t7"
),
(
gh_03_08,
r#"
{{for page in pages}}
{{if page.disabled}}
{{fi}}
# {{page.endpoint}}
{{done}}"#,
Some(r#"
{
"pages": [
{
"disabled": true,
"endpoint": "foo"
},
{
"disabled": false,
"endpoint": "bar"
},
{
"disabled": false,
"endpoint": "baz"
}
]
}
"#),
"\n # foo\n # bar\n # baz\n"
),
(
gh_03_09,
r#" {{for page in pages}}{{if page.disabled}}{{fi}}
# {{page.endpoint}}
{{done}}"#,
Some(r#"
{
"pages": [
{
"disabled": true,
"endpoint": "foo"
},
{
"disabled": false,
"endpoint": "bar"
},
{
"disabled": false,
"endpoint": "baz"
}
]
}
"#),
" # foo\n # bar\n # baz\n"
),
(
gh_03_10,
"\n\n\n\n\n {{v}}",
Some(r#"{"v": "foo"}"#),
"\n\n\n\n\n foo"
)
);

0 comments on commit 3ba58ea

Please sign in to comment.