From 094ffd52eca4f05bfc671f01cb7ad0b7d1bbe77f Mon Sep 17 00:00:00 2001 From: Michael Studman Date: Sun, 29 May 2005 15:54:13 +0000 Subject: [PATCH] Added support for hex float and double constants. --- .../tools/checkstyle/grammars/java.g | 40 ++++++++++++++++++- .../checkstyle/grammars/InputHexFloat.java | 16 ++++++++ .../tools/checkstyle/grammars/AllTests.java | 1 + .../checkstyle/grammars/HexFloatsTest.java | 23 +++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputHexFloat.java create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/grammars/HexFloatsTest.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/grammars/java.g b/src/checkstyle/com/puppycrawl/tools/checkstyle/grammars/java.g index c3dbe6cc9..8e15b0aef 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/grammars/java.g +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/grammars/java.g @@ -1784,6 +1784,8 @@ NUM_INT | (DOT)=>DOT {$setType(DOT);} | (DOUBLE_LITERAL)=>DOUBLE_LITERAL {$setType(NUM_DOUBLE);} | (FLOAT_LITERAL)=>FLOAT_LITERAL {$setType(NUM_FLOAT);} + | (HEX_DOUBLE_LITERAL)=>HEX_DOUBLE_LITERAL {$setType(NUM_DOUBLE);} + | (HEX_FLOAT_LITERAL)=>HEX_FLOAT_LITERAL {$setType(NUM_FLOAT);} | (LONG_LITERAL)=>LONG_LITERAL {$setType(NUM_LONG);} | (INT_LITERAL)=>INT_LITERAL {$setType(NUM_INT);} ; @@ -1834,6 +1836,32 @@ protected DOUBLE_LITERAL (EXPONENT)? ('d'|'D') ; +protected HEX_FLOAT_LITERAL + : '0' ('x'|'X') + ( + ((HEX_DIGIT)* '.')=> + ( (HEX_DIGIT)+ '.' (HEX_DIGIT)* + | '.' (HEX_DIGIT)+ + ) + | + (HEX_DIGIT)+ + ) + BINARY_EXPONENT ('f'|'F')? + ; + +protected HEX_DOUBLE_LITERAL + : '0' ('x'|'X') + ( + ((HEX_DIGIT)* '.')=> + ( (HEX_DIGIT)+ '.' (HEX_DIGIT)* + | '.' (HEX_DIGIT)+ + ) + | + (HEX_DIGIT)+ + ) + BINARY_EXPONENT ('d'|'D') + ; + protected ELLIPSIS : ".." ; @@ -1845,10 +1873,20 @@ protected DOT // a couple protected methods to assist in matching floating point numbers protected EXPONENT - : ('e'|'E') ('+'|'-')? ('0'..'9')+ + : ('e'|'E') SIGNED_INTEGER ; +protected +SIGNED_INTEGER + : ('+'|'-')? ('0'..'9')+ + ; + protected FLOAT_SUFFIX : 'f'|'F'|'d'|'D' ; + +protected +BINARY_EXPONENT + : ('p'|'P') SIGNED_INTEGER + ; \ No newline at end of file diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputHexFloat.java b/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputHexFloat.java new file mode 100644 index 000000000..4ec38c341 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/grammars/InputHexFloat.java @@ -0,0 +1,16 @@ +package com.puppycrawl.tools.checkstyle.grammars; + +/** + * Input for hex float and double test. + */ +public class InputHexFloat +{ + float f1 = 0x.0P10; + float f2 = 0x1.P-1; + float f3 = 0Xab1P0; + float f4 = 0Xab1ap+20; + float f5 = 0Xab1ap+20D; + float f6 = 0Xab1ap+20d; + float f5 = 0Xab1ap+20f; + float f6 = 0Xab1ap+20F; +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/grammars/AllTests.java b/src/tests/com/puppycrawl/tools/checkstyle/grammars/AllTests.java index 1a45554c6..25000db7c 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/grammars/AllTests.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/grammars/AllTests.java @@ -16,6 +16,7 @@ public class AllTests { //$JUnit-BEGIN$ suite.addTest(new TestSuite(GeneratedJava14LexerTest.class)); suite.addTest(new TestSuite(Post13KeywordsAsIdentifiersOKTest.class)); + suite.addTest(new TestSuite(HexFloatsTest.class)); //$JUnit-END$ return suite; } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/grammars/HexFloatsTest.java b/src/tests/com/puppycrawl/tools/checkstyle/grammars/HexFloatsTest.java new file mode 100644 index 000000000..c129b95e6 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/grammars/HexFloatsTest.java @@ -0,0 +1,23 @@ +package com.puppycrawl.tools.checkstyle.grammars; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck; + +/** + * Tests hex floats and doubles can be parsed. + * @author Michael Studman + */ +public class HexFloatsTest + extends BaseCheckTestCase +{ + public void testCanParse() + throws Exception + + { + final DefaultConfiguration checkConfig = + createCheckConfig(MemberNameCheck.class); + final String[] expected = {}; + verify(checkConfig, getPath("grammars/InputHexFloat.java"), expected); + } +}