129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13
|
131
|
14
|
|
15 import dwtx.jface.text.rules.FastPartitioner; // packageimport
|
|
16 import dwtx.jface.text.rules.ITokenScanner; // packageimport
|
|
17 import dwtx.jface.text.rules.Token; // packageimport
|
|
18 import dwtx.jface.text.rules.RuleBasedScanner; // packageimport
|
|
19 import dwtx.jface.text.rules.EndOfLineRule; // packageimport
|
|
20 import dwtx.jface.text.rules.WordRule; // packageimport
|
|
21 import dwtx.jface.text.rules.WhitespaceRule; // packageimport
|
|
22 import dwtx.jface.text.rules.WordPatternRule; // packageimport
|
|
23 import dwtx.jface.text.rules.IPredicateRule; // packageimport
|
|
24 import dwtx.jface.text.rules.DefaultPartitioner; // packageimport
|
|
25 import dwtx.jface.text.rules.SingleLineRule; // packageimport
|
|
26 import dwtx.jface.text.rules.PatternRule; // packageimport
|
|
27 import dwtx.jface.text.rules.IWordDetector; // packageimport
|
|
28 import dwtx.jface.text.rules.RuleBasedDamagerRepairer; // packageimport
|
|
29 import dwtx.jface.text.rules.ICharacterScanner; // packageimport
|
|
30 import dwtx.jface.text.rules.IRule; // packageimport
|
|
31 import dwtx.jface.text.rules.DefaultDamagerRepairer; // packageimport
|
|
32 import dwtx.jface.text.rules.IToken; // packageimport
|
|
33 import dwtx.jface.text.rules.IPartitionTokenScanner; // packageimport
|
|
34 import dwtx.jface.text.rules.MultiLineRule; // packageimport
|
|
35 import dwtx.jface.text.rules.RuleBasedPartitioner; // packageimport
|
|
36 import dwtx.jface.text.rules.RuleBasedPartitionScanner; // packageimport
|
|
37 import dwtx.jface.text.rules.BufferedRuleBasedScanner; // packageimport
|
|
38 import dwtx.jface.text.rules.IWhitespaceDetector; // packageimport
|
|
39
|
129
|
40 module dwtx.jface.text.rules.NumberRule;
|
|
41
|
|
42 import dwt.dwthelper.utils;
|
|
43
|
|
44 import dwtx.core.runtime.Assert;
|
|
45
|
|
46
|
|
47 /**
|
|
48 * An implementation of <code>IRule</code> detecting a numerical value.
|
|
49 */
|
|
50 public class NumberRule : IRule {
|
|
51
|
|
52 /** Internal setting for the un-initialized column constraint */
|
|
53 protected static final int UNDEFINED= -1;
|
|
54 /** The token to be returned when this rule is successful */
|
|
55 protected IToken fToken;
|
|
56 /** The column constraint */
|
|
57 protected int fColumn= UNDEFINED;
|
|
58
|
|
59 /**
|
|
60 * Creates a rule which will return the specified
|
|
61 * token when a numerical sequence is detected.
|
|
62 *
|
|
63 * @param token the token to be returned
|
|
64 */
|
|
65 public NumberRule(IToken token) {
|
|
66 Assert.isNotNull(token);
|
|
67 fToken= token;
|
|
68 }
|
|
69
|
|
70 /**
|
|
71 * Sets a column constraint for this rule. If set, the rule's token
|
|
72 * will only be returned if the pattern is detected starting at the
|
|
73 * specified column. If the column is smaller then 0, the column
|
|
74 * constraint is considered removed.
|
|
75 *
|
|
76 * @param column the column in which the pattern starts
|
|
77 */
|
|
78 public void setColumnConstraint(int column) {
|
|
79 if (column < 0)
|
|
80 column= UNDEFINED;
|
|
81 fColumn= column;
|
|
82 }
|
|
83
|
|
84 /*
|
|
85 * @see IRule#evaluate(ICharacterScanner)
|
|
86 */
|
|
87 public IToken evaluate(ICharacterScanner scanner) {
|
|
88 int c= scanner.read();
|
|
89 if (Character.isDigit((char)c)) {
|
|
90 if (fColumn is UNDEFINED || (fColumn is scanner.getColumn() - 1)) {
|
|
91 do {
|
|
92 c= scanner.read();
|
|
93 } while (Character.isDigit((char) c));
|
|
94 scanner.unread();
|
|
95 return fToken;
|
|
96 }
|
|
97 }
|
|
98
|
|
99 scanner.unread();
|
|
100 return Token.UNDEFINED;
|
|
101 }
|
|
102 }
|