diff options
author | Shevek <[email protected]> | 2008-05-08 19:33:22 +0000 |
---|---|---|
committer | Shevek <[email protected]> | 2008-05-08 19:33:22 +0000 |
commit | e38a59d3a0c51ee5548ea070945cb18f9aef45a7 (patch) | |
tree | 61d1315b3d46f41c18b779ae784b204fbd758395 /src/java/org/anarres | |
parent | 8247b6760685ab26fd518ea0ef13601587aad0f2 (diff) |
fix more to spec
Diffstat (limited to 'src/java/org/anarres')
-rw-r--r-- | src/java/org/anarres/cpp/Main.java | 61 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/Preprocessor.java | 79 |
2 files changed, 81 insertions, 59 deletions
diff --git a/src/java/org/anarres/cpp/Main.java b/src/java/org/anarres/cpp/Main.java index 253b9ad..e62ca83 100644 --- a/src/java/org/anarres/cpp/Main.java +++ b/src/java/org/anarres/cpp/Main.java @@ -53,15 +53,6 @@ public class Main { } } - private List<String> i_default; - private List<String> i_system; - private List<String> i_user; - private List<String> i_quote; - private Map<String,String> d_default; - private Map<String,String> d_user; - private Set<Warning> warnings; - private List<String> f_include; - private static final Option[] OPTS = new Option[] { new Option("help", LongOpt.NO_ARGUMENT, 'h', null, "Displays help and usage information."), @@ -69,15 +60,17 @@ public class Main { "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, 'i', "file", + 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("version", LongOpt.NO_ARGUMENT, 'V', null, + new Option("version", LongOpt.NO_ARGUMENT, 2, null, "Prints jcpp's version number (" + Version.getVersion() + ")"), }; @@ -96,15 +89,6 @@ public class Main { (new Main()).run(OPTS, args); } - public Main() { - i_default = new ArrayList<String>(); - i_system = new ArrayList<String>(); - i_user = new ArrayList<String>(); - i_quote = new ArrayList<String>(); - d_default = new HashMap<String,String>(); - d_user = new HashMap<String,String>(); - } - public void run(Option[] opts, String[] args) throws Exception { String sopts = getShortOpts(opts); Getopt g = new Getopt("jcpp", args, sopts, opts); @@ -115,23 +99,27 @@ public class Main { Preprocessor pp = new Preprocessor(); pp.addFeature(Feature.LINEMARKERS); + pp.addMacro("__JCPP__"); + GETOPT: while ((c = g.getopt()) != -1) { switch (c) { case 'D': arg = g.getOptarg(); idx = arg.indexOf('='); if (idx == -1) - d_user.put(arg, "1"); + pp.addMacro(arg); else - d_user.put(arg.substring(0, idx), + pp.addMacro(arg.substring(0, idx), arg.substring(idx + 1)); break; case 'U': - d_default.remove(g.getOptarg()); - d_user.remove(g.getOptarg()); + pp.getMacros().remove(g.getOptarg()); break; case 'I': - i_user.add(g.getOptarg()); + pp.getSystemIncludePath().add(g.getOptarg()); + break; + case 0: // --iquote= + pp.getQuoteIncludePath().add(g.getOptarg()); break; case 'W': arg = g.getOptarg().toUpperCase(); @@ -144,14 +132,14 @@ public class Main { case 'w': pp.getWarnings().clear(); break; - case 'i': + case 1: // --include= // pp.addInput(new File(g.getOptarg())); // Comply exactly with spec. pp.addInput(new StringLexerSource( "#" + "include \"" + g.getOptarg() + "\"\n" )); break; - case 'V': + case 2: // --version System.out.println("Anarres Java C Preprocessor version " + Version.getVersion()); System.out.println("Copyright (C) 2008 Shevek (http://www.anarres.org/)."); System.out.println("This is free software; see the source for copying conditions. There is NO"); @@ -166,14 +154,10 @@ public class Main { } } - /* XXX include-path, include-files. */ - - for (Map.Entry<String,String> e : d_default.entrySet()) - pp.addMacro(e.getKey(), e.getValue()); - for (Map.Entry<String,String> e : d_user.entrySet()) - pp.addMacro(e.getKey(), e.getValue()); - - /* XXX Include paths. */ + List<String> path = pp.getSystemIncludePath(); + path.add("/usr/local/include"); + path.add("/usr/include"); + path.add("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include"); for (int i = g.getOptind(); i < args.length; i++) pp.addInput(new FileLexerSource(new File(args[i]))); @@ -203,6 +187,8 @@ public class Main { 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( @@ -294,7 +280,8 @@ public class Main { line.append('=').append(opt.eg); break; } - line.append(" (-").append((char)opt.getVal()).append(")"); + if (Character.isLetterOrDigit(opt.getVal())) + line.append(" (-").append((char)opt.getVal()).append(")"); if (line.length() < 30) { while (line.length() < 30) line.append(' '); @@ -323,7 +310,7 @@ public class Main { Source source = new FileLexerSource(new File(args[0])); Preprocessor pp = new Preprocessor(source); - pp.setIncludePath(path); + pp.setSystemIncludePath(path); for (int i = 1; i < args.length; i++) { pp.push_source(new FileLexerSource(new File(args[i])),true); diff --git a/src/java/org/anarres/cpp/Preprocessor.java b/src/java/org/anarres/cpp/Preprocessor.java index 63304b0..b3b1169 100644 --- a/src/java/org/anarres/cpp/Preprocessor.java +++ b/src/java/org/anarres/cpp/Preprocessor.java @@ -54,7 +54,8 @@ public class Preprocessor { private Stack<State> states; private Source source; - private List<String> path; + private List<String> quoteincludepath; /* -iquote */ + private List<String> sysincludepath; /* -I */ private Set<Feature> features; private Set<Warning> warnings; private PreprocessorListener listener; @@ -66,7 +67,8 @@ public class Preprocessor { this.states = new Stack<State>(); states.push(new State()); this.source = null; - this.path = null; + this.quoteincludepath = new ArrayList<String>(); + this.sysincludepath = new ArrayList<String>(); this.features = EnumSet.noneOf(Feature.class); features.add(Feature.DIGRAPHS); features.add(Feature.TRIGRAPHS); @@ -230,11 +232,27 @@ public class Preprocessor { } /** - * Sets the include path used by this Preprocessor. + * Sets the user include path used by this Preprocessor. */ /* Note for future: Create an IncludeHandler? */ - public void setIncludePath(List<String> path) { - this.path = path; + public void setQuoteIncludePath(List<String> path) { + this.quoteincludepath = path; + } + + public List<String> getQuoteIncludePath() { + return quoteincludepath; + } + + /** + * Sets the system include path used by this Preprocessor. + */ + /* Note for future: Create an IncludeHandler? */ + public void setSystemIncludePath(List<String> path) { + this.sysincludepath = path; + } + + public List<String> getSystemIncludePath() { + return sysincludepath; } /** @@ -804,6 +822,28 @@ public class Preprocessor { return source_skipline(true); } + protected boolean include(File file) + throws IOException, + LexerException { + if (!file.exists()) + return false; + if (!file.isFile()) + return false; + push_source(new FileLexerSource(file), true); + return true; + } + + protected boolean include(Iterable<String> path, String name) + throws IOException, + LexerException { + for (String dir : path) { + File file = new File(dir + File.separator + name); + if (include(file)) + return true; + } + return false; + } + /** * Handles a include directive. * @@ -820,27 +860,22 @@ public class Preprocessor { if (dir == null) dir = new File("/"); File file = new File(dir, name); - // System.err.println("Include: " + file); - if (file.exists() && file.isFile()) { - push_source(new FileLexerSource(file), true); + if (include(file)) + return; + if (include(quoteincludepath, name)) return; - } } - if (path != null) { - for (int i = 0; i < path.size(); i++) { - File file = new File( - path.get(i) + File.separator + name - ); - if (file.exists() && file.isFile()) { - // System.err.println("Include: " + file); - push_source(new FileLexerSource(file), true); - return; - } - } - } + if (include(sysincludepath, name)) + return; - error(line, 0, "File not found: " + name + " in " + path); + StringBuilder buf = new StringBuilder(); + if (quoted) + for (String dir : quoteincludepath) + buf.append(" ").append(dir); + for (String dir : sysincludepath) + buf.append(" ").append(dir); + error(line, 0, "File not found: " + name + " in " + buf); } private Token include() |