Testing identifier names for validity

  • Thread starter Thread starter Wavemaker
  • Start date Start date
W

Wavemaker

I was wondering if there is way in the .NET framework to test if a
string represents a valid identifier name for the C# language. I want to
write a program that will generate code based on user input. Part of the
program's job will be to use names provided by the user to name classes,
methods, etc. Some error checking would be involved to make sure the
names use legal C# syntax. Before rolling my own code to do the error
checking, I wanted to check to see if this functionality already exists.
 
Wavemaker said:
I was wondering if there is way in the .NET framework to test if a string
represents a valid identifier name for the C# language. I want to write a
program that will generate code based on user input. Part of the program's
job will be to use names provided by the user to name classes, methods,
etc. Some error checking would be involved to make sure the names use legal
C# syntax. Before rolling my own code to do the error checking, I wanted to
check to see if this functionality already exists.

If you are using the code dom, I'm pretty sure this is handled for you(it'll
output identifiers named the same as keywords with the @ prefix).

Otheriwse, if you are generating code by hand, you will have to write code
to do it yourself, I'm afraid.
 
Daniel O'Connell said:
If you are using the code dom, I'm pretty sure this is handled for
you(it'll output identifiers named the same as keywords with the @
prefix).

Otheriwse, if you are generating code by hand, you will have to write
code to do it yourself, I'm afraid.

It's the latter. I've been thinking about this, though, and it shouldn't
be too hard to do this myself. The trickest part seems to be the first
character in the name. It will have to be treated as a special case.
Here's a small algorithm I just wrote for testing class names:

- Trim all whitespace from the beginning and end of the string.

- If the first character is NOT an underscore OR NOT a character OR NOT
an asterick, the identifier is NOT valid.
Return false.

- For each remaining character:
- If the character is NOT an underscore OR NOT a number OR NOT a
character, the identifier is NOT valid.
Return false.

- Return true.

I think this algorithm should work for what I'm doing, unless I've
missed something. Any comments will be welcomed.
 
Wavemaker said:
- If the first character is NOT an underscore OR NOT a character

Actually, I should have said:

- If the first character is NOT an underscore OR NOT a letter.
 
Wavemaker said:
It's the latter. I've been thinking about this, though, and it shouldn't
be too hard to do this myself. The trickest part seems to be the first
character in the name. It will have to be treated as a special case.
Here's a small algorithm I just wrote for testing class names:

- Trim all whitespace from the beginning and end of the string.

- If the first character is NOT an underscore OR NOT a character OR NOT
an asterick, the identifier is NOT valid.
Return false.

An asterisk is not a valid identifier character, it is an operator and a
type post fix.
 
Daniel O'Connell said:
An asterisk is not a valid identifier character, it is an operator and
a > type post fix.

Thanks!

Erm, I noticed that my algorithm doesn't test to make sure the name is
not a reserved keyword if it is not prepended with an asterick. Then
there are Unicode escape sequences to watch out for... Looks like this
isn't as trivial as I thought it might be.
 
Back
Top