diff --git a/configuration/ConfigurationContainer.cs b/configuration/ConfigurationContainer.cs deleted file mode 100644 index 35eedaa..0000000 --- a/configuration/ConfigurationContainer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace ln.application.configuration -{ - public class ConfigurationContainer - { - public ConfigurationContainer Parent { get; private set; } - - public String Key { get; set; } - - List children = new List(); - Dictionary statements = new Dictionary(); - - public ConfigurationContainer() - { - } - public ConfigurationContainer(ConfigurationContainer parent) - { - parent.Add(this); - } - - public void Add(ConfigurationContainer child) - { - if (child.Parent != null) - child.Parent.Remove(child); - - children.Add(child); - child.Parent = this; - } - public void Remove(ConfigurationContainer child) - { - if (child.Parent == this) - { - children.Remove(child); - child.Parent = null; - } - } - - } -} diff --git a/configuration/ConfigurationParser.cs b/configuration/ConfigurationParser.cs deleted file mode 100644 index 50a2ffb..0000000 --- a/configuration/ConfigurationParser.cs +++ /dev/null @@ -1,56 +0,0 @@ -using ln.collections; -using ln.parse.tokenizer; -using System; -using System.Linq; - -namespace ln.application.configuration -{ - public class ConfigurationParser - { - ConfigurationContainer RootContainer { get; } - - public ConfigurationParser() - { - RootContainer = new ConfigurationContainer(); - } - public ConfigurationParser(ConfigurationContainer rootContainer) - { - RootContainer = rootContainer; - } - - public void Parse(String source) - { - Parse(configurationTokenizer.Parse(source)); - } - public void Parse(Token[] tokens) - { - ArrayStream tokenStream = new ArrayStream(tokens.Where((e) => !(e is Token.WhiteSpaceToken))); - ParseContainer(tokenStream,RootContainer); - } - - private void ParseContainer(ArrayStream tokenStream,ConfigurationContainer container) - { - while (tokenStream.Current is Token.BracketToken bracket && bracket.Value.Equals("}")) - { - - } - } - - - - - - static Tokenizer configurationTokenizer; - - static ConfigurationParser() { - configurationTokenizer = new Tokenizer() - .Add(TokenMatcher.WHITESPACE) - .Add(TokenMatcher.FLOAT) - .Add(TokenMatcher.INTEGER) - .Add(TokenMatcher.STRING) - .Add(TokenMatcher.OPERATOR) - .Add(TokenMatcher.BRACKET); - - } - } -} diff --git a/ln.application.sln b/ln.application.sln new file mode 100644 index 0000000..bf807a2 --- /dev/null +++ b/ln.application.sln @@ -0,0 +1,48 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ln.application", "ln.application\ln.application.csproj", "{AA996706-EAD5-47FD-B453-DF12D19F60FB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ln.application.tests", "ln.application.tests\ln.application.tests.csproj", "{387CB82C-E717-4D11-AE99-1735023E2D51}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AA996706-EAD5-47FD-B453-DF12D19F60FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AA996706-EAD5-47FD-B453-DF12D19F60FB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AA996706-EAD5-47FD-B453-DF12D19F60FB}.Debug|x64.ActiveCfg = Debug|Any CPU + {AA996706-EAD5-47FD-B453-DF12D19F60FB}.Debug|x64.Build.0 = Debug|Any CPU + {AA996706-EAD5-47FD-B453-DF12D19F60FB}.Debug|x86.ActiveCfg = Debug|Any CPU + {AA996706-EAD5-47FD-B453-DF12D19F60FB}.Debug|x86.Build.0 = Debug|Any CPU + {AA996706-EAD5-47FD-B453-DF12D19F60FB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AA996706-EAD5-47FD-B453-DF12D19F60FB}.Release|Any CPU.Build.0 = Release|Any CPU + {AA996706-EAD5-47FD-B453-DF12D19F60FB}.Release|x64.ActiveCfg = Release|Any CPU + {AA996706-EAD5-47FD-B453-DF12D19F60FB}.Release|x64.Build.0 = Release|Any CPU + {AA996706-EAD5-47FD-B453-DF12D19F60FB}.Release|x86.ActiveCfg = Release|Any CPU + {AA996706-EAD5-47FD-B453-DF12D19F60FB}.Release|x86.Build.0 = Release|Any CPU + {387CB82C-E717-4D11-AE99-1735023E2D51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {387CB82C-E717-4D11-AE99-1735023E2D51}.Debug|Any CPU.Build.0 = Debug|Any CPU + {387CB82C-E717-4D11-AE99-1735023E2D51}.Debug|x64.ActiveCfg = Debug|Any CPU + {387CB82C-E717-4D11-AE99-1735023E2D51}.Debug|x64.Build.0 = Debug|Any CPU + {387CB82C-E717-4D11-AE99-1735023E2D51}.Debug|x86.ActiveCfg = Debug|Any CPU + {387CB82C-E717-4D11-AE99-1735023E2D51}.Debug|x86.Build.0 = Debug|Any CPU + {387CB82C-E717-4D11-AE99-1735023E2D51}.Release|Any CPU.ActiveCfg = Release|Any CPU + {387CB82C-E717-4D11-AE99-1735023E2D51}.Release|Any CPU.Build.0 = Release|Any CPU + {387CB82C-E717-4D11-AE99-1735023E2D51}.Release|x64.ActiveCfg = Release|Any CPU + {387CB82C-E717-4D11-AE99-1735023E2D51}.Release|x64.Build.0 = Release|Any CPU + {387CB82C-E717-4D11-AE99-1735023E2D51}.Release|x86.ActiveCfg = Release|Any CPU + {387CB82C-E717-4D11-AE99-1735023E2D51}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/ln.application.tests/ConfigurationTest.conf b/ln.application.tests/ConfigurationTest.conf index 23cdf15..ff450b6 100644 --- a/ln.application.tests/ConfigurationTest.conf +++ b/ln.application.tests/ConfigurationTest.conf @@ -2,7 +2,8 @@ aNumber: 123.987; anInteger: 45467; aString: "This is a test string"; +aString: "Another String"; -objectA { +objectA "Container 1" { something: "stupid"; } diff --git a/ln.application.tests/ConfigurationTests.cs b/ln.application.tests/ConfigurationTests.cs new file mode 100644 index 0000000..19d63cc --- /dev/null +++ b/ln.application.tests/ConfigurationTests.cs @@ -0,0 +1,44 @@ +using ln.application.configuration; +using NUnit.Framework; +using System; +using System.IO; +using System.Linq; + +namespace ln.application.tests +{ + public class Tests + { + StreamWriter output = new StreamWriter(Console.OpenStandardOutput()); + + [SetUp] + public void Setup() + { + using (StreamReader sr = new StreamReader("ConfigurationTest.conf")) + { + testConfiguration = sr.ReadToEnd(); + } + } + + string testConfiguration; + + [Test] + public void Test_0_Configuration() + { + ConfigurationParser configurationParser = new ConfigurationParser(); + configurationParser.Parse(testConfiguration); + + output.WriteLine("test_0_Configuration(): rootContainer: {0}", configurationParser.RootContainer); + output.Flush(); + + Assert.AreEqual(1, configurationParser.RootContainer.Children("objectA").Count()); + + Assert.Pass(); + } + + + + + + + } +} \ No newline at end of file diff --git a/ln.application.tests/TestConfiguration.cs b/ln.application.tests/TestConfiguration.cs new file mode 100644 index 0000000..a200bb2 --- /dev/null +++ b/ln.application.tests/TestConfiguration.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using ln.application.attributes; + +namespace ln.application.tests +{ + public class TestConcfiguration + { + + public TestConcfiguration() + { + } + + List numbers = new List(); + + + [ConfigurationStatement(Name = "aNumber")] + public void AddNumber(int number) + { + numbers.Add(new decimal(number)); + } + [ConfigurationStatement(Name = "aNumber")] + public void AddNumber(float number) + { + numbers.Add(new decimal(number)); + } + + + + } +} \ No newline at end of file diff --git a/ln.application.tests/UnitTest1.cs b/ln.application.tests/UnitTest1.cs deleted file mode 100644 index d23c960..0000000 --- a/ln.application.tests/UnitTest1.cs +++ /dev/null @@ -1,34 +0,0 @@ -using NUnit.Framework; -using System.IO; - -namespace ln.application.tests -{ - public class Tests - { - [SetUp] - public void Setup() - { - using (StreamReader sr = new StreamReader("ConfigurationTest.conf")) - { - testConfiguration = sr.ReadToEnd(); - } - } - - string testConfiguration; - - [Test] - public void TestTokenizer() - { - - - - Assert.Pass(); - } - - - - - - - } -} \ No newline at end of file diff --git a/ln.application.tests/ln.application.tests.csproj b/ln.application.tests/ln.application.tests.csproj index e532582..3867601 100644 --- a/ln.application.tests/ln.application.tests.csproj +++ b/ln.application.tests/ln.application.tests.csproj @@ -6,6 +6,14 @@ false + + + + + + + + diff --git a/Argument.cs b/ln.application/Argument.cs similarity index 100% rename from Argument.cs rename to ln.application/Argument.cs diff --git a/ArgumentContainer.cs b/ln.application/ArgumentContainer.cs similarity index 100% rename from ArgumentContainer.cs rename to ln.application/ArgumentContainer.cs diff --git a/FieldArgument.cs b/ln.application/FieldArgument.cs similarity index 100% rename from FieldArgument.cs rename to ln.application/FieldArgument.cs diff --git a/IArgument.cs b/ln.application/IArgument.cs similarity index 100% rename from IArgument.cs rename to ln.application/IArgument.cs diff --git a/PropertyArgument.cs b/ln.application/PropertyArgument.cs similarity index 100% rename from PropertyArgument.cs rename to ln.application/PropertyArgument.cs diff --git a/StaticArgumentAttribute.cs b/ln.application/StaticArgumentAttribute.cs similarity index 100% rename from StaticArgumentAttribute.cs rename to ln.application/StaticArgumentAttribute.cs diff --git a/ln.application/attributes/ConfigurationStatement.cs b/ln.application/attributes/ConfigurationStatement.cs new file mode 100644 index 0000000..e19d641 --- /dev/null +++ b/ln.application/attributes/ConfigurationStatement.cs @@ -0,0 +1,18 @@ + + +using System; + +namespace ln.application.attributes +{ + + public class ConfigurationStatementAttribute: Attribute + { + + public String Name { get; set;} + + public ConfigurationStatementAttribute() + { + } + + } +} \ No newline at end of file diff --git a/ln.application/configuration/ConfigurationContainer.cs b/ln.application/configuration/ConfigurationContainer.cs new file mode 100644 index 0000000..4878d5c --- /dev/null +++ b/ln.application/configuration/ConfigurationContainer.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace ln.application.configuration +{ + public class ConfigurationContainer + { + public ConfigurationContainer Parent { get; private set; } + + public object Key { get; set; } + public string Name { get; set; } + + List statements = new List(); + Dictionary> children = new Dictionary>(); + + public ConfigurationContainer() + { + } + public ConfigurationContainer(ConfigurationContainer parent,string containerName,object containerKey) + { + Name = containerName; + Key = containerKey; + parent.Add(this); + } + + private void attach(ConfigurationContainer child) + { + Dictionary containerList = GetContainerList(child.Name); + containerList.Add(child.Key, child); + } + + private void detach(ConfigurationContainer child) + { + Dictionary containerList = GetContainerList(child.Name); + containerList.Remove(child.Key); + } + + + public IEnumerable Children() => children.SelectMany((l)=>l.Value.Values); + public IEnumerable Children(string containerName){ + if (children.TryGetValue(containerName,out Dictionary containerList)) + return containerList.Values; + return new ConfigurationContainer[0]; + } + + Dictionary GetContainerList(string containerName) + { + if (!children.TryGetValue(containerName,out Dictionary containerList)) + { + containerList = new Dictionary(); + children.Add(containerName, containerList); + } + return containerList; + } + + + public ConfigurationContainer GetOrCreate(string containerName) => GetOrCreate(containerName, null); + public ConfigurationContainer GetOrCreate(string containerName,object containerKey) + { + Dictionary containerList = GetContainerList(containerName); + if (!containerList.TryGetValue(containerKey,out ConfigurationContainer childContainer)) + { + childContainer = new ConfigurationContainer(this, containerName, containerKey); + } + return childContainer; + } + + public void Add(ConfigurationContainer child) + { + if (child.Parent != null) + child.Parent.detach(child); + + attach(child); + + child.Parent = this; + } + public void Remove(ConfigurationContainer child) + { + if (child.Parent == this) + { + detach(child); + child.Parent = null; + } + } + + public void AddStatement(string statementName,object[] arguments) => AddStatement(new ConfigurationStatement(statementName, arguments)); + public void AddStatement(ConfigurationStatement configurationStatement) => statements.Add(configurationStatement); + public IEnumerable GetStatements() => statements; + public IEnumerable GetStatements(string name) => statements.Where((st)=> st.Name.Equals(name)); + + void ToString(StringBuilder stringBuilder) + { + stringBuilder.AppendFormat("{0} {1} {{\n", Name, Key); + foreach (ConfigurationStatement statement in statements) + { + stringBuilder.AppendLine(statement.ToString()); + } + + foreach (Dictionary containerList in children.Values) + { + foreach (ConfigurationContainer child in containerList.Values) + child.ToString(stringBuilder); + } + stringBuilder.AppendFormat("}}\n"); + } + + public override string ToString() + { + StringBuilder stringBuilder = new StringBuilder(); + + ToString(stringBuilder); + + return stringBuilder.ToString(); + } + + } +} diff --git a/ln.application/configuration/ConfigurationParser.cs b/ln.application/configuration/ConfigurationParser.cs new file mode 100644 index 0000000..1bc67be --- /dev/null +++ b/ln.application/configuration/ConfigurationParser.cs @@ -0,0 +1,89 @@ +using ln.collections; +using ln.parse.tokenizer; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace ln.application.configuration +{ + public class ConfigurationParser + { + public ConfigurationContainer RootContainer { get; } + + public ConfigurationParser() + { + RootContainer = new ConfigurationContainer(); + } + public ConfigurationParser(ConfigurationContainer rootContainer) + { + RootContainer = rootContainer; + } + + public void Parse(String source) + { + Parse(configurationTokenizer.Parse(source, (token) => !(token is Token.WhiteSpaceToken))); + } + public void Parse(Token[] tokens) + { + Queue queue = new Queue(tokens); + ParseContainer(queue, RootContainer); + } + + private void ParseContainer(Queue queue,ConfigurationContainer container) + { + Token identifier; + while (queue.Count > 0) + { + identifier = queue.Dequeue(); + if (identifier is Token.BracketToken && "}".Equals(identifier.Value)) + return; + + if (identifier is Token.IdentifierToken) + { + Token t2 = queue.Dequeue(); + if (t2 is Token.OperatorToken && ":".Equals(t2.Value)) + { + List arguments = new List(); + Token arg; + while (!(((arg = queue.Dequeue()) is Token.OperatorToken) && ";".Equals(arg.Value))) + { + arguments.Add(arg.NativeValue); + } + container.AddStatement(identifier.Value,arguments.ToArray()); + } else { + Token key = null; + if (!t2.Is("{")) + { + key = t2; + t2 = queue.Dequeue(); + } + if (!t2.Is("{")) + { + throw new FormatException(String.Format("expected '{{' at {0}", t2.TextPosition)); + } + + ConfigurationContainer childContainer = new ConfigurationContainer(container,identifier.Value,key.NativeValue); + ParseContainer(queue, childContainer); + // if (queue.Count == 0) + // throw new FormatException(String.Format("Missing '}}'")); + } + } else { + throw new FormatException(String.Format("unexepected token: {0}", identifier.Value)); + } + + } + } + + + + + + static Tokenizer configurationTokenizer; + + static ConfigurationParser() { + configurationTokenizer = Tokenizer.CreateDefaultTokenizer(); + + } + } +} diff --git a/ln.application/configuration/ConfigurationStatement.cs b/ln.application/configuration/ConfigurationStatement.cs new file mode 100644 index 0000000..18771f0 --- /dev/null +++ b/ln.application/configuration/ConfigurationStatement.cs @@ -0,0 +1,23 @@ +using System; + +namespace ln.application.configuration +{ + + public class ConfigurationStatement + { + public string Name { get; } + public Object[] Arguments { get; } + + public ConfigurationStatement(string name,object[] arguments) + { + Name = name; + Arguments = arguments; + } + + public override string ToString() + { + return String.Format("{0}: {1}",Name, String.Join(' ', Arguments)); + } + } + +} diff --git a/ln.application.csproj b/ln.application/ln.application.csproj similarity index 57% rename from ln.application.csproj rename to ln.application/ln.application.csproj index fba25d4..2c5d6e7 100644 --- a/ln.application.csproj +++ b/ln.application/ln.application.csproj @@ -3,18 +3,19 @@ netcoreapp3.1 true - 0.1.0 + 0.1.1 Harald Wolff-Thobaben - 0.0.1.0 - 0.0.1.0 + 0.1.1.0 + 0.1.1.0 - - + + +