Skip to content

Commit

Permalink
cleaning, sonar linting
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed May 2, 2024
1 parent 5c54617 commit 269d727
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
8 changes: 3 additions & 5 deletions src/sly/lexer/GenericLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ protected readonly

protected char StringDelimiterChar;

private readonly IEqualityComparer<string> KeyWordComparer;

private readonly StringComparison KeyWordComparison;
private readonly IEqualityComparer<string> _keyWordComparer;

public GenericLexer(IdentifierType idType = IdentifierType.Alpha,
Action<IN, LexemeAttribute, GenericLexer<IN>> extensionBuilder = null,
Expand All @@ -128,7 +126,7 @@ public GenericLexer(Config lexerConfig, GenericToken[] staticTokens)
derivedTokens =
new Dictionary<GenericToken, Dictionary<string, (IN tokenId, bool isPop, bool isPush, string mode)>>();
ExtensionBuilder = lexerConfig.ExtensionBuilder;
KeyWordComparer = lexerConfig.KeyWordComparer;
_keyWordComparer = lexerConfig.KeyWordComparer;
SubLexersFsm = new Dictionary<string, FSMLexer<GenericToken>>();
InitializeStaticLexer(lexerConfig, staticTokens);
}
Expand Down Expand Up @@ -505,7 +503,7 @@ public void AddLexeme(GenericToken generic, IN token)
if (genericToken == GenericToken.Identifier)
{
tokensForGeneric =
new Dictionary<string, (IN tokenId, bool isPop, bool isPush, string mode)>(KeyWordComparer);
new Dictionary<string, (IN tokenId, bool isPop, bool isPush, string mode)>(_keyWordComparer);
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions src/sly/lexer/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ public Token<T> Previous(int channelId)
public bool IsIndent { get; set; }

public bool IsUnIndent { get; set; }

public bool IsIndentation => IsIndent || IsUnIndent;

public int IndentationLevel { get; set; }

Expand Down
72 changes: 37 additions & 35 deletions src/sly/parser/parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,41 +111,7 @@ public Parser(string i18n, ISyntaxParser<IN, OUT> syntaxParser, SyntaxTreeVisito
return result;
}

private List<Token<IN>> AutoCloseIndentation(List<Token<IN>> tokens)
{
if (SyntaxParser is EBNFRecursiveDescentSyntaxParser<IN,OUT> ebnf && ebnf.Configuration.AutoCloseIndentations)
{
var indents = tokens
.Where(x => x.IsIndent || x.IsUnIndent);
if (indents.Any())
{
var finalIndentation = indents
.Select(x => x.IsIndent ? 1 : -1)
.Aggregate((int x, int y) => x + y);
if (finalIndentation > 0)
{
tokens = tokens.Take(tokens.Count - 1).ToList();
for (int i = 0; i < finalIndentation; i++)
{
tokens.Add(new Token<IN>()
{
IsUnIndent = true,
IsEOS = false,
IsEOL = false,
IndentationLevel = finalIndentation - i - 1
});
}

tokens.Add(new Token<IN>()
{
IsEOS = true
});
}
}
}

return tokens;
}



public ParseResult<IN, OUT> ParseWithContext(IList<Token<IN>> tokens, object parsingContext = null, string startingNonTerminal = null)
Expand Down Expand Up @@ -197,5 +163,41 @@ private List<Token<IN>> AutoCloseIndentation(List<Token<IN>> tokens)

return result;
}

private List<Token<IN>> AutoCloseIndentation(List<Token<IN>> tokens)
{
if (SyntaxParser is EBNFRecursiveDescentSyntaxParser<IN,OUT> ebnf && ebnf.Configuration.AutoCloseIndentations)
{
var indents = tokens
.Where(x => x.IsIndentation);
if (indents.Any())
{
var finalIndentation = indents
.Select(x => x.IsIndent ? 1 : -1)
.Aggregate((int x, int y) => x + y);
if (finalIndentation > 0)
{
tokens = tokens.Take(tokens.Count - 1).ToList();
for (int i = 0; i < finalIndentation; i++)
{
tokens.Add(new Token<IN>()
{
IsUnIndent = true,
IsEOS = false,
IsEOL = false,
IndentationLevel = finalIndentation - i - 1
});
}

tokens.Add(new Token<IN>()
{
IsEOS = true
});
}
}
}

return tokens;
}
}
}

0 comments on commit 269d727

Please sign in to comment.