Sublime Forum

[BUG] C++ Variable Declaration Syntax Hilighting

#1

I like to declare my variables and then assign values with parenthesis, but the syntax hilighting is mistaking multiple declarations on a line as passing values to functions.


int foobar(21);
//instead of
int foobar = 21;
//but when I declare two same-type variables on a single line it thinks anything after the first is a function
//and that's just silly
int foo(21), bar(32);

Is there a way for me to fix this error in the colorscheme file for monokai, or is it something that will have to be fixed by jps?

0 Likes

#2

Bumping, because the C++ syntax highlighting is screwy in other places too.

when declaring a pointer using parenthesis it thinks I’m declaring a function, the syntax highlighting needs to be changed so it will only highlight as a function after an open curly brace follows it (indicating a code block for the function statements)

0 Likes

#3

[quote=“Daftatt”]Bumping, because the C++ syntax highlighting is screwy in other places too.

when declaring a pointer using parenthesis it thinks I’m declaring a function, the syntax highlighting needs to be changed so it will only highlight as a function after an open curly brace follows it (indicating a code block for the function statements)[/quote]

Well, yes, the syntax is screwy. Here’s the problem:

int f(3); // defines ‘f’ as an object of type int with initial value of 3
int g(int); // declares ‘g’ as a function that takes one argument, of type int, and returns int.

You can’t write a regular expression to distinguish between these two, because the difference between them is that in the first case, the text inside the parentheses is a value and in the second it’s the name of a type. Regular expressions, looking only at the text in question, don’t have enough information to make this distinction. Here’s a more perverse example:

// myheader.h:
#if DEFINE_OBJECT
const int xx = 3;
#else
typedef int xx;
#endif

// test.cpp:
#include “myheader.h”
int f(xx);

Understanding what ‘f’ is in this case requires the moral equivalent of compiling the source code. There’s no way that a regular expression can make sense out of it.

0 Likes