Skip to content

Commit

Permalink
Unique Attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed May 29, 2024
1 parent 0ab9731 commit debf68d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
11 changes: 10 additions & 1 deletion YangParser/Generator/YangGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -355,6 +354,11 @@ public class WhenAttribute(string xPath) : Attribute
public string XPath { get; } = xPath;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class MustAttribute(string xPath) : Attribute
{
public string XPath { get; } = xPath;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class TargetAttribute(string xPath) : Attribute
{
public string XPath { get; } = xPath;
Expand All @@ -365,6 +369,11 @@ public class KeyAttribute(params string[] value) : Attribute
public string[] Value { get; } = value;
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class UniqueAttribute(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;
Expand Down
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override string ToCode()
{
var nodes = Children.Select(child => child.ToCode()).ToArray();
var name = Argument;
if (Parent.Argument == Argument)
if (Parent!.Argument == Argument)
{
name = "sub-" + name;
}
Expand Down
15 changes: 11 additions & 4 deletions YangParser/SemanticModel/Must.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ public Must(YangStatement statement) : base(statement)
{
if (statement.Keyword != Keyword)
throw new SemanticError($"Non-matching Keyword '{statement.Keyword}', expected {Keyword}", statement);

}

public const string Keyword = "must";
public override ChildRule[] PermittedChildren { get; } = [

public override ChildRule[] PermittedChildren { get; } =
[
new ChildRule(Description.Keyword),
new ChildRule(Reference.Keyword),
new ChildRule(ErrorAppTag.Keyword),
new ChildRule(ErrorMessage.Keyword)
];
}
];

public override string ToCode()
{
Parent?.Attributes.Add($"Must(\"{SingleLine(Argument).Replace("\"", "\\\"")}\")");
return string.Empty;
}
}
11 changes: 9 additions & 2 deletions YangParser/SemanticModel/Unique.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ public Unique(YangStatement statement) : base(statement)
{
if (statement.Keyword != Keyword)
throw new SemanticError($"Non-matching Keyword '{statement.Keyword}', expected {Keyword}", statement);

ValidateChildren(statement);
Identifiers = Argument.Split(' ');
Identifiers = Argument.Split(' ', '\n', '\t');
}

public const string Keyword = "unique";

public string[] Identifiers { get; }

public override string ToCode()
{
Parent?.Attributes.Add(
$"Unique({string.Join(",", Identifiers.Select(i => $"nameof({MakeName(SingleLine(i).Replace("\"", "\\\""))})"))})");
return string.Empty;
}
}

0 comments on commit debf68d

Please sign in to comment.