From 23f82894b7212f8a7080ad3db93d1d841f082c96 Mon Sep 17 00:00:00 2001 From: Jan Date: Tue, 29 Oct 2019 12:20:08 +0100 Subject: [PATCH] ZoneCodeGenerator: Make GroupOptional always add Tag due to consistency (Tags get added when matches succeeds) --- .../Parsing/CommandFile/Tests/TestWithEvaluation.cs | 4 ++-- .../Parsing/Matching/Matchers/MatcherGroupOptional.cs | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ZoneCodeGenerator/Parsing/CommandFile/Tests/TestWithEvaluation.cs b/src/ZoneCodeGenerator/Parsing/CommandFile/Tests/TestWithEvaluation.cs index df6bb1a0..09667594 100644 --- a/src/ZoneCodeGenerator/Parsing/CommandFile/Tests/TestWithEvaluation.cs +++ b/src/ZoneCodeGenerator/Parsing/CommandFile/Tests/TestWithEvaluation.cs @@ -50,7 +50,7 @@ namespace ZoneCodeGenerator.Parsing.CommandFile.Tests private static readonly TokenMatcher evaluation = new MatcherGroupAnd( new MatcherGroupOr( new MatcherGroupAnd( - new MatcherGroupOptional(new MatcherLiteral("!")).WithTag(TagEvaluationNot), + new MatcherGroupOptional(new MatcherLiteral("!").WithTag(TagEvaluationNot)), new MatcherLiteral("("), new MatcherWithTag(TagEvaluation), new MatcherLiteral(")") @@ -60,7 +60,7 @@ namespace ZoneCodeGenerator.Parsing.CommandFile.Tests new MatcherGroupOptional(new MatcherGroupAnd( new MatcherWithTag(TagOperationType), new MatcherWithTag(TagEvaluation) - )).WithTag(TagEvaluationOperation) + ).WithTag(TagEvaluationOperation)) ).WithTag(TagEvaluation); diff --git a/src/ZoneCodeGenerator/Parsing/Matching/Matchers/MatcherGroupOptional.cs b/src/ZoneCodeGenerator/Parsing/Matching/Matchers/MatcherGroupOptional.cs index 7792cd4d..7e88f5d8 100644 --- a/src/ZoneCodeGenerator/Parsing/Matching/Matchers/MatcherGroupOptional.cs +++ b/src/ZoneCodeGenerator/Parsing/Matching/Matchers/MatcherGroupOptional.cs @@ -12,9 +12,14 @@ protected override TokenMatchingResult PerformTest(MatchingContext context, int tokenOffset) { var result = matcher.Test(context, tokenOffset); - result.PrependTag(Tag); - return !result.Successful ? new TokenMatchingResult(true, 0) : result; + if (!result.Successful) + { + result = new TokenMatchingResult(true, 0); + } + + result.PrependTag(Tag); + return result; } protected override string GetIdentifier()