Skip to content

Commit

Permalink
sonar linting
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed Nov 29, 2024
1 parent ebcfdd6 commit 468f37e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/sly/EnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static T ConvertIntToEnum<T>(int value) where T : struct

public static IN ConvertStringToEnum<IN>(string name) where IN : struct
{
IN token = default(IN);
Enum.TryParse(name, out token);
Enum.TryParse(name, out IN token);
return token;
}

Expand Down
49 changes: 28 additions & 21 deletions src/sly/lexer/LexerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ public static BuildResult<ILexer<IN>> BuildLexer<IN>(BuildResult<ILexer<IN>> res
string lang = null, LexerPostProcess<IN> lexerPostProcess = null, IList<string> explicitTokens = null)
where IN : struct
{
var attributes = GetLexemes<IN>(result, lang);
var attributes = GetLexemes(result, lang);
if (!result.IsError)
{
result = Build<IN>(attributes, result, extensionBuilder, lang, explicitTokens);
result = Build(attributes, result, extensionBuilder, lang, explicitTokens);
if (!result.IsError)
{
var labels = result.Result.LexemeLabels;
Expand All @@ -119,25 +119,7 @@ public static BuildResult<ILexer<IN>> BuildLexer<IN>(BuildResult<ILexer<IN>> res
var count = labels.Select(x => x.Value.Count).Sum();
if (count > 0)
{
labeledTokens = tokens.Select(token =>
{
token.Label = token.TokenID.ToString();
if (labels.TryGetValue(token.TokenID, out var tokenLabels)
&& tokenLabels.TryGetValue(lang, out string label))
{
token.Label = label;
}
else if (token.IsUnIndent)
{
token.Label = "<<UINDENT>>";
}
else if (token.IsIndent)
{
token.Label = "<<INDENT>>";
}

return token;
}).ToList();
labeledTokens = LabelTokens(lang, tokens, labels);
}

if (lexerPostProcess != null)
Expand All @@ -154,6 +136,31 @@ public static BuildResult<ILexer<IN>> BuildLexer<IN>(BuildResult<ILexer<IN>> res
return result;
}

private static List<Token<IN>> LabelTokens<IN>(string lang, List<Token<IN>> tokens, Dictionary<IN, Dictionary<string, string>> labels) where IN : struct
{
List<Token<IN>> labeledTokens;
labeledTokens = tokens.Select(token =>
{
token.Label = token.TokenID.ToString();
if (labels.TryGetValue(token.TokenID, out var tokenLabels)
&& tokenLabels.TryGetValue(lang, out string label))
{
token.Label = label;
}
else if (token.IsUnIndent)
{
token.Label = "<<UINDENT>>";
}
else if (token.IsIndent)
{
token.Label = "<<INDENT>>";
}

return token;
}).ToList();
return labeledTokens;
}


private static BuildResult<ILexer<IN>> Build<IN>(Dictionary<IN, (List<LexemeAttribute>,List<LexemeLabelAttribute>)> attributes,
BuildResult<ILexer<IN>> result, Action<IN, LexemeAttribute, GenericLexer<IN>> extensionBuilder = null,
Expand Down

0 comments on commit 468f37e

Please sign in to comment.