Skip to content

Commit

Permalink
Builds, with warnings.. many warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed May 28, 2024
1 parent 87ace51 commit 8c5259d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 122 deletions.
7 changes: 3 additions & 4 deletions YangParser/Generator/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace YangParser.Generator;

public static class Log
{
private static DateTime Start;
private static DateTime Start = DateTime.Now;
private static FileStream? m_stream;

private static FileStream Stream
Expand All @@ -22,19 +22,18 @@ private static FileStream Stream

public static void Clear()
{
Write("END OF LOG\n*/");
// m_stream?.Dispose();
// m_writer?.Dispose();
// m_stream = null;
// m_writer = null;
}

private static StringBuilder? m_writer;
private static StringBuilder writer => m_writer ??= new StringBuilder("/*\n");
private static StringBuilder writer => m_writer ??= new StringBuilder();

public static void Write(string message)
{
writer.AppendLine($"{(DateTime.Now - Start).TotalSeconds:F2}: " + message);
writer.AppendLine($"/*\n{(DateTime.Now - Start).TotalSeconds:F2}: " + message + "\n*/");
}

public static string Content => writer.ToString();
Expand Down
218 changes: 107 additions & 111 deletions YangParser/Generator/YangGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,29 @@ private void MakeClasses(SourceProductionContext context, ImmutableArray<ResultO
}
catch (Exception e)
{
WriteFile(context, "errors", "#error General Exception" + "\n/*\n" + e.Message + "\n" + e.StackTrace + "\n*/");
WriteFile(context, "errors",
"#error General Exception" + "\n/*\n" + e.Message + "\n" + e.StackTrace + "\n*/");
}

Log.Clear();
WriteFile(context, "log.cs", Log.Content);
}

public void WriteFile(SourceProductionContext context, string fileName, string content)
{
context.AddSource(fileName, content);
Log.Write($"Writing file {fileName}");
var file = "C:/tmp/YangGenerator/" + fileName;
var dir = System.IO.Path.GetDirectoryName(file);
#pragma warning disable RS1035 // Do not use APIs banned for analyzers
if (Directory.Exists(dir) == false)
{
Directory.CreateDirectory(dir);
}
#pragma warning restore RS1035 // Do not use APIs banned for analyzers
using var fs = new FileStream(file, FileMode.Create);
using var writer = new StreamWriter(fs);
writer.Write(content);
Log.Clear();
// Log.Write($"Writing file {fileName}");
// var file = "C:/tmp/YangGenerator/" + fileName;
// var dir = System.IO.Path.GetDirectoryName(file);
// #pragma warning disable RS1035 // Do not use APIs banned for analyzers
// if (Directory.Exists(dir) == false)
// {
// Directory.CreateDirectory(dir);
// }
// #pragma warning restore RS1035 // Do not use APIs banned for analyzers
// using var fs = new FileStream(file, FileMode.Create);
// using var writer = new StreamWriter(fs);
// writer.Write(content);
}

private string Clean(string input)
Expand Down Expand Up @@ -263,14 +266,7 @@ private static void ReportDiagnostics(SourceProductionContext context, ResultOrE

private static readonly DiagnosticDescriptor SemanticError = new DiagnosticDescriptor("YANG0002", "Semantic Error",

Check warning on line 267 in YangParser/Generator/YangGenerator.cs

View workflow job for this annotation

GitHub Actions / Performance regression check

Enable analyzer release tracking for the analyzer project containing rule 'YANG0002' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 267 in YangParser/Generator/YangGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Enable analyzer release tracking for the analyzer project containing rule 'YANG0002' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 267 in YangParser/Generator/YangGenerator.cs

View workflow job for this annotation

GitHub Actions / Performance regression check

Enable analyzer release tracking for the analyzer project containing rule 'YANG0002' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)
"Semantic Error: {0}", "SemanticModel", DiagnosticSeverity.Error, true);

// private void MakeClasses(SourceProductionContext context, AdditionalText text)
// {
// if (!Parse(context, text, out var parsed)) return;
// if (!MakeSemanticModel(context, parsed, out var statement)) return;
// if (statement is not Module module) return;
// context.AddSource(module.Filename, module.ToCode());
// }


private static ResultOrException<IStatement> MakeSemanticModel(ResultOrException<YangStatement> statement)

