diff --git a/grammar.js b/grammar.js index d49fbec..db1ff01 100644 --- a/grammar.js +++ b/grammar.js @@ -46,7 +46,7 @@ module.exports = grammar({ ], rules: { - source_file: $ => $._block, + source_file: $ => optional($._block), _block: $ => seq( $._block_start, diff --git a/src/grammar.json b/src/grammar.json index 99f81a3..d12633d 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -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", diff --git a/src/scanner.cc b/src/scanner.cc index b555f4b..929e39d 100644 --- a/src/scanner.cc +++ b/src/scanner.cc @@ -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(); diff --git a/test/corpus/nesting.txt b/test/corpus/nesting.txt index 314a23b..4acaa02 100644 --- a/test/corpus/nesting.txt +++ b/test/corpus/nesting.txt @@ -52,3 +52,18 @@ p (nested (element (tag_name))))) + +========== +Empty file +========== +--- + +(source_file) + +============================== +Empty file with one empty line +============================== + +--- + +(source_file)