From ac7bb9191d99fa7f1f5743d3de18f6c06bb71504 Mon Sep 17 00:00:00 2001 From: Booklordofthedings Date: Thu, 26 Sep 2024 10:38:00 +0200 Subject: [PATCH] Added rgb and rgba support --- pkkg | 13 ++++++++++ src/Bofa.bf | 6 ++++- src/BofaParser.bf | 22 +++++++++++++++++ src/BofaValue.bf | 2 ++ src/Parser/AdditionalParsers.bf | 43 +++++++++++++++++++++++++++++++++ src/Testing/Default.bf | 6 ++++- src/eBofaType.bf | 6 ++++- 7 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 pkkg create mode 100644 src/Parser/AdditionalParsers.bf diff --git a/pkkg b/pkkg new file mode 100644 index 0000000..44efee0 --- /dev/null +++ b/pkkg @@ -0,0 +1,13 @@ +#This is the pkkg template file +l Name Bofa + +t Desc A simple object notation + +l Url https://code.booklordofthe.dev/Booklordofthedings/Bofa + +a Authors + o Book + l Name Booklordofthedings + l Url https://Boooklordofthe.dev + +a Dependencies \ No newline at end of file diff --git a/src/Bofa.bf b/src/Bofa.bf index 1dc3fb3..58b2626 100644 --- a/src/Bofa.bf +++ b/src/Bofa.bf @@ -91,7 +91,11 @@ class Bofa } } - } + case .RGB: + strBuffer.Append(scope $"rgb {Name} {Value.Rgb[0]:X2)} {Value.Rgb[1]:X2)} {Value.Rgb[2]:X2)}"); + case .RGBA: + strBuffer.Append(scope $"rgb {Name} {Value.Rgba[0]:X2)} {Value.Rgba[1]:X2)} {Value.Rgba[2]:X2)} {Value.Rgba[3]:X2)}"); + } } ///Deletes this object without filling the stack diff --git a/src/BofaParser.bf b/src/BofaParser.bf index 64c3f7e..9bed856 100644 --- a/src/BofaParser.bf +++ b/src/BofaParser.bf @@ -178,6 +178,10 @@ class BofaParser typeName = "Array"; case "o": typeName = "Object"; + case "rgb": + typeName = "RBG"; + case "rgba": + typeName = "RBGA"; default: toReturn.Type = .Error; return toReturn; //Unsupported type error @@ -306,6 +310,24 @@ class BofaParser case "c": bofaRes.Value.Custom = line; bofaRes.Type = .Custom; + case "rgb": + bofaRes.Type = .RGB; + var result = AdditionalParsers.ParseRGB(line); + if(result case .Err) + { + toReturn.Type = .Error; + return toReturn; + } + bofaRes.Value.Rgb = result.Value; + case "rgba": + bofaRes.Type = .RGBA; + var result = AdditionalParsers.ParseRGBA(line); + if(result case .Err) + { + toReturn.Type = .Error; + return toReturn; + } + bofaRes.Value.Rgba = result.Value; default: //Unknown type toReturn.Type = .Error; return toReturn; diff --git a/src/BofaValue.bf b/src/BofaValue.bf index 2399e0e..b748d84 100644 --- a/src/BofaValue.bf +++ b/src/BofaValue.bf @@ -18,4 +18,6 @@ struct BofaValue public StringView Custom; public Dictionary Object; public List Array; + public uint8[3] Rgb; + public uint8[4] Rgba; } \ No newline at end of file diff --git a/src/Parser/AdditionalParsers.bf b/src/Parser/AdditionalParsers.bf new file mode 100644 index 0000000..a812c3a --- /dev/null +++ b/src/Parser/AdditionalParsers.bf @@ -0,0 +1,43 @@ +namespace Bofa.Parser; + +using System; + +class AdditionalParsers +{ + public static Result ParseRGB(StringView pValue) + { + uint8[3] toReturn = .(0,0,0); + var parts = pValue.Split(' '); + int idx = 0; + for(var part in parts) + { + defer {idx = idx + 1;} + + var res = uint8.Parse(part, .Hex); + if(res case .Err) + return .Err; + + toReturn[idx] = res.Value; + } + return .Ok(toReturn); + } + + public static Result ParseRGBA(StringView pValue) + { + uint8[4] toReturn = .(0,0,0,0); + var parts = pValue.Split(' '); + int idx = 0; + for(var part in parts) + { + defer {idx = idx + 1;} + + var res = uint8.Parse(part, .HexNumber); + if(res case .Err) + return .Err; + + toReturn[idx] = res.Value; + } + return .Ok(toReturn); + } + +} \ No newline at end of file diff --git a/src/Testing/Default.bf b/src/Testing/Default.bf index 9b708c7..818856e 100644 --- a/src/Testing/Default.bf +++ b/src/Testing/Default.bf @@ -28,12 +28,16 @@ class Default a array b content true b content true + + # 1.1.0 + rgba color FF FF FF FF + rgb c 34 67 EF """; Dictionary output = new .(); List errors = new .(); BofaParser.Parse(content, output, errors); - Test.Assert(output.Count == 9); + Test.Assert(output.Count == 11); Test.Assert(errors.Count == 0); DeleteDictionaryAndValues!(output); diff --git a/src/eBofaType.bf b/src/eBofaType.bf index d3dab82..595f66e 100644 --- a/src/eBofaType.bf +++ b/src/eBofaType.bf @@ -13,5 +13,9 @@ enum EBofaType Boolean, //8bit true or false Object, // Key-Value container Array, //Numeric container - Custom //String with a type attached to it + Custom, //String with a type attached to it + + //1.1.0 + RGB, //3 * 8bit + RGBA //4 * 8bit } \ No newline at end of file