Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0aff20eeb52d7eb456c509a52e14e53ab4accff9 (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
203
204
205
206
207
208
209
/*******************************************************************************
 * Copyright (c) 2000, 2016 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     QNX Software Systems - Initial API and implementation
 *     Red Hat Inc. - Modified from MacroDefinitionRule to support Automake files
 *******************************************************************************/

package org.eclipse.cdt.internal.autotools.ui.editors.automake;

import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.Token;

class AutomakeMacroDefinitionRule implements IPredicateRule {
	private static final int INIT_STATE = 0;
	private static final int VAR_STATE = 1;
	private static final int END_VAR_STATE = 2;
	private static final int EQUAL_STATE = 3;
	private static final int FINISH_STATE = 4;
	private static final int ERROR_STATE = 5;

	private IToken token;
	private StringBuilder buffer = new StringBuilder();
	protected IToken defaultToken;
	
	public AutomakeMacroDefinitionRule(IToken token, IToken defaultToken) {
		this.token = token;
		this.defaultToken = defaultToken;
	}

	@Override
	public IToken getSuccessToken() {
		return token;
	}

	@Override
	public IToken evaluate(ICharacterScanner scanner, boolean resume) {
		buffer.setLength(0);
		int c;
		int state = INIT_STATE;

		if (resume)
			scanToBeginOfLine(scanner);

		for (c = scanner.read(); c != ICharacterScanner.EOF; c = scanner.read()) {
			switch (state) {
				case INIT_STATE :
					if (c != '\n' && Character.isWhitespace((char) c)) {
						break;
					}
					if (isValidCharacter(c)) {
						state = VAR_STATE;
					} else {
						state = ERROR_STATE;
					}
					break;
				case VAR_STATE :
					if (isValidCharacter(c)) {
						break;
					}
				case END_VAR_STATE :
					if (Character.isWhitespace((char) c)) {
						state = END_VAR_STATE;
					} else if (c == ':' || c == '+') {
						state = EQUAL_STATE;
					} else if (c == '=') {
						state = FINISH_STATE;
					} else {
//						if (state == END_VAR_STATE) {
//							scanner.unread(); // Return back to the space
//						}
						state = ERROR_STATE;
					}
					break;
				case EQUAL_STATE :
					if (c == '=') {
						state = FINISH_STATE;
					} else {
						state = ERROR_STATE;
					}
					break;
				case FINISH_STATE :
					break;
				default :
					break;
			}
			if (state >= FINISH_STATE) {
				break;
			}
			buffer.append((char) c);
		}

		if (state == FINISH_STATE) {
			scanToEndOfLine(scanner);
			return token;
		}

        boolean debug = true;
        if (debug) {
//        	System.out.println("This should be a 'c':  " + peek(scanner));
//                System.out.println("This is what's in the **REST OF** the buffer:");
//                int count = 0;
//                for (int c = scanner.read(); c != ICharacterScanner.EOF; c = scanner.read()) {
//                        System.out.println((char) c);
//                        count++;
//                }
//                // Unread what we just displayed
//                for (int i = 0; i < count; i++) {
//                        scanner.unread();
//                }
        }
		
		if (defaultToken.isUndefined()) {
			// If c is EOF, we've read it and broken out of the for loop above,
			// but we need to unread it since it got read but not put into the
			// buffer
			if (state == ERROR_STATE || c == ICharacterScanner.EOF)
				scanner.unread();
			unreadBuffer(scanner);
            debug = true;
            if (debug) {
//            	System.out.println("This should be an 'i':  " + peek(scanner));
//                    System.out.println("We've supposedly just unread the entire buffer.  Here it is:");
//                    int count = 0;
//                    for (int c = scanner.read(); c != ICharacterScanner.EOF; c = scanner.read()) {
//                            System.out.println((char) c);
//                            count++;
//                    }
//                    // Unread what we just displayed
//                    for (int i = 0; i < count + 1; i++) {
//                            scanner.unread();
//                    }
//                    System.out.println("... just to be safe, here's the first character:  " + peek(scanner));
            }

		}
		
		return Token.UNDEFINED;
	}
	
	public char peek(ICharacterScanner scanner) {
		char c = (char) scanner.read();
		scanner.unread();
		return c;
	}

	@Override
	public IToken evaluate(ICharacterScanner scanner) {
		return evaluate(scanner, false);
	}

	/**
	 * Returns the characters in the buffer to the scanner.
	 *
	 * @param scanner the scanner to be used
	 */
	protected void unreadBuffer(ICharacterScanner scanner) {
		for (int i = buffer.length() - 1; i >= 0; i--)
			scanner.unread();
	}

	private void scanToEndOfLine(ICharacterScanner scanner) {
		int c;
		char[][] delimiters = scanner.getLegalLineDelimiters();
		while ((c = scanner.read()) != ICharacterScanner.EOF) {
			// Check for end of line since it can be used to terminate the pattern.
			for (int i = 0; i < delimiters.length; i++) {
				if (c == delimiters[i][0] && sequenceDetected(scanner, delimiters[i])) {
					return;
				}
			}
		}
	}

	private void scanToBeginOfLine(ICharacterScanner scanner) {
		while(scanner.getColumn() != 0) {
			scanner.unread();
		}
	}

	private boolean sequenceDetected(ICharacterScanner scanner, char[] sequence) {
		for (int i = 1; i < sequence.length; i++) {
			int c = scanner.read();
			if (c == ICharacterScanner.EOF) {
				return true;
			} else if (c != sequence[i]) {
				// Non-matching character detected, rewind the scanner back to the start.
				for (; i > 0; i--) {
					scanner.unread();
				}
				return false;
			}
		}

		return true;
	}
	protected boolean isValidCharacter(int c) {
		char c0 = (char) c;
		return Character.isLetterOrDigit(c0) || (c0 == '_') || (c0 == '-') || 
		(c0 == '@') || (c0 == '+') || (c0 == '$') || (c0 == '(') || (c0 == ')');
	}

}

Back to the top