diff options
author | Shevek <[email protected]> | 2014-01-29 00:27:28 -0800 |
---|---|---|
committer | Shevek <[email protected]> | 2014-01-29 00:27:28 -0800 |
commit | f2b03d6787e89255d68f5398a8a8e0d544f12405 (patch) | |
tree | 5ac3913339715f4f514d02745a5ec8245bdad90c /src/main/java/org/anarres | |
parent | c06119b6d97f700f3843b5b5e5679ff522b47963 (diff) |
Use gradle-velocity-task.
Update PreprocessorListener to be an interface.
Make Source.getName() public.
Diffstat (limited to 'src/main/java/org/anarres')
-rw-r--r-- | src/main/java/org/anarres/cpp/CppTask.java | 2 | ||||
-rw-r--r-- | src/main/java/org/anarres/cpp/DefaultPreprocessorListener.java | 89 | ||||
-rw-r--r-- | src/main/java/org/anarres/cpp/FileLexerSource.java | 2 | ||||
-rw-r--r-- | src/main/java/org/anarres/cpp/InputLexerSource.java | 2 | ||||
-rw-r--r-- | src/main/java/org/anarres/cpp/Main.java | 2 | ||||
-rw-r--r-- | src/main/java/org/anarres/cpp/PreprocessorListener.java | 41 | ||||
-rw-r--r-- | src/main/java/org/anarres/cpp/Source.java | 5 |
7 files changed, 101 insertions, 42 deletions
diff --git a/src/main/java/org/anarres/cpp/CppTask.java b/src/main/java/org/anarres/cpp/CppTask.java index 5e17786..55a8eff 100644 --- a/src/main/java/org/anarres/cpp/CppTask.java +++ b/src/main/java/org/anarres/cpp/CppTask.java @@ -34,7 +34,7 @@ import org.apache.tools.ant.types.Path; */ public class CppTask extends Copy { - private class Listener extends PreprocessorListener { + private class Listener extends DefaultPreprocessorListener { @Override protected void print(String msg) { diff --git a/src/main/java/org/anarres/cpp/DefaultPreprocessorListener.java b/src/main/java/org/anarres/cpp/DefaultPreprocessorListener.java new file mode 100644 index 0000000..6d3929d --- /dev/null +++ b/src/main/java/org/anarres/cpp/DefaultPreprocessorListener.java @@ -0,0 +1,89 @@ +package org.anarres.cpp; + +/* + * 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. + */ +import javax.annotation.Nonnegative; + +/** + * 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 DefaultPreprocessorListener implements PreprocessorListener { + + private int errors; + private int warnings; + + public DefaultPreprocessorListener() { + clear(); + } + + public void clear() { + errors = 0; + warnings = 0; + } + + @Nonnegative + public int getErrors() { + return errors; + } + + @Nonnegative + public int getWarnings() { + return warnings; + } + + protected void print(String msg) { + System.err.println(msg); + } + + /** + * Handles a warning. + * + * The behaviour of this method is defined by the + * implementation. It may simply record the error message, or + * it may throw an exception. + */ + public void handleWarning(Source source, int line, int column, + String msg) + throws LexerException { + warnings++; + print(source.getName() + ":" + line + ":" + column + + ": warning: " + msg); + } + + /** + * Handles an error. + * + * The behaviour of this method is defined by the + * implementation. It may simply record the error message, or + * it may throw an exception. + */ + public void handleError(Source source, int line, int column, + String msg) + throws LexerException { + errors++; + print(source.getName() + ":" + line + ":" + column + + ": error: " + msg); + } + + public void handleSourceChange(Source source, String event) { + } + +} diff --git a/src/main/java/org/anarres/cpp/FileLexerSource.java b/src/main/java/org/anarres/cpp/FileLexerSource.java index 1505506..bd6cbb7 100644 --- a/src/main/java/org/anarres/cpp/FileLexerSource.java +++ b/src/main/java/org/anarres/cpp/FileLexerSource.java @@ -79,7 +79,7 @@ public class FileLexerSource extends LexerSource { } @Override - /* pp */ String getName() { + public String getName() { return getPath(); } diff --git a/src/main/java/org/anarres/cpp/InputLexerSource.java b/src/main/java/org/anarres/cpp/InputLexerSource.java index 3e1dcb3..fac7c51 100644 --- a/src/main/java/org/anarres/cpp/InputLexerSource.java +++ b/src/main/java/org/anarres/cpp/InputLexerSource.java @@ -53,7 +53,7 @@ public class InputLexerSource extends LexerSource { } @Override - /* pp */ String getName() { + public String getName() { return "standard input"; } diff --git a/src/main/java/org/anarres/cpp/Main.java b/src/main/java/org/anarres/cpp/Main.java index 1902798..4c50b70 100644 --- a/src/main/java/org/anarres/cpp/Main.java +++ b/src/main/java/org/anarres/cpp/Main.java @@ -92,7 +92,7 @@ public class Main { pp.addFeature(Feature.TRIGRAPHS); pp.addFeature(Feature.LINEMARKERS); pp.addWarning(Warning.IMPORT); - pp.setListener(new PreprocessorListener()); + pp.setListener(new DefaultPreprocessorListener()); pp.addMacro("__JCPP__"); pp.getSystemIncludePath().add("/usr/local/include"); pp.getSystemIncludePath().add("/usr/include"); diff --git a/src/main/java/org/anarres/cpp/PreprocessorListener.java b/src/main/java/org/anarres/cpp/PreprocessorListener.java index a5b4339..6a4cb22 100644 --- a/src/main/java/org/anarres/cpp/PreprocessorListener.java +++ b/src/main/java/org/anarres/cpp/PreprocessorListener.java @@ -23,31 +23,7 @@ package org.anarres.cpp; * error and warning events will throw an exception. Installing a * listener allows more intelligent handling of these events. */ -public class PreprocessorListener { - - private int errors; - private int warnings; - - public PreprocessorListener() { - clear(); - } - - public void clear() { - errors = 0; - warnings = 0; - } - - public int getErrors() { - return errors; - } - - public int getWarnings() { - return warnings; - } - - protected void print(String msg) { - System.err.println(msg); - } +public interface PreprocessorListener { /** * Handles a warning. @@ -58,11 +34,7 @@ public class PreprocessorListener { */ public void handleWarning(Source source, int line, int column, String msg) - throws LexerException { - warnings++; - print(source.getName() + ":" + line + ":" + column - + ": warning: " + msg); - } + throws LexerException; /** * Handles an error. @@ -73,13 +45,8 @@ public class PreprocessorListener { */ public void handleError(Source source, int line, int column, String msg) - throws LexerException { - errors++; - print(source.getName() + ":" + line + ":" + column - + ": error: " + msg); - } + throws LexerException; - public void handleSourceChange(Source source, String event) { - } + public void handleSourceChange(Source source, String event); } diff --git a/src/main/java/org/anarres/cpp/Source.java b/src/main/java/org/anarres/cpp/Source.java index ef20803..291113a 100644 --- a/src/main/java/org/anarres/cpp/Source.java +++ b/src/main/java/org/anarres/cpp/Source.java @@ -20,6 +20,7 @@ import java.io.Closeable; import java.io.IOException; import java.util.Iterator; import javax.annotation.CheckForNull; +import javax.annotation.Nonnegative; import javax.annotation.Nonnull; import static org.anarres.cpp.Token.*; @@ -139,7 +140,8 @@ public abstract class Source implements Iterable<Token>, Closeable { /** * Returns the human-readable name of the current Source. */ - /* pp */ String getName() { + @CheckForNull + public String getName() { Source parent = getParent(); if (parent != null) return parent.getName(); @@ -149,6 +151,7 @@ public abstract class Source implements Iterable<Token>, Closeable { /** * Returns the current line number within this Source. */ + @Nonnegative public int getLine() { Source parent = getParent(); if (parent == null) |