Skip to content

Commit

Permalink
More XML
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed May 30, 2024
1 parent fae9fb0 commit cf99537
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 12 deletions.
39 changes: 38 additions & 1 deletion YangParser/SemanticModel/Builtins/Binary.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
using System.Collections.Generic;

namespace YangParser.SemanticModel.Builtins;

public class Binary() : BuiltinType("binary", _ => ("byte[]", null));
public class Binary() : BuiltinType("binary", statement =>
{
var hasLength = statement.TryGetChild<Length>(out var length);
List<string> staticFields = new();
var typeName = BuiltinTypeReference.TypeName(statement);
List<string> constructorStatements = new();
if (hasLength)
{
constructorStatements.Add(length!.GetConstructorValidation());
}

return (typeName, $$"""
{{statement.DescriptionString}}{{statement.AttributeString}}
public class {{typeName}}
{
{{Statement.Indent(string.Join("\n", staticFields))}}
public byte[] WrittenValue { get; }
public static implicit operator byte[]?({{typeName}}? input) => input?.WrittenValue;
public static implicit operator {{typeName}}(byte[] input) => new {{typeName}}(input);
public {{typeName}}(byte[] input)
{
{{Statement.Indent(Statement.Indent(string.Join("\n", constructorStatements)))}}
WrittenValue = input;
}
public override string ToString()
{
return Convert.ToBase64String(WrittenValue);
}
public {{typeName}} Parse(string base64)
{
return new {{typeName}}(Convert.FromBase64String(base64));
}
}
""");
});
14 changes: 14 additions & 0 deletions YangParser/SemanticModel/Builtins/Bits.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;

namespace YangParser.SemanticModel.Builtins;
Expand All @@ -12,7 +13,20 @@ public class Bits() : BuiltinType("bits", statement =>
child.ToCode();
}

var cases = new List<string>();
foreach (var e in bits)
{
cases.Add($"if ((value & {name}.{Statement.MakeName(e.Argument)}) == {name}.{Statement.MakeName(e.Argument)}) bits.Add(\"{e.Argument}\");");
}

var definition = $$"""
public static string GetEncodedValue({{name}} value)
{
List<string> bits = new List<string>();
{{Statement.Indent(string.Join("\n", cases))}}
return string.Join(" ", bits);
}
public static string GetEncodedValue({{name}}? value) => GetEncodedValue(value!.Value!);
{{statement.DescriptionString}}{{statement.AttributeString}}
public enum {{name}}
{
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/BuiltinTypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class {{typeName}}
}
public override string ToString()
{
return WrittenValue.ToString();
return WrittenValue.ToString()!;
}
}
""";
Expand Down
16 changes: 16 additions & 0 deletions YangParser/SemanticModel/Builtins/Enumeration.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;

namespace YangParser.SemanticModel.Builtins;
Expand All @@ -13,7 +14,22 @@ public class Enumeration() : BuiltinType("enumeration", (statement) =>
child.ToCode();
}

var cases = new List<string>();
foreach (var e in enums)
{
cases.Add($"case {name}.{Statement.MakeName(e.Argument)}: return \"{e.Argument}\";");
}

var definition = $$"""
public static string GetEncodedValue({{name}} value)
{
switch(value)
{
{{Statement.Indent(Statement.Indent(string.Join("\n", cases)))}}
default: return value.ToString();
}
}
public static string GetEncodedValue({{name}}? value) => GetEncodedValue(value!.Value!);
{{statement.DescriptionString}}{{statement.AttributeString}}
public enum {{name}}
{
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/IdentityReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class {{name}}
public string Identity { get; }
public static string[] Bases = [{{string.Join(", ", inherits)}}];
public {{name}}(string input) => Identity = input;
public static implicit operator string({{name}} input) => input.Identity;
public static implicit operator string?({{name}}? input) => input?.Identity;
public static implicit operator {{name}}(string input) => new(input);
}
""";
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/Union.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class {{name}}
{
{{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 {typeName}?({name}? input) => input is null ? null : 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);")))}}
{{Statement.Indent(string.Join("\n", declarations))}}
}
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ public class {{MakeName(name)}}Container
""";
}

public string TargetName { get; private set; }
public string TargetName { get; private set; } = string.Empty;
}
20 changes: 16 additions & 4 deletions YangParser/SemanticModel/Leaf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,31 @@ public override string ToCode()
""";
}

public string TargetName { get; private set; }
public string TargetName { get; private set; } = string.Empty;

public string WriteCall
{
get
{
var pre = string.IsNullOrWhiteSpace(Prefix) ? "null" : $"\"{Prefix}\"";
if (Type.Argument is "enumeration" or "bits")
{
return $$"""
if({{TargetName}} != default)
{
await writer.WriteStartElementAsync({{pre}},"{{Argument}}",null);
await writer.WriteStringAsync(GetEncodedValue({{TargetName}}!));
await writer.WriteEndElementAsync();
}
""";
}

return $$"""
if({{TargetName}} != default)
{
await writer.WriteStartElementAsync({{pre}},"{{Argument}}",null);
await writer.WriteStringAsync({{TargetName}}!.ToString());
await writer.WriteEndElementAsync();
await writer.WriteStartElementAsync({{pre}},"{{Argument}}",null);
await writer.WriteStringAsync({{TargetName}}!.ToString());
await writer.WriteEndElementAsync();
}
""";
}
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/LeafList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public override string ToCode()
""";
}

public string TargetName { get; private set; }
public string TargetName { get; private set; } = string.Empty;

public string WriteCall
{
Expand Down
23 changes: 22 additions & 1 deletion YangParser/SemanticModel/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace YangParser.SemanticModel;

public class List : Statement, IClassSource
public class List : Statement, IClassSource, IXMLValue
{
private YangStatement m_source;

Expand Down Expand Up @@ -53,6 +53,7 @@ public List(YangStatement statement) : base(statement)
public override string ToCode()
{
var nodes = Children.Select(child => child.ToCode()).ToArray();
TargetName = MakeName(Argument);
string property =
$"\n{DescriptionString}\npublic{KeywordString}List<{MakeName(Argument)}Entry> {MakeName(Argument)} {{ get; }} = new();";
return $$"""
Expand All @@ -61,7 +62,27 @@ public override string ToCode()
public class {{MakeName(Argument)}}Entry
{
{{string.Join("\n\t", nodes.Select(Indent))}}
{{Indent(XmlFunction())}}
}
""";
}

public string TargetName { get; private set; }

public string WriteCall
{
get
{
var pre = string.IsNullOrWhiteSpace(Prefix) ? "null" : $"\"{Prefix}\"";
return $$"""
if({{TargetName}} != null)
{
foreach(var element in {{TargetName}})
{
await element!.WriteXML(writer);
}
}
""";
}
}
}
8 changes: 7 additions & 1 deletion YangParser/SemanticModel/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,16 @@ public Module(YangStatement statement) : base(statement)
{
Extensions.Add(extension);
}

if (child is Feature feature)
{
Features.Add(feature);
}
}
}

public List<Feature> Features { get; } = [];

public Dictionary<string, string> ImportedModules { get; } = [];
public Dictionary<string, string> Usings { get; }
public string Namespace { get; private set; }
Expand Down Expand Up @@ -119,7 +126,6 @@ namespace {{ns}};
{{DescriptionString}}{{AttributeString}}
public class YangNode
{
{{string.Join("\n\t", Usings.Select(p => $"//Importing {p.Value} as {p.Key}"))}}
{{string.Join("\n\t", nodes)}}
{{string.Join("\n\t", extraDefinitions)}}
}
Expand Down

0 comments on commit cf99537

Please sign in to comment.