blob: 42fb5c9e40f7a3f8f85563ca653a99f4ca52e3f0 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jface.text.rules;
import org.eclipse.jface.text.Assert;
/**
* An implementation of <code>IRule</code> capable of detecting a numerical value.
*/
public class NumberRule implements IRule {
/** Internal setting for the uninitialized column constraint */
protected static final int UNDEFINED= -1;
/** The token to be returned when this rule is successful */
protected IToken fToken;
/** The column constraint */
protected int fColumn= UNDEFINED;
/**
* Creates a rule which will return the specified
* token when a numerical sequence is detected.
*
* @param token the token to be returned
*/
public NumberRule(IToken token) {
Assert.isNotNull(token);
fToken= token;
}
/**
* Sets a column constraint for this rule. If set, the rule's token
* will only be returned if the pattern is detected starting at the
* specified column. If the column is smaller then 0, the column
* constraint is considered removed.
*
* @param column the column in which the pattern starts
*/
public void setColumnConstraint(int column) {
if (column < 0)
column= UNDEFINED;
fColumn= column;
}
/*
* @see IRule#evaluate(ICharacterScanner)
*/
public IToken evaluate(ICharacterScanner scanner) {
int c= scanner.read();
if (Character.isDigit((char)c)) {
if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
do {
c= scanner.read();
} while (Character.isDigit((char) c));
scanner.unread();
return fToken;
}
}
scanner.unread();
return Token.UNDEFINED;
}
}