Skip to main content
summaryrefslogtreecommitdiffstats
blob: 306df3e5c97d581b1a795d44a19bf380c9d3294e (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package org.eclipse.cdt.internal.core.index;

/*
 * (c) Copyright QNX Software Systems Ltd. 2002.
 * All Rights Reserved.
 */
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.cdt.core.index.ITagEntry;
import org.eclipse.cdt.core.index.TagFlags;
import org.eclipse.core.resources.IFile;

public class CTagsEntry implements ITagEntry {

	final static String TAB_SEPARATOR = "\t";
	final static String PATTERN_SEPARATOR = ";\"";
	final static String LANGUAGE = "language";
	final static String KIND = "kind";
	final static String LINE = "line";
	final static String FILE = "file";
	final static String INHERITS = "inherits";
	final static String ACCESS = "access";
	final static String IMPLEMENTATION = "implementation";
	final static String CLASS = "class";

	final String[] NONE = new String[0];

	String tagName;

	/* Path of source file containing definition of tag.  */
	String fileName;
	
	/* IFile of parsed file */
	IFile file;

	/* pattern for locating source line
	 * (may be NULL if not present) */
	String pattern;

	int lineNumber;

	/* Miscellaneous extension fields */
	HashMap tagExtensionField;

	String line;

	public CTagsEntry(String line, IFile file) {
		this.line = line; 
		this.file = file;
		tagName = "";
		fileName ="";
		pattern = null;
		lineNumber = 0;
		tagExtensionField = new HashMap();
		parse();
	}

	void parse () {
		String delim = TAB_SEPARATOR;
		StringTokenizer st = new StringTokenizer(line, delim);
		for (int state = 0; st.hasMoreTokens(); state++) {
			String token = st.nextToken();
			switch (state) {
				case 0: // TAG_NAME:
					tagName = token;
				break;

				case 1: // FILE_NAME:
					fileName = token;
				break;

				case 2: // PATTERN;
					try {
						String sub = token.trim();
						if (Character.isDigit(sub.charAt(0))) {
							lineNumber = Integer.parseInt(sub);
						} else {
							// Filter out the prepend delimeter.
							if (sub.startsWith("/") || sub.startsWith("?")) {
								sub = sub.substring(1);
							}
							if (sub.startsWith("^")) {
								sub = sub.substring(1);
							}

							// Filter out the delimiters
							if (sub.endsWith("\"")) {
								int j = sub.lastIndexOf('"');
								sub = sub.substring(0, j);
							}
							if (sub.endsWith(";")) {
								int j = sub.lastIndexOf(';');
								sub = sub.substring(0, j);
							}
							if (sub.endsWith("?")) {
								int j = sub.lastIndexOf('?');
								sub = sub.substring(0, j);
							}
							if (sub.endsWith("/")) {
								int j = sub.lastIndexOf('/');
								sub = sub.substring(0, j);
							}
							if (sub.endsWith("$")) {			
								int j = sub.lastIndexOf('$');
								sub = sub.substring(0, j);
							}
							pattern = sub;
						}
					} catch (NumberFormatException e) {
						pattern = token;
						//e.printStackTrace();
					} catch (IndexOutOfBoundsException e) {
						pattern = token;
						//e.printStackTrace();
					}
				break;

				default: // EXTENSION_FIELDS:
					int i = token.indexOf(':');
					if (i != -1) {
						String key = token.substring(0, i);
						String value = token.substring(i + 1);
						tagExtensionField.put(key, value);
					}
				break;
			}
		}
	}

	public String getTagName () {
		return tagName;
	}

	public String getFileName() {
		return fileName;
	}
	public IFile getIFile() {
		return file;
	}

	public String getPattern() {
		return pattern;
	}

	// line:
	public int getLineNumber() {
		try {
			String sub = (String)tagExtensionField.get(LINE);
			if (sub != null) {
				lineNumber = Integer.parseInt(sub);
			}
		} catch (NumberFormatException e) {
			//System.out.println(e);
		}
		return lineNumber;
	}

	// kind:
	public int getKind() {
		String kind = (String)tagExtensionField.get(KIND);
		return TagFlags.value(kind);
	}

	// language:
	public String getLanguage() {
		return (String)tagExtensionField.get(LANGUAGE);
	}

	// Implementation:
	public int getImplementation() {
		String impl = (String)tagExtensionField.get(IMPLEMENTATION);
		return TagFlags.value(impl);
	}

	// Class:
	public String getClassName() {
		return (String)tagExtensionField.get(CLASS);
	}

	// file:
	public boolean hasFileScope() {
		return (tagExtensionField.get(FILE) != null);
	}

	// inherits:
	public String[] getInherits() {
		String base = (String)tagExtensionField.get(INHERITS);
		if (base != null) {
			StringTokenizer st = new StringTokenizer(base, ",");
			List list = new ArrayList();
			while (st.hasMoreTokens()) {
				list.add(st.nextToken());
			}
			return (String[])list.toArray(new String[0]);
		}
		return NONE;
	}

	// access:
	public int getAccessControl() {
		String access = (String)tagExtensionField.get(ACCESS);
		return TagFlags.value(access);
	}

	public String getLine() {
		return line;
	}

	public static String makeTagLine(ITagEntry tagEntry) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(tagEntry.getTagName());
		buffer.append("\t");
		buffer.append(tagEntry.getFileName());
		buffer.append("\t");
        String pat = tagEntry.getPattern();
		if (pat != null) {
			buffer.append(pat);
		} else {
			buffer.append(tagEntry.getLineNumber());
		}
		buffer.append(";\"");
		buffer.append("\t");

		String kind = TagFlags.value(tagEntry.getKind());
		if (kind != null) {
	        buffer.append(KIND + ":" + tagEntry.getKind());
			buffer.append("\t");
		}

		String lang = tagEntry.getLanguage();
		if (lang != null) {
	        buffer.append(LANGUAGE + ":" + tagEntry.getLanguage());
			buffer.append("\t");
		}

        if (tagEntry.hasFileScope()) {
	        buffer.append(FILE + ":");
	        buffer.append("\t");
		}

        String[] inherits = tagEntry.getInherits();
		for (int i = 0; i < inherits.length; i++) {
			if (i == 0) {
				buffer.append(INHERITS + ":");
			} else {
				buffer.append(",");
			}
			buffer.append(inherits[i]);
		}

		String access = TagFlags.value(tagEntry.getAccessControl());
		if (access != null) {
			buffer.append(ACCESS + ":" + access);
	        buffer.append("\t");
		}

		String impl = TagFlags.value(tagEntry.getImplementation());
		if (impl != null) {
        	buffer.append(IMPLEMENTATION + ":" + impl);
	        buffer.append("\t");
		}

		String clazz = tagEntry.getClassName();
		if (clazz != null) {
			buffer.append(CLASS + ":" + clazz);
	        buffer.append("\t");
		}
		return buffer.toString().trim();
	}

	public void print() {
		System.out.println("TagName " + getTagName());
		System.out.println("FileName " + getFileName());
        System.out.println("Pattern " + getPattern());
        System.out.println("LineNumber " + getLineNumber());
        System.out.println("Kind " + getKind());
        System.out.println("Language " + getLanguage());
        System.out.println("FileScope " + hasFileScope());
        String[] inherits = getInherits();
		for (int i = 0; i < inherits.length; i++) {
			System.out.println("Inherit " + inherits[i]);
		}
		System.out.println("AccessControl " + getAccessControl());
        System.out.println("Implementation " + getImplementation());
		System.out.println("ClassName " + getClassName());
	}
}

Back to the top