Skip to content

Commit

Permalink
fundamentals
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinZonda committed Apr 25, 2021
0 parents commit 25ac636
Show file tree
Hide file tree
Showing 19 changed files with 451 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vs
.idea
**/obj
**/bin
11 changes: 11 additions & 0 deletions Kvm.Analyser/Kvm.Analyser.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Kvm.Log\Kvm.Log.csproj" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions Kvm.Analyser/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Open KevinZonda Programme License

Copyright (c) 2021 KevinZonda

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software non-commercially without restriction, including without
limitation the rights to use, copy, merge, and/or distribute the Software; with
restriction the rights to modify, publish, and/or sell copies of the Software.
For modifying, the modified part of the Software should be licensed under the
same license. For publishing, unless the publisher is the author, the other
publish builds should be mentioned that it is an unofficial build. For dealing
in the Software commercially, including sell copies of the Software, and/or use
the Software, a new sublicense from the author is required. And to permit persons
to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
85 changes: 85 additions & 0 deletions Kvm.Analyser/Lexer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Collections.Generic;
using System.Text;
using Kvm.Analyser.Tokens;

namespace Kvm.Analyser
{
public class Lexer
{
private static char[] open = { '{', '%' };
private static char[] close = { '%', '}' };


public static List<IToken> Parse(string kvm)
{
List<IToken> list = new List<IToken>();
StringBuilder sb = new StringBuilder();


bool isOpen = false;

char lastLastChar = '\0';
char lastChar = '\0';
var m = kvm.ToCharArray();
foreach (var c in m)
{
// Open
if (lastChar == open[0] && c == open[1])
{
if (lastLastChar == '\\')
{
/* FIXMED: If we only verify 2 digit, how do we check user wants to
* input {% or %}?
* We should verify 3 digits, like if it is {{% then print {% and etc
*/
sb.Remove(sb.Length - 1, 2).Append("{%");
continue;
}


isOpen = true;

list.Add(new BlockToken()
{
Data = sb.Remove(sb.Length - 1, 1).ToString()
});
sb.Clear();
//}
}
// Close
else if (lastChar == close[0] && c == close[1])
{
if (isOpen)
{
isOpen = false;
list.Add(new ControlToken()
{
Data = sb.Remove(sb.Length - 1, 1).ToString()
});
sb.Clear();
}
else
{
sb.Append(c);
}
}
else
{
sb.Append(c);
}

lastLastChar = lastChar;
lastChar = c;
}
var s = sb.ToString();
if (!string.IsNullOrWhiteSpace(s))
{
list.Add(new BlockToken()
{
Data = s
});
}
return list;
}
}
}
70 changes: 70 additions & 0 deletions Kvm.Analyser/Parser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Collections.Generic;
using System.Text;
using Kvm.Analyser.Tokens;

namespace Kvm.Analyser
{
public class Parser
{
public static (string, string) SplitByFirstLine(string str)
{
int index = str.IndexOf('\n');
if (index < 0)
return (str, null);
return (str[..index], str[(index + 1) ..]);
}

public static Dictionary<string, string> ParseKey(string str)
{
var s = str.Split('\n');
Dictionary<string, string> dic = new();
foreach (var ss in s)
{
var m = ss.Trim();
if (string.IsNullOrEmpty(m))
continue;
if (m.StartsWith("#"))
continue;
var index = m.IndexOf('=');
if (index < 0)
{
Shared.Log.W($"Cannot parse info: {ss}");
continue;
}

dic.Add(m[..index], m[(index + 1)..]);
}

return dic;
}

public string Parse(string kvm, string prop)
=> Parse(kvm, ParseKey(prop));


public string Parse(string kvm, Dictionary<string, string> prop)
{
StringBuilder sb = new();
var m = Lexer.Parse(kvm);
foreach (var i in m)
{
switch (i.Type)
{
case TokenType.Unknown:
case TokenType.Block:
sb.Append(i.Data);
break;
case TokenType.Control:
lock (prop)
{
sb.Append(((ControlToken) i).Parse(prop));
}

break;
}
}

return sb.ToString();
}
}
}
9 changes: 9 additions & 0 deletions Kvm.Analyser/Shared.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Kvm.Log;

