From debf68da1bd407caf23aa126d131eebfadd61f3c Mon Sep 17 00:00:00 2001 From: caran Date: Wed, 29 May 2024 13:07:31 +0200 Subject: [PATCH] Unique Attribute --- YangParser/Generator/YangGenerator.cs | 11 ++++++++++- YangParser/SemanticModel/Container.cs | 2 +- YangParser/SemanticModel/Must.cs | 15 +++++++++++---- YangParser/SemanticModel/Unique.cs | 11 +++++++++-- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/YangParser/Generator/YangGenerator.cs b/YangParser/Generator/YangGenerator.cs index e3706d1..1c3aaba 100644 --- a/YangParser/Generator/YangGenerator.cs +++ b/YangParser/Generator/YangGenerator.cs @@ -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; @@ -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; @@ -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; diff --git a/YangParser/SemanticModel/Container.cs b/YangParser/SemanticModel/Container.cs index 5a32a25..498c0e3 100644 --- a/YangParser/SemanticModel/Container.cs +++ b/YangParser/SemanticModel/Container.cs @@ -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; } diff --git a/YangParser/SemanticModel/Must.cs b/YangParser/SemanticModel/Must.cs index 12089d4..5554c3c 100644 --- a/YangParser/SemanticModel/Must.cs +++ b/YangParser/SemanticModel/Must.cs @@ -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; + } +} \ No newline at end of file diff --git a/YangParser/SemanticModel/Unique.cs b/YangParser/SemanticModel/Unique.cs index bbc7b79..cf67570 100644 --- a/YangParser/SemanticModel/Unique.cs +++ b/YangParser/SemanticModel/Unique.cs @@ -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; + } } \ No newline at end of file