aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/anarres/cpp/Main.java
blob: da9d3d68036eaf9d7efad1985ace69e75491f18c (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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * Anarres C Preprocessor
 * Copyright (c) 2007-2008, Shevek
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */

package org.anarres.cpp;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import gnu.getopt.Getopt;
import gnu.getopt.LongOpt;

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

/**
 * (Currently a simple test class).
 */
public class Main {

	private static class Option extends LongOpt {
		private String eg;
		private String help;
		public Option(String word, int arg, int ch,
						String eg, String help) {
			super(word, arg, null, ch);
			this.eg = eg;
			this.help = help;
		}
	}

	private static final Option[]	OPTS = new Option[] {
		new Option("help",   LongOpt.NO_ARGUMENT,       'h', null,
			"Displays help and usage information."),
		new Option("define", LongOpt.REQUIRED_ARGUMENT, 'D', "name=definition",
			"Defines the given macro."),
		new Option("undefine", LongOpt.REQUIRED_ARGUMENT, 'U', "name",
			"Undefines the given macro, previously either builtin or defined using -D."),
		new Option("include", LongOpt.REQUIRED_ARGUMENT, 1, "file",
			"Process file as if \"#" + "include \"file\"\" appeared as the first line of the primary source file."),
		new Option("incdir", LongOpt.REQUIRED_ARGUMENT, 'I', "dir",
			"Adds the directory dir to the list of directories to be searched for header files."),
		new Option("iquote", LongOpt.REQUIRED_ARGUMENT, 0, "dir",
			"Adds the directory dir to the list of directories to be searched for header files included using \"\"."),
		new Option("warning", LongOpt.REQUIRED_ARGUMENT, 'W', "type",
			"Enables the named warning class ("  + getWarnings() + ")."),
		new Option("no-warnings", LongOpt.NO_ARGUMENT, 'w', null,
			"Disables ALL warnings."),
		new Option("verbose", LongOpt.NO_ARGUMENT, 'v', null,
			"Operates incredibly verbosely."),
		new Option("debug", LongOpt.NO_ARGUMENT, 3, null,
			"Operates incredibly verbosely."),
		new Option("version", LongOpt.NO_ARGUMENT, 2, null,
			"Prints jcpp's version number (" + Version.getVersion() + ")"),
	};

	private static CharSequence getWarnings() {
		StringBuilder	buf = new StringBuilder();
		for (Warning w : Warning.values()) {
			if (buf.length() > 0)
				buf.append(", ");
			String	name = w.name().toLowerCase();
			buf.append(name.replace('_', '-'));
		}
		return buf;
	}

	public static void main(String[] args) throws Exception {
		(new Main()).run(args);
	}

	public void run(String[] args) throws Exception {
		Option[]opts = OPTS;
		String	sopts = getShortOpts(opts);
		Getopt	g = new Getopt("jcpp", args, sopts, opts);
		int		c;
		String	arg;
		int		idx;

		Preprocessor	pp = new Preprocessor();
		pp.addFeature(Feature.DIGRAPHS);
		pp.addFeature(Feature.TRIGRAPHS);
		pp.addFeature(Feature.LINEMARKERS);
		pp.addWarning(Warning.IMPORT);
		pp.setListener(new PreprocessorListener());
		pp.addMacro("__JCPP__");
		pp.getSystemIncludePath().add("/usr/local/include");
		pp.getSystemIncludePath().add("/usr/include");
		pp.getFrameworksPath().add("/System/Library/Frameworks");
		pp.getFrameworksPath().add("/Library/Frameworks");
		pp.getFrameworksPath().add("/Local/Library/Frameworks");

		GETOPT: while ((c = g.getopt()) != -1) {
			switch (c) {
				case 'D':
					arg = g.getOptarg();
					idx = arg.indexOf('=');
					if (idx == -1)
						pp.addMacro(arg);
					else
						pp.addMacro(arg.substring(0, idx),
									arg.substring(idx + 1));
					break;
				case 'U':
					pp.getMacros().remove(g.getOptarg());
					break;
				case 'I':
					pp.getSystemIncludePath().add(g.getOptarg());
					break;
				case 0:	// --iquote=
					pp.getQuoteIncludePath().add(g.getOptarg());
					break;
				case 'W':
					arg = g.getOptarg().toUpperCase();
					arg = arg.replace('-', '_');
					if (arg.equals("ALL"))
						pp.addWarnings(EnumSet.allOf(Warning.class));
					else
						pp.addWarning(Enum.valueOf(Warning.class, arg));
					break;
				case 'w':
					pp.getWarnings().clear();
					break;
				case 1:	// --include=
					// pp.addInput(new File(g.getOptarg()));
					// Comply exactly with spec.
					pp.addInput(new StringLexerSource(
						"#" + "include \"" + g.getOptarg() + "\"\n"
					));
					break;
				case 2:	// --version
					version(System.out);
					return;
				case 'v':
					pp.addFeature(Feature.VERBOSE);
					break;
				case 3:
					pp.addFeature(Feature.DEBUG);
					break;
				case 'h':
					usage(getClass().getName(), opts);
					return;
				default:
					throw new Exception("Illegal option " + (char)c);
				case '?':
					continue;	/* Make failure-proof. */
			}
		}

		for (int i = g.getOptind(); i < args.length; i++)
			pp.addInput(new FileLexerSource(new File(args[i])));
		if (g.getOptind() == args.length)
			pp.addInput(new InputLexerSource(System.in));

		if (pp.getFeature(Feature.VERBOSE)) {
			System.err.println("#"+"include \"...\" search starts here:");
			for (String dir : pp.getQuoteIncludePath())
				System.err.println("  " + dir);
			System.err.println("#"+"include <...> search starts here:");
			for (String dir : pp.getSystemIncludePath())
				System.err.println("  " + dir);
			System.err.println("End of search list.");
		}

		try {
			for (;;) {
				Token	tok = pp.token();
				if (tok == null)
					break;
				if (tok.getType() == Token.EOF)
					break;
				System.out.print(tok.getText());
			}
		}
		catch (Exception e) {
			e.printStackTrace(System.err);
			Source	s = pp.getSource();
			while (s != null) {
				System.err.println(" -> " + s);
				s = s.getParent();
			}
		}

	}

	private void version(PrintStream out) {
		out.println("Anarres Java C Preprocessor version " + Version.getVersion());
		out.println("Copyright (C) 2008 Shevek (http://www.anarres.org/).");
		out.println("This is free software; see the source for copying conditions.  There is NO");
		out.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
	}


	private static String getShortOpts(Option[] opts)
						throws Exception {
		StringBuilder   buf = new StringBuilder();
		for (int i = 0; i < opts.length; i++) {
			char    c = (char)opts[i].getVal();
			if (!Character.isLetterOrDigit(c))
				continue;
			for (int j = 0; j < buf.length(); j++)
				if (buf.charAt(j) == c)
					throw new Exception(
							"Duplicate short option " + c
								);
			buf.append(c);
			switch (opts[i].getHasArg()) {
				case LongOpt.NO_ARGUMENT:
					break;
				case LongOpt.OPTIONAL_ARGUMENT:
					buf.append("::");
					break;
				case LongOpt.REQUIRED_ARGUMENT:
					buf.append(":");
					break;
			}
		}
		return buf.toString();
	}

	/* This is incomplete but nearly there. */
	/**
	 * Wraps a string.
	 *
	 * The output form is:
	 * <pre>
	 * prefix     in[0]
	 * &lt;--indent-&gt; in[1]
	 * &lt;--indent-&gt; in[2]
	 * &lt;-----width----&gt;
	 * </pre>
	 */
	/* XXX There's some of this in commons. */
	private static String wrap(String in, String prefix,
							int indent, int width) {
		StringBuilder   buf = new StringBuilder(prefix);

		while (buf.length() < indent)
			buf.append(' ');

		int             start = 0;

		while (start < in.length()) {
			while (start < in.length() &&
					Character.isWhitespace(in.charAt(start)))
				start++;

			int     end = start + width - indent;

			if (end > in.length()) {
				buf.append(in.substring(start));
				break;
			}

			int     idx = end;
			while (!Character.isWhitespace(in.charAt(idx)))
				idx--;

			if (idx == start) {
				idx = end - 1;
				buf.append(in.substring(start, idx));
				buf.append('-');
			}
			else {
				buf.append(in.substring(start, idx));
				start = idx;
			}

			start = idx;
		}

		return buf.toString();
	}

	private static void usage(String command, Option[] options) {
		StringBuilder   text = new StringBuilder("Usage: ");
		text.append(command).append('\n');
		for (int i = 0; i < options.length; i++) {
			StringBuilder   line = new StringBuilder();
			Option          opt = options[i];
			line.append("    --").append(opt.getName());
			switch (opt.getHasArg()) {
				case LongOpt.NO_ARGUMENT:
					break;
				case LongOpt.OPTIONAL_ARGUMENT:
					line.append("[=").append(opt.eg).append(']');
					break;
				case LongOpt.REQUIRED_ARGUMENT:
					line.append('=').append(opt.eg);
					break;
			}
			if (Character.isLetterOrDigit(opt.getVal()))
				line.append(" (-").append((char)opt.getVal()).append(")");
			if (line.length() < 30) {
				while (line.length() < 30)
					line.append(' ');
			}
			else {
				line.append('\n');
				for (int j = 0; j < 30; j++)
					line.append(' ');
			}
			/* This should use wrap. */
			line.append(opt.help);
			line.append('\n');
			text.append(line);
		}

		System.out.println(text);
	}



#if (false)
	public static void oldmain(String[] args) throws Exception {
		List<String>	path = new ArrayList<String>();
		path.add("/usr/include");
		path.add("/usr/local/include");
		path.add("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include");

		Source			source = new FileLexerSource(new File(args[0]));
		Preprocessor	pp = new Preprocessor(source);
		pp.setSystemIncludePath(path);

		for (int i = 1; i < args.length; i++) {
			pp.push_source(new FileLexerSource(new File(args[i])),true);
		}

		Macro			m = new Macro("__WORDSIZE");
		m.addToken(new Token(INTEGER, -1, -1, "32", Integer.valueOf(32)));
		pp.addMacro(m);

		m = new Macro("__STDC__");
		m.addToken(new Token(INTEGER, -1, -1, "1", Integer.valueOf(1)));
		pp.addMacro(m);

		try {
			for (;;) {
				Token	tok = pp.token();
				if (tok != null && tok.getType() == Token.EOF)
					break;
				switch (2) {
					case 0:
						System.out.print(tok);
						break;
					case 1:
						System.out.print("[" + tok.getText() + "]");
						break;
					case 2:
						System.out.print(tok.getText());
						break;
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			Source	s = pp.getSource();
			while (s != null) {
				System.out.println(" -> " + s);
				s = s.getParent();
			}

			/*
			Iterator<State>	it = pp.states.iterator();
			while (it.hasNext()) {
				System.out.println(" -? " + it.next());
			}
			*/

		}

		Map<String,Macro>	macros = pp.getMacros();
		List<String>		keys = new ArrayList<String>(
				macros.keySet()
					);
		Collections.sort(keys);
		Iterator<String>	mt = keys.iterator();
		while (mt.hasNext()) {
			String	key = mt.next();
			Macro	macro = macros.get(key);
			System.out.println("#" + "macro " + macro);
		}

	}
#end

}