aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/anarres/cpp/Source.java
blob: e7a6d2d5fa5dc132a888829d28f80b170991f8e8 (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
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
/*
 * Anarres C Preprocessor
 * Copyright (C) 2007 Shevek
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.anarres.cpp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PushbackReader;
import java.io.Reader;
import java.io.StringReader;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import static org.anarres.cpp.Token.*;

/**
 * An input to the Preprocessor.
 *
 * Inputs may come from Files, Strings or other sources. The
 * preprocessor maintains a stack of Sources. Operations such as
 * file inclusion or token pasting will push a new source onto
 * the Preprocessor stack. Sources pop from the stack when they
 * are exhausted; this may be transparent or explicit.
 *
 * BUG: Error messages are not handled properly.
 */
public abstract class Source implements Iterable<Token> {
	private Source					parent;
	private boolean					autopop;
	private PreprocessorListener	listener;

	/* LineNumberReader */

/*
	// We can't do this, since we would lose the LexerException
	private class Itr implements Iterator {
		private Token	next = null;
		private void advance() {
			try {
				if (next != null)
					next = token();
			}
			catch (IOException e) {
				throw new UnsupportedOperationException(
						"Failed to advance token iterator: " +
								e.getMessage()
							);
			}
		}
		public boolean hasNext() {
			return next.getType() != EOF;
		}
		public Token next() {
			advance();
			Token	t = next;
			next = null;
			return t;
		}
		public void remove() {
			throw new UnsupportedOperationException(
					"Cannot remove tokens from a Source."
						);
		}
	}
*/

	public Source() {
		this.parent = null;
		this.autopop = false;
	}

	/* pp */ void setParent(Source parent, boolean autopop) {
		this.parent = parent;
		this.autopop = autopop;
	}

	/* pp */ final Source getParent() {
		return parent;
	}

	// @OverrideMustInvoke
	/* pp */ void init(Preprocessor pp) {
		setListener(pp.getListener());
	}

	/* Actually just used for testing. */
	public void setListener(PreprocessorListener pl) {
		this.listener = pl;
	}

	/**
	 * Returns the File currently being lexed.
	 *
	 * If this Source is not a {@link FileLexerSource}, then
	 * it will ask the parent Source, and so forth recursively.
	 * If no Source on the stack is a FileLexerSource, returns null.
	 */
	/* pp */ File getFile() {
		Source	parent = getParent();
		while (parent != null) {
			File	file = parent.getFile();
			if (file != null)
				return file;
			parent = parent.getParent();
		}
		return null;
	}

	/* pp */ String getName() {
		Source	parent = getParent();
		while (parent != null) {
			String	name = parent.getName();
			if (name != null)
				return name;
			parent = parent.getParent();
		}
		return null;
	}

	public int getLine() {
		Source	parent = getParent();
		if (parent == null)
			return 0;
		return parent.getLine();
	}

	public int getColumn() {
		Source	parent = getParent();
		if (parent == null)
			return 0;
		return parent.getColumn();
	}

	/* pp */ boolean isExpanding(Macro m) {
		Source	parent = getParent();
		if (parent != null)
			return parent.isExpanding(m);
		return false;
	}

	/**
	 * Returns true if this Source should be transparently popped
	 * from the input stack.
	 *
	 * Examples of such sources are macro expansions.
	 */
	/* pp */ boolean isAutopop() {
		return autopop;
	}

	/* pp */ boolean isNumbered() {
		return false;
	}

	/**
	 * Returns the next Token parsed from this input stream.
	 *
	 * @see Token
	 */
	public abstract Token token()
						throws IOException,
								LexerException;

	public Iterator<Token> iterator() {
		return new SourceIterator(this);
	}

	/**
	 * Skips tokens until the end of line.
	 *
	 * @param white true if only whitespace is permitted on the
	 *	remainder of the line.
	 * @return the NL token.
	 */
	public Token skipline(boolean white)
						throws IOException,
								LexerException {
		for (;;) {
			Token	tok = token();
			switch (tok.getType()) {
				case EOF:
					/* There ought to be a newline before EOF.
					 * At least, in any skipline context. */
					/* XXX Are we sure about this? */
					warning(tok.getLine(), tok.getColumn(),
									"No newline before end of file");
					return tok;
				case NL:
					/* This may contain one or more newlines. */
					return tok;
				case COMMENT:
				case WHITESPACE:
					break;
				default:
					/* XXX Check white, if required. */
					if (white)
						warning(tok.getLine(), tok.getColumn(),
										"Unexpected nonwhite token");
					break;
			}
		}
	}

	protected void error(int line, int column, String msg)
						throws LexerException {
		if (listener != null)
			listener.handleError(this, line, column, msg);
		else
			throw new LexerException("Error at " + line + ":" + column + ": " + msg);
	}

	protected void warning(int line, int column, String msg)
						throws LexerException {
		if (listener != null)
			listener.handleWarning(this, line, column, msg);
		else
			throw new LexerException("Warning at " + line + ":" + column + ": " + msg);
	}

}