Skip to content

Commit

Permalink
RESOLVED #6522: Cambiar encoding grappa a UTF-8
Browse files Browse the repository at this point in the history
  • Loading branch information
martalite committed Oct 26, 2023
1 parent fb0dd4e commit ddb1a35
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,23 +95,54 @@ private <P extends SonarParserBase> GrappaSslrFactory(
* @return a new parser instance
*/
public GrappaSslrParser<Grammar> getParser()
{
return getParserWithCharset(null);
}

/**
* Get a Sonar {@link Parser} from this factory
*
* @return a new parser instance
*/
public GrappaSslrParser<Grammar> getParserWithCharset(@Nullable String charsetName)
{
final GrappaChannel channel = new GrappaChannel(rule);

suppliers.forEach(channel::addListenerSupplier);

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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class GrappaSslrLexer {
private List<Token> 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();

Expand Down

0 comments on commit ddb1a35

Please sign in to comment.