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
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "5.0.0",
"version": "6.0.0",
"rollForward": "latestMinor",
"allowPrerelease": false
}
Expand Down
20 changes: 20 additions & 0 deletions src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using Pidgin;
Expand All @@ -19,6 +21,24 @@ internal static class Program
{
static void Main(string[] args)
{

var source = "test;;;test;;;.";
var parser = Parser.OneOf(Parser<char>.Any.ThenReturn(Unit.Value), Parser<char>.End);


var t = Parser<char>.Any.AtLeastOnceAsStringUntil(Lookahead(String(";").Then(Not(String(";"))).Try())).ParseOrThrow(source);


var path = "Test.txt";
var oldText = "0123456789";
var text = "test";
File.WriteAllText(path, oldText);

using var stringReader = text.AsStream();
using var streamWriter = File.OpenWrite(path);

stringReader.CopyPartTo(streamWriter, 0, 7);

// var config = YmlHelper.Parse(
// @"C:\Users\roman\GitHub\SimpleStateMachine.StructuralSearch\src\SimpleStateMachine.StructuralSearch.Tests\ConfigurationFile\FullConfig.yml");
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<TargetFramework>net5.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>9</LangVersion>
<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pidgin" Version="3.0.1" />
<PackageReference Include="Pidgin" Version="3.1.0" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@
# NullUnionOperator

- FindTemplate: |-
if($value1$ $sign$ null)
{
if($value$ $sign$ null)
$var$ = $value2$;
}
else
{
$var$ = $value1$;
}
FindRules:
- $sign$ In ("Is", "==", "!=", "is not")
- $value$ In ($value1$, "$value1$.Value", $value2$, "$value2$.Value")
ReplaceTemplate: |-
$var$ = $value1$ ?? $value2$;
ReplaceRules:
- $sign$ In ("!=", "is not") then $var2$ => $var1$, $var1$ => $var2$

# TernaryOperator

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@
# NullUnionOperator

- FindTemplate: |-
if($value1$ $sign$ null)
{
if($value$ $sign$ null)
$var$ = $value2$;
}
else
{
$var$ = $value1$;
}
FindRules:
- $sign$ In ("Is", "==", "!=", "is not")
- $value$ In ($value1$, "$value1$.Value", $value2$, "$value2$.Value")
ReplaceTemplate: |-
$var$ = $value1$ ?? $value2$;

ReplaceRules:
- $sign$ In ("!=", "is not") then $var2$ => $var1$, $var1$ => $var2$
# TernaryOperator

- FindTemplate: |-
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace SimpleStateMachine.StructuralSearch.Tests.Examples;

public class NullUnionOperator
{
public int Test1()
{
int? temp = 1;

if(temp is null)
return 3;
else
return 4;
}

public void Test2()
{
int? result;
int? temp1 = 5;
int? temp2 = 5;
if(temp1 is null)
result = temp2;
else
result = temp1;
}

public void Test3()
{
int result;
int? temp1 = 5;
int? temp2 = 5;
if(temp1 is null)
result = temp2.Value;
else
result = temp1.Value;
}

public int Test4()
{
int? temp3 = 5;

if(temp3 is null)
return 7;
else if (temp3 == 8)
return 9;
else
return 10;
}

public void Test5()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;

namespace SimpleStateMachine.StructuralSearch.Tests.Examples
{
public class TernaryOperator
{
public int Test1()
{
var temp = 1;

if(temp == 2)
return 3;
else
return 4;
}

public int Test2()
{
var temp = 5;

if(temp == 6)
return 7;
else
return 8;
}

public int Test3()
{
var temp2 = 1;

if(temp2 == 2)
return 3;
else
return 4;
}

public int Test4()
{
var temp3 = 5;

if(temp3 == 6)
return 7;
else if (temp3 == 8)
return 9;
else
return 10;
}

public void Test5()
{

}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
$sign$ In ("Is", "==", "!=", "is not")
$sign$ In ("Is", "==", "!=", "is not")
$value$ In ($value1$, "$value1$.Value", $value2$, "$value2$.Value")
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ namespace SimpleStateMachine.StructuralSearch.Tests
public class FindRuleParserTests
{
[Theory]
[InlineData("equals $var$")]
[InlineData("equals \"\\$\"")]
[InlineData("Not equals $var$.Lenght")]
[InlineData("Not equals $var$.offset.Start")]
[InlineData("equals $var$.Lenght and Not StartsWith \"123\"")]
[InlineData("equals $var$.Lenght and Not StartsWith \"\\\"Test\"")]
[InlineData("Contains $var$.Lenght")]
[InlineData("Contains \"123\"")]
[InlineData("StartsWith $var$.Lenght")]
[InlineData("StartsWith \"123\"")]
[InlineData("EndsWith $var$.Lenght")]
[InlineData("EndsWith \"123\"")]
[InlineData("Match $var$.Lenght")]
[InlineData("Is Int")]
[InlineData("Is DateTime")]
[InlineData("$var$ equals $var$")]
[InlineData("$var$ equals \"\\$\"")]
[InlineData("Not $var$ equals $var$.Lenght")]
[InlineData("Not $var$ equals $var$.offset.Start")]
[InlineData("$var$ equals $var$.Lenght and Not $var$ StartsWith \"123\"")]
[InlineData("Not $var$ equals $var$.Lenght and $var$ StartsWith \"123\"")]
[InlineData("$var$ equals $var$.Lenght and Not $var$ StartsWith \"\\\"Test\"")]
[InlineData("$var$ Contains $var$.Lenght")]
[InlineData("$var1$.Lenght Contains $var2$.Lenght")]
[InlineData("$var$ Contains \"123\"")]
[InlineData("$var$ StartsWith $var$.Lenght")]
[InlineData("$var$.Lenght Equals $var$.Lenght")]
[InlineData("$var$ StartsWith \"123\"")]
[InlineData("$var$ EndsWith $var$.Lenght")]
[InlineData("$var$ EndsWith \"123\"")]
[InlineData("$var$ Match $var$.Lenght")]
[InlineData("$var$ Is Int")]
[InlineData("$var$ Is DateTime")]

public void FindRuleExprParsingShouldBeSuccess(string ruleStr)
{
Expand All @@ -35,8 +38,10 @@ public void FindRuleExprParsingShouldBeSuccess(string ruleStr)
}

[Theory]
[InlineData("In \"Is\", \"==\", \"!=\", \"is not\"", "In \"Is\",\"==\",\"!=\",\"is not\"")]
[InlineData("In (\"Is\", \"==\", \"!=\", \"is not\")", "In \"Is\",\"==\",\"!=\",\"is not\"")]
[InlineData("$var$ In \"Is\", \"==\", \"!=\", \"is not\"", "$var$ In \"Is\",\"==\",\"!=\",\"is not\"")]
[InlineData("$var$ In (\"Is\", \"==\", \"!=\", \"is not\")", "$var$ In \"Is\",\"==\",\"!=\",\"is not\"")]
[InlineData("Not ($var$ equals $var$.Lenght and $var$ StartsWith \"123\")", "Not $var$ equals $var$.Lenght and $var$ StartsWith \"123\"")]
[InlineData("Not ($var$ equals $var$.Lenght)", "Not $var$ equals $var$.Lenght")]
public void FindRuleExprParsingShouldBeEqualsCustomResult(string ruleStr, string customResult)
{
var rule = FindRuleParser.Expr.ParseOrThrow(ruleStr);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
if($value1$ $sign$ null)
{
if($value$ $sign$ null)
$var$ = $value2$;
}
else
{
$var$ = $value1$;
}
$var$ = $value1$;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using Pidgin;
using Xunit;

Expand Down Expand Up @@ -28,13 +29,16 @@ public void SourceParsingBeFindTemplateShouldBeSuccess(string templatePath, stri
{
var findTemplate = File.ReadAllText(templatePath);
var source = File.ReadAllText(sourcePath);
var input = Input.String(source);
var findParser = StructuralSearch.ParseFindTemplate(findTemplate);
IParsingContext parsingContext = new ParsingContext();
var result = findParser.Parse(ref parsingContext, source);
IParsingContext parsingContext = new ParsingContext(input);
var matches = findParser.Parse(ref parsingContext);
Assert.Single(matches);

var match = matches.First();

Assert.NotNull(findParser);
Assert.NotNull(result.Value);
Assert.Equal(result.Lenght, source.Length);
Assert.Equal(match.Match.Lenght, source.Length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public static Configuration GetConfigurationFromFiles(string name)
var fileRule = FileOrNull("FindRule", fileName) ;
var replaceTemplate = FileOrNull("ReplaceTemplate", fileName);
var replaceRule = FileOrNull("ReplaceRule", fileName);

var fileRules = fileRule is null ? null : new List<string>{ fileRule };
var replaceRules = replaceRule is null ? null : new List<string>{ replaceRule };
var fileRules = fileRule is null ? null : new List<string>(fileRule.Split(Constant.LineFeed.ToString()));
var replaceRules = replaceRule is null ? null : new List<string>(replaceRule.Split(Constant.LineFeed.ToString()));
var config = new Configuration
{
FindTemplate = findTemplate,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace SimpleStateMachine.StructuralSearch.Tests.Mock
using System.Collections.Generic;

namespace SimpleStateMachine.StructuralSearch.Tests.Mock
{
public class EmptyParsingContext : IParsingContext
{
public FileProperty File { get; } = new FileProperty();
public IInput Input { get; }

public bool TryGetPlaceholder(string name, out Placeholder value)
{
Expand All @@ -18,5 +20,20 @@ public Placeholder GetPlaceholder(string name)
{
return Placeholder.CreateEmpty(this, name, string.Empty);
}

public IReadOnlyDictionary<string, Placeholder> SwitchOnNew()
{
throw new System.NotImplementedException();
}

public void Set(IReadOnlyDictionary<string, Placeholder> placeholders)
{
throw new System.NotImplementedException();
}

public void Clear()
{
throw new System.NotImplementedException();
}
}
}
Loading