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
|
/*
* 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.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import static org.anarres.cpp.Token.*;
/**
* (Currently a simple test class).
*/
public class Main {
public static void main(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.setIncludePath(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);
}
}
}
|