Skip to content

Commit

Permalink
Deserialization is functional
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed Jun 3, 2024
1 parent fb48140 commit 9aa9dd1
Show file tree
Hide file tree
Showing 30 changed files with 760 additions and 244 deletions.
5 changes: 4 additions & 1 deletion Compiler.Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ public void GroupingTest()
Assert.IsNotType<Uses>(statement);
}
output.WriteLine(compilationUnit.ToCode());

foreach (var child in compilationUnit.Children)
{
output.WriteLine(child.ToCode());
}
Log.Clear();
}

Expand Down
1 change: 1 addition & 0 deletions YangParser/Generator/YangGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ public class NotConfigurationData : Attribute;
public class InstanceIdentifier(string path)
{
public string Path { get; } = path;
public static InstanceIdentifier Parse(string id) => new(id);
}
public interface IChannel
{
Expand Down
78 changes: 33 additions & 45 deletions YangParser/SemanticModel/Action.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Linq;
using System.Text;
using YangParser.Parser;

namespace YangParser.SemanticModel;

public class Action : Statement, IXMLValue
public class Action : Statement, IXMLWriteValue, IXMLAction
{
public Action(YangStatement statement) : base(statement)
{
Expand Down Expand Up @@ -57,17 +56,12 @@ public override string ToCode()
await writer.WriteAttributeStringAsync(null,"message-id",null,messageID.ToString());
await writer.WriteStartElementAsync(null,"action","urn:ietf:params:xml:ns:yang:1");
""");
if (Ingoing is not null)
{
builder.AppendLine($"\tthis.{MakeName(Argument)}InputValue = input;");
}
else
{
builder.AppendLine($"\tthis.{MakeName(Argument)}Active = true;");
}
builder.AppendLine(Ingoing is not null
? $""" this.{MakeName(Argument)}InputValue = input;"""
: $""" this.{MakeName(Argument)}Active = true;""");

builder.AppendLine("""
await WriteXML(writer);
await WriteXMLAsync(writer);
await writer.WriteEndElementAsync();
await writer.WriteEndElementAsync();
await writer.FlushAsync();
Expand All @@ -76,15 +70,9 @@ public override string ToCode()
builder.AppendLine(returnType != "Task"
? "\tvar response = channel.Send(xml);"
: "\tchannel.Send(xml);");
if (Ingoing is not null)
{
builder.AppendLine($"\tthis.{MakeName(Argument)}InputValue = null;");
}
else
{

builder.AppendLine($"\tthis.{MakeName(Argument)}Active = false;");
}
builder.AppendLine(Ingoing is not null
? $"\tthis.{MakeName(Argument)}InputValue = null;"
: $"\tthis.{MakeName(Argument)}Active = false;");

if (returnType != "Task")
{
Expand Down Expand Up @@ -127,29 +115,29 @@ protected override void ValidateParent()

public string TargetName => MakeName(Argument);

public string WriteCall
{
get
{
if (Ingoing is not null)
{
return $$"""
if({{MakeName(Argument)}}InputValue is not null)
{
await writer.WriteStartElementAsync({{xmlPrefix}},"{{Source.Argument}}",{{xmlNs}});
await {{MakeName(Argument)}}InputValue.WriteXML(writer);
await writer.WriteEndElementAsync();
}
""";
}

return $$"""
if({{MakeName(Argument)}}Active)
{
await writer.WriteStartElementAsync(null,"{Source.Argument}",null);
await writer.WriteEndElementAsync();
}
""";
}
}
public string WriteCall =>
Ingoing is not null
? $$"""
if({{MakeName(Argument)}}InputValue is not null)
{
await writer.WriteStartElementAsync({{xmlPrefix}},"{{Source.Argument}}",{{xmlNs}});
await {{MakeName(Argument)}}InputValue.WriteXMLAsync(writer);
await writer.WriteEndElementAsync();
}
"""
: $$"""
if({{MakeName(Argument)}}Active)
{
await writer.WriteStartElementAsync(null,"{Source.Argument}",null);
await writer.WriteEndElementAsync();
}
""";

public string ParseCall =>
Ingoing is not null
? $"""
await {MakeName(Argument)}Input.ParseAsync(reader);
await reader.ReadAsync();//Parse it out, but ignore
"""
: "await reader.ReadAsync();";
}
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/AnyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace YangParser.SemanticModel;

public class AnyData : Statement, IXMLValue
public class AnyData : Statement, IXMLWriteValue
{
public AnyData(YangStatement statement) : base(statement)
{
Expand Down
5 changes: 2 additions & 3 deletions YangParser/SemanticModel/Augment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public void Inject()
}

var top = Argument.StartsWith("/") ? GetModule(components) : Parent!;
Parent?.Replace(this,[]);

Parent?.Replace(this, []);

var target = GetTarget(top, components, sourceNS);

Expand Down Expand Up @@ -111,7 +111,6 @@ private IStatement GetTarget(IStatement top, string[] components, string sourceN
{
if (string.IsNullOrWhiteSpace(xpath)) continue;
xpath.Prefix(out var childName);
if (childName == current.Argument) break;
var origin = current;
foreach (var currentChild in current.Children)
{
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override string ToString()
{
return Convert.ToBase64String(WrittenValue);
}
public {{typeName}} Parse(string base64)
public static {{typeName}} Parse(string base64)
{
return new {{typeName}}(Convert.FromBase64String(base64));
}
Expand Down
33 changes: 30 additions & 3 deletions YangParser/SemanticModel/Builtins/Bits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,46 @@ public class Bits() : BuiltinType("bits", statement =>
child.ToCode();
}

var cases = new List<string>();
var writeCases = new List<string>();
var readCases = 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}\");");
writeCases.Add(
$"if ((value & {name}.{Statement.MakeName(e.Argument)}) == {name}.{Statement.MakeName(e.Argument)}) bits.Add(\"{e.Argument}\");");
readCases.Add(
$"""
case "{e.Argument}":
output ??= {name}.{Statement.MakeName(e.Argument)};
output |= {name}.{Statement.MakeName(e.Argument)};
break;
""");
}

var definition = $$"""
public static string GetEncodedValue({{name}} value)
{
List<string> bits = new List<string>();
{{Statement.Indent(string.Join("\n", cases))}}
{{Statement.Indent(string.Join("\n", writeCases))}}
return string.Join(" ", bits);
}
public static {{name}} Get{{name}}Value(string value)
{
{{name}}? output = null;
foreach(var component in value.Split(' '))
{
switch(component)
{
{{Statement.Indent(Statement.Indent(Statement.Indent(string.Join("\n", readCases))))}}
default: throw new Exception($"{component} is not a valid value for {{name}}");
}
}
if(output is null)
{
throw new Exception($"No value was assigned on decoding of {{name}} from {value}");
}
return output.Value!;
}
public static string GetEncodedValue({{name}}? value) => GetEncodedValue(value!.Value!);
{{statement.DescriptionString}}{{statement.AttributeString}}
public enum {{name}}
Expand Down
Loading

0 comments on commit 9aa9dd1

Please sign in to comment.