tags:

views:

98

answers:

2

E.g:

isValidCppIdentifier("_foo") // returns true
isValidCppIdentifier("9bar") // returns false
isValidCppIdentifier("var'") // returns false

I wrote some quick code but it fails: my regex is "[a-zA-Z_$][a-zA-Z0-9_$]*" and I simply do regex.IsMatch(inputString).

Thanks..

+1  A: 

It should work with some added anchoring:

"^[a-zA-Z_][a-zA-Z0-9_]*$"

If you really need to support ludicrous identifiers using Unicode, feel free to read one of the various versions of the standard and add all the ranges into your regexp (for example, pages 713 and 714 of http://www-d0.fnal.gov/~dladams/cxx_standard.pdf)

Matti Virkkunen
That will miss unicode identifiers
JaredPar
Yes this seems to work, but what about JaredPar comments?
Stringer Bell
@JaredPar: It will also miss identifiers using implementation-defined characters and other stupid identifiers.
Matti Virkkunen
Is there no built-in stuff for matching a cpp identifier? I know there is `:i` for that in VS search..
Stringer Bell
@Stringer: VS isn't not .NET.
Matti Virkkunen
+1  A: 

Matti's answer will work to sanitize identifiers before inserting into C++ code, but won't handle C++ code as input very well. It will be annoying to separate things like L"wchar_t string", where L is not an identifier. And there's Unicode.

Clang, Apple's compiler which is built on a philosophy of modularity, provides a set of tokenizer functions. It looks like you would want clang_createTranslationUnitFromSourceFile and clang_tokenize.

I didn't check to see if it handles \Uxxxx or anything. Can't make any kind of gurarantees. Last time I used LLVM was five years ago and it wasn't the greatest experience… but not the worst either.

On the other hand, GCC certainly has it, although you have to figure out how to use cpp_lex_direct.

Potatoswatter
Thanks for the answer too! Actually I won't need to support the ludicrous identifiers ;)
Stringer Bell