Skip to content

Commit

Permalink
CFamilyLexer: Fix matching of function parameters
Browse files Browse the repository at this point in the history
This fixes an issue where in code like this:

```
int foo(float bar) // hello() {}
```

The lexer would match `(float bar) // hello()`
as the parameters of the function `foo`, instead
of just `(float bar)`.

In addition, a similar test case to what was originally
reported in pygments#2208 is added.
  • Loading branch information
amitkummer committed Aug 16, 2022
1 parent d6968f8 commit db86c20
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 9 deletions.
16 changes: 8 additions & 8 deletions pygments/lexers/c_cpp.py
Expand Up @@ -129,22 +129,22 @@ class CFamilyLexer(RegexLexer):
include('keywords'),
# functions
(r'(' + _namespaced_ident + r'(?:[&*\s])+)' # return arguments
r'(' + _possible_comments + r')' # possible comments
r'(' + _possible_comments + r')'
r'(' + _namespaced_ident + r')' # method name
r'(' + _possible_comments + r')' # possible comments
r'(\([^;"\']*?\))' # signature
r'(' + _possible_comments + r')' # possible comments
r'(' + _possible_comments + r')'
r'(\([^;"\')]*?\))' # signature
r'(' + _possible_comments + r')'
r'([^;{/"\']*)(\{)',
bygroups(using(this), using(this, state='whitespace'), Name.Function, using(this, state='whitespace'),
using(this), using(this, state='whitespace'), using(this), Punctuation),
'function'),
# function declarations
(r'(' + _namespaced_ident + r'(?:[&*\s])+)' # return arguments
r'(' + _possible_comments + r')' # possible comments
r'(' + _possible_comments + r')'
r'(' + _namespaced_ident + r')' # method name
r'(' + _possible_comments + r')' # possible comments
r'(\([^;"\']*?\))' # signature
r'(' + _possible_comments + r')' # possible comments
r'(' + _possible_comments + r')'
r'(\([^;"\')]*?\))' # signature
r'(' + _possible_comments + r')'
r'([^;/"\']*)(;)',
bygroups(using(this), using(this, state='whitespace'), Name.Function, using(this, state='whitespace'),
using(this), using(this, state='whitespace'), using(this), Punctuation)),
Expand Down
18 changes: 17 additions & 1 deletion tests/examplefiles/cpp/functions.cpp
Expand Up @@ -87,4 +87,20 @@ class raz {
// Make sure these are not functions:
else if(flag && func_call()) {}
new T();
const operator int() const {} // so int is lexed as type and not function.name
const operator int() const {} // so int is lexed as type and not function.name

class foo bar : public raz
{
Q_OBJECT
/// \cond INCLUDE_QPROPERTIES
Q_PROPERTY(arg1 arg2)
/// \endcond

public:
/*!
Lorem ipsum core vanditi.
*/
enum duck { dog, // Comment.
cat // Comment (see \ref replot).
};
};
73 changes: 73 additions & 0 deletions tests/examplefiles/cpp/functions.cpp.output

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

0 comments on commit db86c20

Please sign in to comment.