aboutsummaryrefslogtreecommitdiffstats
path: root/org.aspectj.matcher/src/org/aspectj/weaver/patterns/BasicTokenSource.java
blob: 9e2a626183bc0e1fb087922753881ba2447476d1 (plain)
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
/* *******************************************************************
 * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
 * 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: 
 *     PARC     initial implementation 
 * ******************************************************************/


package org.aspectj.weaver.patterns;

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

import org.aspectj.weaver.BCException;
import org.aspectj.weaver.ISourceContext;


public class BasicTokenSource implements ITokenSource {
	private int index = 0;
	private IToken[] tokens;
	private ISourceContext sourceContext;

	public BasicTokenSource(IToken[] tokens, ISourceContext sourceContext) {
		this.tokens = tokens;
		this.sourceContext = sourceContext;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int newIndex) {
		this.index = newIndex;
	}

	public IToken next() {
		try {
		    return tokens[index++];
		} catch (ArrayIndexOutOfBoundsException e) {
			return IToken.EOF;
		}
	}

	public IToken peek() {
		try {
		    return tokens[index];
		} catch (ArrayIndexOutOfBoundsException e) {
			return IToken.EOF;
		}
	}

	public IToken peek(int offset) {
		try {
		    return tokens[index+offset];
		} catch (ArrayIndexOutOfBoundsException e) {
			return IToken.EOF;
		}
	}

	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append("[");
		for (int i = 0; i < tokens.length; i++) {
			IToken t = tokens[i];
			if (t == null)
				break;
			if (i > 0)
				buf.append(", ");
			buf.append(t.toString());
		}
		buf.append("]");
		return buf.toString();
	}
	
	
	//////////////////////////////////////////////////////
	// Convenience, maybe just for testing
	public static ITokenSource makeTokenSource(String input, ISourceContext context) {
		char[] chars = input.toCharArray();
		
		int i = 0;
		List tokens = new ArrayList();
		
		while (i < chars.length) {
			char ch = chars[i++];			
			switch(ch) {
				case ' ':
				case '\t':
				case '\n':
				case '\r':
					continue;
				case '*':
				case '(':
				case ')':
				case '+':
				case '[':
				case ']':
				case ',':
				case '!':
				case ':':
				case '@':
				case '<':
				case '>':
				case '=':
				case 	'?':
				    tokens.add(BasicToken.makeOperator(makeString(ch), i-1, i-1));
				    continue;
				case '.':
					if ((i+2)<=chars.length) {
						// could be '...'
						char nextChar1 = chars[i];
						char nextChar2 = chars[i+1];
						if (ch==nextChar1 && ch==nextChar2) {
							// '...'
							tokens.add(BasicToken.makeIdentifier("...",i-1,i+1));
							i=i+2;
						} else {
							tokens.add(BasicToken.makeOperator(makeString(ch), i-1, i-1));
						}
					} else {
						tokens.add(BasicToken.makeOperator(makeString(ch), i-1, i-1));
					}
					continue;
				case '&':
					if ((i+1) <= chars.length && chars[i] != '&') {
						tokens.add(BasicToken.makeOperator(makeString(ch),i-1,i-1));
						continue;
					}
					// fall-through
				case '|':
				    if (i == chars.length) {
				    	throw new BCException("bad " + ch);
				    }
				    char nextChar = chars[i++];
				    if (nextChar == ch) {
				    	tokens.add(BasicToken.makeOperator(makeString(ch, 2), i-2, i-1));
				    } else {
				    	throw new RuntimeException("bad " + ch);
				    }
				    continue;
				    
				case '\"':
				    int start0 = i-1;
				    while (i < chars.length && !(chars[i]=='\"')) i++;
				    i += 1;
				    tokens.add(BasicToken.makeLiteral(new String(chars, start0+1, i-start0-2), "string", start0, i-1));
				    continue;
				default:
				    int start = i-1;
				    while (i < chars.length && Character.isJavaIdentifierPart(chars[i])) { i++; }
				    tokens.add(BasicToken.makeIdentifier(new String(chars, start, i-start), start, i-1));
				
			}
		}

		//System.out.println(tokens);
		
		return new BasicTokenSource((IToken[])tokens.toArray(new IToken[tokens.size()]), context);
	}

	private static String makeString(char ch) {
		return Character.toString(ch);
	}

	private static String makeString(char ch, int count) {
		// slightly inefficient ;-)
		char[] chars = new char[count];
		for (int i=0; i<count; i++) { chars[i] = ch; }
		return new String(chars);
	}
	public ISourceContext getSourceContext() {
		return sourceContext;
	}
	public void setSourceContext(ISourceContext context) {
		this.sourceContext = context;
	}

}