diff options
author | Shevek <[email protected]> | 2014-09-10 16:10:52 -0700 |
---|---|---|
committer | Shevek <[email protected]> | 2014-09-10 19:07:35 -0700 |
commit | bcc3a2b96b0e8b8b03dad0babd7103903cc9df3f (patch) | |
tree | 09a2833189524f55e5a6c149cc0e4650a6e5a5f0 /src/main | |
parent | aefac8ce01aee2f538db413d5812613facee9c55 (diff) |
NetBeans refactorings to standardize codebase.
Diffstat (limited to 'src/main')
20 files changed, 364 insertions, 401 deletions
diff --git a/src/main/java/org/anarres/cpp/Argument.java b/src/main/java/org/anarres/cpp/Argument.java index a868db5..31d9e93 100644 --- a/src/main/java/org/anarres/cpp/Argument.java +++ b/src/main/java/org/anarres/cpp/Argument.java @@ -14,16 +14,13 @@ * or implied. See the License for the specific language governing * permissions and limitations under the License. */ - package org.anarres.cpp; import java.io.IOException; - import java.util.ArrayList; -import java.util.List; import java.util.Iterator; - -import static org.anarres.cpp.Token.*; +import java.util.List; +import javax.annotation.Nonnull; /** * A macro argument. @@ -31,47 +28,48 @@ import static org.anarres.cpp.Token.*; * This encapsulates a raw and preprocessed token stream. */ /* pp */ class Argument extends ArrayList<Token> { - public static final int NO_ARGS = -1; - private List<Token> expansion; + private List<Token> expansion; - public Argument() { - this.expansion = null; - } + public Argument() { + this.expansion = null; + } - public void addToken(Token tok) { - add(tok); - } + public void addToken(@Nonnull Token tok) { + add(tok); + } - /* pp */ void expand(Preprocessor p) - throws IOException, - LexerException { - /* Cache expansion. */ - if (expansion == null) { - this.expansion = p.expand(this); - // System.out.println("Expanded arg " + this); - } - } + /* pp */ void expand(@Nonnull Preprocessor p) + throws IOException, + LexerException { + /* Cache expansion. */ + if (expansion == null) { + this.expansion = p.expand(this); + // System.out.println("Expanded arg " + this); + } + } - public Iterator<Token> expansion() { - return expansion.iterator(); - } + @Nonnull + public Iterator<Token> expansion() { + return expansion.iterator(); + } - public String toString() { - StringBuilder buf = new StringBuilder(); - buf.append("Argument("); - // buf.append(super.toString()); - buf.append("raw=[ "); - for (int i = 0; i < size(); i++) - buf.append(get(i).getText()); - buf.append(" ];expansion=[ "); - if (expansion == null) - buf.append("null"); - else - for (int i = 0; i < expansion.size(); i++) - buf.append(expansion.get(i).getText()); - buf.append(" ])"); - return buf.toString(); - } + @Override + public String toString() { + StringBuilder buf = new StringBuilder(); + buf.append("Argument("); + // buf.append(super.toString()); + buf.append("raw=[ "); + for (int i = 0; i < size(); i++) + buf.append(get(i).getText()); + buf.append(" ];expansion=[ "); + if (expansion == null) + buf.append("null"); + else + for (Token token : expansion) + buf.append(token.getText()); + buf.append(" ])"); + return buf.toString(); + } } diff --git a/src/main/java/org/anarres/cpp/CppReader.java b/src/main/java/org/anarres/cpp/CppReader.java index 7307f56..9157a93 100644 --- a/src/main/java/org/anarres/cpp/CppReader.java +++ b/src/main/java/org/anarres/cpp/CppReader.java @@ -19,8 +19,9 @@ package org.anarres.cpp; import java.io.Closeable; import java.io.IOException; import java.io.Reader; - -import static org.anarres.cpp.Token.*; +import static org.anarres.cpp.Token.CCOMMENT; +import static org.anarres.cpp.Token.CPPCOMMENT; +import static org.anarres.cpp.Token.EOF; /** * A Reader wrapper around the Preprocessor. @@ -108,13 +109,7 @@ public class CppReader extends Reader implements Closeable { } return true; } catch (LexerException e) { - /* Never happens. - if (e.getCause() instanceof IOException) - throw (IOException)e.getCause(); - */ - IOException ie = new IOException(String.valueOf(e)); - ie.initCause(e); - throw ie; + throw new IOException(String.valueOf(e), e); } } diff --git a/src/main/java/org/anarres/cpp/CppTask.java b/src/main/java/org/anarres/cpp/CppTask.java index 55a8eff..66df1a6 100644 --- a/src/main/java/org/anarres/cpp/CppTask.java +++ b/src/main/java/org/anarres/cpp/CppTask.java @@ -19,8 +19,8 @@ package org.anarres.cpp; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.util.Arrays; import java.util.ArrayList; +import java.util.Arrays; import java.util.Enumeration; import java.util.List; import org.apache.tools.ant.BuildException; diff --git a/src/main/java/org/anarres/cpp/Feature.java b/src/main/java/org/anarres/cpp/Feature.java index e119005..cc7f818 100644 --- a/src/main/java/org/anarres/cpp/Feature.java +++ b/src/main/java/org/anarres/cpp/Feature.java @@ -14,32 +14,30 @@ * or implied. See the License for the specific language governing * permissions and limitations under the License. */ - package org.anarres.cpp; /** * Features of the Preprocessor, which may be enabled or disabled. */ public enum Feature { - /** Supports ANSI digraphs. */ - DIGRAPHS, - /** Supports ANSI trigraphs. */ - TRIGRAPHS, - /** Outputs linemarker tokens. */ - LINEMARKERS, - /** Reports tokens of type INVALID as errors. */ - CSYNTAX, - /** Preserves comments in the lexed output. */ - KEEPCOMMENTS, - /** Preserves comments in the lexed output, even when inactive. */ - KEEPALLCOMMENTS, - VERBOSE, - DEBUG, - - /** Supports lexing of objective-C. */ - OBJCSYNTAX, - INCLUDENEXT, + /** Supports ANSI digraphs. */ + DIGRAPHS, + /** Supports ANSI trigraphs. */ + TRIGRAPHS, + /** Outputs linemarker tokens. */ + LINEMARKERS, + /** Reports tokens of type INVALID as errors. */ + CSYNTAX, + /** Preserves comments in the lexed output. */ + KEEPCOMMENTS, + /** Preserves comments in the lexed output, even when inactive. */ + KEEPALLCOMMENTS, + VERBOSE, + DEBUG, + /** Supports lexing of objective-C. */ + OBJCSYNTAX, + INCLUDENEXT, /** Random extensions. */ PRAGMA_ONCE } diff --git a/src/main/java/org/anarres/cpp/FileLexerSource.java b/src/main/java/org/anarres/cpp/FileLexerSource.java index 3f5a0d3..bdc411f 100644 --- a/src/main/java/org/anarres/cpp/FileLexerSource.java +++ b/src/main/java/org/anarres/cpp/FileLexerSource.java @@ -20,7 +20,6 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; - import javax.annotation.Nonnull; /** diff --git a/src/main/java/org/anarres/cpp/FixedTokenSource.java b/src/main/java/org/anarres/cpp/FixedTokenSource.java index a1c7500..4d9f41f 100644 --- a/src/main/java/org/anarres/cpp/FixedTokenSource.java +++ b/src/main/java/org/anarres/cpp/FixedTokenSource.java @@ -17,7 +17,6 @@ package org.anarres.cpp; import java.io.IOException; - import java.util.Arrays; import java.util.List; diff --git a/src/main/java/org/anarres/cpp/InputLexerSource.java b/src/main/java/org/anarres/cpp/InputLexerSource.java index 5eb8f94..ab2e397 100644 --- a/src/main/java/org/anarres/cpp/InputLexerSource.java +++ b/src/main/java/org/anarres/cpp/InputLexerSource.java @@ -17,9 +17,9 @@ package org.anarres.cpp; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.IOException; /** * A {@link Source} which lexes a file. diff --git a/src/main/java/org/anarres/cpp/InternalException.java b/src/main/java/org/anarres/cpp/InternalException.java index ac53017..fc3b650 100644 --- a/src/main/java/org/anarres/cpp/InternalException.java +++ b/src/main/java/org/anarres/cpp/InternalException.java @@ -14,7 +14,6 @@ * or implied. See the License for the specific language governing * permissions and limitations under the License. */ - package org.anarres.cpp; /** @@ -25,7 +24,8 @@ package org.anarres.cpp; * report it as a bug. */ public class InternalException extends RuntimeException { - public InternalException(String msg) { - super(msg); - } + + public InternalException(String msg) { + super(msg); + } } diff --git a/src/main/java/org/anarres/cpp/JoinReader.java b/src/main/java/org/anarres/cpp/JoinReader.java index 0286577..c39ee79 100644 --- a/src/main/java/org/anarres/cpp/JoinReader.java +++ b/src/main/java/org/anarres/cpp/JoinReader.java @@ -17,8 +17,8 @@ package org.anarres.cpp; import java.io.Closeable; -import java.io.Reader; import java.io.IOException; +import java.io.Reader; /* pp */ class JoinReader /* extends Reader */ implements Closeable { diff --git a/src/main/java/org/anarres/cpp/LexerException.java b/src/main/java/org/anarres/cpp/LexerException.java index 41c6275..d4b2e9c 100644 --- a/src/main/java/org/anarres/cpp/LexerException.java +++ b/src/main/java/org/anarres/cpp/LexerException.java @@ -14,7 +14,6 @@ * or implied. See the License for the specific language governing * permissions and limitations under the License. */ - package org.anarres.cpp; /** @@ -23,11 +22,12 @@ package org.anarres.cpp; * Note to users: I don't really like the name of this class. S. */ public class LexerException extends Exception { - public LexerException(String msg) { - super(msg); - } - public LexerException(Throwable cause) { - super(cause); - } + public LexerException(String msg) { + super(msg); + } + + public LexerException(Throwable cause) { + super(cause); + } } diff --git a/src/main/java/org/anarres/cpp/LexerSource.java b/src/main/java/org/anarres/cpp/LexerSource.java index d9331fe..ef779b7 100644 --- a/src/main/java/org/anarres/cpp/LexerSource.java +++ b/src/main/java/org/anarres/cpp/LexerSource.java @@ -18,7 +18,6 @@ package org.anarres.cpp; import java.io.IOException; import java.io.Reader; - import javax.annotation.Nonnull; import static org.anarres.cpp.Token.*; diff --git a/src/main/java/org/anarres/cpp/Macro.java b/src/main/java/org/anarres/cpp/Macro.java index 534cb2b..7ab38a8 100644 --- a/src/main/java/org/anarres/cpp/Macro.java +++ b/src/main/java/org/anarres/cpp/Macro.java @@ -14,7 +14,6 @@ * or implied. See the License for the specific language governing * permissions and limitations under the License. */ - package org.anarres.cpp; import java.util.ArrayList; @@ -29,161 +28,160 @@ import java.util.List; * extra tokens {@link Token#M_ARG} and {@link Token#M_STRING}. */ public class Macro { - private Source source; - private String name; - /* It's an explicit decision to keep these around here. We don't - * need to; the argument token type is M_ARG and the value - * is the index. The strings themselves are only used in - * stringification of the macro, for debugging. */ - private List<String> args; - private boolean variadic; - private List<Token> tokens; - - public Macro(Source source, String name) { - this.source = source; - this.name = name; - this.args = null; - this.variadic = false; - this.tokens = new ArrayList<Token>(); - } - - public Macro(String name) { - this(null, name); - } - - /** - * Sets the Source from which this macro was parsed. - */ - public void setSource(Source s) { - this.source = s; - } - - /** - * Returns the Source from which this macro was parsed. - * - * This method may return null if the macro was not parsed - * from a regular file. - */ - public Source getSource() { - return source; - } - - /** - * Returns the name of this macro. - */ - public String getName() { - return name; - } - - /** - * Sets the arguments to this macro. - */ - public void setArgs(List<String> args) { - this.args = args; - } - - /** - * Returns true if this is a function-like macro. - */ - public boolean isFunctionLike() { - return args != null; - } - - /** - * Returns the number of arguments to this macro. - */ - public int getArgs() { - return args.size(); - } - - /** - * Sets the variadic flag on this Macro. - */ - public void setVariadic(boolean b) { - this.variadic = b; - } - - /** - * Returns true if this is a variadic function-like macro. - */ - public boolean isVariadic() { - return variadic; - } - - /** - * Adds a token to the expansion of this macro. - */ - public void addToken(Token tok) { - this.tokens.add(tok); - } - - /** - * Adds a "paste" operator to the expansion of this macro. - * - * A paste operator causes the next token added to be pasted - * to the previous token when the macro is expanded. - * It is an error for a macro to end with a paste token. - */ - public void addPaste(Token tok) { - /* - * Given: tok0 ## tok1 - * We generate: M_PASTE, tok0, tok1 - * This extends as per a stack language: - * tok0 ## tok1 ## tok2 -> - * M_PASTE, tok0, M_PASTE, tok1, tok2 - */ - this.tokens.add(tokens.size() - 1, tok); - } - - /* pp */ List<Token> getTokens() { - return tokens; - } - - /* Paste tokens are inserted before the first of the two pasted - * tokens, so it's a kind of bytecode notation. This method - * swaps them around again. We know that there will never be two - * sequential paste tokens, so a boolean is sufficient. */ - public String getText() { - StringBuilder buf = new StringBuilder(); - boolean paste = false; - for (int i = 0; i < tokens.size(); i++) { - Token tok = tokens.get(i); - if (tok.getType() == Token.M_PASTE) { - assert paste == false : "Two sequential pastes."; - paste = true; - continue; - } - else { - buf.append(tok.getText()); - } - if (paste) { - buf.append(" #" + "# "); - paste = false; - } - // buf.append(tokens.get(i)); - } - return buf.toString(); - } + + private Source source; + private String name; + /* It's an explicit decision to keep these around here. We don't + * need to; the argument token type is M_ARG and the value + * is the index. The strings themselves are only used in + * stringification of the macro, for debugging. */ + private List<String> args; + private boolean variadic; + private List<Token> tokens; + + public Macro(Source source, String name) { + this.source = source; + this.name = name; + this.args = null; + this.variadic = false; + this.tokens = new ArrayList<Token>(); + } + + public Macro(String name) { + this(null, name); + } + + /** + * Sets the Source from which this macro was parsed. + */ + public void setSource(Source s) { + this.source = s; + } + + /** + * Returns the Source from which this macro was parsed. + * + * This method may return null if the macro was not parsed + * from a regular file. + */ + public Source getSource() { + return source; + } + + /** + * Returns the name of this macro. + */ + public String getName() { + return name; + } + + /** + * Sets the arguments to this macro. + */ + public void setArgs(List<String> args) { + this.args = args; + } + + /** + * Returns true if this is a function-like macro. + */ + public boolean isFunctionLike() { + return args != null; + } + + /** + * Returns the number of arguments to this macro. + */ + public int getArgs() { + return args.size(); + } + + /** + * Sets the variadic flag on this Macro. + */ + public void setVariadic(boolean b) { + this.variadic = b; + } + + /** + * Returns true if this is a variadic function-like macro. + */ + public boolean isVariadic() { + return variadic; + } + + /** + * Adds a token to the expansion of this macro. + */ + public void addToken(Token tok) { + this.tokens.add(tok); + } + + /** + * Adds a "paste" operator to the expansion of this macro. + * + * A paste operator causes the next token added to be pasted + * to the previous token when the macro is expanded. + * It is an error for a macro to end with a paste token. + */ + public void addPaste(Token tok) { + /* + * Given: tok0 ## tok1 + * We generate: M_PASTE, tok0, tok1 + * This extends as per a stack language: + * tok0 ## tok1 ## tok2 -> + * M_PASTE, tok0, M_PASTE, tok1, tok2 + */ + this.tokens.add(tokens.size() - 1, tok); + } + + /* pp */ List<Token> getTokens() { + return tokens; + } + + /* Paste tokens are inserted before the first of the two pasted + * tokens, so it's a kind of bytecode notation. This method + * swaps them around again. We know that there will never be two + * sequential paste tokens, so a boolean is sufficient. */ + public String getText() { + StringBuilder buf = new StringBuilder(); + boolean paste = false; + for (Token tok : tokens) { + if (tok.getType() == Token.M_PASTE) { + assert paste == false : "Two sequential pastes."; + paste = true; + continue; + } else { + buf.append(tok.getText()); + } + if (paste) { + buf.append(" #" + "# "); + paste = false; + } + // buf.append(tokens.get(i)); + } + return buf.toString(); + } @Override - public String toString() { - StringBuilder buf = new StringBuilder(name); - if (args != null) { - buf.append('('); - Iterator<String> it = args.iterator(); - while (it.hasNext()) { - buf.append(it.next()); - if (it.hasNext()) - buf.append(", "); - else if (isVariadic()) - buf.append("..."); - } - buf.append(')'); - } - if (!tokens.isEmpty()) { - buf.append(" => ").append(getText()); - } - return buf.toString(); - } + public String toString() { + StringBuilder buf = new StringBuilder(name); + if (args != null) { + buf.append('('); + Iterator<String> it = args.iterator(); + while (it.hasNext()) { + buf.append(it.next()); + if (it.hasNext()) + buf.append(", "); + else if (isVariadic()) + buf.append("..."); + } + buf.append(')'); + } + if (!tokens.isEmpty()) { + buf.append(" => ").append(getText()); + } + return buf.toString(); + } } diff --git a/src/main/java/org/anarres/cpp/MacroTokenSource.java b/src/main/java/org/anarres/cpp/MacroTokenSource.java index 92317eb..5874584 100644 --- a/src/main/java/org/anarres/cpp/MacroTokenSource.java +++ b/src/main/java/org/anarres/cpp/MacroTokenSource.java @@ -19,7 +19,6 @@ package org.anarres.cpp; import java.io.IOException; import java.util.Iterator; import java.util.List; - import javax.annotation.Nonnull; import static org.anarres.cpp.Token.*; diff --git a/src/main/java/org/anarres/cpp/Main.java b/src/main/java/org/anarres/cpp/Main.java index 4c50b70..3bc02eb 100644 --- a/src/main/java/org/anarres/cpp/Main.java +++ b/src/main/java/org/anarres/cpp/Main.java @@ -16,11 +16,11 @@ */ package org.anarres.cpp; +import gnu.getopt.Getopt; +import gnu.getopt.LongOpt; import java.io.File; import java.io.PrintStream; import java.util.EnumSet; -import gnu.getopt.Getopt; -import gnu.getopt.LongOpt; /** * (Currently a simple test class). @@ -29,8 +29,8 @@ public class Main { private static class Option extends LongOpt { - private String eg; - private String help; + private final String eg; + private final String help; public Option(String word, int arg, int ch, String eg, String help) { @@ -204,8 +204,8 @@ public class Main { 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(); + for (Option opt : opts) { + char c = (char) opt.getVal(); if (!Character.isLetterOrDigit(c)) continue; for (int j = 0; j < buf.length(); j++) @@ -214,7 +214,7 @@ public class Main { "Duplicate short option " + c ); buf.append(c); - switch (opts[i].getHasArg()) { + switch (opt.getHasArg()) { case LongOpt.NO_ARGUMENT: break; case LongOpt.OPTIONAL_ARGUMENT: @@ -284,9 +284,9 @@ public class Main { 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++) { + for (Option option : options) { StringBuilder line = new StringBuilder(); - Option opt = options[i]; + Option opt = option; line.append(" --").append(opt.getName()); switch (opt.getHasArg()) { case LongOpt.NO_ARGUMENT: diff --git a/src/main/java/org/anarres/cpp/NumericValue.java b/src/main/java/org/anarres/cpp/NumericValue.java index e38d28f..52fd2d8 100644 --- a/src/main/java/org/anarres/cpp/NumericValue.java +++ b/src/main/java/org/anarres/cpp/NumericValue.java @@ -101,7 +101,7 @@ public class NumericValue extends Number { if (t_fraction != null) { text += getFractionalPart(); // XXX Wrong for anything but base 10. - scale += getFractionalPart().length(); + scale += t_fraction.length(); } if (getExponent() != null) scale -= Integer.parseInt(getExponent()); @@ -148,7 +148,7 @@ public class NumericValue extends Number { if (expbase == 2) v = v << exponentValue(); else if (expbase != 0) - v = (int) (v * Math.pow(expbase, exponentValue())); + v = (long) (v * Math.pow(expbase, exponentValue())); return v; } diff --git a/src/main/java/org/anarres/cpp/Preprocessor.java b/src/main/java/org/anarres/cpp/Preprocessor.java index 3569b86..43fba89 100644 --- a/src/main/java/org/anarres/cpp/Preprocessor.java +++ b/src/main/java/org/anarres/cpp/Preprocessor.java @@ -19,7 +19,6 @@ package org.anarres.cpp; import java.io.Closeable; import java.io.File; import java.io.IOException; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -27,16 +26,15 @@ import java.util.Collections; import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Stack; - +import java.util.TreeMap; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; -import static org.anarres.cpp.Token.*; import static org.anarres.cpp.PreprocessorCommand.*; +import static org.anarres.cpp.Token.*; /** * A C Preprocessor. @@ -587,7 +585,7 @@ public class Preprocessor implements Closeable { .append(" \""); /* XXX This call to escape(name) is correct but ugly. */ if (name == null) - buf.append("<unnamed-source>"); + buf.append("<no file>"); else MacroTokenSource.escape(buf, name); buf.append("\"").append(extra).append("\n"); @@ -792,12 +790,8 @@ public class Preprocessor implements Closeable { return false; } - /* - * for (Argument a : args) - * a.expand(this); - */ - for (int i = 0; i < args.size(); i++) { - args.get(i).expand(this); + for (Argument a : args) { + a.expand(this); } // System.out.println("Macro " + m + " args " + args); @@ -1537,7 +1531,7 @@ public class Preprocessor implements Closeable { lhs = value.longValue(); break; case CHARACTER: - lhs = (long) ((Character) tok.getValue()).charValue(); + lhs = ((Character) tok.getValue()).charValue(); break; case IDENTIFIER: if (warnings.contains(Warning.UNDEF)) @@ -2079,15 +2073,8 @@ public class Preprocessor implements Closeable { s = s.getParent(); } - Map<String, Macro> macros = 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 = getMacro(key); + Map<String, Macro> macros = new TreeMap<String, Macro>(getMacros()); + for (Macro macro : macros.values()) { buf.append("#").append("macro ").append(macro).append("\n"); } diff --git a/src/main/java/org/anarres/cpp/Source.java b/src/main/java/org/anarres/cpp/Source.java index 76ee1f2..3fba3ab 100644 --- a/src/main/java/org/anarres/cpp/Source.java +++ b/src/main/java/org/anarres/cpp/Source.java @@ -22,7 +22,11 @@ import java.util.Iterator; import javax.annotation.CheckForNull; import javax.annotation.Nonnegative; import javax.annotation.Nonnull; -import static org.anarres.cpp.Token.*; +import static org.anarres.cpp.Token.CCOMMENT; +import static org.anarres.cpp.Token.CPPCOMMENT; +import static org.anarres.cpp.Token.EOF; +import static org.anarres.cpp.Token.NL; +import static org.anarres.cpp.Token.WHITESPACE; /** * An input to the Preprocessor. diff --git a/src/main/java/org/anarres/cpp/SourceIterator.java b/src/main/java/org/anarres/cpp/SourceIterator.java index d5c63c7..82e36d9 100644 --- a/src/main/java/org/anarres/cpp/SourceIterator.java +++ b/src/main/java/org/anarres/cpp/SourceIterator.java @@ -14,79 +14,74 @@ * or implied. See the License for the specific language governing * permissions and limitations under the License. */ - package org.anarres.cpp; import java.io.IOException; - import java.util.Iterator; import java.util.NoSuchElementException; - -import static org.anarres.cpp.Token.*; +import static org.anarres.cpp.Token.EOF; /** * An Iterator for {@link Source Sources}, * returning {@link Token Tokens}. */ public class SourceIterator implements Iterator<Token> { - private Source source; - private Token tok; - public SourceIterator(Source s) { - this.source = s; - this.tok = null; - } + private final Source source; + private Token tok; - /** - * Rethrows IOException inside IllegalStateException. - */ - private void advance() { - try { - if (tok == null) - tok = source.token(); - } - catch (LexerException e) { - throw new IllegalStateException(e); - } - catch (IOException e) { - throw new IllegalStateException(e); - } - } + public SourceIterator(Source s) { + this.source = s; + this.tok = null; + } - /** - * Returns true if the enclosed Source has more tokens. - * - * The EOF token is never returned by the iterator. - * @throws IllegalStateException if the Source - * throws a LexerException or IOException - */ - public boolean hasNext() { - advance(); - return tok.getType() != EOF; - } + /** + * Rethrows IOException inside IllegalStateException. + */ + private void advance() { + try { + if (tok == null) + tok = source.token(); + } catch (LexerException e) { + throw new IllegalStateException(e); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } - /** - * Returns the next token from the enclosed Source. - * - * The EOF token is never returned by the iterator. - * @throws IllegalStateException if the Source - * throws a LexerException or IOException - */ - public Token next() { - if (!hasNext()) - throw new NoSuchElementException(); - Token t = this.tok; - this.tok = null; - return t; - } + /** + * Returns true if the enclosed Source has more tokens. + * + * The EOF token is never returned by the iterator. + * @throws IllegalStateException if the Source + * throws a LexerException or IOException + */ + public boolean hasNext() { + advance(); + return tok.getType() != EOF; + } - /** - * Not supported. - * - * @throws UnsupportedOperationException. - */ - public void remove() { - throw new UnsupportedOperationException(); - } -} + /** + * Returns the next token from the enclosed Source. + * + * The EOF token is never returned by the iterator. + * @throws IllegalStateException if the Source + * throws a LexerException or IOException + */ + public Token next() { + if (!hasNext()) + throw new NoSuchElementException(); + Token t = this.tok; + this.tok = null; + return t; + } + /** + * Not supported. + * + * @throws UnsupportedOperationException. + */ + public void remove() { + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/org/anarres/cpp/State.java b/src/main/java/org/anarres/cpp/State.java index ed5c736..e24195d 100644 --- a/src/main/java/org/anarres/cpp/State.java +++ b/src/main/java/org/anarres/cpp/State.java @@ -14,54 +14,55 @@ * or implied. See the License for the specific language governing * permissions and limitations under the License. */ - package org.anarres.cpp; /* pp */ class State { - boolean parent; - boolean active; - boolean sawElse; - /* pp */ State() { - this.parent = true; - this.active = true; - this.sawElse = false; - } + boolean parent; + boolean active; + boolean sawElse; + + /* pp */ State() { + this.parent = true; + this.active = true; + this.sawElse = false; + } - /* pp */ State(State parent) { - this.parent = parent.isParentActive() && parent.isActive(); - this.active = true; - this.sawElse = false; - } + /* pp */ State(State parent) { + this.parent = parent.isParentActive() && parent.isActive(); + this.active = true; + this.sawElse = false; + } - /* Required for #elif */ - /* pp */ void setParentActive(boolean b) { - this.parent = b; - } + /* Required for #elif */ + /* pp */ void setParentActive(boolean b) { + this.parent = b; + } - /* pp */ boolean isParentActive() { - return parent; - } + /* pp */ boolean isParentActive() { + return parent; + } - /* pp */ void setActive(boolean b) { - this.active = b; - } + /* pp */ void setActive(boolean b) { + this.active = b; + } - /* pp */ boolean isActive() { - return active; - } + /* pp */ boolean isActive() { + return active; + } - /* pp */ void setSawElse() { - sawElse = true; - } + /* pp */ void setSawElse() { + sawElse = true; + } - /* pp */ boolean sawElse() { - return sawElse; - } + /* pp */ boolean sawElse() { + return sawElse; + } - public String toString() { - return "parent=" + parent + - ", active=" + active + - ", sawelse=" + sawElse; - } + @Override + public String toString() { + return "parent=" + parent + + ", active=" + active + + ", sawelse=" + sawElse; + } } diff --git a/src/main/java/org/anarres/cpp/TokenSnifferSource.java b/src/main/java/org/anarres/cpp/TokenSnifferSource.java index 1512b2e..21e1cb8 100644 --- a/src/main/java/org/anarres/cpp/TokenSnifferSource.java +++ b/src/main/java/org/anarres/cpp/TokenSnifferSource.java @@ -14,41 +14,32 @@ * or implied. See the License for the specific language governing * permissions and limitations under the License. */ - package org.anarres.cpp; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; import java.io.IOException; -import java.io.PushbackReader; -import java.io.Reader; -import java.io.StringReader; - -import java.util.ArrayList; import java.util.List; -import java.util.Iterator; - -import static org.anarres.cpp.Token.*; +import static org.anarres.cpp.Token.EOF; @Deprecated /* pp */ class TokenSnifferSource extends Source { - private List<Token> target; - - /* pp */ TokenSnifferSource(List<Token> target) { - this.target = target; - } - - public Token token() - throws IOException, - LexerException { - Token tok = getParent().token(); - if (tok.getType() != EOF) - target.add(tok); - return tok; - } - public String toString() { - return getParent().toString(); - } + private final List<Token> target; + + /* pp */ TokenSnifferSource(List<Token> target) { + this.target = target; + } + + public Token token() + throws IOException, + LexerException { + Token tok = getParent().token(); + if (tok.getType() != EOF) + target.add(tok); + return tok; + } + + @Override + public String toString() { + return getParent().toString(); + } } |