Skip to content

Commit 70137c5

Browse files
committed
lexer: recognize a preprocessor directive with no trailing newline
The cpp lexer's PREPRO pattern (`^#.-[^\\]\n`) requires a literal trailing newline, so a `#...` directive on the final line of the input without a newline never matched. It fell through to the `^.` catch-all and was tokenized as `#`, then the following words as identifiers, instead of a single `prepro` token. Add a PREPRO_EOF pattern (`^#.-[^\\]$`) tried right after PREPRO, so a trailing directive at end-of-input is recognized while newline-terminated and backslash-continued directives keep matching PREPRO first. Fixes #450
1 parent 4776299 commit 70137c5

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

lua/pl/lexer.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ local CHAR1 = "^''"
5151
local CHAR2 = [[^'(\*)%1']]
5252
local CHAR3 = [[^'.-[^\](\*)%1']]
5353
local PREPRO = '^#.-[^\\]\n'
54+
-- A preprocessor directive on the final line may have no trailing newline;
55+
-- match it up to the end of the input so it is still recognized (issue #450).
56+
local PREPRO_EOF = '^#.-[^\\]$'
5457

5558
local plain_matches,lua_matches,cpp_matches,lua_keyword,cpp_keyword
5659

@@ -393,6 +396,7 @@ function lexer.cpp(s,filter,options)
393396
cpp_matches = {
394397
{WSPACE,wsdump},
395398
{PREPRO,pdump},
399+
{PREPRO_EOF,pdump},
396400
{NUMBER3,ndump},
397401
{IDEN,cpp_vdump},
398402
{NUMBER4,ndump},

tests/test-lexer.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ test_scan([['' "" " \\" '\'' "'"]], nil, nil, {
121121
{'string', "'"}
122122
}, 'cpp')
123123

124+
-- A preprocessor directive on the final line without a trailing newline must
125+
-- still be recognized as a single 'prepro' token (issue #450).
126+
test_scan("#define ASDF", {}, nil, {
127+
{'prepro', '#define ASDF'}
128+
}, 'cpp')
129+
124130
local iter = lexer.lua([[
125131
foo
126132
bar

0 commit comments

Comments
 (0)