Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c044af9df5f6dcf6daa4e073da4d5a2f39d9190a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*******************************************************************************
 * Copyright (c) 2006, 2015 Red Hat, Inc.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Red Hat Incorporated - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.autotools.ui.editors;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.SingleLineRule;
import org.eclipse.jface.text.rules.Token;

public class RecursiveSingleLineRule extends SingleLineRule {
	
	private List<IRule> rules;
	private int evalIndex;
	private int startIndex;
	private int endIndex;
	private int endBoundary;
	private String startSequence;
	private String endSequence;
	/**
	 * Creates a rule for the given starting and ending sequence
	 * which, if detected, will return the specified token.
	 *
	 * @param startSequence the pattern's start sequence
	 * @param endSequence the pattern's end sequence
	 * @param token the token to be returned on success
	 */
	public RecursiveSingleLineRule(String startSequence, String endSequence, IToken token) {
		this(startSequence, endSequence, token, (char) 0);
	}

	/**
	 * Creates a rule for the given starting and ending sequence
	 * which, if detected, will return the specified token.
	 * Any character which follows the given escape character
	 * will be ignored.
	 *
	 * @param startSequence the pattern's start sequence
	 * @param endSequence the pattern's end sequence
	 * @param token the token to be returned on success
	 * @param escapeCharacter the escape character
	 */
	public RecursiveSingleLineRule(String startSequence, String endSequence, IToken token, char escapeCharacter) {
		this(startSequence, endSequence, token, escapeCharacter, false);
	}

	/**
	 * Creates a rule for the given starting and ending sequence
	 * which, if detected, will return the specified token. Alternatively, the
	 * line can also be ended with the end of the file.
	 * Any character which follows the given escape character
	 * will be ignored.
	 *
	 * @param startSequence the pattern's start sequence
	 * @param endSequence the pattern's end sequence
	 * @param token the token to be returned on success
	 * @param escapeCharacter the escape character
	 * @param breaksOnEOF indicates whether the end of the file successfully terminates this rule
	 * @since 2.1
	 */
	public RecursiveSingleLineRule(String startSequence, String endSequence, IToken token, char escapeCharacter, boolean breaksOnEOF) {
		super(startSequence, endSequence, token, escapeCharacter, breaksOnEOF);
		this.startSequence = startSequence;
		this.endSequence = endSequence;
		rules = new ArrayList<>();
		startIndex = 0;
		endIndex = 0;
	}

	/**
	 * Creates a rule for the given starting and ending sequence
	 * which, if detected, will return the specified token. Alternatively, the
	 * line can also be ended with the end of the file.
	 * Any character which follows the given escape character
	 * will be ignored. In addition, an escape character immediately before an
	 * end of line can be set to continue the line.
	 *
	 * @param startSequence the pattern's start sequence
	 * @param endSequence the pattern's end sequence
	 * @param token the token to be returned on success
	 * @param escapeCharacter the escape character
	 * @param breaksOnEOF indicates whether the end of the file successfully terminates this rule
	 * @param escapeContinuesLine indicates whether the specified escape character is used for line
	 *        continuation, so that an end of line immediately after the escape character does not
	 *        terminate the line, even if <code>breakOnEOL</code> is true
	 * @since 3.0
	 */
	public RecursiveSingleLineRule(String startSequence, String endSequence, IToken token, char escapeCharacter, boolean breaksOnEOF, boolean escapeContinuesLine) {
		super(startSequence, endSequence, token, escapeCharacter, breaksOnEOF, escapeContinuesLine);
		this.startSequence = startSequence;
		this.endSequence = endSequence;
		rules = new ArrayList<>();
		startIndex = 0;
		endIndex = 0;
	}
	
	public void addRule(SingleLineRule rule) {
		rules.add(rule);
	}
	
	@Override
	public IToken getSuccessToken() {
		// We need to be aware of what success token we are referring to.
		// The current internal rule index will help us determine which
		// one.
		if (evalIndex < rules.size()) {
			SingleLineRule x = (SingleLineRule)rules.get(evalIndex);
			return x.getSuccessToken();
		}
		return super.getSuccessToken();
	}
	
	protected void backupScanner(ICharacterScanner scanner, int position) {
		int count = scanner.getColumn() - position;
		while (count-- > 0)
			scanner.unread();
	}
	
	@Override
	public IToken evaluate(ICharacterScanner scanner, boolean resume) {
		int column = scanner.getColumn();
		// Check if we are at EOF, in which case rules don't hold
		if (column < 0)
			return Token.UNDEFINED;
		if (!resume) {
			evalIndex = 0;
			// Check if we are within outer rule boundaries.
			if (column >= endIndex || column < startIndex) {
				// If not, then we should evaluate to see if the
				// outer rule is true starting at the current position.
				startIndex = scanner.getColumn();
				if (super.evaluate(scanner, false) != Token.UNDEFINED) {
					// Outer rule is true for a section.  Now we can
					// set the boundaries for the internal rules.
					// End boundary for internal rules is the start of
					// the end sequence.
					endIndex = scanner.getColumn();
					endBoundary = endIndex - endSequence.length();
					// Back up scanner to just after start sequence.
					backupScanner(scanner, startIndex + startSequence.length());
					return super.getSuccessToken();
				}
				else
					// Outer rule doesn't hold.
					return Token.UNDEFINED;
			}
		}

		// At this point, we want to subdivide up the area covered by the
		// outer rule into success tokens for internal areas separated by
		// areas of the outer rule.
		
		int start = scanner.getColumn();
		column = start;
		while (column < endBoundary) {
			while (evalIndex < rules.size()) {
				SingleLineRule x = (SingleLineRule)rules.get(evalIndex);
				IToken token = x.evaluate(scanner, false);
				if (!token.isUndefined()) {
					// Found internal token.  If we had to read to get
					// to the start of the internal token, then back up
					// the scanner to the start of the internal token and
					// return the initial read area as part of an outer token.
					// Otherwise, return the internal token.
					if (column == start) {
						evalIndex = 0;
						return token;
					} else {
						backupScanner(scanner, column);
						return super.getSuccessToken();
					}
				}
				++evalIndex;
			}
			evalIndex = 0;
			scanner.read();
			++column;
		}
		
		// Outside internal area.  Read until end of outer area and return
		// outer token.
		while (column++ < endIndex)
			scanner.read();
		startIndex = 0;
		endIndex = 0;
		return super.getSuccessToken();
	}
}

Back to the top