Skip to content

Commit 7fd38af

Browse files
committed
add: feature-empty-collection-item
1 parent c44a275 commit 7fd38af

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/parser.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,24 @@ fn collection(tokens: &Vec<Token>) -> Result<Node, ParsingError> {
370370
// start and end positions.
371371
let mut pos = (0_usize, 0_usize);
372372
// in the seperate function, we're dealing with `{}}` or `{{}`, no need to deal with it here.
373-
let mut count = (0, 0);
373+
// count of OBra (`{`), CBra (`}`), and the seperator (`,`).
374+
let mut count = (0_usize, 0_usize, 0_usize);
374375
let mut collections: Vec<Vec<Token>> = vec![];
375376
let mut current = vec![];
376377
for token in tokens {
377378
match token {
378379
Token::Comma(s) if count.0 == (count.1 + 1) => {
380+
// increase the seperator count by 1.
381+
count.2 += 1;
379382
if current.is_empty() {
380-
return Err(ParsingError::InvalidCommaUsage(*s));
383+
match collections.len() == 0 {
384+
true => current.push(Token::Text(String::new(), s.clone())),
385+
// The previous token was comma.
386+
false => current.push(Token::Text(String::new(), s - 1)),
387+
}
381388
}
389+
// we dealt with if it's empty.
390+
// so it can't be empty.
382391
collections.push(current.clone());
383392
current.clear();
384393
}
@@ -404,9 +413,10 @@ fn collection(tokens: &Vec<Token>) -> Result<Node, ParsingError> {
404413
_ => current.push(token.clone()),
405414
}
406415
}
407-
if !current.is_empty() {
408-
collections.push(current);
416+
if current.is_empty() && collections.len() == count.2 {
417+
current.push(Token::Text(String::new(), pos.1 - 1));
409418
}
419+
collections.push(current);
410420
match collections.len() {
411421
0 => Err(ParsingError::NothingInBraces(pos.0)),
412422
1 => {

0 commit comments

Comments
 (0)