Commit 576904ea authored by Miklos Magyari's avatar Miklos Magyari
Browse files

Improved number scanner for special float values, negative numbers and ranges


Signed-off-by: Miklos Magyari's avatarMiklos Magyari <miklos.magyari@sigmatechnology.se>
parent d97a59c9
......@@ -21,6 +21,7 @@ import org.eclipse.jface.text.rules.Token;
public class NumberDetectionPatternRule implements IRule {
private final IToken integerToken;
private final IToken floatToken;
private int nrRead;
public NumberDetectionPatternRule(final IToken integerToken, final IToken floatToken) {
this.integerToken = integerToken;
......@@ -29,19 +30,42 @@ public class NumberDetectionPatternRule implements IRule {
@Override
public IToken evaluate(ICharacterScanner scanner) {
StringBuffer sb = new StringBuffer();
nrRead = 0;
boolean isDot = false;
boolean isE = false;
char lastChar = 0;
int c = scanner.read();
if (isNumber((char)c)) {
if (isNumber((char)c) || (char)c == '-') {
sb.append((char)c);
do {
c = scanner.read();
nrRead++;
c = scanner.read();
if ((char)c == '.') {
isDot = true;
if (lastChar == '.') {
// assuming range operator '..'
scanner.unread();
scanner.unread();
sb.deleteCharAt(sb.length() - 1);
final String num = sb.toString();
return (num.contains(".") || num.contains("E")) ? floatToken : integerToken;
}
} else if ((char)c == 'E') {
if (isE) {
// malformed number, roll back
return unreadScanner(scanner);
}
isE = true;
}
} while (isNumber((char)c) || (char)c == '.');
lastChar = (char)c;
sb.append((char)c);
} while (isNumber((char)c) || (char)c == '.' || (char)c == 'E' || (char)c == '-');
scanner.unread();
return isDot ? floatToken : integerToken;
final String num = sb.toString();
return (num.contains(".") || num.contains("E")) ? floatToken : integerToken;
}
scanner.unread();
return Token.UNDEFINED;
}
......@@ -49,4 +73,19 @@ public class NumberDetectionPatternRule implements IRule {
private boolean isNumber(char c) {
return c >= '0' && c <= '9';
}
/**
* Reset the scanner to the position where the evaluation started.
* Used when the number is obviously malformed
*
* @param scanner
* @return Always returns <b>Token.UNDEFINED</b>
*/
private IToken unreadScanner(ICharacterScanner scanner) {
for (int i = 0; i < nrRead; i++) {
scanner.unread();
}
return Token.UNDEFINED;
}
}
......@@ -40,13 +40,17 @@ public final class CodeScanner extends RuleBasedScanner implements IDocumentGett
public static final String[] KEYWORDS = new String[] { "activate", "address", "alive", "all", "alt", "altstep", "and", "and4b", "any",
"break", "case", "component", "const", "continue", "control", "deactivate", "display", "do", "else", "encode", "enumerated",
"except", "exception", "execute", "extends", "extension", "external", "for", "from", "function", "goto", "group", "if",
"import", "in", "infinity", "inout", "interleave", "label", "language", "length", "log", "match", "message", "mixed", "mod",
"modifies", "module", "modulepar", "mtc", "noblock", "not", "not4b", "not_a_number", "nowait", "of", "omit", "on", "optional", "or", "or4b", "out",
"import", "in", "inout", "interleave", "label", "language", "length", "log", "match", "message", "mixed", "mod",
"modifies", "module", "modulepar", "mtc", "noblock", "not", "not4b", "nowait", "of", "omit", "on", "optional", "or", "or4b", "out",
"override", "param", "pattern", "port", "present", "procedure", "realtime", "record", "recursive", "rem", "repeat", "return", "runs", "select",
"self", "sender", "set", "signature", "system", "timestamp", "template", "testcase", "to", "type", "union", "value", "valueof", "var",
"variant", "while", "with", "xor", "xor4b", "now",
};
public static final String[] SPECIAL_FLOAT_VALUES = {
"infinity", "not_a_number"
};
public static final String[] BRACKETS = new String[] {
"(", ")", "{", "}"
};
......@@ -145,6 +149,9 @@ public final class CodeScanner extends RuleBasedScanner implements IDocumentGett
for (final String element : CodeScanner.KEYWORDS) {
wordRule.addWord(element, keyword);
}
for (final String element : CodeScanner.SPECIAL_FLOAT_VALUES) {
wordRule.addWord(element, nfloat);
}
final IPreferencesService prefs = Platform.getPreferencesService();
final boolean oopEnabled = prefs.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.ENABLEOOPEXTENSION, false, null);
if (oopEnabled) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment