From 9b33682d92d43af0c612483ee33553ba81e30f7b Mon Sep 17 00:00:00 2001 From: GMIKE Date: Thu, 27 Oct 2022 01:23:41 +0400 Subject: [PATCH 1/3] context as parameter, remove IContextDependent --- .../ReplaceTemplateTests.cs | 7 ++- .../FindParser.cs | 3 +- .../Parsers/DebugParser.cs | 4 +- .../Parsers/EmptyStringParser.cs | 2 +- .../Parsers/EnumParser.cs | 4 +- .../Parsers/IContextDependent.cs | 7 --- .../Parsers/ParserWithContext.cs | 8 +++ .../Parsers/ParserWithLookahead.cs | 8 +-- .../Parsers/PlaceholderParser.cs | 25 +++------ .../Placeholder/Placeholder.cs | 4 +- .../Rules/FindRule/BinaryRule.cs | 12 +--- .../Rules/FindRule/BinarySubRule.cs | 12 +--- .../Rules/FindRule/EmptyRule.cs | 5 +- .../Rules/FindRule/EmptySubRule.cs | 2 +- .../Rules/FindRule/IRule.cs | 4 +- .../Rules/FindRule/InSubRule.cs | 29 ++++------ .../Rules/FindRule/IsSubRule.cs | 9 +-- .../Rules/FindRule/UnaryRule.cs | 9 +-- .../Rules/Parameters/IRuleParameter.cs | 4 +- .../Parameters/ParenthesisedParameter.cs | 20 +++---- .../Parameters/PlaceholderColumnParameter.cs | 10 +--- .../Parameters/PlaceholderFileParameter.cs | 11 ++-- .../Parameters/PlaceholderLenghtParameter.cs | 10 +--- .../Parameters/PlaceholderLineParameter.cs | 10 +--- .../Parameters/PlaceholderOffsetParameter.cs | 10 +--- .../Rules/Parameters/PlaceholderParameter.cs | 15 ++--- .../Rules/Parameters/StringFormatParameter.cs | 24 ++++---- .../Rules/Parameters/StringParameter.cs | 7 +-- .../ReplaceRule/ChangeBinaryParameter.cs | 9 +-- .../Rules/ReplaceRule/ChangeParameter.cs | 9 +-- .../Rules/ReplaceRule/ChangeUnaryParameter.cs | 12 +--- .../Rules/ReplaceRule/IReplaceRule.cs | 2 +- .../Rules/ReplaceRule/ReplaceRule.cs | 10 ---- .../Rules/ReplaceRule/ReplaceSubRule.cs | 8 +-- .../SeriesParser.cs | 54 +++++++----------- .../StructuralSearchParser.cs | 56 ++++++++----------- .../ReplaceTemplate/IReplaceBuilder.cs | 4 +- .../Templates/ReplaceTemplate/IReplaceStep.cs | 2 +- .../ReplaceTemplate/PlaceholderReplace.cs | 13 ++--- .../ReplaceTemplate/ReplaceBuilder.cs | 11 +--- .../Templates/ReplaceTemplate/TokenReplace.cs | 2 +- 41 files changed, 164 insertions(+), 303 deletions(-) delete mode 100644 src/SimpleStateMachine.StructuralSearch/Parsers/IContextDependent.cs create mode 100644 src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithContext.cs diff --git a/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceTemplateTests.cs b/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceTemplateTests.cs index 42ef7a1..6654852 100644 --- a/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceTemplateTests.cs +++ b/src/SimpleStateMachine.StructuralSearch.Tests/ReplaceTemplateTests.cs @@ -15,7 +15,8 @@ public static void ReplaceTemplateParsingShouldHaveStepCount(string templatePath { var replaceTemplate = File.ReadAllText(templatePath); var replaceBuilder = StructuralSearch.ParseReplaceTemplate(replaceTemplate); - var result = replaceBuilder.Build(ParsingContext.Empty); + IParsingContext context = ParsingContext.Empty; + var result = replaceBuilder.Build(ref context); Assert.NotNull(replaceTemplate); Assert.Equal(replaceBuilder.Steps.Count(), stepsCount); @@ -37,13 +38,13 @@ public static void ReplaceBuildShouldBeSuccess(string templatePath, string resul var replaceResult = File.ReadAllText(resultPath); var replaceBuilder = StructuralSearch.ParseReplaceTemplate(replaceTemplate); - var parsingContext = new ParsingContext(Input.Empty); + IParsingContext parsingContext = new ParsingContext(Input.Empty); for (int i = 0; i < keys.Length; i++) { parsingContext.AddPlaceholder(Placeholder.CreateEmpty(parsingContext, keys[i], values[i])); } - var result = replaceBuilder.Build(parsingContext); + var result = replaceBuilder.Build(ref parsingContext); Assert.NotNull(replaceTemplate); Assert.NotNull(replaceResult); diff --git a/src/SimpleStateMachine.StructuralSearch/FindParser.cs b/src/SimpleStateMachine.StructuralSearch/FindParser.cs index 527acc7..5f46d53 100644 --- a/src/SimpleStateMachine.StructuralSearch/FindParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/FindParser.cs @@ -20,8 +20,7 @@ public IEnumerable Parse(ref IParsingContext context) { List matches = new(); StringBuilder res = new(); - Parser.SetContext(ref context); - + var parsingContext = context; var parser = Parser.Select(x => string.Join(string.Empty, x)) .Match() diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/DebugParser.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/DebugParser.cs index a25c590..5b8135a 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/DebugParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/DebugParser.cs @@ -10,9 +10,9 @@ public DebugParser(Parser parser) _parser = parser; } - public override bool TryParse(ref ParseState state, ref PooledList> expecteds, out T result) + public override bool TryParse(ref ParseState state, ref PooledList> expected, out T result) { - var res = _parser.TryParse(ref state, ref expecteds, out result); + var res = _parser.TryParse(ref state, ref expected, out result); return res; } } diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs index 3b199e5..6820c87 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs @@ -11,7 +11,7 @@ public EmptyStringParser(bool value) _value = value; } - public override bool TryParse(ref ParseState state, ref PooledList> expecteds, + public override bool TryParse(ref ParseState state, ref PooledList> expected, out string result) { result = string.Empty; diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/EnumParser.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/EnumParser.cs index ca99b86..657bc31 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/EnumParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/EnumParser.cs @@ -19,10 +19,10 @@ public EnumParser(bool ignoreCase, params TEnum [] excluded) .AsEnum(ignoreCase); } - public override bool TryParse(ref ParseState state, ref PooledList> expecteds, + public override bool TryParse(ref ParseState state, ref PooledList> expected, out TEnum result) { - return _parser.TryParse(ref state, ref expecteds, out result); + return _parser.TryParse(ref state, ref expected, out result); } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/IContextDependent.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/IContextDependent.cs deleted file mode 100644 index 71ac468..0000000 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/IContextDependent.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace SimpleStateMachine.StructuralSearch -{ - public interface IContextDependent - { - void SetContext(ref IParsingContext context); - } -} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithContext.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithContext.cs new file mode 100644 index 0000000..4bb6111 --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithContext.cs @@ -0,0 +1,8 @@ +using Pidgin; + +namespace SimpleStateMachine.StructuralSearch; + +// public abstract class ParserWithContext : Parser +// { +// public abstract bool TryParse(ref IParsingContext context, ref ParseState state, ref PooledList> expected, out T result); +// } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs index 9f7da64..3eecf07 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs @@ -28,15 +28,15 @@ public override bool TryParse(ref ParseState state, ref PooledList { + public T Result { get; } + public int TokensCount { get; } + public readonly Parser Parser; + public LookaheadResult(Parser parser, T result, int tokensCount) { Parser = parser; Result = result; TokensCount = tokensCount; } - - public T Result { get; } - public int TokensCount { get; } - public Parser Parser; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs index d4aaee7..c6af2e3 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs @@ -5,17 +5,15 @@ namespace SimpleStateMachine.StructuralSearch { - public class PlaceholderParser : ParserWithLookahead, IContextDependent + public class PlaceholderParser : ParserWithLookahead { - private IParsingContext _context; - + private readonly string _name; + public PlaceholderParser(string name) { - Name = name; + _name = name; } - public string Name { get; } - public override Parser BuildParser(Func?> next, Func?> nextNext) { @@ -79,9 +77,9 @@ public override bool TryParse(ref ParseState state, ref PooledList state, ref PooledList _match; private readonly IParsingContext _context; - public Placeholder(IParsingContext context, string name, Match match) + public Placeholder(ref IParsingContext context, string name, Match match) { _context = context; Name = name; @@ -22,7 +22,7 @@ public Placeholder(IParsingContext context, string name, Match match) public static Placeholder CreateEmpty(IParsingContext context, string name, string value) { return new Placeholder( - context: context, + context: ref context, name: name, new Match( value, diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs index 10eff58..b17b4a6 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs @@ -15,10 +15,10 @@ public BinaryRule(BinaryRuleType type, IRule left, IRule right) _right = right; } - public bool Execute() + public bool Execute(ref IParsingContext context) { - var left = _left.Execute(); - var right = _right.Execute(); + var left = _left.Execute(ref context); + var right = _right.Execute(ref context); return LogicalHelper.Calculate(_type, left, right); } @@ -27,11 +27,5 @@ public override string ToString() { return $"{_left}{Constant.Space}{_type}{Constant.Space}{_right}"; } - - public void SetContext(ref IParsingContext context) - { - _left.SetContext(ref context); - _right.SetContext(ref context); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs index a503fda..637db44 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs @@ -16,10 +16,10 @@ public BinarySubRule(SubRuleType type, IRuleParameter left, IRuleParameter right _right = right; } - public bool Execute() + public bool Execute(ref IParsingContext context) { - var left = _left.GetValue(); - var right = _right.GetValue(); + var left = _left.GetValue(ref context); + var right = _right.GetValue(ref context); return _type switch { @@ -36,11 +36,5 @@ public override string ToString() { return $"{_left}{Constant.Space}{_type}{Constant.Space}{_right}"; } - - public void SetContext(ref IParsingContext context) - { - _left.SetContext(ref context); - _right.SetContext(ref context); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs index 1c3228f..71b0ecf 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs @@ -2,14 +2,11 @@ public class EmptyRule: IRule { - public bool Execute() + public bool Execute(ref IParsingContext context) { return true; } - public void SetContext(ref IParsingContext context) - { - } } public static class Rule diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs index ad68450..f04e4b2 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs @@ -2,7 +2,7 @@ public class EmptySubRule : IRule { - public bool Execute() + public bool Execute(ref IParsingContext context) { return true; } diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IRule.cs index 1d2f9b3..bf7455a 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IRule.cs @@ -1,7 +1,7 @@ namespace SimpleStateMachine.StructuralSearch.Rules { - public interface IRule : IContextDependent + public interface IRule { - bool Execute(); + bool Execute(ref IParsingContext context); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs index a539f67..19000c8 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs @@ -16,31 +16,24 @@ public InSubRule(IRuleParameter parameter, IEnumerable arguments _arguments = arguments; } - public bool Execute() + public bool Execute(ref IParsingContext context) { - var value = _parameter.GetValue(); - var result = _arguments.Any(parameter => + var value = _parameter.GetValue(ref context); + + foreach (var argument in _arguments) { - var valueForResult = parameter.GetValue(); - var equal = Equals(value, valueForResult); - return equal; - }); - return result; + var valueForResult = argument.GetValue(ref context); + + if (Equals(value, valueForResult)) + return true; + } + + return false; } public override string ToString() { return $"{_parameter}{Constant.Space}{SubRuleType.In}{Constant.Space}{string.Join(Constant.Comma, _arguments.Select(x=>x.ToString()))}"; } - - public void SetContext(ref IParsingContext context) - { - _parameter.SetContext(ref context); - - foreach (var argument in _arguments) - { - argument.SetContext(ref context); - } - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsSubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsSubRule.cs index 0a988f2..e0e102e 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsSubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsSubRule.cs @@ -14,9 +14,9 @@ public IsSubRule(IRuleParameter parameter, PlaceholderType argument) _argument = argument; } - public bool Execute() + public bool Execute(ref IParsingContext context) { - var value = _parameter.GetValue(); + var value = _parameter.GetValue(ref context); return _argument switch { @@ -33,10 +33,5 @@ public override string ToString() { return $"{_parameter}{Constant.Space}{SubRuleType.Is}{Constant.Space}{_argument}"; } - - public void SetContext(ref IParsingContext context) - { - _parameter.SetContext(ref context); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs index 2b3b467..43b1ddf 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs @@ -13,9 +13,9 @@ public UnaryRule(UnaryRuleType type, IRule parameter) _parameter = parameter; } - public bool Execute() + public bool Execute(ref IParsingContext context) { - var result = _parameter.Execute(); + var result = _parameter.Execute(ref context); return _type switch { @@ -28,10 +28,5 @@ public override string ToString() { return $"{_type}{Constant.Space}{_parameter}"; } - - public void SetContext(ref IParsingContext context) - { - _parameter.SetContext(ref context); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/IRuleParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/IRuleParameter.cs index ecb0836..a6f63df 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/IRuleParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/IRuleParameter.cs @@ -1,7 +1,7 @@ namespace SimpleStateMachine.StructuralSearch.Rules { - public interface IRuleParameter : IContextDependent + public interface IRuleParameter { - string GetValue(); + string GetValue(ref IParsingContext context); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParenthesisedParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParenthesisedParameter.cs index 554f0ba..4e74c12 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParenthesisedParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParenthesisedParameter.cs @@ -16,24 +16,22 @@ public ParenthesisedParameter(ParenthesisType parenthesisType, IEnumerable x.GetValue()))); + var values = new List(); + foreach (var parameter in _parameters) + { + values.Add(parameter.GetValue(ref context)); + } + + return string.Format(_template, string.Join(string.Empty, values)); } public override string ToString() { return string.Format(_template, string.Join(string.Empty, _parameters.Select(x=> x.ToString()))); } - - public void SetContext(ref IParsingContext context) - { - foreach (var parameter in _parameters) - { - parameter.SetContext(ref context); - } - } - + private static string GetTemplate(ParenthesisType parenthesisType) { return parenthesisType switch diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs index e872d2b..f7906a8 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs @@ -13,9 +13,10 @@ public PlaceholderColumnParameter(PlaceholderParameter parameter, ColumnProperty _property = property; } - public string GetValue() + public string GetValue(ref IParsingContext context) { - var column = _placeholderParameter.GetPlaceholder().Column; + var placeHolder = _placeholderParameter.GetPlaceholder(ref context); + var column = placeHolder.Column; var value = _property switch { @@ -31,10 +32,5 @@ public override string ToString() { return $"{_placeholderParameter}{Constant.Dote}{PlaceholderProperty.Column}{Constant.Dote}{_property}"; } - - public void SetContext(ref IParsingContext context) - { - _placeholderParameter.SetContext(ref context); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs index ab99179..51dbb53 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs @@ -13,9 +13,11 @@ public PlaceholderFileParameter(PlaceholderParameter parameter, FileProperty pro _property = property; } - public string GetValue() + public string GetValue(ref IParsingContext context) { - var input = _placeholderParameter.GetPlaceholder().Input; + var placeHolder = _placeholderParameter.GetPlaceholder(ref context); + var input = placeHolder.Input; + return _property switch { FileProperty.Path => input.Path, @@ -31,10 +33,5 @@ public override string ToString() { return $"{_placeholderParameter}{Constant.Dote}{PlaceholderProperty.File}{Constant.Dote}{_property}"; } - - public void SetContext(ref IParsingContext context) - { - _placeholderParameter.SetContext(ref context); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs index 4c83c7b..c5e1a97 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs @@ -11,19 +11,15 @@ public PlaceholderLenghtParameter(PlaceholderParameter parameter, PlaceholderPro _property = property; } - public string GetValue() + public string GetValue(ref IParsingContext context) { - return _placeholderParameter.GetPlaceholder().Lenght.ToString(); + var placeHolder = _placeholderParameter.GetPlaceholder(ref context); + return placeHolder.Lenght.ToString(); } public override string ToString() { return $"{_placeholderParameter}{Constant.Dote}{_property}"; } - - public void SetContext(ref IParsingContext context) - { - _placeholderParameter.SetContext(ref context); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs index 8d296e1..6539fca 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs @@ -13,9 +13,10 @@ public PlaceholderLineParameter(PlaceholderParameter parameter, LineProperty pro _property = property; } - public string GetValue() + public string GetValue(ref IParsingContext context) { - var line = _placeholderParameter.GetPlaceholder().Line; + var placeHolder = _placeholderParameter.GetPlaceholder(ref context); + var line = placeHolder.Line; var value = _property switch { @@ -31,10 +32,5 @@ public override string ToString() { return $"{_placeholderParameter}{Constant.Dote}{PlaceholderProperty.Line}{Constant.Dote}{_property}"; } - - public void SetContext(ref IParsingContext context) - { - _placeholderParameter.SetContext(ref context); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs index f6670a2..aa3f86b 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs @@ -12,9 +12,10 @@ public PlaceholderOffsetParameter(PlaceholderParameter parameter, OffsetProperty _placeholderParameter = parameter; _property = property; } - public string GetValue() + public string GetValue(ref IParsingContext context) { - var offset = _placeholderParameter.GetPlaceholder().Offset; + var placeHolder = _placeholderParameter.GetPlaceholder(ref context); + var offset = placeHolder.Offset; var value = _property switch { @@ -30,10 +31,5 @@ public override string ToString() { return $"{_placeholderParameter}{Constant.Dote}{PlaceholderProperty.Offset}{Constant.Dote}{_property}"; } - - public void SetContext(ref IParsingContext context) - { - _placeholderParameter.SetContext(ref context); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs index 8393359..10a0862 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs @@ -2,8 +2,6 @@ { public class PlaceholderParameter : IRuleParameter { - private IParsingContext _context; - public PlaceholderParameter(string name) { Name = name; @@ -11,24 +9,19 @@ public PlaceholderParameter(string name) public string Name { get; } - public string GetValue() + public string GetValue(ref IParsingContext context) { - return _context.GetPlaceholder(Name).Value; + return context.GetPlaceholder(Name).Value; } - public IPlaceholder GetPlaceholder() + public IPlaceholder GetPlaceholder(ref IParsingContext context) { - return _context.GetPlaceholder(Name); + return context.GetPlaceholder(Name); } public override string ToString() { return $"{Constant.PlaceholderSeparator}{Name}{Constant.PlaceholderSeparator}"; } - - public void SetContext(ref IParsingContext context) - { - _context = context; - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs index 77275cd..2f93adf 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs @@ -5,29 +5,27 @@ namespace SimpleStateMachine.StructuralSearch.Rules { public class StringFormatParameter : IRuleParameter { - public IEnumerable Parameters { get; } + private readonly IEnumerable _parameters; public StringFormatParameter(IEnumerable parameters) { - Parameters = parameters; + _parameters = parameters; } - public string GetValue() + public string GetValue(ref IParsingContext context) { - return string.Join(string.Empty, Parameters.Select(x => x.GetValue())); + var values = new List(); + foreach (var parameter in _parameters) + { + values.Add(parameter.GetValue(ref context)); + } + + return string.Join(string.Empty, values); } public override string ToString() { - return $"{Constant.DoubleQuotes}{string.Join(string.Empty, Parameters.Select(x=> x.ToString()))}{Constant.DoubleQuotes}"; - } - - public void SetContext(ref IParsingContext context) - { - foreach (var parameter in Parameters) - { - parameter.SetContext(ref context); - } + return $"{Constant.DoubleQuotes}{string.Join(string.Empty, _parameters.Select(x=> x.ToString()))}{Constant.DoubleQuotes}"; } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs index 4bc8ad3..b41ddb1 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs @@ -10,7 +10,7 @@ public StringParameter(string value) { _value = value; } - public string GetValue() + public string GetValue(ref IParsingContext context) { return _value; } @@ -21,10 +21,5 @@ public override string ToString() return $"{value}"; } - - public void SetContext(ref IParsingContext context) - { - - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryParameter.cs index 8d20186..0458357 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryParameter.cs @@ -14,9 +14,9 @@ public ChangeBinaryParameter(IRuleParameter parameter, ChangeType type) _type = type; } - public string GetValue() + public string GetValue(ref IParsingContext context) { - var value = _parameter.GetValue(); + var value = _parameter.GetValue(ref context); return _type switch { ChangeType.Trim => value.Trim(), @@ -32,9 +32,4 @@ public override string ToString() { return $"{_parameter}{Constant.Dote}{_type}"; } - - public void SetContext(ref IParsingContext context) - { - _parameter.SetContext(ref context); - } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs index 83a90ac..9e1ca16 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs @@ -14,9 +14,9 @@ public ChangeParameter(IRuleParameter parameter, ChangeType type) _type = type; } - public string GetValue() + public string GetValue(ref IParsingContext context) { - var value = _parameter.GetValue(); + var value = _parameter.GetValue(ref context); return _type switch { ChangeType.Trim => value.Trim(), @@ -32,10 +32,5 @@ public override string ToString() { return $"{_parameter}{Constant.Dote}{_type}"; } - - public void SetContext(ref IParsingContext context) - { - _parameter.SetContext(ref context); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryParameter.cs index d3e37da..b20beba 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryParameter.cs @@ -16,10 +16,10 @@ public ChangeUnaryParameter(IRuleParameter parameter, ChangeUnaryType type, IRul _arg = arg; } - public string GetValue() + public string GetValue(ref IParsingContext context) { - var parameter = _parameter.GetValue(); - var arg = _arg.GetValue(); + var parameter = _parameter.GetValue(ref context); + var arg = _arg.GetValue(ref context); return _type switch { ChangeUnaryType.RemoveSubStr => parameter.Replace(arg, string.Empty), @@ -31,10 +31,4 @@ public override string ToString() { return $"{_parameter}{Constant.Dote}{_type}{Constant.LeftParenthesis}{_arg}{Constant.RightParenthesis}"; } - - public void SetContext(ref IParsingContext context) - { - _parameter.SetContext(ref context); - _arg.SetContext(ref context); - } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs index 216b18e..bde1ea4 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs @@ -4,7 +4,7 @@ namespace SimpleStateMachine.StructuralSearch; -public interface IReplaceRule : IContextDependent +public interface IReplaceRule { IEnumerable Rules { get; } IRule ConditionRule { get; } diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs index d3ac5b8..5fcd9e4 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs @@ -20,15 +20,5 @@ public override string ToString() { return $"{ConditionRule}{Constant.Space}{Constant.Then}{Constant.Space}{string.Join(Constant.Comma, Rules)}"; } - - public void SetContext(ref IParsingContext context) - { - ConditionRule.SetContext(ref context); - - foreach (var rule in Rules) - { - rule.SetContext(ref context); - } - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceSubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceSubRule.cs index f609273..ed3c15a 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceSubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceSubRule.cs @@ -2,7 +2,7 @@ namespace SimpleStateMachine.StructuralSearch; -public class ReplaceSubRule: IContextDependent +public class ReplaceSubRule { public readonly PlaceholderParameter Placeholder; public readonly IRuleParameter Parameter; @@ -17,10 +17,4 @@ public override string ToString() { return $"{Placeholder}{Constant.Space}{Constant.Should}{Constant.Space}{Parameter}"; } - - public void SetContext(ref IParsingContext context) - { - Placeholder.SetContext(ref context); - Parameter.SetContext(ref context); - } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs b/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs index fc1ab85..69fca76 100644 --- a/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs @@ -4,7 +4,7 @@ namespace SimpleStateMachine.StructuralSearch { - public class SeriesParser : Parser>, IContextDependent + public class SeriesParser : Parser> { private readonly IEnumerable> _parsers; @@ -15,8 +15,22 @@ public SeriesParser(IEnumerable> parsers) InitializeLookaheadParsers(); } - public override bool TryParse(ref ParseState state, ref PooledList> expecteds, - out IEnumerable result) + private void InitializeLookaheadParsers() + { + var count = _parsers.Count(); + + for (var i = count-1; i >= 0 ; i--) + { + var parser = _parsers.ElementAt(i); + if (parser is not ParserWithLookahead lookaheadParser) continue; + + var number = i; + lookaheadParser.Lookahead(() => _parsers.ElementAtOrDefault(number + 1), () => + _parsers.ElementAtOrDefault(number + 2)); + } + } + + public override bool TryParse(ref ParseState state, ref PooledList> expected, out IEnumerable result) { var results = new List(); var count = _parsers.Count(); @@ -24,13 +38,14 @@ public override bool TryParse(ref ParseState state, ref PooledList parser, ref ParseState state) result = results; return true; } - - public void SetContext(ref IParsingContext parsingContext) - { - foreach (var parser in _parsers) - { - if (parser is IContextDependent element) - { - element.SetContext(ref parsingContext); - } - } - } - - private void InitializeLookaheadParsers() - { - var count = _parsers.Count(); - - for (var i = count-1; i >= 0 ; i--) - { - var parser = _parsers.ElementAt(i); - if (parser is ParserWithLookahead lookaheadParser) - { - var number = i; - lookaheadParser.Lookahead(() => _parsers.ElementAtOrDefault(number + 1), () => - _parsers.ElementAtOrDefault(number + 2)); - } - } - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs index 31a6c41..470f9c3 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs @@ -40,24 +40,22 @@ public IEnumerable Parse(ref IParsingContext context) public IEnumerable ApplyFindRule(ref IParsingContext context, IEnumerable matches) { var result = new List(); - SetFindRulesContext(ref context); + foreach (var match in matches) { context.Fill(match.Placeholders); - var all = _findRules.All(x => x.Execute()); - if (all) + + if (FindRuleIsMatch(match, ref context)) { - result.Add(match); + result.Add(match); } } - IParsingContext emptyParsingContext = ParsingContext.Empty; - SetFindRulesContext(ref emptyParsingContext); return result; } + public IEnumerable ApplyReplaceRule(ref IParsingContext context, IEnumerable matches) { - SetReplaceRulesContext(ref context); var result = new List(); foreach (var match in matches) @@ -67,35 +65,33 @@ public IEnumerable ApplyReplaceRule(ref IParsingContext contex x => x.Value); context.Fill(match.Placeholders); - - var rules = _replaceRules - .Where(x => - { - var result = x.ConditionRule.Execute(); - return result; - }) - .SelectMany(x => x.Rules); + List rules = new(); + foreach (var replaceRule in _replaceRules) + { + if (replaceRule.ConditionRule.Execute(ref context)) + { + rules.AddRange(replaceRule.Rules); + } + } + foreach (var rule in rules) { var name = rule.Placeholder.Name; var placeholder = placeholders[name]; - var value = rule.Parameter.GetValue(); + var value = rule.Parameter.GetValue(ref context); placeholders[name] = new ReplacedPlaceholder(placeholder, value); } result.Add(match with { Placeholders = placeholders }); } - - IParsingContext emptyParsingContext = ParsingContext.Empty; - SetReplaceRulesContext(ref emptyParsingContext); - + return result; } public ReplaceMatch GetReplaceMatch(ref IParsingContext context, FindParserResult parserResult) { context.Fill(parserResult.Placeholders); - var replaceText = _replaceBuilder.Build(context); + var replaceText = _replaceBuilder.Build(ref context); return new ReplaceMatch(parserResult.Match, replaceText); // context.Input.Replace(parserResult.Match, replaceText); //ReplaceBuilder.Build(context); @@ -114,23 +110,15 @@ public IEnumerable GetReplaceMatches(ref IParsingContext context, return replaceMatches; } - - private void SetFindRulesContext(ref IParsingContext context) + private bool FindRuleIsMatch(FindParserResult parserResult, ref IParsingContext context) { foreach (var findRule in _findRules) { - if (findRule is IContextDependent contextDependent) - contextDependent.SetContext(ref context); - } - } - - private void SetReplaceRulesContext(ref IParsingContext context) - { - foreach (var replaceRule in _replaceRules) - { - if (replaceRule is IContextDependent contextDependent) - contextDependent.SetContext(ref context); + if (!findRule.Execute(ref context)) + return false; } + + return true; } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceBuilder.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceBuilder.cs index 03e3b13..0ea12a7 100644 --- a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceBuilder.cs +++ b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceBuilder.cs @@ -7,14 +7,14 @@ namespace SimpleStateMachine.StructuralSearch.ReplaceTemplate public interface IReplaceBuilder { IEnumerable Steps { get; } - string Build(IParsingContext context); + string Build(ref IParsingContext context); } public class EmptyReplaceBuilder: IReplaceBuilder { public IEnumerable Steps { get; } = Array.Empty(); - public string Build(IParsingContext context) + public string Build(ref IParsingContext context) { return string.Empty; } diff --git a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceStep.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceStep.cs index 5973e68..f92fc89 100644 --- a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceStep.cs +++ b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceStep.cs @@ -2,6 +2,6 @@ { public interface IReplaceStep { - string GetValue(); + string GetValue(ref IParsingContext context); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/PlaceholderReplace.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/PlaceholderReplace.cs index 4746e19..ee7eabe 100644 --- a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/PlaceholderReplace.cs +++ b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/PlaceholderReplace.cs @@ -1,8 +1,7 @@ namespace SimpleStateMachine.StructuralSearch.ReplaceTemplate { - public class PlaceholderReplace : IReplaceStep, IContextDependent + public class PlaceholderReplace : IReplaceStep { - private IParsingContext _context; private readonly string _name; public PlaceholderReplace(string name) @@ -10,14 +9,10 @@ public PlaceholderReplace(string name) _name = name; } - public string GetValue() + public string GetValue(ref IParsingContext context) { - return _context.GetPlaceholder(_name).Value; - } - - public void SetContext(ref IParsingContext context) - { - _context = context; + var placeHolder = context.GetPlaceholder(_name); + return placeHolder.Value; } public override string ToString() diff --git a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ReplaceBuilder.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ReplaceBuilder.cs index 487cee5..645327d 100644 --- a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ReplaceBuilder.cs +++ b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ReplaceBuilder.cs @@ -15,18 +15,13 @@ public ReplaceBuilder(IEnumerable steps) Steps = steps; } - public string Build(IParsingContext context) + public string Build(ref IParsingContext context) { - StringBuilder stringBuilder = new StringBuilder(); + var stringBuilder = new StringBuilder(); foreach (var step in Steps) { - if (step is IContextDependent contextDependentStep) - { - contextDependentStep.SetContext(ref context); - } - - stringBuilder.Append(step.GetValue()); + stringBuilder.Append(step.GetValue(ref context)); } var result = stringBuilder.ToString(); diff --git a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/TokenReplace.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/TokenReplace.cs index 08c880c..c335e4e 100644 --- a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/TokenReplace.cs +++ b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/TokenReplace.cs @@ -9,7 +9,7 @@ public TokenReplace(string token) _token = token; } - public string GetValue() + public string GetValue(ref IParsingContext context) { return _token; } From c84251a7324adbaf1e98a4a05fa37725b27370ee Mon Sep 17 00:00:00 2001 From: GMIKE Date: Thu, 27 Oct 2022 01:49:43 +0400 Subject: [PATCH 2/3] IContextDependent for parsers --- .../FindParser.cs | 2 ++ .../Parsers/IContextDependent.cs | 6 ++++++ .../Parsers/PlaceholderParser.cs | 15 ++++++++++----- .../Rules/FindRule/EmptySubRule.cs | 4 ---- .../Rules/ReplaceRule/IReplaceRule.cs | 4 ---- .../SeriesParser.cs | 15 +++++++++++++-- 6 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 src/SimpleStateMachine.StructuralSearch/Parsers/IContextDependent.cs diff --git a/src/SimpleStateMachine.StructuralSearch/FindParser.cs b/src/SimpleStateMachine.StructuralSearch/FindParser.cs index 5f46d53..7844cb9 100644 --- a/src/SimpleStateMachine.StructuralSearch/FindParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/FindParser.cs @@ -21,6 +21,8 @@ public IEnumerable Parse(ref IParsingContext context) List matches = new(); StringBuilder res = new(); + Parser.SetContext(ref context); + var parsingContext = context; var parser = Parser.Select(x => string.Join(string.Empty, x)) .Match() diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/IContextDependent.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/IContextDependent.cs new file mode 100644 index 0000000..3e639eb --- /dev/null +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/IContextDependent.cs @@ -0,0 +1,6 @@ +namespace SimpleStateMachine.StructuralSearch; + +public interface IContextDependent +{ + void SetContext(ref IParsingContext context); +} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs index c6af2e3..4d83abc 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs @@ -5,10 +5,10 @@ namespace SimpleStateMachine.StructuralSearch { - public class PlaceholderParser : ParserWithLookahead + public class PlaceholderParser : ParserWithLookahead, IContextDependent { private readonly string _name; - + private IParsingContext _context; public PlaceholderParser(string name) { _name = name; @@ -79,7 +79,7 @@ public override bool TryParse(ref ParseState state, ref PooledList state, ref PooledList state, ref PooledList Rules { get; } = Array.Empty(); public IRule ConditionRule => Rule.Empty; - - public void SetContext(ref IParsingContext context) - { - } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs b/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs index 69fca76..6f9fe04 100644 --- a/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs @@ -4,10 +4,10 @@ namespace SimpleStateMachine.StructuralSearch { - public class SeriesParser : Parser> + public class SeriesParser : Parser>, IContextDependent { private readonly IEnumerable> _parsers; - + public SeriesParser(IEnumerable> parsers) { _parsers = parsers; @@ -75,5 +75,16 @@ void SkipLookedParsers(Parser parser, ref ParseState state) result = results; return true; } + + public void SetContext(ref IParsingContext parsingContext) + { + foreach (var parser in _parsers) + { + if (parser is IContextDependent element) + { + element.SetContext(ref parsingContext); + } + } + } } } \ No newline at end of file From 13691b37061629e6345df1a0e481edf4a5eebbf0 Mon Sep 17 00:00:00 2001 From: GMIKE Date: Thu, 27 Oct 2022 01:56:44 +0400 Subject: [PATCH 3/3] IReplaceRule with check method --- .../Rules/FindRule/EmptyRule.cs | 1 - .../Rules/ReplaceRule/IReplaceRule.cs | 8 ++++++-- .../Rules/ReplaceRule/ReplaceRule.cs | 5 +++++ .../StructuralSearchParser.cs | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs index 71b0ecf..01712e8 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs @@ -5,7 +5,6 @@ public class EmptyRule: IRule public bool Execute(ref IParsingContext context) { return true; - } } diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs index 6093eb3..e6e9708 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs @@ -7,11 +7,15 @@ namespace SimpleStateMachine.StructuralSearch; public interface IReplaceRule { IEnumerable Rules { get; } - IRule ConditionRule { get; } + bool IsMatch(ref IParsingContext context); } public class EmptyReplaceRule : IReplaceRule { public IEnumerable Rules { get; } = Array.Empty(); - public IRule ConditionRule => Rule.Empty; + + public bool IsMatch(ref IParsingContext context) + { + return Rule.Empty.Execute(ref context); + } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs index 5fcd9e4..1cc9451 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs @@ -16,6 +16,11 @@ public ReplaceRule(IRule conditionRule, IEnumerable rules) Rules = rules; } + public bool IsMatch(ref IParsingContext context) + { + return ConditionRule.Execute(ref context); + } + public override string ToString() { return $"{ConditionRule}{Constant.Space}{Constant.Then}{Constant.Space}{string.Join(Constant.Comma, Rules)}"; diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs index 470f9c3..aa2b7ea 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs @@ -69,7 +69,7 @@ public IEnumerable ApplyReplaceRule(ref IParsingContext contex List rules = new(); foreach (var replaceRule in _replaceRules) { - if (replaceRule.ConditionRule.Execute(ref context)) + if (replaceRule.IsMatch(ref context)) { rules.AddRange(replaceRule.Rules); }