From 5fd3fab179d700d68281fee316519fdd907bbcd6 Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Sun, 13 Oct 2002 13:31:42 +0000 Subject: [PATCH] Stopped the tokens LPAREN and RPAREN being silently eaten by the grammar. Now need to implement the ParenPad check. Should be easy. Also need to refactor some the existing tests to use utility methods for locating tokens in the tree. Currently hard code the positions, which is not ideal. --- .../checks/OtherLeftCurlyCheck.java | 3 +- .../checkstyle/checks/ParenPadCheck.java | 46 +++++++++++++++++++ .../checkstyle/checks/RightCurlyCheck.java | 4 +- .../puppycrawl/tools/checkstyle/java_new.g | 36 +++++++-------- 4 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ParenPadCheck.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/OtherLeftCurlyCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/OtherLeftCurlyCheck.java index 3fff5c25b..560f4a9e1 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/OtherLeftCurlyCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/OtherLeftCurlyCheck.java @@ -75,7 +75,8 @@ public class OtherLeftCurlyCheck break; case JavaTokenTypes.LITERAL_switch: case JavaTokenTypes.LITERAL_if: - brace = (DetailAST) aAST.getFirstChild().getNextSibling(); + brace = (DetailAST) aAST.getFirstChild().getNextSibling() + .getNextSibling().getNextSibling(); break; default: brace = null; diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ParenPadCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ParenPadCheck.java new file mode 100644 index 000000000..654339e60 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ParenPadCheck.java @@ -0,0 +1,46 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2002 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.checks; + +import com.puppycrawl.tools.checkstyle.JavaTokenTypes; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.Check; + +public class ParenPadCheck + extends Check +{ + /** @see com.puppycrawl.tools.checkstyle.api.Check */ + public int[] getDefaultTokens() + { + return new int[] {JavaTokenTypes.RPAREN, + JavaTokenTypes.LPAREN, + JavaTokenTypes.CTOR_CALL, + JavaTokenTypes.SUPER_CTOR_CALL, + JavaTokenTypes.TYPECAST, // TODO: treat this? + JavaTokenTypes.METHOD_CALL, + }; + } + + /** @see com.puppycrawl.tools.checkstyle.api.Check */ + public void visitToken(DetailAST aAST) + { + // TODO: implement + } +} diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/RightCurlyCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/RightCurlyCheck.java index 7d8aacc5a..55d4972c8 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/RightCurlyCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/RightCurlyCheck.java @@ -47,12 +47,12 @@ public class RightCurlyCheck nextToken = aAST; rcurly = Utils.getLastSibling( aAST.getParent().getFirstChild().getNextSibling() - .getFirstChild()); + .getNextSibling().getNextSibling().getFirstChild()); } else if (aAST.getType() == JavaTokenTypes.LITERAL_catch) { nextToken = (DetailAST) aAST.getNextSibling(); rcurly = Utils.getLastSibling( - aAST.getFirstChild().getNextSibling().getFirstChild()); + Utils.getLastSibling(aAST.getFirstChild()).getFirstChild()); } else if (aAST.getType() == JavaTokenTypes.LITERAL_try) { nextToken = (DetailAST) aAST.getFirstChild().getNextSibling(); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/java_new.g b/src/checkstyle/com/puppycrawl/tools/checkstyle/java_new.g index b5bbe8d20..5783dd6be 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/java_new.g +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/java_new.g @@ -267,7 +267,7 @@ field! ( IDENT // the name of the method // parse the formal parameter declarations. - LPAREN! param:parameterDeclarationList RPAREN! + LPAREN param:parameterDeclarationList RPAREN rt:declaratorBrackets[#t] @@ -318,14 +318,14 @@ explicitConstructorInvocation // it sees this( or super( generateAmbigWarnings=false; } - : "this"! lp1:LPAREN^ argList RPAREN! SEMI! + : "this"! lp1:LPAREN^ argList RPAREN SEMI! {#lp1.setType(CTOR_CALL);} - | "super"! lp2:LPAREN^ argList RPAREN! SEMI! + | "super"! lp2:LPAREN^ argList RPAREN SEMI! {#lp2.setType(SUPER_CTOR_CALL);} // (new Outer()).super() (create enclosing instance) - | primaryExpression DOT! "super"! lp3:LPAREN^ argList RPAREN! SEMI! + | primaryExpression DOT! "super"! lp3:LPAREN^ argList RPAREN SEMI! {#lp3.setType(SUPER_CTOR_CALL);} ) ; @@ -392,7 +392,7 @@ ctorHead : IDENT // the name of the method // parse the formal parameter declarations. - LPAREN! parameterDeclarationList RPAREN! + LPAREN parameterDeclarationList RPAREN // get the list of exceptions that this method is declared to throw (throwsClause)? @@ -470,7 +470,7 @@ traditionalStatement | IDENT c:COLON^ {#c.setType(LABELED_STAT);} statement // If-else statement - | "if"^ LPAREN! expression RPAREN! statement + | "if"^ LPAREN expression RPAREN statement ( // CONFLICT: the old "dangling-else" problem... // ANTLR generates proper code matching @@ -484,18 +484,18 @@ traditionalStatement // For statement | "for"^ - LPAREN! + LPAREN forInit SEMI! // initializer forCond SEMI! // condition test forIter // updater - RPAREN! + RPAREN statement // statement to loop over // While statement - | "while"^ LPAREN! expression RPAREN! statement + | "while"^ LPAREN expression RPAREN statement // do-while statement - | "do"^ statement "while"! LPAREN! expression RPAREN! SEMI! + | "do"^ statement "while"! LPAREN expression RPAREN SEMI! // get out of a loop (or switch) | "break"^ (IDENT)? SEMI! @@ -507,7 +507,7 @@ traditionalStatement | "return"^ (expression)? SEMI! // switch/case statement - | "switch"^ LPAREN! expression RPAREN! LCURLY + | "switch"^ LPAREN expression RPAREN LCURLY ( casesGroup )* RCURLY @@ -518,7 +518,7 @@ traditionalStatement | "throw"^ expression SEMI! // synchronize a statement - | "synchronized"^ LPAREN! expression RPAREN! compoundStatement + | "synchronized"^ LPAREN expression RPAREN compoundStatement // empty statement | s:SEMI {#s.setType(EMPTY_STAT);} @@ -582,7 +582,7 @@ tryBlock // an exception handler handler - : "catch"^ LPAREN! parameterDeclaration RPAREN! compoundStatement + : "catch"^ LPAREN parameterDeclaration RPAREN compoundStatement ; finallyHandler @@ -754,14 +754,14 @@ unaryExpressionNotPlusMinus } : // If typecast is built in type, must be numeric operand // Also, no reason to backtrack if type keyword like int, float... - lpb:LPAREN^ {#lpb.setType(TYPECAST);} builtInTypeSpec[true] RPAREN! + lpb:LPAREN^ {#lpb.setType(TYPECAST);} builtInTypeSpec[true] RPAREN unaryExpression // Have to backtrack to see if operator follows. If no operator // follows, it's a typecast. No semantic checking needed to parse. // if it _looks_ like a cast, it _is_ a cast; else it's a "(expr)" | (LPAREN classTypeSpec[true] RPAREN unaryExpressionNotPlusMinus)=> - lp:LPAREN^ {#lp.setType(TYPECAST);} classTypeSpec[true] RPAREN! + lp:LPAREN^ {#lp.setType(TYPECAST);} classTypeSpec[true] RPAREN unaryExpressionNotPlusMinus | postfixExpression @@ -799,7 +799,7 @@ postfixExpression // be hard to syntactically prevent ctor calls here | lp:LPAREN^ {#lp.setType(METHOD_CALL);} argList - RPAREN! + RPAREN )* // possibly add on a post-increment or post-decrement. @@ -819,7 +819,7 @@ primaryExpression | "this" | "null" | newExpression - | LPAREN! assignmentExpression RPAREN! + | LPAREN assignmentExpression RPAREN | "super" // look for int.class and int[].class | builtInType @@ -878,7 +878,7 @@ primaryExpression */ newExpression : "new"^ type - ( LPAREN! argList RPAREN! (classBlock)? + ( LPAREN argList RPAREN (classBlock)? //java 1.1 // Note: This will allow bad constructs like