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
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18 import dwtx.core.runtime.Assert;
|
|
19
|
|
20
|
|
21
|
|
22 /**
|
|
23 * A specific single line rule which stipulates that the start
|
|
24 * and end sequence occur within a single word, as defined by a word detector.
|
|
25 *
|
|
26 * @see IWordDetector
|
|
27 */
|
|
28 public class WordPatternRule : SingleLineRule {
|
|
29
|
|
30 /** The word detector used by this rule */
|
|
31 protected IWordDetector fDetector;
|
|
32 /** The internal buffer used for pattern detection */
|
|
33 private StringBuffer fBuffer= new StringBuffer();
|
|
34
|
|
35 /**
|
|
36 * Creates a rule for the given starting and ending word
|
|
37 * pattern which, if detected, will return the specified token.
|
|
38 * A word detector is used to identify words.
|
|
39 *
|
|
40 * @param detector the word detector to be used
|
|
41 * @param startSequence the start sequence of the word pattern
|
|
42 * @param endSequence the end sequence of the word pattern
|
|
43 * @param token the token to be returned on success
|
|
44 */
|
|
45 public WordPatternRule(IWordDetector detector, String startSequence, String endSequence, IToken token) {
|
|
46 this(detector, startSequence, endSequence, token, (char)0);
|
|
47 }
|
|
48
|
|
49 /**
|
|
50 /**
|
|
51 * Creates a rule for the given starting and ending word
|
|
52 * pattern which, if detected, will return the specified token.
|
|
53 * A word detector is used to identify words.
|
|
54 * Any character which follows the given escapeCharacter will be ignored.
|
|
55 *
|
|
56 * @param detector the word detector to be used
|
|
57 * @param startSequence the start sequence of the word pattern
|
|
58 * @param endSequence the end sequence of the word pattern
|
|
59 * @param token the token to be returned on success
|
|
60 * @param escapeCharacter the escape character
|
|
61 */
|
|
62 public WordPatternRule(IWordDetector detector, String startSequence, String endSequence, IToken token, char escapeCharacter) {
|
|
63 super(startSequence, endSequence, token, escapeCharacter);
|
|
64 Assert.isNotNull(detector);
|
|
65 fDetector= detector;
|
|
66 }
|
|
67
|
|
68 /**
|
|
69 * Returns whether the end sequence was detected.
|
|
70 * The rule acquires the rest of the word, using the
|
|
71 * provided word detector, and tests to determine if
|
|
72 * it ends with the end sequence.
|
|
73 *
|
|
74 * @param scanner the scanner to be used
|
|
75 * @return <code>true</code> if the word ends on the given end sequence
|
|
76 */
|
|
77 protected bool endSequenceDetected(ICharacterScanner scanner) {
|
|
78 fBuffer.setLength(0);
|
|
79 int c= scanner.read();
|
|
80 while (fDetector.isWordPart((char) c)) {
|
|
81 fBuffer.append((char) c);
|
|
82 c= scanner.read();
|
|
83 }
|
|
84 scanner.unread();
|
|
85
|
|
86 if (fBuffer.length() >= fEndSequence.length) {
|
|
87 for (int i=fEndSequence.length - 1, j= fBuffer.length() - 1; i >= 0; i--, j--) {
|
|
88 if (fEndSequence[i] !is fBuffer.charAt(j)) {
|
|
89 unreadBuffer(scanner);
|
|
90 return false;
|
|
91 }
|
|
92 }
|
|
93 return true;
|
|
94 }
|
|
95
|
|
96 unreadBuffer(scanner);
|
|
97 return false;
|
|
98 }
|
|
99
|
|
100 /**
|
|
101 * Returns the characters in the buffer to the scanner.
|
|
102 * Note that the rule must also return the characters
|
|
103 * read in as part of the start sequence expect the first one.
|
|
104 *
|
|
105 * @param scanner the scanner to be used
|
|
106 */
|
|
107 protected void unreadBuffer(ICharacterScanner scanner) {
|
|
108 fBuffer.insert(0, fStartSequence);
|
|
109 for (int i= fBuffer.length() - 1; i > 0; i--)
|
|
110 scanner.unread();
|
|
111 }
|
|
112 }
|