Mercurial > projects > dwt-addons
annotate dwtx/jface/text/rules/WordPatternRule.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | 51e6e63f930e |
children | eb98a5cbfd78 |
rev | line source |
---|---|
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 */ | |
162 | 59 private StringBuffer fBuffer; |
129 | 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 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
71 public this(IWordDetector detector, String startSequence, String endSequence, IToken token) { |
134 | 72 this(detector, startSequence, endSequence, token, cast(wchar)0); |
129 | 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 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
88 public this(IWordDetector detector, String startSequence, String endSequence, IToken token, char escapeCharacter) { |
162 | 89 fBuffer= new StringBuffer(); |
129 | 90 super(startSequence, endSequence, token, escapeCharacter); |
162 | 91 Assert.isNotNull(cast(Object)detector); |
129 | 92 fDetector= detector; |
93 } | |
94 | |
95 /** | |
96 * Returns whether the end sequence was detected. | |
97 * The rule acquires the rest of the word, using the | |
98 * provided word detector, and tests to determine if | |
99 * it ends with the end sequence. | |
100 * | |
101 * @param scanner the scanner to be used | |
102 * @return <code>true</code> if the word ends on the given end sequence | |
103 */ | |
104 protected bool endSequenceDetected(ICharacterScanner scanner) { | |
162 | 105 fBuffer.truncate(0); |
129 | 106 int c= scanner.read(); |
162 | 107 while (fDetector.isWordPart(cast(char) c)) { |
108 fBuffer.append(cast(char) c); | |
129 | 109 c= scanner.read(); |
110 } | |
111 scanner.unread(); | |
112 | |
113 if (fBuffer.length() >= fEndSequence.length) { | |
114 for (int i=fEndSequence.length - 1, j= fBuffer.length() - 1; i >= 0; i--, j--) { | |
162 | 115 if (fEndSequence[i] !is fBuffer.slice()[j]) { |
129 | 116 unreadBuffer(scanner); |
117 return false; | |
118 } | |
119 } | |
120 return true; | |
121 } | |
122 | |
123 unreadBuffer(scanner); | |
124 return false; | |
125 } | |
126 | |
127 /** | |
128 * Returns the characters in the buffer to the scanner. | |
129 * Note that the rule must also return the characters | |
130 * read in as part of the start sequence expect the first one. | |
131 * | |
132 * @param scanner the scanner to be used | |
133 */ | |
134 protected void unreadBuffer(ICharacterScanner scanner) { | |
162 | 135 fBuffer.select(0, 0 ); |
136 fBuffer.replace(fStartSequence); | |
129 | 137 for (int i= fBuffer.length() - 1; i > 0; i--) |
138 scanner.unread(); | |
139 } | |
140 } |