Rename to OrderParser

This commit is contained in:
Tim Van Baak 2024-08-27 03:23:28 +00:00
parent 4f276df6c1
commit 416f2aa919
5 changed files with 24 additions and 16 deletions

View File

@ -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.
/// </summary>
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);

View File

@ -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}\"");

View File

@ -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;
}

View File

@ -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;

View File

@ -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<MoveOrder>());