Skip to content

Commit

Permalink
qodana cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed Jan 8, 2024
1 parent 724dea2 commit 7205aec
Show file tree
Hide file tree
Showing 16 changed files with 295,404 additions and 67 deletions.
295,346 changes: 295,346 additions & 0 deletions --qodana.sarif.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions src/sly/EnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static IN ConvertIntToEnum<IN>(int intValue)
public static IN ConvertStringToEnum<IN>(string name) where IN : struct
{
IN token = default(IN);

Check warning on line 27 in src/sly/EnumConverter.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Assignment is not used

Value assigned is not used in any execution path
if (!Enum.TryParse<IN>(name, out token))
if (!Enum.TryParse(name, out token))
{
throw new ParserConfigurationException($"bad enum name {name} on Operation definition.");
}
Expand All @@ -34,8 +34,7 @@ public static IN ConvertIntToEnum<IN>(int intValue)

public static bool IsEnumValue<IN>(string name) where IN : struct
{

return Enum.TryParse<IN>(name, out IN token);
return Enum.TryParse(name, out IN token);
}
}
}
2 changes: 1 addition & 1 deletion src/sly/i18n/I18N.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public string GetText(string lang, I18NMessage key, params string[] args)
private IDictionary<I18NMessage,string> Load(string lang)
{
var translation = new Dictionary<I18NMessage, string>();
Assembly assembly = GetType().Assembly;
var assembly = GetType().Assembly;
using (var stream = assembly.GetManifestResourceStream($"sly.i18n.translations.{lang}.txt"))
{
if (stream != null)
Expand Down
6 changes: 3 additions & 3 deletions src/sly/lexer/CallBacksBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ public static class CallBacksBuilder

public static void ExtractCallBacks<IN>(Type callbackClass, GenericLexer<IN> lexer) where IN : struct

Check notice on line 19 in src/sly/lexer/CallBacksBuilder.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Member can be made private (non-private accessibility)

Method 'ExtractCallBacks' can be made private
{
var methods = callbackClass.GetMethods().ToList<MethodInfo>();
var methods = callbackClass.GetMethods().ToList();
methods = methods.Where<MethodInfo>(m =>
{
var attributes = m.GetCustomAttributes().ToList<Attribute>();
var attributes = m.GetCustomAttributes().ToList();
var attr = attributes.Find(a => a.GetType() == typeof(TokenCallbackAttribute));
return m.IsStatic && attr != null;
}).ToList<MethodInfo>();

foreach (var method in methods)
{
var attributes = method.GetCustomAttributes(typeof(TokenCallbackAttribute), false).Cast<TokenCallbackAttribute>().ToList<TokenCallbackAttribute>();
var attributes = method.GetCustomAttributes(typeof(TokenCallbackAttribute), false).Cast<TokenCallbackAttribute>().ToList();
AddCallback<IN>(lexer, method, EnumConverter.ConvertIntToEnum<IN>(attributes[0].EnumValue));
}
}
Expand Down
33 changes: 13 additions & 20 deletions src/sly/lexer/GenericLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,10 @@ private FSMLexer<GenericToken> SetLexerMode(FSMMatch<GenericToken> r, Stack<FSML

if (r.IsLineEnding) // only compute if token is eol

Check notice on line 296 in src/sly/lexer/GenericLexer.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Invert 'if' statement to reduce nesting

Invert 'if' statement to reduce nesting
{
var eols = tokens.Where<Token<IN>>(t => t.IsLineEnding).ToList<Token<IN>>();
int line = eols.Any<Token<IN>>() ? eols.Count : 0;
var eols = tokens.Where(t => t.IsLineEnding).ToList();
int line = eols.Any() ? eols.Count : 0;
int column = 0;

Check notice on line 300 in src/sly/lexer/GenericLexer.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Convert local variable or field into constant (private accessibility)

Convert into constant
int index = newPosition.Index;
// r.Result.Position.Line = line+1;
r.NewPosition.Line = line + 1;
r.NewPosition.Column = column;
}
Expand All @@ -323,16 +322,16 @@ private void InitializeStaticLexer(Config config, GenericToken[] staticTokens)
// start machine definition
FSMBuilder.Mark(start);

if (staticTokens.Contains<GenericToken>(GenericToken.Identifier) ||
staticTokens.Contains<GenericToken>(GenericToken.KeyWord))
if (staticTokens.Contains(GenericToken.Identifier) ||
staticTokens.Contains(GenericToken.KeyWord))
{
InitializeIdentifier(config);
}

// numeric
if (staticTokens.Contains<GenericToken>(GenericToken.Int) ||
staticTokens.Contains<GenericToken>(GenericToken.Double) ||
staticTokens.Contains<GenericToken>(GenericToken.Date))
if (staticTokens.Contains(GenericToken.Int) ||
staticTokens.Contains(GenericToken.Double) ||
staticTokens.Contains(GenericToken.Date))
{
FSMBuilder = FSMBuilder.GoTo(start)
.RangeTransition('0', '9')
Expand Down Expand Up @@ -527,8 +526,6 @@ public void AddDouble(IN token, string separator, BuildResult<ILexer<IN>> result

FSMBuilder.GoTo(in_int)
.Transition(separatorChar)
// .RangeTransition('0','9')
// .RangeTransition('0','9')
.RangeTransition('0', '9')
.Mark(in_double)
.RangeTransitionTo('0', '9', in_double)
Expand Down Expand Up @@ -572,7 +569,7 @@ private void AddDoubleWhenNoDoubleToken(IN token, DateFormat format, char separa
};
NodeCallback<GenericToken> callback = delegate(FSMMatch<GenericToken> match)

Check notice on line 570 in src/sly/lexer/GenericLexer.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Convert delegate variable into local function

Use local function
{
match.Properties[GenericLexer<IN>.DerivedToken] = token;
match.Properties[DerivedToken] = token;
var elements = match.Result.Value.Split(separator);
DateTime date;
if (format == DateFormat.DDMMYYYY)

Check notice on line 575 in src/sly/lexer/GenericLexer.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

'if' statement can be rewritten as '?:' expression

Convert into '?:' expression
Expand Down Expand Up @@ -633,7 +630,7 @@ private void AddDoubleWhenDoubleTokenPresent(IN token, DateFormat format, char s
};
NodeCallback<GenericToken> callback = delegate(FSMMatch<GenericToken> match)

Check notice on line 631 in src/sly/lexer/GenericLexer.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Convert delegate variable into local function

Use local function
{
match.Properties[GenericLexer<IN>.DerivedToken] = token;
match.Properties[DerivedToken] = token;
var elements = match.Result.Value.Split(separator);
DateTime date;
if (format == DateFormat.DDMMYYYY)

Check notice on line 636 in src/sly/lexer/GenericLexer.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

'if' statement can be rewritten as '?:' expression

Convert into '?:' expression
Expand Down Expand Up @@ -772,7 +769,7 @@ private void AddDoubleWhenDoubleTokenPresent(IN token, DateFormat format, char s

if (substitutionHappened)

Check notice on line 770 in src/sly/lexer/GenericLexer.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Invert 'if' statement to reduce nesting

Invert 'if' statement to reduce nesting
{
r += value.At<char>(value.Length - 1);
r += value.At(value.Length - 1);
value = r.AsMemory();
}

Expand Down Expand Up @@ -969,9 +966,9 @@ private void AddDoubleWhenDoubleTokenPresent(IN token, DateFormat format, char s
return match;
};

Func<int, TransitionPrecondition> precond = (int i) =>
Func<int, TransitionPrecondition> precond = (i) =>

Check notice on line 969 in src/sly/lexer/GenericLexer.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Convert delegate variable into local function

Use local function
{
return (ReadOnlyMemory<char> value) => { return value.Length == i + 1; };
return (value) => { return value.Length == i + 1; };

Check notice on line 971 in src/sly/lexer/GenericLexer.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Convert into lambda expression

Use lambda expression
};

FSMBuilder.GoTo(start);
Expand Down Expand Up @@ -1021,8 +1018,6 @@ private void AddDoubleWhenDoubleTokenPresent(IN token, DateFormat format, char s
FSMBuilder.GoTo(startNode);
FSMBuilder.SafeTransition(exceptionChar);
FSMBuilder.Mark(end);
// FSMBuilder.End(GenericToken.AllExcept)
// .CallBack(callback);

startNode = start;
}
Expand Down Expand Up @@ -1080,7 +1075,6 @@ private void AddDoubleWhenDoubleTokenPresent(IN token, DateFormat format, char s
comment.SpanValue = "".ToCharArray();
}
return new LexerPosition(position, lexerPosition.Line + 1, 0);
//LexerFsm.MovePosition(position, LexerFsm.CurrentLine + 1, 0);
}
else if (comment.IsMultiLineComment)
{
Expand All @@ -1099,12 +1093,11 @@ private void AddDoubleWhenDoubleTokenPresent(IN token, DateFormat format, char s
var newLine = lexerPosition.Line + lines.Count - 1;
int newColumn;
if (lines.Count > 1)
newColumn = lines.Last<int>() + MultiLineCommentEnd.Length;
newColumn = lines.Last() + MultiLineCommentEnd.Length;
else
newColumn = lexerPosition.Column + lines[0] + MultiLineCommentEnd.Length;

return new LexerPosition(newPosition, newLine, newColumn);
// LexerFsm.MovePosition(newPosition, newLine, newColumn);
}

return lexerPosition;
Expand Down
1 change: 0 additions & 1 deletion src/sly/lexer/ILexer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

using System;
using System.Collections.Generic;
using sly.i18n;
using sly.lexer.fsm;

namespace sly.lexer
Expand Down
32 changes: 16 additions & 16 deletions src/sly/lexer/LexerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static class LexerBuilder
var attributes = new Dictionary<IN, (List<LexemeAttribute>,List<LexemeLabelAttribute>)>();

var values = Enum.GetValues(typeof(IN));
var grouped = values.Cast<IN>().GroupBy<IN, IN>(x => x).ToList<IGrouping<IN, IN>>();
var grouped = values.Cast<IN>().GroupBy(x => x).ToList();
foreach (var group in grouped)
{
var v = group.Key;
Expand Down Expand Up @@ -606,7 +606,7 @@ private static NodeCallback<GenericToken> GetCallbackMulti<IN>(IN token, bool do
fsmBuilder.ConstantTransition(commentAttr.MultiLineCommentStart);
fsmBuilder.Mark(GenericLexer<IN>.multi_line_comment_start);
fsmBuilder.End(GenericToken.Comment);
fsmBuilder.CallBack(GetCallbackMulti<IN>(comment.Key, commentAttr.DoNotIgnore,
fsmBuilder.CallBack(GetCallbackMulti(comment.Key, commentAttr.DoNotIgnore,
commentAttr.Channel));
}
}
Expand Down Expand Up @@ -682,13 +682,13 @@ private static NodeCallback<GenericToken> GetCallbackMulti<IN>(IN token, bool do
var allLexemes = attributes.Values.SelectMany<List<LexemeAttribute>, LexemeAttribute>(a => a);

var allDelimiters = allLexemes
.Where<LexemeAttribute>(a => a.IsString || a.IsChar)
.Where<LexemeAttribute>(a => a.HasGenericTokenParameters)
.Select<LexemeAttribute, string>(a => a.GenericTokenParameters[0]);
.Where(a => a.IsString || a.IsChar)
.Where(a => a.HasGenericTokenParameters)
.Select(a => a.GenericTokenParameters[0]);

var duplicates = allDelimiters.GroupBy<string, string>(x => x)
.Where<IGrouping<string, string>>(g => g.Count<string>() > 1)
.Select(y => new { Element = y.Key, Counter = y.Count<string>() });
var duplicates = allDelimiters.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(y => new { Element = y.Key, Counter = y.Count() });

foreach (var duplicate in duplicates)
{
Expand All @@ -712,16 +712,16 @@ private static NodeCallback<GenericToken> GetCallbackMulti<IN>(IN token, bool do
{
var tokenID = (IN)(object)value;
var enumAttributes = value.GetAttributesOfType<CommentAttribute>();
if (enumAttributes != null && enumAttributes.Any<CommentAttribute>())
attributes[tokenID] = enumAttributes.ToList<CommentAttribute>();
if (enumAttributes != null && enumAttributes.Any())
attributes[tokenID] = enumAttributes.ToList();
}

var commentCount = attributes.Values.Select<List<CommentAttribute>, int>(l =>
l?.Count<CommentAttribute>(attr => attr.GetType() == typeof(CommentAttribute)) ?? 0).Sum();
var multiLineCommentCount = attributes.Values.Select<List<CommentAttribute>, int>(l =>
l?.Count<CommentAttribute>(attr => attr.GetType() == typeof(MultiLineCommentAttribute)) ?? 0).Sum();
var singleLineCommentCount = attributes.Values.Select<List<CommentAttribute>, int>(l =>
l?.Count<CommentAttribute>(attr => attr.GetType() == typeof(SingleLineCommentAttribute)) ?? 0).Sum();
var commentCount = attributes.Values.Select(l =>
l?.Count(attr => attr.GetType() == typeof(CommentAttribute)) ?? 0).Sum();
var multiLineCommentCount = attributes.Values.Select(l =>
l?.Count(attr => attr.GetType() == typeof(MultiLineCommentAttribute)) ?? 0).Sum();
var singleLineCommentCount = attributes.Values.Select(l =>
l?.Count(attr => attr.GetType() == typeof(SingleLineCommentAttribute)) ?? 0).Sum();

if (commentCount > 1)
{
Expand Down
4 changes: 2 additions & 2 deletions src/sly/parser/parser/GroupItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public X Match<X>(Func<string, Token<IN>, X> fToken, Func<string, OUT, X> fValue
[ExcludeFromCodeCoverage]
public static implicit operator OUT(GroupItem<IN, OUT> item)
{
return item.Match<OUT>((name, token) => default(OUT), (name, value) => item.Value);
return item.Match((name, token) => default, (name, value) => item.Value);
}

[ExcludeFromCodeCoverage]
public static implicit operator Token<IN>(GroupItem<IN, OUT> item)
{
return item.Match<Token<IN>>((name, token) => item.Token, (name, value) => default(Token<IN>));
return item.Match((name, token) => item.Token, (name, value) => default);
}

[ExcludeFromCodeCoverage]
Expand Down
10 changes: 5 additions & 5 deletions src/sly/parser/parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,22 @@ public Parser(string i18n, ISyntaxParser<IN, OUT> syntaxParser, SyntaxTreeVisito
else
{
result.Errors = new List<ParseError>();
var unexpectedTokens = syntaxResult.Errors.ToList<UnexpectedTokenSyntaxError<IN>>();
var byEnding = unexpectedTokens.GroupBy<UnexpectedTokenSyntaxError<IN>, LexerPosition>(x => x.UnexpectedToken.Position).OrderBy<IGrouping<LexerPosition, UnexpectedTokenSyntaxError<IN>>, LexerPosition>(x => x.Key);
var unexpectedTokens = syntaxResult.Errors.ToList();
var byEnding = unexpectedTokens.GroupBy(x => x.UnexpectedToken.Position).OrderBy(x => x.Key);
var errors = new List<ParseError>();
foreach (var expecting in byEnding)
{
var expectingTokens = expecting.SelectMany<UnexpectedTokenSyntaxError<IN>, LeadingToken<IN>>(x => x.ExpectedTokens ?? new List<LeadingToken<IN>>()).Distinct();
var expectingTokens = expecting.SelectMany(x => x.ExpectedTokens ?? new List<LeadingToken<IN>>()).Distinct();
var expectedTokens = expectingTokens != null && expectingTokens.Any() ? expectingTokens?.ToArray() : null;

Check warning on line 138 in src/sly/parser/parser/Parser.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Expression is always 'true' or always 'false'

Expression is always true

Check warning on line 138 in src/sly/parser/parser/Parser.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Conditional access qualifier expression is known to be null or not null

Conditional access qualifier expression is known to be not null

Check warning on line 138 in src/sly/parser/parser/Parser.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible multiple enumeration

Possible multiple enumeration

Check warning on line 138 in src/sly/parser/parser/Parser.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible multiple enumeration

Possible multiple enumeration
if (expectedTokens != null)
{
var expected = new UnexpectedTokenSyntaxError<IN>(expecting.First<UnexpectedTokenSyntaxError<IN>>().UnexpectedToken, LexemeLabels, I18n,
var expected = new UnexpectedTokenSyntaxError<IN>(expecting.First().UnexpectedToken, LexemeLabels, I18n,
expectedTokens);
errors.Add(expected);
}
else
{
var expected = new UnexpectedTokenSyntaxError<IN>(expecting.First<UnexpectedTokenSyntaxError<IN>>().UnexpectedToken, LexemeLabels, I18n,
var expected = new UnexpectedTokenSyntaxError<IN>(expecting.First().UnexpectedToken, LexemeLabels, I18n,
new LeadingToken<IN>[]{});
errors.Add(expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public EBNFRecursiveDescentSyntaxParser(ParserConfiguration<IN, OUT> configurati
{
var choiceResult = ParseChoice(tokens, choice, currentPosition, parsingContext);
currentPosition = choiceResult.EndingPosition;
if (choiceResult.IsError && choiceResult.Errors != null && choiceResult.Errors.Any<UnexpectedTokenSyntaxError<IN>>())
if (choiceResult.IsError && choiceResult.Errors != null && choiceResult.Errors.Any())
{
errors.AddRange(choiceResult.Errors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ public SyntaxParseResult<IN> SafeParse(IList<Token<IN>> tokens, SyntaxParsingCon
if (errors.Count > 0)
{
var lastErrorPosition = errors
.Select<UnexpectedTokenSyntaxError<IN>, int>(e => e.UnexpectedToken.PositionInTokenFlow)
.Select(e => e.UnexpectedToken.PositionInTokenFlow)
.Max();
var lastErrors = errors
.Where<UnexpectedTokenSyntaxError<IN>>(e =>
.Where(e =>
e.UnexpectedToken.PositionInTokenFlow == lastErrorPosition)
.ToList<UnexpectedTokenSyntaxError<IN>>();
.ToList();
result.Errors = lastErrors;
}
else
Expand Down
8 changes: 4 additions & 4 deletions src/sly/parser/syntax/grammar/ChoiceClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public sealed class ChoiceClause<T> : IClause<T> where T : struct
{

public bool IsDiscarded { get; set; } = false;
public bool IsTerminalChoice => Choices.Select<IClause<T>, bool>(c => c is TerminalClause<T>).Aggregate<bool>((x, y) => x && y);
public bool IsNonTerminalChoice => Choices.Select<IClause<T>, bool>(c => c is NonTerminalClause<T>).Aggregate<bool>((x, y) => x && y);
public bool IsTerminalChoice => Choices.Select(c => c is TerminalClause<T>).Aggregate((x, y) => x && y);
public bool IsNonTerminalChoice => Choices.Select(c => c is NonTerminalClause<T>).Aggregate((x, y) => x && y);

public List<IClause<T>> Choices { get; set; }
public List<IClause<T>> Choices { get; }
public ChoiceClause(IClause<T> clause)
{
Choices = new List<IClause<T>> {clause};
Expand All @@ -31,7 +31,7 @@ public ChoiceClause(IClause<T> choice, List<IClause<T>> choices) : this(choice)
[ExcludeFromCodeCoverage]
public override string ToString()
{
var choices = string.Join(" | ", Choices.Select<IClause<T>, string>(c => c.Dump()));
var choices = string.Join(" | ", Choices.Select(c => c.Dump()));
return $"[ {choices} ]";
}

Expand Down
2 changes: 1 addition & 1 deletion src/sly/parser/syntax/grammar/ClauseSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void AddRange(ClauseSequence<T> seq)
[ExcludeFromCodeCoverage]
public string Dump()
{
return Clauses.Select<IClause<T>, string>(c => c.Dump()).Aggregate<string>((d1, d2) => d1 + " " + d2);
return Clauses.Select(c => c.Dump()).Aggregate((d1, d2) => d1 + " " + d2);
}

public bool Equals(IClause<T> other)
Expand Down
2 changes: 1 addition & 1 deletion src/sly/parser/syntax/grammar/GroupClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public string Dump()
{
StringBuilder dump = new StringBuilder();
dump.Append("( ");
dump.Append(Clauses.Select<IClause<T>, string>(c => c.Dump()).Aggregate<string>((d1, d2) => d1 + " " + d2));
dump.Append(Clauses.Select(c => c.Dump()).Aggregate((d1, d2) => d1 + " " + d2));
dump.Append(" )");
return dump.ToString();
}
Expand Down
Loading

0 comments on commit 7205aec

Please sign in to comment.