Expand Down Expand Up @@ -305,99 +301,99 @@ private void AddAttributesClass(IncrementalGeneratorPostInitializationContext co
{
var fileName = "YangModules/Attributes/Yang.Attributes.cs";
var contents = """
using System;
namespace Yang.Attributes;
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class RevisionAttribute(string date) : Attribute
{
public string Date { get; } = date;
}
using System;
namespace Yang.Attributes;
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class RevisionAttribute(string date) : Attribute
{
public string Date { get; } = date;
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class PresenceAttribute(string meaning) : Attribute
{
public string Meaning { get; } = meaning;
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ProvidesFeatureAttribute(string flag) : Attribute
{
public string FeatureFlag { get; } = flag;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class IfFeatureAttribute(string flag) : Attribute
{
public string FeatureFlag { get; } = flag;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class ReferenceAttribute(string reference) : Attribute
{
public string Reference { get; } = reference;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class WhenAttribute(string xPath) : Attribute
{
public string XPath { get; } = xPath;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class TargetAttribute(string xPath) : Attribute
{
public string XPath { get; } = xPath;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class KeyAttribute(params string[] value) : Attribute
{
public string[] Value { get; } = value;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MinElementsAttribute(int value) : Attribute
{
public int Value { get; } = value;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MaxElementsAttribute(int value) : Attribute
{
public int Value { get; } = value;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class InheritsAttribute(string baseName) : Attribute
{
public string BaseName { get; } = baseName;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class OrderedByAttribute(string value) : Attribute
{
public string Value { get; } = value;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class NotConfigurationData : Attribute;
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class PresenceAttribute(string meaning) : Attribute
{
public string Meaning { get; } = meaning;
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ProvidesFeatureAttribute(string flag) : Attribute
{
public string FeatureFlag { get; } = flag;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class IfFeatureAttribute(string flag) : Attribute
{
public string FeatureFlag { get; } = flag;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class ReferenceAttribute(string reference) : Attribute
{
public string Reference { get; } = reference;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class WhenAttribute(string xPath) : Attribute
{
public string XPath { get; } = xPath;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class TargetAttribute(string xPath) : Attribute
{
public string XPath { get; } = xPath;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class KeyAttribute(params string[] value) : Attribute
{
public string[] Value { get; } = value;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MinElementsAttribute(int value) : Attribute
{
public int Value { get; } = value;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MaxElementsAttribute(int value) : Attribute
{
public int Value { get; } = value;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class InheritsAttribute(string baseName) : Attribute
{
public string BaseName { get; } = baseName;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class OrderedByAttribute(string value) : Attribute
{
public string Value { get; } = value;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class NotConfigurationData : Attribute;
public class InstanceIdentifier(string path)
{
public string Path { get; } = path;
}
public interface IChannel
{
string Send(string xml);
}
public interface IXMLSource
{
string ToXML();
}
""";
public class InstanceIdentifier(string path)
{
public string Path { get; } = path;
}
public interface IChannel
{
string Send(string xml);
}
public interface IXMLSource
{
string ToXML();
}
""";
context.AddSource(fileName, contents);

Log.Write($"Writing file {fileName}");
var file = "C:/tmp/YangGenerator/" + fileName;
var dir = System.IO.Path.GetDirectoryName(file);
#pragma warning disable RS1035 // Do not use APIs banned for analyzers
if (Directory.Exists(dir) == false)
{
Directory.CreateDirectory(dir);
}
#pragma warning restore RS1035 // Do not use APIs banned for analyzers
using var fs = new FileStream(file, FileMode.Create);
using var writer = new StreamWriter(fs);
writer.Write(contents);
Log.Clear();
// var file = "C:/tmp/YangGenerator/" + fileName;
// var dir = System.IO.Path.GetDirectoryName(file);
// #pragma warning disable RS1035 // Do not use APIs banned for analyzers
// if (Directory.Exists(dir) == false)
// {
// Directory.CreateDirectory(dir);
// }
// #pragma warning restore RS1035 // Do not use APIs banned for analyzers
// using var fs = new FileStream(file, FileMode.Create);
// using var writer = new StreamWriter(fs);
// writer.Write(contents);
// Log.Clear();
}
}
14 changes: 9 additions & 5 deletions YangParser/SemanticModel/StatementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static T GetChild<T>(this IStatement statement) where T : class, IStateme
public static bool TryGetChild<T>(this IStatement statement, out T? child) where T : class, IStatement
{
child = null;
if (statement?.Children.FirstOrDefault(c => c is T) is T chosen)
if (statement.Children.FirstOrDefault(c => c is T) is T chosen)
{
child = chosen;
return true;
Expand Down Expand Up @@ -81,7 +81,7 @@ public static IEnumerable<IStatement> Unwrap(this IStatement source)
public static IStatement? FindSourceFor(this IStatement source, string prefix)
{
var module = source.GetModule();
var imports = module.Imports;
var imports = module?.Imports;
return imports?.FirstOrDefault(import => import.GetChild<Prefix>().Argument == prefix);
}

Expand Down Expand Up @@ -120,7 +120,7 @@ public static Grouping GetGrouping(this Uses use)
use.Source);
}

var prefix = use.Argument.Prefix(out var name);
var prefix = use.Argument.Prefix(out _);
if (!string.IsNullOrEmpty(prefix)) //Is 'outside-of-tree'
{
if (prefix == use.GetInheritedPrefix())
Expand All @@ -135,7 +135,8 @@ public static Grouping GetGrouping(this Uses use)

return grouping;
}
else if (!prefix.Contains("."))

if (!prefix.Contains("."))
{
var import = use.FindSourceFor(prefix);
if (import is null)
Expand All @@ -157,7 +158,10 @@ public static Grouping GetGrouping(this Uses use)
{
var source = use.Root().Children.OfType<Module>().FirstOrDefault(m => m.Namespace == prefix) ??
throw new SemanticError($"Could not find a module with the key {prefix}", use.Source);
var grouping = use.FindGrouping(source);
var grouping = use.FindGrouping(source) ?? throw new SemanticError(
$"Could not find a grouping statement to use for 'uses {use.Argument}' in module '{source.Argument}' from prefix '{prefix}'",
use.Source);
return grouping;
}
}

Expand Down
3 changes: 1 addition & 2 deletions YangParser/SemanticModel/Type.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Linq;
using YangParser.Parser;
using YangParser.SemanticModel.Builtins;
Expand Down Expand Up @@ -40,7 +39,7 @@ public string? Definition
if (!BuiltinTypeReference.IsBuiltin(this, out var typeName, out var definition))
{
if (this.FindReference<IStatement>(Argument) != null) return null;
m_definition = BuiltinTypeReference.DefaultPattern(this, [], [], TypeName(this), m_name);
m_definition = BuiltinTypeReference.DefaultPattern(this, [], [], TypeName(this), Name!);
return m_definition;
}
m_name = typeName;
Expand Down

0 comments on commit 8c5259d

Please sign in to comment.