Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed May 28, 2024
1 parent 8c39c94 commit 903cdea
Show file tree
Hide file tree
Showing 35 changed files with 573 additions and 327 deletions.
28 changes: 24 additions & 4 deletions YangParser/Generator/Log.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace YangParser.Generator;

public static class Log
{
private static StringBuilder m_builder = new();
public static void Clear() => m_builder.Clear();
public static void Write(string message) => m_builder.AppendLine(message);
public static string Get() => m_builder.ToString();
private static DateTime Start;
private static FileStream? m_stream;
private static FileStream Stream
{
get
{
if (m_stream is null) Start = DateTime.Now;
return m_stream ??= new FileStream(@"C:\tmp\YangGenerator\log", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
}
}

public static void Clear()
{
m_writer?.Flush();
// m_stream?.Dispose();
// m_writer?.Dispose();
// m_stream = null;
// m_writer = null;
}
private static StreamWriter? m_writer;
private static StreamWriter writer => m_writer ??= new StreamWriter(Stream);
public static void Write(string message) => writer.WriteLine($"{(DateTime.Now - Start).TotalSeconds:F2}: " + message);
}
219 changes: 126 additions & 93 deletions YangParser/Generator/YangGenerator.cs

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion YangParser/SemanticModel/Augment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace YangParser.SemanticModel;

public class Augment : Statement
public class Augment : Statement, IUnexpandable
{
public Augment(YangStatement statement) : base(statement)
{
Expand Down Expand Up @@ -55,4 +55,8 @@ public override string ToCode()
*/
""";
}
protected override void ValidateParent()
{
this.GetModule()?.Augments.Add(this);
}
}
9 changes: 7 additions & 2 deletions YangParser/SemanticModel/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

namespace YangParser.SemanticModel;

public class Base : Statement
public class Base : Statement, IUnexpandable
{
public Base(YangStatement statement) : base(statement)
{
if (statement.Keyword != Keyword)
throw new SemanticError($"Non-matching Keyword '{statement.Keyword}', expected {Keyword}", statement);

}
public const string Keyword = "base";
override public string ToCode()
{
Parent?.Attributes.Add("Inherits(\"" + Argument + "\")");
return string.Empty;
}
}
11 changes: 5 additions & 6 deletions YangParser/SemanticModel/Builtins/Bits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

namespace YangParser.SemanticModel.Builtins;

public class Bits() : BuiltinType("bits", s =>
public class Bits() : BuiltinType("bits", statement =>
{
var name = Statement.MakeName(s.Parent!.Argument);
var bits = s.Children.OfType<Bit>().ToArray();
var others = s.Children.Except(bits);
var name = BuiltinTypeReference.TypeName(statement);
var bits = statement.Children.OfType<Bit>().ToArray();
var strings = bits.Select(e => e.ToCode());
foreach (var child in others)
foreach (var child in statement.Children)
{
child.ToCode();
}

var definition = $$"""
{{s.DescriptionString}}{{s.AttributeString}}
{{statement.DescriptionString}}{{statement.AttributeString}}
public enum {{name}}
{
{{Statement.Indent(string.Join("\n", strings))}}
Expand Down
16 changes: 16 additions & 0 deletions YangParser/SemanticModel/Builtins/BuiltinTypeReference.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -42,6 +43,10 @@ public static bool IsBuiltin(Type type, out string? cSharpType, out string? defi

return false;
}
public static bool IsBuiltinKeyword(string keyword)
{
return m_builtIns.Any(b => b.Name == keyword);
}

public static string DefaultPattern(IStatement statement, IEnumerable<string> staticFields,
IEnumerable<string> constructorStatements,
Expand All @@ -64,4 +69,15 @@ public class {{typeName}}
}
""";
}
public static string TypeName(IStatement type)
{
string Postfix = string.Empty;
var parent = type.Parent!;
while (IsBuiltinKeyword(parent.Argument))
{
Postfix += Array.IndexOf(parent.Children, type);
parent = parent.Parent!;
}
return Statement.MakeName(parent.Argument) + Postfix;
}
}
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/Decimal64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace YangParser.SemanticModel.Builtins;
public class Decimal64() : BuiltinType("decimal64", statement =>
{
if (!statement.TryGetChild<Range>(out var range)) return ("double", null);
var name = Statement.MakeName(statement.Parent!.Argument);
var name = BuiltinTypeReference.TypeName(statement);
return (name,
BuiltinTypeReference.DefaultPattern(statement, [], [range!.GetConstructorValidation()], "long", name));
});
10 changes: 5 additions & 5 deletions YangParser/SemanticModel/Builtins/Enumeration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace YangParser.SemanticModel.Builtins;

public class Enumeration() : BuiltinType("enumeration", (s) =>
public class Enumeration() : BuiltinType("enumeration", (statement) =>
{
var name = Statement.MakeName(s.Parent!.Argument);
var enums = s.Children.OfType<Enum>().ToArray();
var others = s.Children.Except(enums);
var name = BuiltinTypeReference.TypeName(statement);
var enums = statement.Children.OfType<Enum>().ToArray();
var others = statement.Children.Except(enums);
var strings = enums.Select(e => e.ToCode());
foreach (var child in others)
{
child.ToCode();
}

var definition = $$"""
{{s.DescriptionString}}{{s.AttributeString}}
{{statement.DescriptionString}}{{statement.AttributeString}}
public enum {{name}}
{
{{Statement.Indent(string.Join("\n", strings))}}
Expand Down
26 changes: 13 additions & 13 deletions YangParser/SemanticModel/Builtins/IdentityReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@

namespace YangParser.SemanticModel.Builtins;

public class IdentityReference() : BuiltinType("identityref", s =>
public class IdentityReference() : BuiltinType("identityref", statement =>
{
var inherits = s.Children.OfType<Base>().Select(Statement.InterfaceName).ToArray();
if (inherits.Length == 1)
{
var value = inherits[0];
return (value, null);
}

var inheritance = inherits.Length == 0 ? string.Empty : " : " + string.Join(", ", inherits);
var name = Statement.MakeName(s.Parent!.Argument);
var definition = $"""
{s.DescriptionString}{s.AttributeString}
public class {name}{inheritance};
var inherits = statement.Children.OfType<Base>().Select(x => '"' + x.Argument + '"').ToArray();
var name = BuiltinTypeReference.TypeName(statement);
var definition = $$"""
{{statement.DescriptionString}}{{statement.AttributeString}}
public class {{name}}
{
public string Value { get; }
public static string[] Bases = [{{string.Join(", ", inherits)}}];
public {{name}}(string input) => Value = input;
public static implicit operator string({{name}} input) => input.Value;
public static implicit operator {{name}}(string input) => new(input);
}
""";
return (name, definition);
});
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/Int16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace YangParser.SemanticModel.Builtins;
public class Int16() : BuiltinType("int16", statement =>
{
if (!statement.TryGetChild<Range>(out var range)) return ("short", null);
var name = Statement.MakeName(statement.Parent!.Argument);
var name = BuiltinTypeReference.TypeName(statement);
return (name,
BuiltinTypeReference.DefaultPattern(statement, [], [range!.GetConstructorValidation()], "short", name));
});
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/Int32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace YangParser.SemanticModel.Builtins;
public class Int32() : BuiltinType("int32", statement =>
{
if (!statement.TryGetChild<Range>(out var range)) return ("int", null);
var name = Statement.MakeName(statement.Parent!.Argument);
var name = BuiltinTypeReference.TypeName(statement);
return (name,
BuiltinTypeReference.DefaultPattern(statement, [], [range!.GetConstructorValidation()], "int", name));
});
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/Int64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace YangParser.SemanticModel.Builtins;
public class Int64() : BuiltinType("int64", statement =>
{
if (!statement.TryGetChild<Range>(out var range)) return ("long", null);
var name = Statement.MakeName(statement.Parent!.Argument);
var name = BuiltinTypeReference.TypeName(statement);
return (name,
BuiltinTypeReference.DefaultPattern(statement, [], [range!.GetConstructorValidation()], "long", name));
});
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/Int8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace YangParser.SemanticModel.Builtins;
public class Int8() : BuiltinType("int8", statement =>
{
if (!statement.TryGetChild<Range>(out var range)) return ("sbyte", null);
var name = Statement.MakeName(statement.Parent!.Argument);
var name = BuiltinTypeReference.TypeName(statement);
return (name,
BuiltinTypeReference.DefaultPattern(statement, [], [range!.GetConstructorValidation()], "sbyte", name));
});
8 changes: 4 additions & 4 deletions YangParser/SemanticModel/Builtins/LeafReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace YangParser.SemanticModel.Builtins;

public class LeafReference() : BuiltinType("leafref", (s) =>
public class LeafReference() : BuiltinType("leafref", (statement) =>
{
var path = (Path)s.Children.First(c => c is Path);
var name = Statement.MakeName(s.Parent!.Argument);
var path = (Path)statement.Children.First(c => c is Path);
var name = BuiltinTypeReference.TypeName(statement);
var definition = $$"""
{{s.DescriptionString}}{{s.AttributeString}}
{{statement.DescriptionString}}{{statement.AttributeString}}
public class {{name}}() : InstanceIdentifier("{{path.Argument}}");
""";
return (name, definition);
Expand Down
10 changes: 5 additions & 5 deletions YangParser/SemanticModel/Builtins/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace YangParser.SemanticModel.Builtins;

public class String() : BuiltinType("string", s =>
public class String() : BuiltinType("string", statement =>
{
var hasPattern = s.TryGetChild<Pattern>(out var pattern);
var hasLength = s.TryGetChild<Length>(out var length);
var hasPattern = statement.TryGetChild<Pattern>(out var pattern);
var hasLength = statement.TryGetChild<Length>(out var length);
if (hasPattern || hasLength)
{
var name = Statement.MakeName(s.Parent!.Argument);
var name = BuiltinTypeReference.TypeName(statement);
List<string> staticFields = new();
List<string> constructorStatements = new();
if (hasPattern)
Expand All @@ -29,7 +29,7 @@ public class String() : BuiltinType("string", s =>
constructorStatements.Add(length!.GetConstructorValidation());
}

return (name, BuiltinTypeReference.DefaultPattern(s, staticFields, constructorStatements, "string", name));
return (name, BuiltinTypeReference.DefaultPattern(statement, staticFields, constructorStatements, "string", name));
}

return ("string", null);
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/Uint16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace YangParser.SemanticModel.Builtins;
public class Uint16() : BuiltinType("uint16", statement =>
{
if (!statement.TryGetChild<Range>(out var range)) return ("ushort", null);
var name = Statement.MakeName(statement.Parent!.Argument);
var name = BuiltinTypeReference.TypeName(statement);
return (name,
BuiltinTypeReference.DefaultPattern(statement, [], [range!.GetConstructorValidation()], "ushort", name));
});
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/Uint32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace YangParser.SemanticModel.Builtins;
public class Uint32() : BuiltinType("uint32", statement =>
{
if (!statement.TryGetChild<Range>(out var range)) return ("uint", null);
var name = Statement.MakeName(statement.Parent!.Argument);
var name = BuiltinTypeReference.TypeName(statement);
return (name,
BuiltinTypeReference.DefaultPattern(statement, [], [range!.GetConstructorValidation()], "uint", name));
});
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/Uint64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace YangParser.SemanticModel.Builtins;
public class Uint64() : BuiltinType("uint64", statement =>
{
if (!statement.TryGetChild<Range>(out var range)) return ("ulong", null);
var name = Statement.MakeName(statement.Parent!.Argument);
var name = BuiltinTypeReference.TypeName(statement);
return (name,
BuiltinTypeReference.DefaultPattern(statement, [], [range!.GetConstructorValidation()], "ulong", name));
});
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/Uint8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace YangParser.SemanticModel.Builtins;
public class Uint8() : BuiltinType("uint8", statement =>
{
if (!statement.TryGetChild<Range>(out var range)) return ("byte", null);
var name = Statement.MakeName(statement.Parent!.Argument);
var name = BuiltinTypeReference.TypeName(statement);
return (name, BuiltinTypeReference.DefaultPattern(statement, [], [range!.GetConstructorValidation()], "byte", name));
});
12 changes: 7 additions & 5 deletions YangParser/SemanticModel/Builtins/Union.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ namespace YangParser.SemanticModel.Builtins;
public class Union() : BuiltinType("union", s =>
{
var options = s.Children.OfType<Type>().ToArray();
var sourceName = Statement.MakeName(s.Parent!.Argument);
var sourceName = BuiltinTypeReference.TypeName(s);
List<string> types = [];
List<string> declarations = [];
foreach (var option in options)
{
types.Add(option.Name!.Replace("Union", sourceName + Array.IndexOf(options, option)));
var typeName = option.Name!;
types.Add(typeName);
if (option.Definition != null)
{
declarations.Add(option.Definition.Replace("Union", sourceName + Array.IndexOf(options, option)));
var declaration = option.Definition;
declarations.Add(declaration);
}
}

var varName = (string t) => Statement.Capitalize(t).Split(':').Last() + "Value";
var varName = (string t) => Statement.Capitalize(t).Split(':', '.').Last() + "Value";
var name = sourceName;
var definition = $$"""
{{s.DescriptionString}}{{s.AttributeString}}
public class {{name}}
{
{{Statement.Indent(string.Join("\n", types.Select(typeName => $"private {name}({typeName} input){{ {varName(typeName)} = input; }}")))}}
{{Statement.Indent(string.Join("\n", types.Select(typeName => $"public {name}({typeName} input){{ {varName(typeName)} = input; }}")))}}
{{Statement.Indent(string.Join("\n", types.Select(typeName => $"public {typeName}? {varName(typeName)} {{ get; }}")))}}
{{Statement.Indent(string.Join("\n", types.Select(typeName => $"public static implicit operator {typeName}({name} input) => input.{varName(typeName)} ?? throw new InvalidOperationException(\"Union was not of effective type '{typeName}'\");")))}}
{{Statement.Indent(string.Join("\n", types.Select(typeName => $"public static implicit operator {name}({typeName} input) => new {name}(input);")))}}
Expand Down
Loading

0 comments on commit 903cdea

Please sign in to comment.