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..7844cb9 100644 --- a/src/SimpleStateMachine.StructuralSearch/FindParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/FindParser.cs @@ -20,6 +20,7 @@ public IEnumerable Parse(ref IParsingContext context) { List matches = new(); StringBuilder res = new(); + Parser.SetContext(ref context); var parsingContext = context; 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 index 71ac468..3e639eb 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/IContextDependent.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/IContextDependent.cs @@ -1,7 +1,6 @@ -namespace SimpleStateMachine.StructuralSearch +namespace SimpleStateMachine.StructuralSearch; + +public interface IContextDependent { - public interface IContextDependent - { - void SetContext(ref IParsingContext context); - } + 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..4d83abc 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs @@ -7,15 +7,13 @@ namespace SimpleStateMachine.StructuralSearch { public class PlaceholderParser : ParserWithLookahead, IContextDependent { + private readonly string _name; private IParsingContext _context; - 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..01712e8 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs @@ -2,13 +2,9 @@ public class EmptyRule: IRule { - public bool Execute() + public bool Execute(ref IParsingContext context) { return true; - - } - public void SetContext(ref IParsingContext context) - { } } diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs index ad68450..ae0fcb3 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; } @@ -11,8 +11,4 @@ public override string ToString() { return $"{Constant.Underscore}"; } - - public void SetContext(ref IParsingContext context) - { - } } \ No newline at end of file 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..e6e9708 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs @@ -4,18 +4,18 @@ namespace SimpleStateMachine.StructuralSearch; -public interface IReplaceRule : IContextDependent +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 void SetContext(ref IParsingContext context) + + 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 d3ac5b8..1cc9451 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs @@ -16,19 +16,14 @@ public ReplaceRule(IRule conditionRule, IEnumerable rules) Rules = rules; } - public override string ToString() + public bool IsMatch(ref IParsingContext context) { - return $"{ConditionRule}{Constant.Space}{Constant.Then}{Constant.Space}{string.Join(Constant.Comma, Rules)}"; + return ConditionRule.Execute(ref context); } - - public void SetContext(ref IParsingContext context) + + public override string ToString() { - ConditionRule.SetContext(ref context); - - foreach (var rule in Rules) - { - rule.SetContext(ref context); - } + return $"{ConditionRule}{Constant.Space}{Constant.Then}{Constant.Space}{string.Join(Constant.Comma, Rules)}"; } } } \ 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..6f9fe04 100644 --- a/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs @@ -7,7 +7,7 @@ namespace SimpleStateMachine.StructuralSearch public class SeriesParser : Parser>, IContextDependent { private readonly IEnumerable> _parsers; - + public SeriesParser(IEnumerable> parsers) { _parsers = 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= 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..aa2b7ea 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.IsMatch(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; }