Skip to content

Commit

Permalink
CFamily: improve label detection (#2022)
Browse files Browse the repository at this point in the history
* Remove unused variable

This variable is unused since the first commit to this file
in the commit history, so it's probably safe to remove it.

* Lex identifier as label only if it's at line start

* Stop matching identifiers that begin with a digit

This is so we don't match numbers as labels with the new labels rule.

* Add label tests

* Fix existing tests
  • Loading branch information
amitkummer committed Jan 6, 2022
1 parent 8fdd6b9 commit bd6d682
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 54 deletions.
10 changes: 4 additions & 6 deletions pygments/lexers/c_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class CFamilyLexer(RegexLexer):
definitions.
"""

#: optional Comment or Whitespace
_ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'

# The trailing ?, rather than *, avoids a geometric performance drop here.
#: only one /* */ style comment
_ws1 = r'\s*(?:/[*].*?[*]/\s*)?'
Expand All @@ -42,8 +39,8 @@ class CFamilyLexer(RegexLexer):
_intsuffix = r'(([uU][lL]{0,2})|[lL]{1,2}[uU]?)?'

# Identifier regex with C and C++ Universal Character Name (UCN) support.
_ident = r'(?:[\w$]|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8})+'
_namespaced_ident = r'(?:[\w$]|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|::)+'
_ident = r'(?!\d)(?:[\w$]|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8})+'
_namespaced_ident = r'(?!\d)(?:[\w$]|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|::)+'

tokens = {
'whitespace': [
Expand All @@ -55,6 +52,8 @@ class CFamilyLexer(RegexLexer):
bygroups(using(this), Comment.Preproc), 'if0'),
('^(' + _ws1 + ')(#)',
bygroups(using(this), Comment.Preproc), 'macro'),
(r'(^[ \t]*)(?!(?:public|private|protected|default)\b)(case\b\s+)?(' + _ident + r')(\s*)(:)(?!:)',
bygroups(Whitespace, using(this), Name.Label, Whitespace, Punctuation)),
(r'\n', Whitespace),
(r'[^\S\n]+', Whitespace),
(r'\\\n', Text), # line continuation
Expand Down Expand Up @@ -82,7 +81,6 @@ class CFamilyLexer(RegexLexer):
(r'[~!%^&*+=|?:<>/-]', Operator),
(r'[()\[\],.]', Punctuation),
(r'(true|false|NULL)\b', Name.Builtin),
(r'(' + _ident + r')(\s*)(:)(?!:)', bygroups(Name.Label, Whitespace, Punctuation)),
(_ident, Name)
],
'types': [
Expand Down
4 changes: 2 additions & 2 deletions tests/examplefiles/c/ceval.c.output

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

4 changes: 2 additions & 2 deletions tests/examplefiles/c/example.c.output

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

14 changes: 14 additions & 0 deletions tests/examplefiles/c/labels.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
int id() {
id:
int i = true ? 1 : 2;
default_value:

switch (2) {
case Qfalse:
break;
case Qnil:
return Qnil;
default:
return 0;
}
}
94 changes: 94 additions & 0 deletions tests/examplefiles/c/labels.c.output

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

4 changes: 2 additions & 2 deletions tests/examplefiles/charmci/Charmci.ci.output

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

64 changes: 32 additions & 32 deletions tests/examplefiles/ec/test.ec.output

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

8 changes: 4 additions & 4 deletions tests/examplefiles/objective-c/objc_example.m.output

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

Loading

0 comments on commit bd6d682

Please sign in to comment.