Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/SimpleStateMachine.StructuralSearch/FindParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public IEnumerable<FindParserResult> Parse(ref IParsingContext context)
{
List<FindParserResult> matches = new();
StringBuilder res = new();

Parser.SetContext(ref context);

var parsingContext = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public DebugParser(Parser<TToken, T> parser)
_parser = parser;
}

public override bool TryParse(ref ParseState<TToken> state, ref PooledList<Expected<TToken>> expecteds, out T result)
public override bool TryParse(ref ParseState<TToken> state, ref PooledList<Expected<TToken>> 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public EmptyStringParser(bool value)
_value = value;
}

public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expecteds,
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected,
out string result)
{
result = string.Empty;
Expand Down
4 changes: 2 additions & 2 deletions src/SimpleStateMachine.StructuralSearch/Parsers/EnumParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public EnumParser(bool ignoreCase, params TEnum [] excluded)
.AsEnum<TEnum>(ignoreCase);
}

public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expecteds,
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected,
out TEnum result)
{
return _parser.TryParse(ref state, ref expecteds, out result);
return _parser.TryParse(ref state, ref expected, out result);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Pidgin;

namespace SimpleStateMachine.StructuralSearch;

// public abstract class ParserWithContext<TToken, T> : Parser<TToken, T>
// {
// public abstract bool TryParse(ref IParsingContext context, ref ParseState<TToken> state, ref PooledList<Expected<TToken>> expected, out T result);
// }
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public override bool TryParse(ref ParseState<TToken> state, ref PooledList<Expec

public class LookaheadResult<TToken, T>
{
public T Result { get; }
public int TokensCount { get; }
public readonly Parser<TToken, T> Parser;

public LookaheadResult(Parser<TToken, T> parser, T result, int tokensCount)
{
Parser = parser;
Result = result;
TokensCount = tokensCount;
}

public T Result { get; }
public int TokensCount { get; }
public Parser<TToken, T> Parser;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ namespace SimpleStateMachine.StructuralSearch
{
public class PlaceholderParser : ParserWithLookahead<char, string>, IContextDependent
{
private readonly string _name;
private IParsingContext _context;

public PlaceholderParser(string name)
{
Name = name;
_name = name;
}

public string Name { get; }

public override Parser<char, string> BuildParser(Func<Parser<char, string>?> next,
Func<Parser<char, string>?> nextNext)
{
Expand Down Expand Up @@ -79,9 +77,9 @@ public override bool TryParse(ref ParseState<char> state, ref PooledList<Expecte
out string result)
{
bool res;

// No use look-ahead if placeholder is already defined
if (_context.TryGetPlaceholder(Name, out var placeholder))
if (_context.TryGetPlaceholder(_name, out var placeholder))
{
res = Parser.String(placeholder.Value).TryParse(ref state, ref expected, out result);
}
Expand All @@ -92,8 +90,8 @@ public override bool TryParse(ref ParseState<char> state, ref PooledList<Expecte
if (res)
{
_context.AddPlaceholder(new Placeholder(
context: _context,
name: Name,
context: ref _context,
name: _name,
match: match));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
private readonly Match<string> _match;
private readonly IParsingContext _context;
public Placeholder(IParsingContext context, string name, Match<string> match)
public Placeholder(ref IParsingContext context, string name, Match<string> match)
{
_context = context;
Name = name;
Expand All @@ -22,7 +22,7 @@ public Placeholder(IParsingContext context, string name, Match<string> match)
public static Placeholder CreateEmpty(IParsingContext context, string name, string value)
{
return new Placeholder(
context: context,
context: ref context,
name: name,
new Match<string>(
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class EmptySubRule : IRule
{
public bool Execute()
public bool Execute(ref IParsingContext context)
{
return true;
}
Expand All @@ -11,8 +11,4 @@ public override string ToString()
{
return $"{Constant.Underscore}";
}

public void SetContext(ref IParsingContext context)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace SimpleStateMachine.StructuralSearch.Rules
{
public interface IRule : IContextDependent
public interface IRule
{
bool Execute();
bool Execute(ref IParsingContext context);
}
}
29 changes: 11 additions & 18 deletions src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,24 @@ public InSubRule(IRuleParameter parameter, IEnumerable<IRuleParameter> 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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -28,10 +28,5 @@ public override string ToString()
{
return $"{_type}{Constant.Space}{_parameter}";
}

public void SetContext(ref IParsingContext context)
{
_parameter.SetContext(ref context);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace SimpleStateMachine.StructuralSearch.Rules
{
public interface IRuleParameter : IContextDependent
public interface IRuleParameter
{
string GetValue();
string GetValue(ref IParsingContext context);
}
}
Loading