[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Applied Re: [cdt-patch] Fix to CConventions
|
Thanks,
Hoda Amer
Staff Software Engineer
Rational Software - IBM Software Group
Keith Campbell/Ottawa/IBM@IBMCA
Sent by: cdt-patch-admin@xxxxxxxxxxx
06/26/2004 03:42 PM
Please respond to
cdt-patch |
|
To
| cdt-patch@xxxxxxxxxxx
|
cc
|
|
Subject
| [cdt-patch] Fix to CConventions |
|
Here's a patch to correct some problems in CConventions.
1. isLegalIdentifier: Now it returns false for an empty string.
2. isValidIdentifier: Previously it would return true given a blank (or
empty) string or something other than a valid identifier. It would answer
false if the name began with an identifer. Now it returns true if the input
contains exactly one token which is an identifier.
3. validateIdentifier: Corrected use of isValidIdentifier.
Later, I plan to fix validateMethodName so it accepts strings like 'operator->'
and 'operator type *'.
-Keith
Index: src/org/eclipse/cdt/core/CConventions.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CConventions.java,v
retrieving revision 1.7
diff -u -r1.7 CConventions.java
--- src/org/eclipse/cdt/core/CConventions.java 4 Jun 2004 21:01:48 -0000 1.7
+++ src/org/eclipse/cdt/core/CConventions.java 26 Jun 2004 19:28:09 -0000
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2001 Rational Software Corp. and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Common Public License v0.5
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
- *
+ *
* Contributors:
* Rational Software - initial implementation
******************************************************************************/
@@ -13,6 +13,7 @@
import java.util.StringTokenizer;
import org.eclipse.cdt.core.parser.CodeReader;
+import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.core.parser.ParserFactory;
@@ -24,55 +25,56 @@
import org.eclipse.cdt.internal.core.model.CModelStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
+
/**
* @author hamer
- *
- * To change the template for this generated type comment go to
- * Window>Preferences>Java>Code Generation>Code and Comments
*/
-
public class CConventions {
private final static String scopeResolutionOperator= "::"; //$NON-NLS-1$
private final static char fgDot= '.';
- //private final static char fgColon= ':';
-
+
private static boolean isLegalIdentifier(String name) {
if (name == null) {
return false;
}
- String trimmed = name.trim();
- if ((!name.equals(trimmed)) || (name.indexOf(" ") != -1) ){ //$NON-NLS-1$
+
+ if (name.indexOf(' ') != -1) {
return false;
}
int length = name.length();
- if (length > 0) {
- char first = name.charAt(0);
- if( (! Character.isLetter(first) ) && (first != '_')){
- return false;
- }
+ char c;
- for (int i = 1; i < length; i++) {
- char c = name.charAt(i);
- if((! Character.isLetterOrDigit(c)) && (c != '_') ){
- return false;
- }
+ if (length == 0) {
+ return false;
+ }
+
+ c = name.charAt(0);
+ if ((!Character.isLetter(c)) && (c != '_')) {
+ return false;
+ }
+
+ for (int i = 1; i < length; ++i) {
+ c = name.charAt(i);
+ if ((!Character.isLetterOrDigit(c)) && (c != '_')) {
+ return false;
}
}
- return true;
+
+ return true;
}
-
+
/**
- * Validate the given CPP class name, either simple or qualified.
- * For example, <code>"A::B::C"</code>, or <code>"C"</code>.
+ * Validate the given CPP class name, either simple or qualified. For
+ * example, <code>"A::B::C"</code>, or <code>"C"</code>.
* <p>
*
* @param name the name of a class
* @return a status object with code <code>IStatus.OK</code> if
- * the given name is valid as a CPP class name,
+ * the given name is valid as a CPP class name,
* a status with code <code>IStatus.WARNING</code>
- * indicating why the given name is discouraged,
- * otherwise a status object indicating what is wrong with
+ * indicating why the given name is discouraged,
+ * otherwise a status object indicating what is wrong with
* the name
*/
public static IStatus validateClassName(String name) {
@@ -89,9 +91,9 @@
// simple name
IStatus status = validateIdentifier(name);
if (!status.isOK()){
- return status;
+ return status;
}
-
+
scannedID = name.toCharArray();
} else {
// qualified name
@@ -103,11 +105,11 @@
String type = name.substring(index + scopeResolutionOperator.length()).trim();
status = validateIdentifier(type);
if (!status.isOK()){
- return status;
+ return status;
}
scannedID = type.toCharArray();
}
-
+
if (scannedID != null) {
if (CharOperation.contains('$', scannedID)) {
return new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.class.dollarName"), null); //$NON-NLS-1$
@@ -124,11 +126,11 @@
* Validate the given scope name.
* <p>
* @return a status object with code <code>IStatus.OK</code> if
- * the given name is valid as a class name, otherwise a status
+ * the given name is valid as a class name, otherwise a status
* object indicating what is wrong with the name
*/
public static IStatus validateScopeName(String name) {
-
+
if (name == null) {
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.scope.nullName"), null); //$NON-NLS-1$
}
@@ -148,7 +150,7 @@
while (st.hasMoreTokens()) {
String typeName = st.nextToken();
typeName = typeName.trim(); // grammar allows spaces
- char[] scannedID = typeName.toCharArray();
+ char[] scannedID = typeName.toCharArray();
if (scannedID == null) {
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.illegalIdentifier", typeName), null); //$NON-NLS-1$
}
@@ -167,13 +169,13 @@
*
* @param name the name of a field
* @return a status object with code <code>IStatus.OK</code> if
- * the given name is valid as a field name, otherwise a status
+ * the given name is valid as a field name, otherwise a status
* object indicating what is wrong with the name
*/
public static IStatus validateFieldName(String name) {
return validateIdentifier(name);
}
-
+
/**
* Validate the given C identifier.
* The identifier must not have the same spelling as a C keyword,
@@ -183,21 +185,21 @@
*
* @param id the C identifier
* @return a status object with code <code>IStatus.OK</code> if
- * the given identifier is a valid C identifier, otherwise a status
+ * the given identifier is a valid C identifier, otherwise a status
* object indicating what is wrong with the identifier
*/
public static IStatus validateIdentifier(String id) {
- if (isLegalIdentifier(id)) {
- if(!isValidIdentifier(id)){
- return CModelStatus.VERIFIED_OK;
- } else {
- return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.invalid", id), null); //$NON-NLS-1$
- }
- } else {
+ if (!isLegalIdentifier(id)) {
return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.illegalIdentifier", id), null); //$NON-NLS-1$
}
+
+ if (!isValidIdentifier(id)) {
+ return new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, Util.bind("convention.invalid", id), null); //$NON-NLS-1$
+ }
+
+ return CModelStatus.VERIFIED_OK;
}
-
+
/**
* Validate the given method name.
* The special names "<init>" and "<clinit>" are not valid.
@@ -207,36 +209,44 @@
*
* @param name the name of a method
* @return a status object with code <code>IStatus.OK</code> if
- * the given name is valid as a method name, otherwise a status
+ * the given name is valid as a method name, otherwise a status
* object indicating what is wrong with the name
*/
public static IStatus validateMethodName(String name) {
- if(name.startsWith("~"))
- return validateIdentifier(name.substring(1, name.length()));
+ if(name.startsWith("~")) //$NON-NLS-1$
+ return validateIdentifier(name.substring(1));
else
return validateIdentifier(name);
}
-
+
public static boolean isValidIdentifier(String name){
// create a scanner and get the type of the token
// assuming that you are given a valid identifier
IToken token = null;
IScanner scanner = ParserFactory.createScanner(
new CodeReader(name.toCharArray()),
- new ScannerInfo(),
- ParserMode.QUICK_PARSE,
- ParserLanguage.CPP,
- null,
- null,
+ new ScannerInfo(),
+ ParserMode.QUICK_PARSE,
+ ParserLanguage.CPP,
+ null,
+ null,
null
);
- try{
+
+ try {
token = scanner.nextToken();
} catch (Exception e) {
}
- if((token != null) && (token.getType() != IToken.tIDENTIFIER))
- return true;
- return false;
+
+ if ((token != null) && (token.getType() == IToken.tIDENTIFIER)) {
+ try {
+ scanner.nextToken();
+ } catch (EndOfFileException e) {
+ return true;
+ } catch (Exception e) {
+ }
+ }
+
+ return false;
}
-
-}
\ No newline at end of file
+}