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 module dwtx.jface.text.rules.WordPatternRule;
|
|
14
|
131
|
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.IPredicateRule; // packageimport
|
|
23 import dwtx.jface.text.rules.DefaultPartitioner; // packageimport
|
|
24 import dwtx.jface.text.rules.NumberRule; // 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
|
|
40
|
129
|
41 import dwt.dwthelper.utils;
|
|
42
|
|
43
|
|
44 import dwtx.core.runtime.Assert;
|
|
45
|
|
46
|
|
47
|
|
48 /**
|
|
49 * A specific single line rule which stipulates that the start
|
|
50 * and end sequence occur within a single word, as defined by a word detector.
|
|
51 *
|
|
52 * @see IWordDetector
|
|
53 */
|
|
54 public class WordPatternRule : SingleLineRule {
|
|
55
|
|
56 /** The word detector used by this rule */
|
|
57 protected IWordDetector fDetector;
|
|
58 /** The internal buffer used for pattern detection */
|
|
59 private StringBuffer fBuffer= new StringBuffer();
|
|
60
|
|
61 /**
|
|
62 * Creates a rule for the given starting and ending word
|
|
63 * pattern which, if detected, will return the specified token.
|
|
64 * A word detector is used to identify words.
|
|
65 *
|
|
66 * @param detector the word detector to be used
|
|
67 * @param startSequence the start sequence of the word pattern
|
|
68 * @param endSequence the end sequence of the word pattern
|
|
69 * @param token the token to be returned on success
|
|
70 */
|
|
71 public WordPatternRule(IWordDetector detector, String startSequence, String endSequence, IToken token) {
|
|
72 this(detector, startSequence, endSequence, token, (char)0);
|
|
73 }
|
|
74
|
|
75 /**
|
|
76 /**
|
|
77 * Creates a rule for the given starting and ending word
|
|
78 * pattern which, if detected, will return the specified token.
|
|
79 * A word detector is used to identify words.
|
|
80 * Any character which follows the given escapeCharacter will be ignored.
|
|
81 *
|
|
82 * @param detector the word detector to be used
|
|
83 * @param startSequence the start sequence of the word pattern
|
|
84 * @param endSequence the end sequence of the word pattern
|
|
85 * @param token the token to be returned on success
|
|
86 * @param escapeCharacter the escape character
|
|
87 */
|
|
88 public WordPatternRule(IWordDetector detector, String startSequence, String endSequence, IToken token, char escapeCharacter) {
|
|
89 super(startSequence, endSequence, token, escapeCharacter);
|
|
90 Assert.isNotNull(detector);
|
|
91 fDetector= detector;
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * Returns whether the end sequence was detected.
|
|
96 * The rule acquires the rest of the word, using the
|
|
97 * provided word detector, and tests to determine if
|
|
98 * it ends with the end sequence.
|
|
99 *
|
|
100 * @param scanner the scanner to be used
|
|
101 * @return <code>true</code> if the word ends on the given end sequence
|
|
102 */
|
|
103 protected bool endSequenceDetected(ICharacterScanner scanner) {
|
|
104 fBuffer.setLength(0);
|
|
105 int c= scanner.read();
|
|
106 while (fDetector.isWordPart((char) c)) {
|
|
107 fBuffer.append((char) c);
|
|
108 c= scanner.read();
|
|
109 }
|
|
110 scanner.unread();
|
|
111
|
|
112 if (fBuffer.length() >= fEndSequence.length) {
|
|
113 for (int i=fEndSequence.length - 1, j= fBuffer.length() - 1; i >= 0; i--, j--) {
|
|
114 if (fEndSequence[i] !is fBuffer.charAt(j)) {
|
|
115 unreadBuffer(scanner);
|
|
116 return false;
|
|
117 }
|
|
118 }
|
|
119 return true;
|
|
120 }
|
|
121
|
|
122 unreadBuffer(scanner);
|
|
123 return false;
|
|
124 }
|
|
125
|
|
126 /**
|
|
127 * Returns the characters in the buffer to the scanner.
|
|
128 * Note that the rule must also return the characters
|
|
129 * read in as part of the start sequence expect the first one.
|
|
130 *
|
|
131 * @param scanner the scanner to be used
|
|
132 */
|
|
133 protected void unreadBuffer(ICharacterScanner scanner) {
|
|
134 fBuffer.insert(0, fStartSequence);
|
|
135 for (int i= fBuffer.length() - 1; i > 0; i--)
|
|
136 scanner.unread();
|
|
137 }
|
|
138 }
|