From f9a6cd0f639cb7b8caed208a3ca1d56bc0a09994 Mon Sep 17 00:00:00 2001 From: caran Date: Fri, 17 May 2024 10:07:04 +0200 Subject: [PATCH] Start of Generator --- .gitignore | 3 +- Compiler.Tests/ParsingTests.cs | 1 + TestData/YangSource/YangSource.csproj | 18 ++ TestData/YangSource/ietf-yang-library.yang | 208 ++++++++++++++++++ YangParser/Generator/YangGenerator.cs | 129 +++++++++++ YangParser/{ => Parser}/Position.cs | 2 +- YangParser/{ => Parser}/TermSymbol.cs | 2 +- YangParser/{ => Parser}/Token.cs | 2 +- YangParser/{ => Parser}/TokenScanner.cs | 2 +- .../{ => Parser}/YangStatementScanner.cs | 2 +- YangParser/SemanticModel/Action.cs | 1 + YangParser/SemanticModel/AnyXml.cs | 1 + YangParser/SemanticModel/Argument.cs | 1 + YangParser/SemanticModel/Augment.cs | 1 + YangParser/SemanticModel/Base.cs | 1 + YangParser/SemanticModel/Bit.cs | 1 + YangParser/SemanticModel/Case.cs | 3 +- YangParser/SemanticModel/Choice.cs | 3 +- YangParser/SemanticModel/Contact.cs | 1 + YangParser/SemanticModel/Container.cs | 3 +- YangParser/SemanticModel/DefaultValue.cs | 1 + YangParser/SemanticModel/Description.cs | 2 + YangParser/SemanticModel/Deviate.cs | 1 + YangParser/SemanticModel/Deviation.cs | 1 + YangParser/SemanticModel/Enum.cs | 1 + YangParser/SemanticModel/ErrorAppTag.cs | 1 + YangParser/SemanticModel/ErrorMessage.cs | 1 + YangParser/SemanticModel/Extension.cs | 1 + YangParser/SemanticModel/Feature.cs | 1 + YangParser/SemanticModel/FeatureFlag.cs | 1 + YangParser/SemanticModel/Grouping.cs | 3 +- YangParser/SemanticModel/IStatement.cs | 10 + YangParser/SemanticModel/Identity.cs | 1 + YangParser/SemanticModel/Import.cs | 1 + YangParser/SemanticModel/Include.cs | 1 + YangParser/SemanticModel/Input.cs | 1 + YangParser/SemanticModel/Key.cs | 1 + YangParser/SemanticModel/Leaf.cs | 3 +- YangParser/SemanticModel/LeafList.cs | 3 +- YangParser/SemanticModel/Length.cs | 1 + YangParser/SemanticModel/List.cs | 3 +- YangParser/SemanticModel/Mandatory.cs | 2 + YangParser/SemanticModel/MaxElements.cs | 1 + YangParser/SemanticModel/MinElements.cs | 1 + YangParser/SemanticModel/Module.cs | 4 + YangParser/SemanticModel/Must.cs | 1 + YangParser/SemanticModel/Namespace.cs | 1 + YangParser/SemanticModel/Notification.cs | 1 + YangParser/SemanticModel/OrderedBy.cs | 1 + YangParser/SemanticModel/Organization.cs | 1 + YangParser/SemanticModel/Output.cs | 1 + YangParser/SemanticModel/Path.cs | 1 + YangParser/SemanticModel/Pattern.cs | 1 + YangParser/SemanticModel/Position.cs | 1 + YangParser/SemanticModel/Prefix.cs | 1 + YangParser/SemanticModel/Range.cs | 1 + YangParser/SemanticModel/Reference.cs | 1 + YangParser/SemanticModel/Refine.cs | 1 + YangParser/SemanticModel/RequireInstance.cs | 1 + YangParser/SemanticModel/Revision.cs | 1 + YangParser/SemanticModel/RevisionDate.cs | 1 + YangParser/SemanticModel/Rpc.cs | 3 +- YangParser/SemanticModel/StateData.cs | 2 + YangParser/SemanticModel/Statement.cs | 1 + YangParser/SemanticModel/StatementFactory.cs | 1 + YangParser/SemanticModel/Status.cs | 9 +- YangParser/SemanticModel/Type.cs | 1 + YangParser/SemanticModel/TypeDefinition.cs | 3 +- YangParser/SemanticModel/Unique.cs | 1 + YangParser/SemanticModel/Units.cs | 1 + YangParser/SemanticModel/Uses.cs | 1 + YangParser/SemanticModel/Value.cs | 2 +- YangParser/SemanticModel/When.cs | 1 + YangParser/SemanticModel/YangVersion.cs | 1 + YangParser/SemanticModel/YinElement.cs | 1 + YangParser/YangParser.csproj | 5 + benchmarks/Program.cs | 1 + yang-compiler.sln | 10 + 78 files changed, 474 insertions(+), 18 deletions(-) create mode 100644 TestData/YangSource/YangSource.csproj create mode 100644 TestData/YangSource/ietf-yang-library.yang create mode 100644 YangParser/Generator/YangGenerator.cs rename YangParser/{ => Parser}/Position.cs (86%) rename YangParser/{ => Parser}/TermSymbol.cs (85%) rename YangParser/{ => Parser}/Token.cs (92%) rename YangParser/{ => Parser}/TokenScanner.cs (99%) rename YangParser/{ => Parser}/YangStatementScanner.cs (99%) diff --git a/.gitignore b/.gitignore index f2d5742..79152c7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ obj/ /packages/ riderModule.iml /_ReSharper.Caches/ -*/.idea/* \ No newline at end of file +*/.idea/* +.idea/ \ No newline at end of file diff --git a/Compiler.Tests/ParsingTests.cs b/Compiler.Tests/ParsingTests.cs index a24c10c..2e93152 100644 --- a/Compiler.Tests/ParsingTests.cs +++ b/Compiler.Tests/ParsingTests.cs @@ -1,6 +1,7 @@ using System.Text; using Xunit.Abstractions; using YangParser; +using YangParser.Parser; using YangParser.SemanticModel; namespace Compiler.Tests; diff --git a/TestData/YangSource/YangSource.csproj b/TestData/YangSource/YangSource.csproj new file mode 100644 index 0000000..cd42c21 --- /dev/null +++ b/TestData/YangSource/YangSource.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + latest + enable + enable + + + + + + + + + + + diff --git a/TestData/YangSource/ietf-yang-library.yang b/TestData/YangSource/ietf-yang-library.yang new file mode 100644 index 0000000..be388ee --- /dev/null +++ b/TestData/YangSource/ietf-yang-library.yang @@ -0,0 +1,208 @@ +module ietf-yang-library { + namespace "urn:ietf:params:xml:ns:yang:ietf-yang-library"; + prefix "yanglib"; + import ietf-yang-types { + prefix yang; + } + import ietf-inet-types { + prefix inet; + } + organization + "IETF NETCONF (Network Configuration) Working Group"; + contact + "WG Web: + WG List: + WG Chair: Mehmet Ersue + + WG Chair: Mahesh Jethanandani + + Editor: Andy Bierman + + Editor: Martin Bjorklund + + Editor: Kent Watsen + "; + description + "This module contains monitoring information about the YANG + modules and submodules that are used within a YANG-based + server. + Copyright (c) 2016 IETF Trust and the persons identified as + authors of the code. All rights reserved. + Redistribution and use in source and binary forms, with or + without modification, is permitted pursuant to, and subject + to the license terms contained in, the Simplified BSD License + set forth in Section 4.c of the IETF Trust's Legal Provisions + Relating to IETF Documents + (http://trustee.ietf.org/license-info). + This version of this YANG module is part of RFC 7895; see + the RFC itself for full legal notices."; + revision 2016-06-21 { + description + "Initial revision."; + reference + "RFC 7895: YANG Module Library."; + } + /* + * Typedefs + */ + typedef revision-identifier { + type string { + pattern '\d{4}-\d{2}-\d{2}'; + } + description + "Represents a specific date in YYYY-MM-DD format."; + } + /* + * Groupings + */ + grouping module-list { + description + "The module data structure is represented as a grouping + so it can be reused in configuration or another monitoring + data structure."; + grouping common-leafs { + description + "Common parameters for YANG modules and submodules."; + leaf name { + type yang:yang-identifier; + description + "The YANG module or submodule name."; + } + leaf revision { + type union { + type revision-identifier; + type string { length 0; } + } + description + "The YANG module or submodule revision date. + A zero-length string is used if no revision statement + is present in the YANG module or submodule."; + } + } + grouping schema-leaf { + description + "Common schema leaf parameter for modules and submodules."; + leaf schema { + type inet:uri; + description + "Contains a URL that represents the YANG schema + resource for this module or submodule. + This leaf will only be present if there is a URL + available for retrieval of the schema for this entry."; + } + } + list module { + key "name revision"; + description + "Each entry represents one revision of one module + currently supported by the server."; + uses common-leafs; + uses schema-leaf; + leaf namespace { + type inet:uri; + mandatory true; + description + "The XML namespace identifier for this module."; + } + leaf-list feature { + type yang:yang-identifier; + description + "List of YANG feature names from this module that are + supported by the server, regardless of whether they are + defined in the module or any included submodule."; + } + list deviation { + key "name revision"; + description + "List of YANG deviation module names and revisions + used by this server to modify the conformance of + the module associated with this entry. Note that + the same module can be used for deviations for + multiple modules, so the same entry MAY appear + within multiple 'module' entries. + The deviation module MUST be present in the 'module' + list, with the same name and revision values. + The 'conformance-type' value will be 'implement' for + the deviation module."; + uses common-leafs; + } + leaf conformance-type { + type enumeration { + enum implement { + description + "Indicates that the server implements one or more + protocol-accessible objects defined in the YANG module + identified in this entry. This includes deviation + statements defined in the module. + For YANG version 1.1 modules, there is at most one + module entry with conformance type 'implement' for a + particular module name, since YANG 1.1 requires that, + at most, one revision of a module is implemented. + For YANG version 1 modules, there SHOULD NOT be more + than one module entry for a particular module name."; + } + enum import { + description + "Indicates that the server imports reusable definitions + from the specified revision of the module but does + not implement any protocol-accessible objects from + this revision. + Multiple module entries for the same module name MAY + exist. This can occur if multiple modules import the + same module but specify different revision dates in + the import statements."; + } + } + mandatory true; + description + "Indicates the type of conformance the server is claiming + for the YANG module identified by this entry."; + } + list submodule { + key "name revision"; + description + "Each entry represents one submodule within the + parent module."; + uses common-leafs; + uses schema-leaf; + } + } + } + /* + * Operational state data nodes + */ + container modules-state { + config false; + description + "Contains YANG module monitoring information."; + leaf module-set-id { + type string; + mandatory true; + description + "Contains a server-specific identifier representing + the current set of modules and submodules. The + server MUST change the value of this leaf if the + information represented by the 'module' list instances + has changed."; + } + uses module-list; + } + /* + * Notifications + */ + notification yang-library-change { + description + "Generated when the set of modules and submodules supported + by the server has changed."; + leaf module-set-id { + type leafref { + path "/yanglib:modules-state/yanglib:module-set-id"; + } + mandatory true; + description + "Contains the module-set-id value representing the + set of modules and submodules supported at the server at + the time the notification is generated."; + } + } +} \ No newline at end of file diff --git a/YangParser/Generator/YangGenerator.cs b/YangParser/Generator/YangGenerator.cs new file mode 100644 index 0000000..7fe99c0 --- /dev/null +++ b/YangParser/Generator/YangGenerator.cs @@ -0,0 +1,129 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.CodeAnalysis; +using YangParser.SemanticModel; + +namespace YangParser.Generator; + +[Generator] +public class YangGenerator : IIncrementalGenerator +{ + public void Initialize(IncrementalGeneratorInitializationContext context) + { + var yangFiles = context.AdditionalTextsProvider.Where(text => text.Path.EndsWith(".yang")); + var parsed = yangFiles.Select((p, _) => Parser.Parser.Parse(p.Path, p.GetText()!.ToString())); + var model = parsed.Select((p, _) => StatementFactory.Create(p)); + context.RegisterSourceOutput(model, MakeClasses); + } + + private void MakeClasses(SourceProductionContext context, IStatement statement) + { + if (statement is Module module) + { + var usings = new Dictionary(); + string ns = MakeNamespace(module.Argument); + var functions = new List(); + foreach (var child in module.Children) + { + if (child is Import import) + { + var use = MakeNamespace(import.Argument); + if (child.Children.FirstOrDefault(x => x is Prefix) is Prefix prefix) + { + usings[prefix.Argument] = use; + } + else + { + usings[use] = use; + } + } + + if (child is Prefix modulePrefix) + { + usings[modulePrefix.Argument] = string.Empty; + } + } + + WalkTree(context, ns, statement, functions); + context.AddSource(ns, $$""" + using System; + namespace {{ns}}; + public static class RemoteProcedureCalls + { + public const string Namespace = "{{module.XmlNamespace.Argument}}"; + {{string.Join("\n", functions)}} + } + """); + } + } + + private void WalkTree(SourceProductionContext context, string Namespace, IStatement statement, + List functions) + { + foreach (var child in statement.Children) + { + if (child is IClassSource classSource) + { + MakeClass(context, Namespace, classSource, functions); + } + + if (child is IFunctionSource functionSource) + { + functions.Add(MakeFunction(functionSource)); + } + } + } + + private string MakeFunction(IFunctionSource functionSource) + { + return "dummy"; + } + + private void MakeClass(SourceProductionContext context, string Namespace, IClassSource classSource, + List functions) + { + var name = MakeClassName(classSource.Argument); + context.AddSource(name, $$""" + using System; + + namespace {{Namespace}}; + public class {{name}} + { + public string Dummy; + } + """); + WalkTree(context, Namespace, classSource, functions); + } + + private string Capitalize(string section) + { + var first = section[0]; + var rest = section.Substring(1, section.Length - 1); + return char.ToUpperInvariant(first) + rest; + } + + private string MakeNamespace(string argument) + { + var output = new StringBuilder(argument.Length); + foreach (var section in argument.Split('-')) + { + output.Append(Capitalize(section)); + if (output.Length < argument.Length) output.Append('.'); + } + + return output.ToString(); + } + + private string MakeClassName(string argument) + { + var output = new StringBuilder(argument.Length); + foreach (var section in argument.Split('-')) + { + output.Append(Capitalize(section)); + } + + return output.ToString(); + } +} \ No newline at end of file diff --git a/YangParser/Position.cs b/YangParser/Parser/Position.cs similarity index 86% rename from YangParser/Position.cs rename to YangParser/Parser/Position.cs index d8f16d5..47cd518 100644 --- a/YangParser/Position.cs +++ b/YangParser/Parser/Position.cs @@ -1,4 +1,4 @@ -namespace YangParser; +namespace YangParser.Parser; public readonly struct Position(int column, int line, int offset) { diff --git a/YangParser/TermSymbol.cs b/YangParser/Parser/TermSymbol.cs similarity index 85% rename from YangParser/TermSymbol.cs rename to YangParser/Parser/TermSymbol.cs index ba32769..8429997 100644 --- a/YangParser/TermSymbol.cs +++ b/YangParser/Parser/TermSymbol.cs @@ -1,4 +1,4 @@ -namespace YangParser; +namespace YangParser.Parser; public enum TermSymbol { diff --git a/YangParser/Token.cs b/YangParser/Parser/Token.cs similarity index 92% rename from YangParser/Token.cs rename to YangParser/Parser/Token.cs index 4530c8f..f30170d 100644 --- a/YangParser/Token.cs +++ b/YangParser/Parser/Token.cs @@ -1,4 +1,4 @@ -namespace YangParser; +namespace YangParser.Parser; public readonly struct Token(TermSymbol symbol, Position position, string lexeme, int length, object? value = null) { diff --git a/YangParser/TokenScanner.cs b/YangParser/Parser/TokenScanner.cs similarity index 99% rename from YangParser/TokenScanner.cs rename to YangParser/Parser/TokenScanner.cs index 4ef68c3..82fe0f1 100644 --- a/YangParser/TokenScanner.cs +++ b/YangParser/Parser/TokenScanner.cs @@ -1,7 +1,7 @@ using System; using System.Text; -namespace YangParser; +namespace YangParser.Parser; public class TokenScanner(string text) { diff --git a/YangParser/YangStatementScanner.cs b/YangParser/Parser/YangStatementScanner.cs similarity index 99% rename from YangParser/YangStatementScanner.cs rename to YangParser/Parser/YangStatementScanner.cs index 2fc5340..6f86169 100644 --- a/YangParser/YangStatementScanner.cs +++ b/YangParser/Parser/YangStatementScanner.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace YangParser; +namespace YangParser.Parser; public class Metadata(string source, Position position, int length) { diff --git a/YangParser/SemanticModel/Action.cs b/YangParser/SemanticModel/Action.cs index a14e633..c5ead61 100644 --- a/YangParser/SemanticModel/Action.cs +++ b/YangParser/SemanticModel/Action.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/AnyXml.cs b/YangParser/SemanticModel/AnyXml.cs index 3826374..0d584f5 100644 --- a/YangParser/SemanticModel/AnyXml.cs +++ b/YangParser/SemanticModel/AnyXml.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Argument.cs b/YangParser/SemanticModel/Argument.cs index 61d3023..5054cc9 100644 --- a/YangParser/SemanticModel/Argument.cs +++ b/YangParser/SemanticModel/Argument.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Augment.cs b/YangParser/SemanticModel/Augment.cs index f22ca2d..5dbb10e 100644 --- a/YangParser/SemanticModel/Augment.cs +++ b/YangParser/SemanticModel/Augment.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Base.cs b/YangParser/SemanticModel/Base.cs index 006d918..b743a95 100644 --- a/YangParser/SemanticModel/Base.cs +++ b/YangParser/SemanticModel/Base.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Bit.cs b/YangParser/SemanticModel/Bit.cs index 741bc5c..e930252 100644 --- a/YangParser/SemanticModel/Bit.cs +++ b/YangParser/SemanticModel/Bit.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Case.cs b/YangParser/SemanticModel/Case.cs index d2bb479..1a791c7 100644 --- a/YangParser/SemanticModel/Case.cs +++ b/YangParser/SemanticModel/Case.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; @@ -68,7 +69,7 @@ namespace YangParser.SemanticModel; /// | when | 7.19.5 | 0..1 | /// +--------------+---------+-------------+ /// -public class Case : Statement +public class Case : Statement, IClassSource { public Case(YangStatement statement) { diff --git a/YangParser/SemanticModel/Choice.cs b/YangParser/SemanticModel/Choice.cs index dddb84a..fe08426 100644 --- a/YangParser/SemanticModel/Choice.cs +++ b/YangParser/SemanticModel/Choice.cs @@ -1,9 +1,10 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; -public class Choice : Statement +public class Choice : Statement, IClassSource { public Choice(YangStatement statement) { diff --git a/YangParser/SemanticModel/Contact.cs b/YangParser/SemanticModel/Contact.cs index 8146332..2413708 100644 --- a/YangParser/SemanticModel/Contact.cs +++ b/YangParser/SemanticModel/Contact.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Container.cs b/YangParser/SemanticModel/Container.cs index c0963cf..105d91e 100644 --- a/YangParser/SemanticModel/Container.cs +++ b/YangParser/SemanticModel/Container.cs @@ -1,9 +1,10 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; -public class Container : Statement +public class Container : Statement, IClassSource { public Container(YangStatement statement) { diff --git a/YangParser/SemanticModel/DefaultValue.cs b/YangParser/SemanticModel/DefaultValue.cs index eaac4da..e82212a 100644 --- a/YangParser/SemanticModel/DefaultValue.cs +++ b/YangParser/SemanticModel/DefaultValue.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Description.cs b/YangParser/SemanticModel/Description.cs index 73649b7..f6e1d88 100644 --- a/YangParser/SemanticModel/Description.cs +++ b/YangParser/SemanticModel/Description.cs @@ -1,3 +1,5 @@ +using YangParser.Parser; + namespace YangParser.SemanticModel; public class Description : Statement diff --git a/YangParser/SemanticModel/Deviate.cs b/YangParser/SemanticModel/Deviate.cs index 7fdb33e..cd581b6 100644 --- a/YangParser/SemanticModel/Deviate.cs +++ b/YangParser/SemanticModel/Deviate.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Deviation.cs b/YangParser/SemanticModel/Deviation.cs index 8162d23..e04a705 100644 --- a/YangParser/SemanticModel/Deviation.cs +++ b/YangParser/SemanticModel/Deviation.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Enum.cs b/YangParser/SemanticModel/Enum.cs index 220a7e0..a1549f9 100644 --- a/YangParser/SemanticModel/Enum.cs +++ b/YangParser/SemanticModel/Enum.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/ErrorAppTag.cs b/YangParser/SemanticModel/ErrorAppTag.cs index 23a0973..48c46b1 100644 --- a/YangParser/SemanticModel/ErrorAppTag.cs +++ b/YangParser/SemanticModel/ErrorAppTag.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/ErrorMessage.cs b/YangParser/SemanticModel/ErrorMessage.cs index b314132..31d38f1 100644 --- a/YangParser/SemanticModel/ErrorMessage.cs +++ b/YangParser/SemanticModel/ErrorMessage.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Extension.cs b/YangParser/SemanticModel/Extension.cs index a9f981b..718f7a6 100644 --- a/YangParser/SemanticModel/Extension.cs +++ b/YangParser/SemanticModel/Extension.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Feature.cs b/YangParser/SemanticModel/Feature.cs index a2d540f..8ccb971 100644 --- a/YangParser/SemanticModel/Feature.cs +++ b/YangParser/SemanticModel/Feature.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/FeatureFlag.cs b/YangParser/SemanticModel/FeatureFlag.cs index 1954039..fdc2702 100644 --- a/YangParser/SemanticModel/FeatureFlag.cs +++ b/YangParser/SemanticModel/FeatureFlag.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Grouping.cs b/YangParser/SemanticModel/Grouping.cs index 318f29e..0a6136f 100644 --- a/YangParser/SemanticModel/Grouping.cs +++ b/YangParser/SemanticModel/Grouping.cs @@ -1,9 +1,10 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; -public class Grouping : Statement +public class Grouping : Statement, IClassSource { public Grouping(YangStatement statement) { diff --git a/YangParser/SemanticModel/IStatement.cs b/YangParser/SemanticModel/IStatement.cs index ab2d2e4..f11d647 100644 --- a/YangParser/SemanticModel/IStatement.cs +++ b/YangParser/SemanticModel/IStatement.cs @@ -6,4 +6,14 @@ public interface IStatement ChildRule[] PermittedChildren { get; } IStatement[] Children { get; } IStatement? Parent { get; set; } +} + +public interface IFunctionSource : IStatement; + +public interface IClassSource : IStatement; + +public interface IAttributeSource : IStatement +{ + string AttributeName { get; } + bool Active { get; } } \ No newline at end of file diff --git a/YangParser/SemanticModel/Identity.cs b/YangParser/SemanticModel/Identity.cs index b592715..791a382 100644 --- a/YangParser/SemanticModel/Identity.cs +++ b/YangParser/SemanticModel/Identity.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Import.cs b/YangParser/SemanticModel/Import.cs index 05e5c75..8cfc26d 100644 --- a/YangParser/SemanticModel/Import.cs +++ b/YangParser/SemanticModel/Import.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Include.cs b/YangParser/SemanticModel/Include.cs index 64b6175..5578d8e 100644 --- a/YangParser/SemanticModel/Include.cs +++ b/YangParser/SemanticModel/Include.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Input.cs b/YangParser/SemanticModel/Input.cs index 92150f1..96e195d 100644 --- a/YangParser/SemanticModel/Input.cs +++ b/YangParser/SemanticModel/Input.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Key.cs b/YangParser/SemanticModel/Key.cs index c929cfd..4a01e28 100644 --- a/YangParser/SemanticModel/Key.cs +++ b/YangParser/SemanticModel/Key.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Leaf.cs b/YangParser/SemanticModel/Leaf.cs index c01c6c4..5357667 100644 --- a/YangParser/SemanticModel/Leaf.cs +++ b/YangParser/SemanticModel/Leaf.cs @@ -1,9 +1,10 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; -public class Leaf : Statement +public class Leaf : Statement, IClassSource { public override ChildRule[] PermittedChildren { get; } = [ diff --git a/YangParser/SemanticModel/LeafList.cs b/YangParser/SemanticModel/LeafList.cs index bb31620..c1e16cb 100644 --- a/YangParser/SemanticModel/LeafList.cs +++ b/YangParser/SemanticModel/LeafList.cs @@ -1,9 +1,10 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; -public class LeafList : Statement +public class LeafList : Statement, IClassSource { public override ChildRule[] PermittedChildren { get; } = [ diff --git a/YangParser/SemanticModel/Length.cs b/YangParser/SemanticModel/Length.cs index 38cc4c5..9568735 100644 --- a/YangParser/SemanticModel/Length.cs +++ b/YangParser/SemanticModel/Length.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/List.cs b/YangParser/SemanticModel/List.cs index b9d30b6..2775f57 100644 --- a/YangParser/SemanticModel/List.cs +++ b/YangParser/SemanticModel/List.cs @@ -1,9 +1,10 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; -public class List : Statement +public class List : Statement, IClassSource { public override ChildRule[] PermittedChildren { get; } = [ diff --git a/YangParser/SemanticModel/Mandatory.cs b/YangParser/SemanticModel/Mandatory.cs index 22fa63a..674c02a 100644 --- a/YangParser/SemanticModel/Mandatory.cs +++ b/YangParser/SemanticModel/Mandatory.cs @@ -1,3 +1,5 @@ +using YangParser.Parser; + namespace YangParser.SemanticModel; public class Mandatory : Statement diff --git a/YangParser/SemanticModel/MaxElements.cs b/YangParser/SemanticModel/MaxElements.cs index d797e8c..18f09ac 100644 --- a/YangParser/SemanticModel/MaxElements.cs +++ b/YangParser/SemanticModel/MaxElements.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/MinElements.cs b/YangParser/SemanticModel/MinElements.cs index e667c5a..f952aeb 100644 --- a/YangParser/SemanticModel/MinElements.cs +++ b/YangParser/SemanticModel/MinElements.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Module.cs b/YangParser/SemanticModel/Module.cs index 42a8a74..9cc8b2b 100644 --- a/YangParser/SemanticModel/Module.cs +++ b/YangParser/SemanticModel/Module.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; @@ -12,8 +13,11 @@ public Module(YangStatement statement) Argument = statement.Argument!.ToString(); ValidateChildren(statement); Children = statement.Children.Select(StatementFactory.Create).ToArray(); + XmlNamespace = Children.First(child => child is Namespace); } + public IStatement XmlNamespace { get; set; } + public override ChildRule[] PermittedChildren { get; } = [ new ChildRule(AnyXml.Keyword, Cardinality.ZeroOrMore), diff --git a/YangParser/SemanticModel/Must.cs b/YangParser/SemanticModel/Must.cs index e5cc858..e3b538f 100644 --- a/YangParser/SemanticModel/Must.cs +++ b/YangParser/SemanticModel/Must.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Namespace.cs b/YangParser/SemanticModel/Namespace.cs index a8e23b1..de4ac46 100644 --- a/YangParser/SemanticModel/Namespace.cs +++ b/YangParser/SemanticModel/Namespace.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Notification.cs b/YangParser/SemanticModel/Notification.cs index 5a8c7e0..951bb0f 100644 --- a/YangParser/SemanticModel/Notification.cs +++ b/YangParser/SemanticModel/Notification.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/OrderedBy.cs b/YangParser/SemanticModel/OrderedBy.cs index 600a32b..9d71b68 100644 --- a/YangParser/SemanticModel/OrderedBy.cs +++ b/YangParser/SemanticModel/OrderedBy.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Organization.cs b/YangParser/SemanticModel/Organization.cs index 21d9f5a..8c44190 100644 --- a/YangParser/SemanticModel/Organization.cs +++ b/YangParser/SemanticModel/Organization.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Output.cs b/YangParser/SemanticModel/Output.cs index 8b2cc74..d5e27a4 100644 --- a/YangParser/SemanticModel/Output.cs +++ b/YangParser/SemanticModel/Output.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Path.cs b/YangParser/SemanticModel/Path.cs index 17e2bb1..c196f27 100644 --- a/YangParser/SemanticModel/Path.cs +++ b/YangParser/SemanticModel/Path.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Pattern.cs b/YangParser/SemanticModel/Pattern.cs index b5fdcde..f8ec474 100644 --- a/YangParser/SemanticModel/Pattern.cs +++ b/YangParser/SemanticModel/Pattern.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Position.cs b/YangParser/SemanticModel/Position.cs index aaa81a7..55cc356 100644 --- a/YangParser/SemanticModel/Position.cs +++ b/YangParser/SemanticModel/Position.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Prefix.cs b/YangParser/SemanticModel/Prefix.cs index dbb2b82..572cc9b 100644 --- a/YangParser/SemanticModel/Prefix.cs +++ b/YangParser/SemanticModel/Prefix.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Range.cs b/YangParser/SemanticModel/Range.cs index 208c1b5..8f4f16a 100644 --- a/YangParser/SemanticModel/Range.cs +++ b/YangParser/SemanticModel/Range.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Reference.cs b/YangParser/SemanticModel/Reference.cs index c74b139..19ade8d 100644 --- a/YangParser/SemanticModel/Reference.cs +++ b/YangParser/SemanticModel/Reference.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Refine.cs b/YangParser/SemanticModel/Refine.cs index 9f0b6fa..2d536bc 100644 --- a/YangParser/SemanticModel/Refine.cs +++ b/YangParser/SemanticModel/Refine.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/RequireInstance.cs b/YangParser/SemanticModel/RequireInstance.cs index 5dcd885..b9fbb6b 100644 --- a/YangParser/SemanticModel/RequireInstance.cs +++ b/YangParser/SemanticModel/RequireInstance.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Revision.cs b/YangParser/SemanticModel/Revision.cs index a9559de..f2ff9b2 100644 --- a/YangParser/SemanticModel/Revision.cs +++ b/YangParser/SemanticModel/Revision.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/RevisionDate.cs b/YangParser/SemanticModel/RevisionDate.cs index 86365d6..0f3f475 100644 --- a/YangParser/SemanticModel/RevisionDate.cs +++ b/YangParser/SemanticModel/RevisionDate.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Rpc.cs b/YangParser/SemanticModel/Rpc.cs index 9f852ee..a54d714 100644 --- a/YangParser/SemanticModel/Rpc.cs +++ b/YangParser/SemanticModel/Rpc.cs @@ -1,9 +1,10 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; -public class Rpc : Statement +public class Rpc : Statement, IFunctionSource { public Rpc(YangStatement statement) { diff --git a/YangParser/SemanticModel/StateData.cs b/YangParser/SemanticModel/StateData.cs index 31c14ea..95656df 100644 --- a/YangParser/SemanticModel/StateData.cs +++ b/YangParser/SemanticModel/StateData.cs @@ -1,3 +1,5 @@ +using YangParser.Parser; + namespace YangParser.SemanticModel; public class StateData : Statement diff --git a/YangParser/SemanticModel/Statement.cs b/YangParser/SemanticModel/Statement.cs index 2e01fe8..8441158 100644 --- a/YangParser/SemanticModel/Statement.cs +++ b/YangParser/SemanticModel/Statement.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/StatementFactory.cs b/YangParser/SemanticModel/StatementFactory.cs index ea8f20b..5070cc1 100644 --- a/YangParser/SemanticModel/StatementFactory.cs +++ b/YangParser/SemanticModel/StatementFactory.cs @@ -1,4 +1,5 @@ using System; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Status.cs b/YangParser/SemanticModel/Status.cs index 0acf886..dad44eb 100644 --- a/YangParser/SemanticModel/Status.cs +++ b/YangParser/SemanticModel/Status.cs @@ -1,9 +1,10 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; -public class Status : Statement +public class Status : Statement, IAttributeSource { public Status(YangStatement statement) { @@ -14,8 +15,10 @@ public Status(YangStatement statement) switch (Argument) { case "current": + break; case "deprecated": case "obsolete": + Active = true; break; default: throw new InvalidOperationException($"Invalid {Keyword} value '{Argument}'"); @@ -23,4 +26,6 @@ public Status(YangStatement statement) } public const string Keyword = "status"; -} + public string AttributeName => "Obsolete"; + public bool Active { get; } +} \ No newline at end of file diff --git a/YangParser/SemanticModel/Type.cs b/YangParser/SemanticModel/Type.cs index 2c139e1..224aa54 100644 --- a/YangParser/SemanticModel/Type.cs +++ b/YangParser/SemanticModel/Type.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/TypeDefinition.cs b/YangParser/SemanticModel/TypeDefinition.cs index a15bcd6..20cf843 100644 --- a/YangParser/SemanticModel/TypeDefinition.cs +++ b/YangParser/SemanticModel/TypeDefinition.cs @@ -1,9 +1,10 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; -public class TypeDefinition : Statement +public class TypeDefinition : Statement, IClassSource { public TypeDefinition(YangStatement statement) { diff --git a/YangParser/SemanticModel/Unique.cs b/YangParser/SemanticModel/Unique.cs index 2875157..d6710ca 100644 --- a/YangParser/SemanticModel/Unique.cs +++ b/YangParser/SemanticModel/Unique.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Units.cs b/YangParser/SemanticModel/Units.cs index db026c8..af56af8 100644 --- a/YangParser/SemanticModel/Units.cs +++ b/YangParser/SemanticModel/Units.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Uses.cs b/YangParser/SemanticModel/Uses.cs index eb221b1..30d39be 100644 --- a/YangParser/SemanticModel/Uses.cs +++ b/YangParser/SemanticModel/Uses.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/Value.cs b/YangParser/SemanticModel/Value.cs index 7fff414..b4c3ff2 100644 --- a/YangParser/SemanticModel/Value.cs +++ b/YangParser/SemanticModel/Value.cs @@ -1,5 +1,5 @@ using System; -using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/When.cs b/YangParser/SemanticModel/When.cs index f364b56..ade5638 100644 --- a/YangParser/SemanticModel/When.cs +++ b/YangParser/SemanticModel/When.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/YangVersion.cs b/YangParser/SemanticModel/YangVersion.cs index c87d395..5bfe4f1 100644 --- a/YangParser/SemanticModel/YangVersion.cs +++ b/YangParser/SemanticModel/YangVersion.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/SemanticModel/YinElement.cs b/YangParser/SemanticModel/YinElement.cs index c0d102d..75ac0ff 100644 --- a/YangParser/SemanticModel/YinElement.cs +++ b/YangParser/SemanticModel/YinElement.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using YangParser.Parser; namespace YangParser.SemanticModel; diff --git a/YangParser/YangParser.csproj b/YangParser/YangParser.csproj index 661cc23..426a667 100644 --- a/YangParser/YangParser.csproj +++ b/YangParser/YangParser.csproj @@ -4,6 +4,11 @@ netstandard2.0 latest enable + true + + + + diff --git a/benchmarks/Program.cs b/benchmarks/Program.cs index d17e719..c3ee347 100644 --- a/benchmarks/Program.cs +++ b/benchmarks/Program.cs @@ -1,6 +1,7 @@ using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; using YangParser; +using YangParser.Parser; using YangParser.SemanticModel; namespace Benchmarks; diff --git a/yang-compiler.sln b/yang-compiler.sln index 86d04d1..c4e5092 100644 --- a/yang-compiler.sln +++ b/yang-compiler.sln @@ -12,6 +12,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Benchmarking", "Benchmarkin EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "benchmarks", "benchmarks\benchmarks.csproj", "{9F3CA1F2-9BBE-4D01-88B3-6218EB59DC3C}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestData", "TestData", "{E9FC49A1-6D19-4B52-B1AE-9DF0AC6B0663}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YangSource", "TestData\YangSource\YangSource.csproj", "{714DB9A0-7B83-4CBA-807B-9413C8FB4139}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -30,10 +34,16 @@ Global {9F3CA1F2-9BBE-4D01-88B3-6218EB59DC3C}.Debug|Any CPU.Build.0 = Debug|Any CPU {9F3CA1F2-9BBE-4D01-88B3-6218EB59DC3C}.Release|Any CPU.ActiveCfg = Release|Any CPU {9F3CA1F2-9BBE-4D01-88B3-6218EB59DC3C}.Release|Any CPU.Build.0 = Release|Any CPU + {714DB9A0-7B83-4CBA-807B-9413C8FB4139}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {714DB9A0-7B83-4CBA-807B-9413C8FB4139}.Debug|Any CPU.Build.0 = Debug|Any CPU + {714DB9A0-7B83-4CBA-807B-9413C8FB4139}.Release|Any CPU.ActiveCfg = Release|Any CPU + {714DB9A0-7B83-4CBA-807B-9413C8FB4139}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {E35D4026-B610-4BDF-9DFE-497FDD49EACD} = {7DF11EFE-894D-4357-B7CE-A390651300BC} {AD7EC36C-091F-4C68-BBB0-2E9CD5CAA7C1} = {40A1D392-CFC1-403E-A593-5A0E012182AF} {9F3CA1F2-9BBE-4D01-88B3-6218EB59DC3C} = {53A82AB5-2141-453F-B2E5-2E93E2D3B671} + {E9FC49A1-6D19-4B52-B1AE-9DF0AC6B0663} = {7DF11EFE-894D-4357-B7CE-A390651300BC} + {714DB9A0-7B83-4CBA-807B-9413C8FB4139} = {E9FC49A1-6D19-4B52-B1AE-9DF0AC6B0663} EndGlobalSection EndGlobal