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
|
151
|
15 module dwtx.jface.text.rules.RuleBasedPartitionScanner;
|
|
16
|
131
|
17 import dwtx.jface.text.rules.FastPartitioner; // packageimport
|
|
18 import dwtx.jface.text.rules.ITokenScanner; // packageimport
|
|
19 import dwtx.jface.text.rules.Token; // packageimport
|
|
20 import dwtx.jface.text.rules.RuleBasedScanner; // packageimport
|
|
21 import dwtx.jface.text.rules.EndOfLineRule; // packageimport
|
|
22 import dwtx.jface.text.rules.WordRule; // packageimport
|
|
23 import dwtx.jface.text.rules.WhitespaceRule; // packageimport
|
|
24 import dwtx.jface.text.rules.WordPatternRule; // packageimport
|
|
25 import dwtx.jface.text.rules.IPredicateRule; // packageimport
|
|
26 import dwtx.jface.text.rules.DefaultPartitioner; // packageimport
|
|
27 import dwtx.jface.text.rules.NumberRule; // packageimport
|
|
28 import dwtx.jface.text.rules.SingleLineRule; // packageimport
|
|
29 import dwtx.jface.text.rules.PatternRule; // packageimport
|
|
30 import dwtx.jface.text.rules.RuleBasedDamagerRepairer; // packageimport
|
|
31 import dwtx.jface.text.rules.ICharacterScanner; // packageimport
|
|
32 import dwtx.jface.text.rules.IRule; // packageimport
|
|
33 import dwtx.jface.text.rules.DefaultDamagerRepairer; // packageimport
|
|
34 import dwtx.jface.text.rules.IToken; // packageimport
|
|
35 import dwtx.jface.text.rules.IPartitionTokenScanner; // packageimport
|
|
36 import dwtx.jface.text.rules.MultiLineRule; // packageimport
|
|
37 import dwtx.jface.text.rules.RuleBasedPartitioner; // packageimport
|
|
38 import dwtx.jface.text.rules.BufferedRuleBasedScanner; // packageimport
|
|
39 import dwtx.jface.text.rules.IWhitespaceDetector; // packageimport
|
|
40
|
129
|
41 import dwt.dwthelper.utils;
|
|
42
|
|
43
|
|
44 import dwtx.jface.text.IDocument;
|
|
45
|
|
46
|
|
47 /**
|
|
48 * Scanner that exclusively uses predicate rules.
|
|
49 * @since 2.0
|
|
50 */
|
|
51 public class RuleBasedPartitionScanner : BufferedRuleBasedScanner , IPartitionTokenScanner {
|
|
52
|
|
53 /** The content type of the partition in which to resume scanning. */
|
|
54 protected String fContentType;
|
|
55 /** The offset of the partition inside which to resume. */
|
|
56 protected int fPartitionOffset;
|
|
57
|
|
58
|
|
59 /**
|
|
60 * Disallow setting the rules since this scanner
|
|
61 * exclusively uses predicate rules.
|
162
|
62 *
|
129
|
63 * @param rules the sequence of rules controlling this scanner
|
|
64 */
|
|
65 public void setRules(IRule[] rules) {
|
|
66 throw new UnsupportedOperationException();
|
|
67 }
|
|
68
|
|
69 /*
|
|
70 * @see RuleBasedScanner#setRules(IRule[])
|
|
71 */
|
|
72 public void setPredicateRules(IPredicateRule[] rules) {
|
|
73 super.setRules(rules);
|
|
74 }
|
|
75
|
|
76 /*
|
|
77 * @see ITokenScanner#setRange(IDocument, int, int)
|
|
78 */
|
|
79 public void setRange(IDocument document, int offset, int length) {
|
|
80 setPartialRange(document, offset, length, null, -1);
|
|
81 }
|
|
82
|
|
83 /*
|
|
84 * @see IPartitionTokenScanner#setPartialRange(IDocument, int, int, String, int)
|
|
85 */
|
|
86 public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) {
|
|
87 fContentType= contentType;
|
|
88 fPartitionOffset= partitionOffset;
|
|
89 if (partitionOffset > -1) {
|
|
90 int delta= offset - partitionOffset;
|
|
91 if (delta > 0) {
|
|
92 super.setRange(document, partitionOffset, length + delta);
|
|
93 fOffset= offset;
|
|
94 return;
|
|
95 }
|
|
96 }
|
|
97 super.setRange(document, offset, length);
|
|
98 }
|
|
99
|
|
100 /*
|
|
101 * @see ITokenScanner#nextToken()
|
|
102 */
|
|
103 public IToken nextToken() {
|
|
104
|
|
105
|
|
106 if (fContentType is null || fRules is null) {
|
|
107 //don't try to resume
|
|
108 return super.nextToken();
|
|
109 }
|
|
110
|
|
111 // inside a partition
|
|
112
|
|
113 fColumn= UNDEFINED;
|
|
114 bool resume= (fPartitionOffset > -1 && fPartitionOffset < fOffset);
|
|
115 fTokenOffset= resume ? fPartitionOffset : fOffset;
|
|
116
|
|
117 IPredicateRule rule;
|
|
118 IToken token;
|
|
119
|
|
120 for (int i= 0; i < fRules.length; i++) {
|
134
|
121 rule= cast(IPredicateRule) fRules[i];
|
129
|
122 token= rule.getSuccessToken();
|
162
|
123 if (fContentType.equals(stringcast(token.getData()))) {
|
129
|
124 token= rule.evaluate(this, resume);
|
|
125 if (!token.isUndefined()) {
|
|
126 fContentType= null;
|
|
127 return token;
|
|
128 }
|
|
129 }
|
|
130 }
|
|
131
|
|
132 // haven't found any rule for this type of partition
|
|
133 fContentType= null;
|
|
134 if (resume)
|
|
135 fOffset= fPartitionOffset;
|
|
136 return super.nextToken();
|
|
137 }
|
|
138 }
|