-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 25ac636
Showing
19 changed files
with
451 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.vs | ||
.idea | ||
**/obj | ||
**/bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Kvm.Analyser.Tokens | ||
{ | ||
public enum TokenType | ||
{ | ||
Control, | ||
Block, | ||
Unknown | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.