namespace Kvm.Analyser
{
public class Shared
{
public static IKLog Log = new Default();
}
}
8 changes: 8 additions & 0 deletions Kvm.Analyser/Tokens/BlockToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Kvm.Analyser.Tokens
{
public class BlockToken : IToken
{
public string Data { get; set; }
public TokenType Type => TokenType.Block;
}
}
31 changes: 31 additions & 0 deletions Kvm.Analyser/Tokens/ControlToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;

namespace Kvm.Analyser.Tokens
{
public class ControlToken : IToken
{
private string _data;

public string Data
{
get => _data ?? "";
set => _data = value.Trim();
}

public TokenType Type => TokenType.Control;

private void Parse()
{
throw new NotSupportedException();
}

public string Parse(Dictionary<string, string> prop)
{
if (prop.ContainsKey(Data))
return prop[Data];
Shared.Log.W("Cannot found prop: " + Data);
return string.Empty;
}
}
}
9 changes: 9 additions & 0 deletions Kvm.Analyser/Tokens/IToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Kvm.Analyser.Tokens
{
public interface IToken
{
public string Data { get; set; }

public TokenType Type => TokenType.Unknown;
}
}
9 changes: 9 additions & 0 deletions Kvm.Analyser/Tokens/TokenType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Kvm.Analyser.Tokens
{
public enum TokenType
{
Control,
Block,
Unknown
}
}
12 changes: 12 additions & 0 deletions Kvm.ConsoleApp/Kvm.ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Kvm.Analyser\Kvm.Analyser.csproj" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions Kvm.ConsoleApp/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Open KevinZonda Programme License

Copyright (c) 2021 KevinZonda

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software non-commercially without restriction, including without
limitation the rights to use, copy, merge, and/or distribute the Software; with
restriction the rights to modify, publish, and/or sell copies of the Software.
For modifying, the modified part of the Software should be licensed under the
same license. For publishing, unless the publisher is the author, the other
publish builds should be mentioned that it is an unofficial build. For dealing
in the Software commercially, including sell copies of the Software, and/or use
the Software, a new sublicense from the author is required. And to permit persons
to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
28 changes: 28 additions & 0 deletions Kvm.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using Kvm.Analyser;

namespace Kvm.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string x = @"mmm
KKK=auhsbcj
A=CBA
D=";
var d = Parser.SplitByFirstLine(x);
Console.WriteLine("=====1=======");
Console.WriteLine(d.Item1);
Console.WriteLine("=====2=======");
Console.WriteLine(d.Item2);
Console.WriteLine("=====3=======");
var dix = Parser.ParseKey(x);
foreach (var kv in dix)
{
Console.WriteLine($"K->{kv.Key} && V->{kv.Value}");
}
}
}
}
27 changes: 27 additions & 0 deletions Kvm.Log/Default.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Kvm.Log
{
public class Default : IKLog
{
public void W(string msg)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"[W {DateTime.Now.ToLocalTime().ToString()}] {msg}");
Console.ResetColor();
}

public void I(string msg)
{
Console.Error.WriteLine($"[I {DateTime.Now.ToLocalTime().ToString()}] {msg}");
Console.ResetColor();
}

public void E(string msg)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Error.WriteLine($"[E {DateTime.Now.ToLocalTime().ToString()}] {msg}");
Console.ResetColor();
}
}
}
9 changes: 9 additions & 0 deletions Kvm.Log/IKLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Kvm.Log
{
public interface IKLog
{
public void W(string msg);
public void I(string msg);
public void E(string msg);
}
}
7 changes: 7 additions & 0 deletions Kvm.Log/Kvm.Log.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

</Project>
Loading

0 comments on commit 25ac636

Please sign in to comment.