| /******************************************************************************* |
| * Copyright (c) 2005, 2016 IBM Corporation and others. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.dltk.python.internal.ui.rules; |
| |
| import org.eclipse.core.runtime.Assert; |
| import org.eclipse.jface.text.rules.ICharacterScanner; |
| import org.eclipse.jface.text.rules.IRule; |
| import org.eclipse.jface.text.rules.IToken; |
| import org.eclipse.jface.text.rules.Token; |
| |
| /** |
| * An implementation of <code>IRule</code> detecting a numerical value. |
| */ |
| public class PythonFloatNumberRule implements IRule { |
| protected static final int UNDEFINED = -1; |
| protected IToken fToken; |
| protected int fColumn = UNDEFINED; |
| |
| public PythonFloatNumberRule(IToken token) { |
| Assert.isNotNull(token); |
| fToken = token; |
| } |
| |
| public void setColumnConstraint(int column) { |
| if (column < 0) |
| column = UNDEFINED; |
| fColumn = column; |
| } |
| |
| @Override |
| public IToken evaluate(ICharacterScanner scanner) { |
| if( scanner.getColumn() > 0 ) { |
| scanner.unread(); |
| int cc = scanner.read(); |
| if( !Character.isWhitespace((char)cc)) { |
| return Token.UNDEFINED; |
| } |
| } |
| int c = scanner.read(); |
| int p = c; |
| if (Character.isDigit((char) c) || c == '.') { |
| boolean hex = false; |
| if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) { |
| do { |
| p = c; |
| c = scanner.read(); |
| if (c == 'x' || c == 'X' && !hex) { |
| hex = true; |
| p = c; |
| c = scanner.read(); |
| } |
| } while (Character.isDigit((char) c)); |
| if (c != 'e' && c != 'E') { |
| scanner.unread(); |
| } |
| if (p == '.') { |
| scanner.unread(); |
| return Token.UNDEFINED; |
| } |
| return fToken; |
| } |
| } |
| scanner.unread(); |
| return Token.UNDEFINED; |
| } |
| } |