Skip to content

Commit

Permalink
Support parsing of empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
kolen committed Feb 17, 2024
1 parent 73f2c13 commit 15daba0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = grammar({
],

rules: {
source_file: $ => $._block,
source_file: $ => optional($._block),

_block: $ => seq(
$._block_start,
Expand Down
12 changes: 10 additions & 2 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
"name": "slim",
"rules": {
"source_file": {
"type": "SYMBOL",
"name": "_block"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_block"
},
{
"type": "BLANK"
}
]
},
"_block": {
"type": "SEQ",
Expand Down
9 changes: 7 additions & 2 deletions src/scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@ class Scanner {
if (indents.empty()) {
// Found initial indentation level
indents.push_back(indent_length);
lexer->result_symbol = BLOCK_START;
return true;

if (valid_symbols[BLOCK_START] && !lexer->eof(lexer)) {
lexer->result_symbol = BLOCK_START;
return true;
} else {
return false;
}
}

int last_indent_length = (int)indents.back();
Expand Down
15 changes: 15 additions & 0 deletions test/corpus/nesting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,18 @@ p
(nested
(element
(tag_name)))))

==========
Empty file
==========
---

(source_file)

==============================
Empty file with one empty line
==============================

---

(source_file)

0 comments on commit 15daba0

Please sign in to comment.