From 7915f08bc95538e717ad6051336c0673c5d715ad Mon Sep 17 00:00:00 2001 From: Jan van Nunen Date: Thu, 18 Feb 2016 13:40:32 +0100 Subject: [PATCH] Extended Objective-C grammar to accept UTF-8 escapes (\uXXXX) in string literals. --- pmd-objectivec/etc/grammar/ObjC2.0.jj | 4 +- ...capesInStringLiteralObjCTokenizerTest.java | 37 +++++++++++++++++++ .../cpd/FileWithUTF8EscapeInStringLiteral.m | 8 ++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 pmd-objectivec/src/test/java/net/sourceforge/pmd/cpd/UTF8EscapesInStringLiteralObjCTokenizerTest.java create mode 100644 pmd-objectivec/src/test/resources/net/sourceforge/pmd/cpd/FileWithUTF8EscapeInStringLiteral.m diff --git a/pmd-objectivec/etc/grammar/ObjC2.0.jj b/pmd-objectivec/etc/grammar/ObjC2.0.jj index ba888ee9ed..f25936bdfa 100644 --- a/pmd-objectivec/etc/grammar/ObjC2.0.jj +++ b/pmd-objectivec/etc/grammar/ObjC2.0.jj @@ -375,8 +375,8 @@ TOKEN : { | )? (["f","F","d","D"])? | "." (["0"-"9"])+ ()? (["f","F","d","D"])? | (["0"-"9"])+ (["f","F","d","D"])? | (["0"-"9"])+ ()? ["f","F","d","D"]> | <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+> | -| -| +| +| } TOKEN : diff --git a/pmd-objectivec/src/test/java/net/sourceforge/pmd/cpd/UTF8EscapesInStringLiteralObjCTokenizerTest.java b/pmd-objectivec/src/test/java/net/sourceforge/pmd/cpd/UTF8EscapesInStringLiteralObjCTokenizerTest.java new file mode 100644 index 0000000000..bd3d87d930 --- /dev/null +++ b/pmd-objectivec/src/test/java/net/sourceforge/pmd/cpd/UTF8EscapesInStringLiteralObjCTokenizerTest.java @@ -0,0 +1,37 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.cpd; + +import java.io.IOException; + +import net.sourceforge.pmd.testframework.AbstractTokenizerTest; + +import org.apache.commons.io.IOUtils; +import org.junit.Before; +import org.junit.Test; + + +//Tests if the ObjectiveC tokenizer supports UTF-8 escapes in string literals +public class UTF8EscapesInStringLiteralObjCTokenizerTest extends AbstractTokenizerTest { + + private static final String FILENAME = "FileWithUTF8EscapeInStringLiteral.m"; + + @Before + @Override + public void buildTokenizer() throws IOException { + this.tokenizer = new ObjectiveCTokenizer(); + this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME)); + } + + @Override + public String getSampleCode() throws IOException { + return IOUtils.toString(ObjectiveCTokenizer.class.getResourceAsStream(FILENAME), "UTF-8"); + } + + @Test + public void tokenizeTest() throws IOException { + this.expectedTokenCount = 45; + super.tokenizeTest(); + } +} diff --git a/pmd-objectivec/src/test/resources/net/sourceforge/pmd/cpd/FileWithUTF8EscapeInStringLiteral.m b/pmd-objectivec/src/test/resources/net/sourceforge/pmd/cpd/FileWithUTF8EscapeInStringLiteral.m new file mode 100644 index 0000000000..dafd9e3c88 --- /dev/null +++ b/pmd-objectivec/src/test/resources/net/sourceforge/pmd/cpd/FileWithUTF8EscapeInStringLiteral.m @@ -0,0 +1,8 @@ +@property (nonatomic, strong) UIButton *copyrightsButton; + +- (void)setupCopyrightsButton { + self.copyrightsButton = [[UIButton alloc] initWithFrame:CGRectZero]; + [self.copyrightsButton setTitle:@"\u00a9" forState:UIControlStateNormal]; +} + +@end