Skip to content

Commit

Permalink
Advances #24 ; some code fixes & suppressions for MS FxCop code analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitz committed Oct 28, 2019
1 parent d11cc5a commit 459c072
Show file tree
Hide file tree
Showing 27 changed files with 308 additions and 155 deletions.
2 changes: 1 addition & 1 deletion 2017.Standard/2017.Standard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
</PropertyGroup>
<Import Project="..\Shared\Shared.projitems" Label="Shared" />
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.6.0" />
<PackageReference Include="Antlr4.Runtime.Standard" Version="4.7.2">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CSharp" Version="4.6.0" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions 2019/2019.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<PackageReference Include="Antlr4.Runtime" Version="4.6.6">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CSharp" Version="4.6.0" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
Expand Down
39 changes: 0 additions & 39 deletions ANTLR4ParseTreeVisualizer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,63 +29,24 @@ Global
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
ReleaseCI|Any CPU = ReleaseCI|Any CPU
ReleaseCI2017|Any CPU = ReleaseCI2017|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AFDED9CF-6E6D-461B-B996-3E2F364DD3AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AFDED9CF-6E6D-461B-B996-3E2F364DD3AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFDED9CF-6E6D-461B-B996-3E2F364DD3AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFDED9CF-6E6D-461B-B996-3E2F364DD3AD}.Release|Any CPU.Build.0 = Release|Any CPU
{AFDED9CF-6E6D-461B-B996-3E2F364DD3AD}.ReleaseCI|Any CPU.ActiveCfg = Debug|Any CPU
{AFDED9CF-6E6D-461B-B996-3E2F364DD3AD}.ReleaseCI|Any CPU.Build.0 = Debug|Any CPU
{AFDED9CF-6E6D-461B-B996-3E2F364DD3AD}.ReleaseCI2017|Any CPU.ActiveCfg = Release|Any CPU
{34D2CC07-B4FA-4CAE-8A90-2B3081B5B9A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{34D2CC07-B4FA-4CAE-8A90-2B3081B5B9A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34D2CC07-B4FA-4CAE-8A90-2B3081B5B9A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34D2CC07-B4FA-4CAE-8A90-2B3081B5B9A3}.Release|Any CPU.Build.0 = Release|Any CPU
{34D2CC07-B4FA-4CAE-8A90-2B3081B5B9A3}.ReleaseCI|Any CPU.ActiveCfg = Release|Any CPU
{34D2CC07-B4FA-4CAE-8A90-2B3081B5B9A3}.ReleaseCI2017|Any CPU.ActiveCfg = Release|Any CPU
{BEB4FBE6-54E8-4212-9D94-C4E8F318FD9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BEB4FBE6-54E8-4212-9D94-C4E8F318FD9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BEB4FBE6-54E8-4212-9D94-C4E8F318FD9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BEB4FBE6-54E8-4212-9D94-C4E8F318FD9F}.Release|Any CPU.Build.0 = Release|Any CPU
{BEB4FBE6-54E8-4212-9D94-C4E8F318FD9F}.ReleaseCI|Any CPU.ActiveCfg = Release|Any CPU
{BEB4FBE6-54E8-4212-9D94-C4E8F318FD9F}.ReleaseCI2017|Any CPU.ActiveCfg = Debug|Any CPU
{BEB4FBE6-54E8-4212-9D94-C4E8F318FD9F}.ReleaseCI2017|Any CPU.Build.0 = Debug|Any CPU
{CADA5CBA-1FE0-48A2-BF4F-7158C1CFF44F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CADA5CBA-1FE0-48A2-BF4F-7158C1CFF44F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CADA5CBA-1FE0-48A2-BF4F-7158C1CFF44F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CADA5CBA-1FE0-48A2-BF4F-7158C1CFF44F}.Release|Any CPU.Build.0 = Release|Any CPU
{CADA5CBA-1FE0-48A2-BF4F-7158C1CFF44F}.ReleaseCI|Any CPU.ActiveCfg = Release|Any CPU
{CADA5CBA-1FE0-48A2-BF4F-7158C1CFF44F}.ReleaseCI2017|Any CPU.ActiveCfg = Release|Any CPU
{2D92774C-E2F4-47C4-9A04-09198B1182F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D92774C-E2F4-47C4-9A04-09198B1182F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D92774C-E2F4-47C4-9A04-09198B1182F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D92774C-E2F4-47C4-9A04-09198B1182F8}.Release|Any CPU.Build.0 = Release|Any CPU
{2D92774C-E2F4-47C4-9A04-09198B1182F8}.ReleaseCI|Any CPU.ActiveCfg = Release|Any CPU
{2D92774C-E2F4-47C4-9A04-09198B1182F8}.ReleaseCI2017|Any CPU.ActiveCfg = Release|Any CPU
{7EF79EC6-FBBC-4CD2-8A14-8B7FF052561E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EF79EC6-FBBC-4CD2-8A14-8B7FF052561E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EF79EC6-FBBC-4CD2-8A14-8B7FF052561E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EF79EC6-FBBC-4CD2-8A14-8B7FF052561E}.Release|Any CPU.Build.0 = Release|Any CPU
{7EF79EC6-FBBC-4CD2-8A14-8B7FF052561E}.ReleaseCI|Any CPU.ActiveCfg = Release|Any CPU
{7EF79EC6-FBBC-4CD2-8A14-8B7FF052561E}.ReleaseCI2017|Any CPU.ActiveCfg = Release|Any CPU
{B24951ED-6A47-4F73-84ED-0A152039F884}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B24951ED-6A47-4F73-84ED-0A152039F884}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B24951ED-6A47-4F73-84ED-0A152039F884}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B24951ED-6A47-4F73-84ED-0A152039F884}.Release|Any CPU.Build.0 = Release|Any CPU
{B24951ED-6A47-4F73-84ED-0A152039F884}.ReleaseCI|Any CPU.ActiveCfg = Release|Any CPU
{B24951ED-6A47-4F73-84ED-0A152039F884}.ReleaseCI|Any CPU.Build.0 = Release|Any CPU
{B24951ED-6A47-4F73-84ED-0A152039F884}.ReleaseCI2017|Any CPU.ActiveCfg = Release|Any CPU
{66A64056-77CE-441D-9BE8-20A9437DEEE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66A64056-77CE-441D-9BE8-20A9437DEEE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66A64056-77CE-441D-9BE8-20A9437DEEE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66A64056-77CE-441D-9BE8-20A9437DEEE3}.Release|Any CPU.Build.0 = Release|Any CPU
{66A64056-77CE-441D-9BE8-20A9437DEEE3}.ReleaseCI|Any CPU.ActiveCfg = Release|Any CPU
{66A64056-77CE-441D-9BE8-20A9437DEEE3}.ReleaseCI2017|Any CPU.ActiveCfg = Release|Any CPU
{66A64056-77CE-441D-9BE8-20A9437DEEE3}.ReleaseCI2017|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
7 changes: 7 additions & 0 deletions Shared/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods")]

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters")]
[assembly: System.CLSCompliant(false)]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1717:Only FlagsAttribute enums should have plural names")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Documentation", "CA1200:Avoid using cref tags with a prefix", Justification = "<Pending>")]
17 changes: 15 additions & 2 deletions Shared/Models/ClassInfo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Antlr4.Runtime.Tree;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using ParseTreeVisualizer.Util;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -20,8 +22,12 @@ public class ClassInfo {
public string RuleName { get; }
public int? RuleID { get; }

public ReadOnlyCollection<string> MethodNames { get; }

private ClassInfo(string name) => Name = name;
public ClassInfo(Type t, string ruleName = null, int? ruleID = null) {
public ClassInfo(Type t, string ruleName = null, int? ruleID = null, bool loadMethodNames = false) {
if (t is null) { throw new ArgumentNullException(nameof(t)); }

Name = t.Name;
Namespace = t.Namespace;
Assembly = t.Assembly.Location;
Expand All @@ -33,6 +39,13 @@ public ClassInfo(Type t, string ruleName = null, int? ruleID = null) {
FullName = t.FullName;
if (!ruleName.IsNullOrWhitespace()) { RuleName = ruleName; }
RuleID = ruleID;

if (loadMethodNames) {
MethodNames = t.GetMethods()
.Where(x => x.ReturnType.InheritsFromOrImplements<ParserRuleContext>())
.Select(x => x.Name)
.ToList().AsReadOnly();
}
}

public override string ToString() => FullName ?? Name;
Expand Down
2 changes: 2 additions & 0 deletions Shared/Models/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static Config Get() {
}

public string SelectedParserName { get; set; }
public string ParseTokensWithRule { get; set; }
public string SelectedLexerName { get; set; }
public bool ShowTextTokens { get; set; } = true;
public bool ShowWhitespaceTokens { get; set; } = true;
Expand All @@ -64,6 +65,7 @@ public static Config Get() {
public string RootNodePath { get; set; }

public bool HasTreeFilter() => !(ShowTreeErrorTokens && ShowTreeWhitespaceTokens && ShowTreeTextTokens && ShowRuleContextNodes && SelectedRuleContexts.None());
public bool HasTokenListFilter() => !(ShowTextTokens && ShowErrorTokens && ShowWhitespaceTokens && SelectedTokenTypes.None());

public void Write() {
if (!Directory.Exists(ConfigFolder)) {
Expand Down
35 changes: 15 additions & 20 deletions Shared/Models/ParseTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
namespace ParseTreeVisualizer {
[Serializable]
public class ParseTreeNode {
public static ParseTreeNode GetPlaceholder(ParseTreeNode actualRoot) => new ParseTreeNode {
Caption = "(parent nodes)",
NodeType = TreeNodeType.Placeholder,
Children = new List<ParseTreeNode> { actualRoot },
CharSpan = actualRoot.CharSpan
};
public static ParseTreeNode GetPlaceholder(ParseTreeNode actualRoot) {
if (actualRoot is null) { throw new ArgumentNullException(nameof(actualRoot)); }
return new ParseTreeNode {
Caption = "(parent nodes)",
NodeType = TreeNodeType.Placeholder,
Children = new List<ParseTreeNode> { actualRoot },
CharSpan = actualRoot.CharSpan
};
}

public string Caption { get; private set; }
public List<PropertyValue> Properties { get; }
Expand All @@ -28,6 +31,11 @@ public class ParseTreeNode {

private ParseTreeNode() { }
public ParseTreeNode(IParseTree tree, List<Token> tokens, string[] ruleNames, Dictionary<int,string> tokenTypeMapping, Config config, Dictionary<Type, (string caption, int index)> ruleMapping, string path) {
if (tree is null) { throw new ArgumentNullException(nameof(tree)); }
if (ruleMapping is null) { throw new ArgumentNullException(nameof(ruleMapping)); }
if (tokens is null) { throw new ArgumentNullException(nameof(tokens)); }
if (config is null) { throw new ArgumentNullException(nameof(config)); }

var type = tree.GetType();

if (tree is ParserRuleContext ruleContext) {
Expand Down Expand Up @@ -60,20 +68,7 @@ public ParseTreeNode(IParseTree tree, List<Token> tokens, string[] ruleNames, Di
}
CharSpan = token.Span;

// should the token be added to the token list?
var addToken = false;

if (!token.IsError && !token.IsWhitespace) {
addToken = config.ShowTextTokens;
} else {
addToken =
(token.IsError ? config.ShowErrorTokens : true) &&
(token.IsWhitespace ? config.ShowWhitespaceTokens : true);
}

addToken &= config.SelectedTokenTypes.None() || token.TokenTypeID.In(config.SelectedTokenTypes);

if (addToken) {
if (token.ShowToken(config)) {
tokens.Add(token);
}
}
Expand Down
4 changes: 3 additions & 1 deletion Shared/Models/PropertyValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class PropertyValue {
public string Key { get; }
public string Value { get; }
public PropertyValue(object instance, PropertyInfo prp) {
if (prp is null) { throw new ArgumentNullException(nameof(prp)); }

Key = prp.Name;

// null values map to null strings
Expand All @@ -25,7 +27,7 @@ public PropertyValue(object instance, PropertyInfo prp) {
}
if (value != null) { Value = StringValue(value); }

Custom = !prp.DeclaringType?.Namespace?.StartsWith("Antlr4") ?? false;
Custom = !prp.DeclaringType?.Namespace?.StartsWith("Antlr4", StringComparison.Ordinal) ?? false;
}
}
}
42 changes: 31 additions & 11 deletions Shared/Models/Token.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Antlr4.Runtime.Tree;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using ParseTreeVisualizer.Util;
using System;
using System.Collections.Generic;
Expand All @@ -18,25 +19,44 @@ public class Token {
public bool IsError { get; }
public (int start, int stop) Span { get; }
public bool IsWhitespace { get; }
public Token(TerminalNodeImpl terminalNode, Dictionary<int,string> tokenTypeMapping) {
Index = terminalNode.Payload.TokenIndex;

TokenTypeID = terminalNode.Payload.Type;
public Token(IToken itoken, Dictionary<int,string> tokenTypeMapping) {
Index = itoken.TokenIndex;
TokenTypeID = itoken.Type;
Line = itoken.Line;
Col = itoken.Column;
Text = itoken.Text.ToCSharpLiteral(false);
Span = (itoken.StartIndex, itoken.StopIndex);
IsWhitespace = itoken.Text.IsNullOrWhitespace();

string tokenType = null;
TokenType =
tokenTypeMapping?[TokenTypeID] ??
TokenTypeID.ToString();
tokenTypeMapping?.TryGetValue(TokenTypeID, out tokenType) ?? false ?
tokenType :
$"{TokenTypeID}";
}

Line = terminalNode.Payload.Line;
Col = terminalNode.Payload.Column;
Text = terminalNode.Payload.Text.ToCSharpLiteral(false);
public Token(TerminalNodeImpl terminalNode, Dictionary<int,string> tokenTypeMapping) : this(terminalNode.Payload, tokenTypeMapping) {
if (terminalNode is ErrorNodeImpl) {
IsError = true;
}
}

public bool ShowToken(Config config) {
if (!config.HasTokenListFilter()) { return true; }
var showToken = false;

if (!IsError && !IsWhitespace) {
showToken = config.ShowTextTokens;
} else {
showToken =
(IsError ? config.ShowErrorTokens : true) &&
(IsWhitespace ? config.ShowWhitespaceTokens : true);
}

Span = (terminalNode.Payload.StartIndex, terminalNode.Payload.StopIndex);
showToken &= config.SelectedTokenTypes.None() || TokenTypeID.In(config.SelectedTokenTypes);

IsWhitespace = terminalNode.Payload.Text.IsNullOrWhitespace();
return showToken;
}
}
}
Loading

0 comments on commit 459c072

Please sign in to comment.