diff options
author | Shevek <[email protected]> | 2008-06-06 20:28:16 +0000 |
---|---|---|
committer | Shevek <[email protected]> | 2008-06-06 20:28:16 +0000 |
commit | 8ffeabfd101d4895cf6d046dd2868c359ce042ae (patch) | |
tree | 4a3322056f628294e77fc5c2c663686942ad0a91 | |
parent | 7ee4b6326d535a947c9f068066548b73485965d3 (diff) |
implement VFS properly
-rw-r--r-- | src/java/org/anarres/cpp/ChrootFileSystem.java | 72 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/CppTask.java | 5 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/Feature.java | 3 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/JavaFileSystem.java | 63 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/Main.java | 2 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/Preprocessor.java | 44 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/PreprocessorListener.java | 7 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/VirtualFile.java | 33 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/VirtualFileSystem.java | 26 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/Warning.java | 3 |
10 files changed, 237 insertions, 21 deletions
diff --git a/src/java/org/anarres/cpp/ChrootFileSystem.java b/src/java/org/anarres/cpp/ChrootFileSystem.java new file mode 100644 index 0000000..fda0463 --- /dev/null +++ b/src/java/org/anarres/cpp/ChrootFileSystem.java @@ -0,0 +1,72 @@ +/* + * 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; + +/** + * A virtual filesystem implementation using java.io in a virtual + * chroot. + */ +public class ChrootFileSystem implements VirtualFileSystem { + private File root; + + public ChrootFileSystem(File root) { + this.root = root; + } + + public ChrootFile getFile(String path) { + return new ChrootFile(path); + } + + public ChrootFile getFile(String dir, String name) { + return new ChrootFile(dir, name); + } + + private class ChrootFile extends File implements VirtualFile { + private File rfile; + + public ChrootFile(String path) { + super(path); + } + + public ChrootFile(String dir, String name) { + super(dir, name); + } + + /* private */ + public ChrootFile(File dir, String name) { + super(dir, name); + } + + public ChrootFile getParentFile() { + return new ChrootFile(getParent()); + } + + public ChrootFile getChildFile(String name) { + return new ChrootFile(this, name); + } + + public Source getSource() throws IOException { + return new FileLexerSource(new File(root, getPath()), + getPath()); + } + } + +} diff --git a/src/java/org/anarres/cpp/CppTask.java b/src/java/org/anarres/cpp/CppTask.java index af1bb8e..f86c2d1 100644 --- a/src/java/org/anarres/cpp/CppTask.java +++ b/src/java/org/anarres/cpp/CppTask.java @@ -28,9 +28,12 @@ import org.anarres.cpp.LexerException; import org.anarres.cpp.Preprocessor; import org.anarres.cpp.Token; +/** + * An ant task for jcpp. + */ public class CppTask extends Task { - public static class Macro { + private static class Macro { private String name; private String value; diff --git a/src/java/org/anarres/cpp/Feature.java b/src/java/org/anarres/cpp/Feature.java index 16dcf75..629b0ec 100644 --- a/src/java/org/anarres/cpp/Feature.java +++ b/src/java/org/anarres/cpp/Feature.java @@ -17,6 +17,9 @@ package org.anarres.cpp; +/** + * Features of the Preprocessor, which may be enabled or disabled. + */ public enum Feature { DIGRAPHS, TRIGRAPHS, diff --git a/src/java/org/anarres/cpp/JavaFileSystem.java b/src/java/org/anarres/cpp/JavaFileSystem.java new file mode 100644 index 0000000..25a13dc --- /dev/null +++ b/src/java/org/anarres/cpp/JavaFileSystem.java @@ -0,0 +1,63 @@ +/* + * 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; + +/** + * A virtual filesystem implementation using java.io. + */ +public class JavaFileSystem implements VirtualFileSystem { + public JavaFile getFile(String path) { + return new JavaFile(path); + } + + public JavaFile getFile(String dir, String name) { + return new JavaFile(dir, name); + } + + private class JavaFile extends File implements VirtualFile { + public JavaFile(String path) { + super(path); + } + + public JavaFile(String dir, String name) { + super(dir, name); + } + + /* private */ + public JavaFile(File dir, String name) { + super(dir, name); + } + + public JavaFile getParentFile() { + return new JavaFile(getParent()); + } + + public JavaFile getChildFile(String name) { + return new JavaFile(this, name); + } + + public Source getSource() throws IOException { + return new FileLexerSource(this); + } + + } + +} diff --git a/src/java/org/anarres/cpp/Main.java b/src/java/org/anarres/cpp/Main.java index 6aecd8e..b7e11df 100644 --- a/src/java/org/anarres/cpp/Main.java +++ b/src/java/org/anarres/cpp/Main.java @@ -40,7 +40,7 @@ import static org.anarres.cpp.Token.*; */ public class Main { - protected static class Option extends LongOpt { + private static class Option extends LongOpt { private String eg; private String help; public Option(String word, int arg, int ch, diff --git a/src/java/org/anarres/cpp/Preprocessor.java b/src/java/org/anarres/cpp/Preprocessor.java index 674114b..ed8da44 100644 --- a/src/java/org/anarres/cpp/Preprocessor.java +++ b/src/java/org/anarres/cpp/Preprocessor.java @@ -56,6 +56,7 @@ public class Preprocessor { private List<String> sysincludepath; /* -I */ private Set<Feature> features; private Set<Warning> warnings; + private VirtualFileSystem filesystem; private PreprocessorListener listener; public Preprocessor() { @@ -69,6 +70,8 @@ public class Preprocessor { this.sysincludepath = new ArrayList<String>(); this.features = EnumSet.noneOf(Feature.class); this.warnings = EnumSet.noneOf(Warning.class); + this.filesystem = new JavaFileSystem(); + this.listener = null; } public Preprocessor(Source initial) { @@ -85,6 +88,20 @@ public class Preprocessor { } /** + * Sets the VirtualFileSystem used by this Preprocessor. + */ + public void setFileSystem(VirtualFileSystem filesystem) { + this.filesystem = filesystem; + } + + /** + * Returns the VirtualFileSystem used by this Preprocessor. + */ + public VirtualFileSystem getFileSystem() { + return filesystem; + } + + /** * Sets the PreprocessorListener which handles events for * this Preprocessor. * @@ -942,25 +959,16 @@ public class Preprocessor { * User code may override this method to implement a virtual * file system. */ - protected boolean include(String path) + private boolean include(VirtualFile file) throws IOException, LexerException { - File file = new File(path); // System.out.println("Try to include " + file); if (!file.isFile()) return false; - push_source(new FileLexerSource(file), true); + push_source(file.getSource(), true); return true; } - protected String include_dirname(String path) { - return (new File(path)).getParentFile().getPath(); - } - - protected String include_filename(String dir, String name) { - return dir + File.separator + name; - } - /** * Includes a file from an include path, by name. */ @@ -968,7 +976,8 @@ public class Preprocessor { throws IOException, LexerException { for (String dir : path) { - if (include(include_filename(dir, name))) + VirtualFile file = filesystem.getFile(dir, name); + if (include(file)) return true; } return false; @@ -986,13 +995,10 @@ public class Preprocessor { throws IOException, LexerException { if (quoted) { - String dir = include_dirname(parent); - String path; - if (dir == null) - path = name; - else - path = include_filename(dir, name); - if (include(path)) + VirtualFile pfile = filesystem.getFile(parent); + VirtualFile dir = pfile.getParentFile(); + VirtualFile ifile = dir.getChildFile(name); + if (include(ifile)) return; if (include(quoteincludepath, name)) return; diff --git a/src/java/org/anarres/cpp/PreprocessorListener.java b/src/java/org/anarres/cpp/PreprocessorListener.java index 0c40932..d149a70 100644 --- a/src/java/org/anarres/cpp/PreprocessorListener.java +++ b/src/java/org/anarres/cpp/PreprocessorListener.java @@ -19,6 +19,13 @@ package org.anarres.cpp; import java.io.File; +/** + * A handler for preprocessor events, primarily errors and warnings. + * + * If no PreprocessorListener is installed in a Preprocessor, all + * error and warning events will throw an exception. Installing a + * listener allows more intelligent handling of these events. + */ public class PreprocessorListener { private int errors; diff --git a/src/java/org/anarres/cpp/VirtualFile.java b/src/java/org/anarres/cpp/VirtualFile.java new file mode 100644 index 0000000..405891a --- /dev/null +++ b/src/java/org/anarres/cpp/VirtualFile.java @@ -0,0 +1,33 @@ +/* + * 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.IOException; + +/** + * An extremely lightweight virtual file interface. + */ +public interface VirtualFile { + // public String getParent(); + public boolean isFile(); + public String getPath(); + public String getName(); + public VirtualFile getParentFile(); + public VirtualFile getChildFile(String name); + public Source getSource() throws IOException; +} diff --git a/src/java/org/anarres/cpp/VirtualFileSystem.java b/src/java/org/anarres/cpp/VirtualFileSystem.java new file mode 100644 index 0000000..eb5c4a1 --- /dev/null +++ b/src/java/org/anarres/cpp/VirtualFileSystem.java @@ -0,0 +1,26 @@ +/* + * 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; + +/** + * An extremely lightweight virtual file system interface. + */ +public interface VirtualFileSystem { + public VirtualFile getFile(String path); + public VirtualFile getFile(String dir, String name); +} diff --git a/src/java/org/anarres/cpp/Warning.java b/src/java/org/anarres/cpp/Warning.java index cb7755e..abe38d0 100644 --- a/src/java/org/anarres/cpp/Warning.java +++ b/src/java/org/anarres/cpp/Warning.java @@ -17,6 +17,9 @@ package org.anarres.cpp; +/** + * Warning classes which may optionally be emitted by the Preprocessor. + */ public enum Warning { TRIGRAPHS, // TRADITIONAL, |