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 7205aec commit ea7a84d
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 74 deletions.
2 changes: 1 addition & 1 deletion qodana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ exclude:
- src/bench2.4
- src/benchCurrent
- src/profiler
- src/samples/ParserExample
- src/samples
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,41 @@ public class FunctionCall : Expression

public static double Sin(IEnumerable<double?> parameters)
{
if (parameters.Count() == 1 && parameters.First().HasValue)
var enumerable = parameters as double?[] ?? parameters.ToArray();
if (enumerable.Any() && enumerable.First().HasValue)
{
return Math.Sin(parameters.First().Value);
return Math.Sin(enumerable.First().Value);
}

return 0.0;
}

public static double Cos(IEnumerable<double?> parameters)
{
if (parameters.Count() == 1 && parameters.First().HasValue)
var enumerable = parameters as double?[] ?? parameters.ToArray();
if (enumerable.Count() == 1 && enumerable.First().HasValue)
{
return Math.Cos(parameters.First().Value);
return Math.Cos(enumerable.First().Value);
}
return 0.0;
}

public static double Tan(IEnumerable<double?> parameters)
{
if (parameters.Count() == 1 && parameters.First().HasValue)
var enumerable = parameters as double?[] ?? parameters.ToArray();
if (enumerable.Count() == 1 && enumerable.First().HasValue)
{
return Math.Tan(parameters.First().Value);
return Math.Tan(enumerable.First().Value);
}
return 0.0;
}

