From 416f2aa919d5b409adb5a0439f8d7a575025044f Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Tue, 27 Aug 2024 03:23:28 +0000 Subject: [PATCH] Rename to OrderParser --- .../Model/{Regex.cs => OrderParser.cs} | 13 ++++++++++-- .../Script/AdjudicationQueryScriptHandler.cs | 3 +-- .../Script/GameScriptHandler.cs | 2 +- .../Script/SetupScriptHandler.cs | 2 +- .../{RegexTest.cs => OrderParserTest.cs} | 20 +++++++++---------- 5 files changed, 24 insertions(+), 16 deletions(-) rename MultiversalDiplomacy/Model/{Regex.cs => OrderParser.cs} (97%) rename MultiversalDiplomacyTests/{RegexTest.cs => OrderParserTest.cs} (94%) diff --git a/MultiversalDiplomacy/Model/Regex.cs b/MultiversalDiplomacy/Model/OrderParser.cs similarity index 97% rename from MultiversalDiplomacy/Model/Regex.cs rename to MultiversalDiplomacy/Model/OrderParser.cs index 938bfce..90e6472 100644 --- a/MultiversalDiplomacy/Model/Regex.cs +++ b/MultiversalDiplomacy/Model/OrderParser.cs @@ -10,7 +10,7 @@ namespace MultiversalDiplomacy.Model; /// and other script inputs. It also provides helper functions to extract the captured order elements as tuples, /// which function as the structured intermediate representation between raw user input and full Order objects. /// -public class OrderRegex(World world) +public class OrderParser(World world) { public const string Type = "(A|F|Army|Fleet)"; @@ -34,6 +34,15 @@ public class OrderRegex(World world) public const string ViaConvoy = "(convoy|via convoy|by convoy)"; + public Regex PowerCommand = new($"^{world.Map.PowerRegex}(?:[:])? (.*)$"); + + public static ( + string power, + string command) + ParsePowerCommand(Match match) => ( + match.Groups[1].Value, + match.Groups[2].Value); + public Regex Hold => new( $"^{UnitSpec} {HoldVerb}$", RegexOptions.IgnoreCase); @@ -197,7 +206,7 @@ public class OrderRegex(World world) public static bool TryParseOrder(World world, string power, string command, [NotNullWhen(true)] out Order? order) { order = null; - OrderRegex re = new(world); + OrderParser re = new(world); if (re.Hold.Match(command) is Match holdMatch && holdMatch.Success) { return TryParseHoldOrder(world, power, holdMatch, out order); diff --git a/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs b/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs index 70e2fa2..dd1f253 100644 --- a/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs +++ b/MultiversalDiplomacy/Script/AdjudicationQueryScriptHandler.cs @@ -36,8 +36,7 @@ public class AdjudicationQueryScriptHandler(World world, bool strict = false) : case "assert": string assertion = input["assert ".Length..]; - OrderRegex re = new(World); - Regex prov = new($"{re.Province} (.*)"); + Regex prov = new($"{World.Map.ProvinceRegex} (.*)"); Match match = prov.Match(assertion); if (!match.Success) { Console.WriteLine($"Could not parse province from \"{assertion}\""); diff --git a/MultiversalDiplomacy/Script/GameScriptHandler.cs b/MultiversalDiplomacy/Script/GameScriptHandler.cs index 1fda3de..5d2ad32 100644 --- a/MultiversalDiplomacy/Script/GameScriptHandler.cs +++ b/MultiversalDiplomacy/Script/GameScriptHandler.cs @@ -69,7 +69,7 @@ public class GameScriptHandler(World world, bool strict = false) : IScriptHandle orderText = match.Groups[2].Value; } - if (OrderRegex.TryParseOrder(World, orderPower, orderText, out Order? order)) { + if (OrderParser.TryParseOrder(World, orderPower, orderText, out Order? order)) { Console.WriteLine($"Parsed {orderPower} order \"{orderText}\" but doing anything with it isn't implemented yet"); return this; } diff --git a/MultiversalDiplomacy/Script/SetupScriptHandler.cs b/MultiversalDiplomacy/Script/SetupScriptHandler.cs index 7c74523..80e8bc5 100644 --- a/MultiversalDiplomacy/Script/SetupScriptHandler.cs +++ b/MultiversalDiplomacy/Script/SetupScriptHandler.cs @@ -74,7 +74,7 @@ public class SetupScriptHandler(World world, bool strict = false) : IScriptHandl case "unit": string unitSpec = input["unit ".Length..]; - if (OrderRegex.TryParseUnit(World, unitSpec, out Unit? newUnit)) { + if (OrderParser.TryParseUnit(World, unitSpec, out Unit? newUnit)) { World = World.Update(units: World.Units.Append(newUnit)); Console.WriteLine($"Created {newUnit}"); return this; diff --git a/MultiversalDiplomacyTests/RegexTest.cs b/MultiversalDiplomacyTests/OrderParserTest.cs similarity index 94% rename from MultiversalDiplomacyTests/RegexTest.cs rename to MultiversalDiplomacyTests/OrderParserTest.cs index d6aeb71..cb97e33 100644 --- a/MultiversalDiplomacyTests/RegexTest.cs +++ b/MultiversalDiplomacyTests/OrderParserTest.cs @@ -41,10 +41,10 @@ public class RegexTest [TestCaseSource(nameof(HoldRegexMatchesTestCases))] public void HoldRegexMatches(string order, string[] expected) { - OrderRegex re = new(World.WithStandardMap()); + OrderParser re = new(World.WithStandardMap()); var match = re.Hold.Match(order); Assert.True(match.Success, "Match failed"); - var (type, timeline, province, location, turn, holdVerb) = OrderRegex.ParseHold(match); + var (type, timeline, province, location, turn, holdVerb) = OrderParser.ParseHold(match); string[] actual = [type, timeline, province, location, turn, holdVerb]; // Use EquivalentTo for more detailed error message Assert.That(actual, Is.EquivalentTo(expected), "Unexpected parse results"); @@ -94,11 +94,11 @@ public class RegexTest [TestCaseSource(nameof(MoveRegexMatchesTestCases))] public void MoveRegexMatches(string order, string[] expected) { - OrderRegex re = new(World.WithStandardMap()); + OrderParser re = new(World.WithStandardMap()); var match = re.Move.Match(order); Assert.True(match.Success, "Match failed"); var (type, timeline, province, location, turn, moveVerb, - destTimeline, destProvince, destLocation, destTurn, viaConvoy) = OrderRegex.ParseMove(match); + destTimeline, destProvince, destLocation, destTurn, viaConvoy) = OrderParser.ParseMove(match); string[] actual = [type, timeline, province, location, turn, moveVerb, destTimeline, destProvince, destLocation, destTurn, viaConvoy]; // Use EquivalentTo for more detailed error message @@ -145,11 +145,11 @@ public class RegexTest [TestCaseSource(nameof(SupportHoldRegexMatchesTestCases))] public void SupportHoldRegexMatches(string order, string[] expected) { - OrderRegex re = new(World.WithStandardMap()); + OrderParser re = new(World.WithStandardMap()); var match = re.SupportHold.Match(order); Assert.True(match.Success, "Match failed"); var (type, timeline, province, location, turn, supportVerb, - targetType, targetTimeline, targetProvince, targetLocation, targetTurn) = OrderRegex.ParseSupportHold(match); + targetType, targetTimeline, targetProvince, targetLocation, targetTurn) = OrderParser.ParseSupportHold(match); string[] actual = [type, timeline, province, location, turn, supportVerb, targetType, targetTimeline, targetProvince, targetLocation, targetTurn]; // Use EquivalentTo for more detailed error message @@ -184,12 +184,12 @@ public class RegexTest [TestCaseSource(nameof(SupportMoveRegexMatchesTestCases))] public void SupportMoveRegexMatches(string order, string[] expected) { - OrderRegex re = new(World.WithStandardMap()); + OrderParser re = new(World.WithStandardMap()); var match = re.SupportMove.Match(order); Assert.True(match.Success, "Match failed"); var (type, timeline, province, location, turn, supportVerb, targetType, targetTimeline, targetProvince, targetLocation, targetTurn, moveVerb, - destTimeline, destProvince, destLocation, destTurn) = OrderRegex.ParseSupportMove(match); + destTimeline, destProvince, destLocation, destTurn) = OrderParser.ParseSupportMove(match); string[] actual = [type, timeline, province, location, turn, supportVerb, targetType, targetTimeline, targetProvince, targetLocation, targetTurn, moveVerb, destTimeline, destProvince, destLocation, destTurn]; @@ -202,10 +202,10 @@ public class RegexTest public void OrderParsingTest() { World world = World.WithStandardMap().AddUnits("Germany A Mun"); - OrderRegex re = new(world); + OrderParser re = new(world); var match = re.Move.Match("A Mun - Tyr"); - var success = OrderRegex.TryParseMoveOrder(world, "Germany", match, out Order? order); + var success = OrderParser.TryParseMoveOrder(world, "Germany", match, out Order? order); Assert.That(success, Is.True); Assert.That(order, Is.TypeOf());