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.
This commit is contained in:
Oliver Burn 2002-10-13 13:31:42 +00:00
parent 5e1da0a269
commit 5fd3fab179
4 changed files with 68 additions and 21 deletions

View File

@ -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;

View File

@ -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
}
}

View File

@ -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();

View File

@ -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