public static double Sqrt(IEnumerable<double?> parameters)
{
if (parameters.Count() == 1 && parameters.First().HasValue)
var enumerable = parameters as double?[] ?? parameters.ToArray();
if (enumerable.Count() == 1 && enumerable.First().HasValue)
{
return Math.Sqrt(parameters.First().Value);
return Math.Sqrt(enumerable.First().Value);
}
return 0.0;
}
Expand Down
9 changes: 4 additions & 5 deletions src/sly/lexer/GenericLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,10 @@ public void AddLexeme(GenericToken generic, IN token)
{
case GenericToken.Identifier:
{
if (derivedTokens.ContainsKey(GenericToken.Identifier))
if (derivedTokens.TryGetValue(GenericToken.Identifier, out var possibleTokens))
{
var possibleTokens = derivedTokens[GenericToken.Identifier];
if (possibleTokens.ContainsKey(match.Result.Value))
match.Properties[DerivedToken] = possibleTokens[match.Result.Value].tokenId;
if (possibleTokens.TryGetValue(match.Result.Value, out var possibleToken))
match.Properties[DerivedToken] = possibleToken.tokenId;
else
match.Properties[DerivedToken] = identifierDerivedToken;
}
Expand Down Expand Up @@ -1114,7 +1113,7 @@ public Token<IN> Transcode(FSMMatch<GenericToken> match)
tok.Position = inTok.Position;
tok.Discarded = inTok.Discarded;
tok.StringDelimiter = match.StringDelimiterChar;
tok.TokenID = match.Properties.ContainsKey(DerivedToken) ? (IN)match.Properties[DerivedToken] : default(IN);
tok.TokenID = match.Properties.TryGetValue(DerivedToken, out var property) ? (IN)property : default;
tok.IsLineEnding = match.IsLineEnding;
tok.IsEOS = match.IsEOS;
tok.IsIndent = match.IsIndent;
Expand Down
1 change: 0 additions & 1 deletion src/sly/lexer/LexerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,6 @@ private static BuildResult<ILexer<IN>> BuildGenericLexer<IN>(IDictionary<IN, Lis
result.Errors.AddRange(resultx.Errors);
lexer.AddKeyWord(default(IN), explicitToken, false, false,

Check notice on line 637 in src/sly/lexer/LexerBuilder.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred style of default value expression when type is not evident

Redundant type specification
ModeAttribute.DefaultLexerMode, resultx);
;
}
else
{
Expand Down
9 changes: 3 additions & 6 deletions src/sly/lexer/fsm/FSMLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,9 @@ public FSMTransition GetTransition(int nodeId, char token)
{
FSMTransition transition = null;
if (HasState(nodeId))

Check notice on line 113 in src/sly/lexer/fsm/FSMLexer.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Invert 'if' statement to reduce nesting

Invert 'if' statement to reduce nesting
if (Transitions.ContainsKey(nodeId))
if (Transitions.TryGetValue(nodeId, out var leavingTransitions))
{
var leavingTransitions = Transitions[nodeId];
transition = leavingTransitions.FirstOrDefault<FSMTransition>(t => t.Match(token));
transition = leavingTransitions.FirstOrDefault(t => t.Match(token));
}

return transition;
Expand All @@ -124,7 +123,7 @@ public FSMTransition GetTransition(int nodeId, char token)
public void AddTransition(FSMTransition transition)
{
var transitions = new List<FSMTransition>();
if (Transitions.ContainsKey(transition.FromNode)) transitions = Transitions[transition.FromNode];
if (Transitions.TryGetValue(transition.FromNode, out var transition1)) transitions = transition1;
transitions.Add(transition);
Transitions[transition.FromNode] = transitions;
}
Expand Down Expand Up @@ -182,7 +181,6 @@ public List<char> GetIndentations(ReadOnlyMemory<char> source, int index)
if (index >= source.Length)
{
return new List<char>();
;
}

char current = source.At<char>(index + i);

Check warning on line 186 in src/sly/lexer/fsm/FSMLexer.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Redundant type arguments of method

Type argument specification is redundant

Check notice on line 186 in src/sly/lexer/fsm/FSMLexer.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (for built-in types)

Use 'var' (built-in types)
Expand Down Expand Up @@ -412,7 +410,6 @@ private FSMMatch<N> ConsumeIndents(ReadOnlyMemory<char> source, LexerPosition le
return indent;
}

;
return null;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/sly/lexer/fsm/FSMLexerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public FSMLexerBuilder<N> GoTo(int state)

public FSMLexerBuilder<N> GoTo(string mark)
{
if (Marks.ContainsKey(mark))
GoTo(Marks[mark]);
if (Marks.TryGetValue(mark, out var mark1))
GoTo(mark1);
else
throw new ArgumentException($"mark {mark} does not exist in current builder");
return this;
Expand All @@ -63,7 +63,7 @@ public FSMNode<N> GetNode(int nodeId)
public FSMNode<N> GetNode(string mark)
{
FSMNode<N> node = null;
if (Marks.ContainsKey(mark)) node = GetNode(Marks[mark]);
if (Marks.TryGetValue(mark, out var mark1)) node = GetNode(mark1);
return node;
}

Expand Down
4 changes: 2 additions & 2 deletions src/sly/parser/generator/ExpressionRulesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public BuildResult<ParserConfiguration<IN, OUT>> BuildExpressionRules(
}

var operations = new List<OperationMetaData<IN>>();
if (operationsByPrecedence.ContainsKey(operation.Precedence))
operations = operationsByPrecedence[operation.Precedence];
if (operationsByPrecedence.TryGetValue(operation.Precedence, out var value))
operations = value;
operations.Add(operation);
operationsByPrecedence[operation.Precedence] = operations;
}
Expand Down
7 changes: 1 addition & 6 deletions src/sly/parser/generator/LeftRecursionChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ public static (bool recursionFound,List<List<string>> recursion) CheckLeftRecurs

foreach (var leftClause in leftClauses)
{
if (configuration.NonTerminals.ContainsKey(leftClause))
if (configuration.NonTerminals.TryGetValue(leftClause, out var newNonTerminal))

Check notice on line 112 in src/sly/parser/generator/LeftRecursionChecker.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 newNonTerminal = configuration.NonTerminals[leftClause];
if (newNonTerminal != null)

Check notice on line 114 in src/sly/parser/generator/LeftRecursionChecker.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 nPath = BuildPath(currentPath, leftClause);
Expand All @@ -124,10 +123,6 @@ public static (bool recursionFound,List<List<string>> recursion) CheckLeftRecurs

}
}
else
{
;
}
}

return (foundRecursion, recursions);
Expand Down
4 changes: 0 additions & 4 deletions src/sly/parser/generator/ParserBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,6 @@ private BuildResult<Parser<IN, OUT>> CheckVisitorSignature(BuildResult<Parser<IN
if (!rule.IsExpressionRule)
{
var visitor = rule.GetVisitor();
if (visitor == null)
{
;
}

var returnInfo = visitor.ReturnParameter;
var expectedReturn = typeof(OUT);
Expand Down
8 changes: 4 additions & 4 deletions src/sly/parser/parser/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace sly.parser.parser
{
public class Group<IN, OUT> where IN : struct
{
public List<GroupItem<IN, OUT>> Items;
public readonly List<GroupItem<IN, OUT>> Items;

Check notice on line 10 in src/sly/parser/parser/Group.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Member can be made private (non-private accessibility)

Field 'Items' can be made private

public Dictionary<string, GroupItem<IN, OUT>> ItemsByName;
private Dictionary<string, GroupItem<IN, OUT>> ItemsByName;

Check notice on line 12 in src/sly/parser/parser/Group.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Field can be made readonly (private accessibility)

Field can be made readonly

public Group()

Check notice on line 14 in src/sly/parser/parser/Group.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Convert constructor into member initializers

Convert constructor into member initializers
{
Expand All @@ -33,12 +33,12 @@ public Token<IN> Token(int i)

public OUT Value(string name)
{
return ItemsByName.ContainsKey(name) ? ItemsByName[name].Value : default(OUT);
return ItemsByName.TryGetValue(name, out var value) ? value.Value : default(OUT);

Check notice on line 36 in src/sly/parser/parser/Group.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred style of default value expression when type is not evident

Redundant type specification
}

public Token<IN> Token(string name)
{
return ItemsByName.ContainsKey(name) ? ItemsByName[name].Token : null;
return ItemsByName.TryGetValue(name, out var value) ? value.Token : null;
}

public void Add(string name, Token<IN> token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ private void InitStartingTokensWithNonTerminal(Rule<IN> rule, NonTerminalClause<
Dictionary<string, NonTerminal<IN>> nonTerminals)
{
InitStartingTokensForNonTerminal(nonTerminals, nonterm.NonTerminalName);
if (nonTerminals.ContainsKey(nonterm.NonTerminalName))
if (nonTerminals.TryGetValue(nonterm.NonTerminalName, out var firstNonTerminal))

Check notice on line 172 in src/sly/parser/parser/llparser/EBNFRecursiveDescentSyntaxParserStarter.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 firstNonTerminal = nonTerminals[nonterm.NonTerminalName];
firstNonTerminal.Rules.ForEach(r => { rule.PossibleLeadingTokens.AddRange(r.PossibleLeadingTokens); });
rule.PossibleLeadingTokens = rule.PossibleLeadingTokens.Distinct().ToList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,48 +120,42 @@ protected virtual void InitializeStartingTokens(ParserConfiguration<IN, OUT> con
protected virtual void InitStartingTokensForNonTerminal(Dictionary<string, NonTerminal<IN>> nonTerminals,
string name)
{
if (nonTerminals.ContainsKey(name))
if (nonTerminals.TryGetValue(name, out var nt))
{
var nt = nonTerminals[name];
nt.Rules.ForEach(r => InitStartingTokensForRule(nonTerminals, r));
}
}

protected void InitStartingTokensForRule(Dictionary<string, NonTerminal<IN>> nonTerminals,

Check notice on line 129 in src/sly/parser/parser/llparser/RecursiveDescentSyntaxParserStarter.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Member can be made private (non-private accessibility)

Method 'InitStartingTokensForRule' can be made private
Rule<IN> rule)
{
if (rule.PossibleLeadingTokens == null || rule.PossibleLeadingTokens.Count == 0)
if (rule.PossibleLeadingTokens != null && rule.PossibleLeadingTokens.Count != 0) return;
rule.PossibleLeadingTokens = new List<LeadingToken<IN>>();
if (rule.Clauses.Count <= 0) return;
var first = rule.Clauses[0];
switch (first)
{
rule.PossibleLeadingTokens = new List<LeadingToken<IN>>();
if (rule.Clauses.Count > 0)
case TerminalClause<IN> term:
rule.PossibleLeadingTokens.Add(term.ExpectedToken);
rule.PossibleLeadingTokens = rule.PossibleLeadingTokens.Distinct().ToList();
break;
case NonTerminalClause<IN> nonTerminalClause:
{
var first = rule.Clauses[0];
switch (first)
InitStartingTokensForNonTerminal(nonTerminals, nonTerminalClause.NonTerminalName);
if (nonTerminals.TryGetValue(nonTerminalClause.NonTerminalName, out var firstNonTerminal))
{
case TerminalClause<IN> term:
rule.PossibleLeadingTokens.Add(term.ExpectedToken);
rule.PossibleLeadingTokens = rule.PossibleLeadingTokens.Distinct().ToList();
break;
case NonTerminalClause<IN> nonterm:
firstNonTerminal.Rules.ForEach(r =>
{
InitStartingTokensForNonTerminal(nonTerminals, nonterm.NonTerminalName);
if (nonTerminals.ContainsKey(nonterm.NonTerminalName))
{
var firstNonTerminal = nonTerminals[nonterm.NonTerminalName];
firstNonTerminal.Rules.ForEach(r =>
{
rule.PossibleLeadingTokens.AddRange(r.PossibleLeadingTokens);
});
rule.PossibleLeadingTokens = rule.PossibleLeadingTokens.ToList();
}

break;
}
default:
InitStartingTokensForRuleExtensions(first,rule,nonTerminals);
break;
rule.PossibleLeadingTokens.AddRange(r.PossibleLeadingTokens);
});
rule.PossibleLeadingTokens = rule.PossibleLeadingTokens.ToList();
}

break;
}
default:
InitStartingTokensForRuleExtensions(first,rule,nonTerminals);
break;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/sly/parser/syntax/grammar/Rule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public OperationMetaData<IN> GetOperation(IN token = default)
{
if (IsExpressionRule)

Check notice on line 94 in src/sly/parser/syntax/grammar/Rule.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 operation = VisitorMethodsForOperation.ContainsKey(token)
? VisitorMethodsForOperation[token]
var operation = VisitorMethodsForOperation.TryGetValue(token, out var value)
? value
: null;
return operation;
}
Expand All @@ -117,8 +117,8 @@ public MethodInfo GetVisitor(IN token = default)
MethodInfo visitor = null;

Check warning on line 117 in src/sly/parser/syntax/grammar/Rule.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 (IsExpressionRule)
{
var operation = VisitorMethodsForOperation.ContainsKey(token)
? VisitorMethodsForOperation[token]
var operation = VisitorMethodsForOperation.TryGetValue(token, out var value)
? value
: null;
visitor = operation?.VisitorMethod;
}
Expand Down

0 comments on commit ea7a84d

Please sign in to comment.