Skip to main content
summaryrefslogtreecommitdiffstats
blob: 61966933a797d1f39c3958ab92d7a59beee93b5a (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*******************************************************************************
 * Copyright (c) 2003, 2004 IBM Corporation 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:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Created on Nov 11, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.eclipse.jst.common.internal.annotations.core;

/**
 * @author Pat Kelley
 * 
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class AnnotationTagParser {

	private TagParseEventHandler handler;

	private char[] input;

	int pos;

	int endOfLastGoodAttParse;

	public AnnotationTagParser(TagParseEventHandler tp) {
		if (tp == null) {
			throw new IllegalArgumentException(AnnotationsCoreResources.AnnotationTagParser_0); 
		}
		handler = tp;
	}

	private boolean eos() {
		return pos >= input.length;
	}

	private boolean isWS(char c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t';
	}

	private void skipWS() {
		while (pos < input.length && (isWS(input[pos]) || input[pos] == '*')) {
			pos++;
		}
	}

	// Caller is expected to make sure the eos has not been reached.
	private char peek() {
		return input[pos];
	}

	// Caller is expected to check for EOS.
	private char nextChar() {
		return input[pos++];
	}

	private boolean isNextChar(char c) {
		if (eos())
			return false;
		return peek() == c;
	}

	private boolean isIDChar(char c) {
		return !isWS(c) && c != '=' && c != '@' && c != '\"';
	}

	private Token collectID() {
		StringBuffer b = new StringBuffer(16);
		Token t = new Token();

		t.setBeginning(pos);
		while (!eos() && isIDChar(peek())) {
			b.append(nextChar());
		}
		t.setEnd(pos - 1);
		t.setText(b.toString());
		return t;
	}

	private Token expectAttribName() {
		if (eos()) {
			return null;
		}
		int save = pos;

		Token retval = collectID();
		if (retval.length() == 0) {
			pos = save;
			return null;
		}
		return retval;
	}

	private Token expectTag() {
		if (eos()) {
			return null;
		}
		int savePos = pos;

		if (nextChar() != '@') {
			return null;
		}

		if (eos() || isWS(peek())) {
			return null;
		}

		Token retval = expectAttribName();

		if (retval.length() == 0) {
			pos = savePos + 1;
		}
		retval.setBeginning(savePos);

		// Save end of parse so we can pass it as the end of the parsed tag.
		endOfLastGoodAttParse = pos;
		return retval;
	}

	private Token expectQuotedValue() {
		skipWS();
		if (eos()) {
			return null;
		}

		Token tok = new Token();

		tok.setBeginning(pos);
		if (peek() != '\"') {
			return null;
		}
		nextChar();

		if (eos()) {
			return null;
		}

		StringBuffer b = new StringBuffer(64);

		while (!eos() && peek() != '\"') {
			b.append(nextChar());
		}
		if (!eos()) {
			nextChar();
		}

		tok.setEnd(pos - 1);
		tok.setText(b.toString());
		return tok;
	}

	private boolean expectAssign() {
		if (eos()) {
			return false;
		}

		if (nextChar() == '=') {
			return true;
		}
		pos--;
		return false;
	}

	private Token mkNullToken() {
		Token retval = new Token();

		retval.setBeginning(pos);
		retval.setEnd(pos - 1);
		retval.setText(""); //$NON-NLS-1$
		return retval;
	}

	private boolean parseNextAttribute() {
		skipWS();
		if (eos()) {
			return false;
		}
		Token key = collectID();

		if (key == null || key.length() == 0) {
			return false;
		}

		skipWS();
		if (eos()) {
			// Go ahead and report it, even though it is a partial attribute. (
			// we still fail here )
			handler.attribute(key, -1, mkNullToken());
			return false;
		}

		int eqPos = pos;

		if (!expectAssign()) {
			// Even though we won't parse this as a full attribute, go ahead and
			// call the handler with it. Some clients want to see partial
			// attributes.
			handler.attribute(key, -1, mkNullToken());
			return false;
		}
		skipWS();

		if (eos()) {
			// Same here - we fail on it, but we report it anyway
			handler.attribute(key, eqPos, mkNullToken());
			return false;
		}
		Token value = expectQuotedValue();

		if (value == null) {
			value = collectID();
			if (isNextChar('=')) {
				pos = value.getBeginning();
				value = mkNullToken();
			}
		}
		endOfLastGoodAttParse = pos;
		handler.attribute(key, eqPos, value);
		return true;
	}

	private void parseAttributes() {
		while (!eos() && parseNextAttribute()) {
			// loop while not end of string
		}
	}

	private void skipToTagChar() {
		while (!eos() && peek() != '@') {
			nextChar();
		}
	}

	public void setParserInput(char[] text) {
		input = text;
		pos = 0;
		endOfLastGoodAttParse = 0;
	}

	public void setParserInput(String text) {
		setParserInput(text.toCharArray());
	}

	public void parse() {
		while (!eos()) {
			skipToTagChar();
			Token tag = expectTag();
			if (tag == null) {
				break;
			}
			handler.annotationTag(tag);
			parseAttributes();
			handler.endOfTag(endOfLastGoodAttParse);
		}
	}

}

Back to the top