Skip to content

Commit

Permalink
Fix CFamilyLexer preprocessor tokenization errors (#1830)
Browse files Browse the repository at this point in the history
CFamilyLexer failed to tokenize preprocessor macros when they were
preceded by line break surrounded by spaces. This was the case because
prerpocessor regex rule expected to start at the beginning of the line,
but the space regex rule matched also the whitespace after the line
break. Now the space rule has been refined not to match the line break.
Because of this, the preprocessor regex rule correctly matches
prerpocessor tokens even when they are preceded by white spaces, at the
cost of adding some more tokens in the token stream in some cases. This
change preserves the behavior of invalid preprocessor usage failing to
tokenize.
  • Loading branch information
henkkuli committed Jun 20, 2021
1 parent a65613e commit fea1fbc
Show file tree
Hide file tree
Showing 19 changed files with 913 additions and 226 deletions.
2 changes: 1 addition & 1 deletion pygments/lexers/c_cpp.py
Expand Up @@ -56,7 +56,7 @@ class CFamilyLexer(RegexLexer):
('^(' + _ws1 + ')(#)',
bygroups(using(this), Comment.Preproc), 'macro'),
(r'\n', Text),
(r'\s+', Text),
(r'[^\S\n]+', Text),
(r'\\\n', Text), # line continuation
(r'//(\n|[\w\W]*?[^\\]\n)', Comment.Single),
(r'/(\\\n)?[*][\w\W]*?[*](\\\n)?/', Comment.Multiline),
Expand Down
6 changes: 4 additions & 2 deletions tests/examplefiles/arduino/Blink.ino.output

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

40 changes: 32 additions & 8 deletions tests/examplefiles/c/example.c.output

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

5 changes: 4 additions & 1 deletion tests/examplefiles/charmci/Charmci.ci.output

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

25 changes: 20 additions & 5 deletions tests/examplefiles/cpp/example.cpp.output

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

3 changes: 2 additions & 1 deletion tests/examplefiles/cpp/namespace.cpp.output

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

5 changes: 4 additions & 1 deletion tests/examplefiles/cuda/test.cu.output

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

0 comments on commit fea1fbc

Please sign in to comment.