Skip to content

C++ lexer: ");" in function params breaks the lexer, rest of file is lexed as string #2331

Description

@KasperBasalte

Name of the lexer
C++ (Rouge::Lexers::Cpp).

Code sample

Foo f(");");
int after = 1;  // This line and everything below is lexed as a string.

I can reproduce it as follows:

docker run --rm -i ruby:3.3 sh -c '
    gem install rouge -q >/dev/null
    ruby -e "
      require \"rouge\"
      Rouge::Lexers::Cpp.new.lex(STDIN.read).each { |t, v| puts \"#{t.qualname}\t#{v.inspect}\" }
    "' << 'EOF'
Foo f(");");
int after = 1;
EOF

Output:

Name	"Foo"
Text	" "
Name.Function	"f"
Punctuation	"("
Literal.String	"\")"
Punctuation	";"
Literal.String	"\");"
Error	"\n"
Literal.String	"int after = 1;"
Error	"\n"

Additional context
Seems to be caused by this rule in state :root of lib/rouge/lexers/c.rb:

rule %r(
  ([\w*\s]+?[\s*]) # return arguments
  (#{id})          # function name
  (\s*\([^;]*?\))  # signature
  (#{ws}?)({|;)    # open brace or semicolon
)mx

It matches up to the first ) followed by ;, even when that ); is inside a string.

My suggestion is to make it string aware:

rule %r(
  ([\w*\s]+?[\s*]) # return arguments
  (#{id})          # function name
  (\s*\((?:"(?:[^"\\\n]|\\.)*"|[^;"'])*?\))  # signature
  (#{ws}?)({|;)    # open brace or semicolon
)mx

Note that this doesn't handle r-strings. I also noticed that multicharacters are not supported by the lexer. If they were ');' would also trip the lexer.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugfix-requestA request for a bugfix to be developed.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions