diff --git a/build.gradle b/build.gradle index c631bc9..7986055 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ plugins { } group = "org.litesolutions" -version = "0.7.1" +version = "0.7.2" description = "Grappa integration for Sonar's SSLR" def javaVersion = JavaVersion.VERSION_1_8 diff --git a/src/main/java/org/litesolutions/sonar/grappa/GrappaSslrFactory.java b/src/main/java/org/litesolutions/sonar/grappa/GrappaSslrFactory.java index 701dd2e..e34c34b 100644 --- a/src/main/java/org/litesolutions/sonar/grappa/GrappaSslrFactory.java +++ b/src/main/java/org/litesolutions/sonar/grappa/GrappaSslrFactory.java @@ -25,7 +25,11 @@ import org.sonar.sslr.grammar.GrammarRuleKey; import org.sonar.sslr.grammar.LexerfulGrammarBuilder; +import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; + +import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -91,6 +95,16 @@ private

GrappaSslrFactory( * @return a new parser instance */ public GrappaSslrParser getParser() + { + return getParserWithCharset(null); + } + + /** + * Get a Sonar {@link Parser} from this factory + * + * @return a new parser instance + */ + public GrappaSslrParser getParserWithCharset(@Nullable String charsetName) { final GrappaChannel channel = new GrappaChannel(rule); @@ -98,16 +112,37 @@ public GrappaSslrParser getParser() final LexerfulGrammarBuilder builder = getGrammarBuilder(); builder.setRootRule(entryPoint); - - final GrappaSslrLexer lexer = GrappaSslrLexer.builder() - .withFailIfNoChannelToConsumeOneCharacter(true) - .withChannel(channel) - .build(); - + + GrappaSslrLexer lexer = getLexer(channel, charsetName); + return GrappaSslrParser.grappaBuilder(builder.build()) .withLexer(lexer) .build(); } + + private GrappaSslrLexer getLexer(GrappaChannel channel,@Nullable String charsetName) { + if(charsetName!=null) { + Charset charset = getCharset(charsetName); + return GrappaSslrLexer.builder().withCharset(charset) + .withFailIfNoChannelToConsumeOneCharacter(true) + .withChannel(channel) + .build(); + }else return GrappaSslrLexer.builder() + .withFailIfNoChannelToConsumeOneCharacter(true) + .withChannel(channel) + .build(); + } + + private Charset getCharset(String charset) { + + try { + return Charset.forName(charset); + } catch (UnsupportedCharsetException e) { + return Charset.defaultCharset(); + } + + + } private LexerfulGrammarBuilder getGrammarBuilder() { diff --git a/src/main/java/org/litesolutions/sonar/grappa/GrappaSslrLexer.java b/src/main/java/org/litesolutions/sonar/grappa/GrappaSslrLexer.java index 566d0b6..5c24b55 100644 --- a/src/main/java/org/litesolutions/sonar/grappa/GrappaSslrLexer.java +++ b/src/main/java/org/litesolutions/sonar/grappa/GrappaSslrLexer.java @@ -37,7 +37,7 @@ public class GrappaSslrLexer { private List tokens = new ArrayList<>(); public GrappaSslrLexer(GrappaSslrLexer.Builder builder) { - this.charset = sun.nio.cs.UTF_8.INSTANCE; + this.charset = builder.charset; this.configuration = builder.configuration; this.channelDispatcher = builder.getChannelDispatcher();