Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
start creating tests and features
Browse files Browse the repository at this point in the history
  • Loading branch information
Lotes committed Mar 28, 2018
1 parent b6b57cc commit 79b3e1d
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
/Lexer/obj
/Main/bin
/Main/obj
/TestResults
/Tests/obj
/Tests/bin
/.vs
6 changes: 6 additions & 0 deletions CQLLexer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lexer", "Lexer\Lexer.csproj
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Main", "Main\Main.csproj", "{FFBF79FD-6B1E-4E02-B086-8BCD2ABAC14B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{61123303-1217-4491-B7E0-ED2CF0C918CE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -18,5 +20,9 @@ Global
{FFBF79FD-6B1E-4E02-B086-8BCD2ABAC14B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FFBF79FD-6B1E-4E02-B086-8BCD2ABAC14B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FFBF79FD-6B1E-4E02-B086-8BCD2ABAC14B}.Release|Any CPU.Build.0 = Release|Any CPU
{61123303-1217-4491-B7E0-ED2CF0C918CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{61123303-1217-4491-B7E0-ED2CF0C918CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61123303-1217-4491-B7E0-ED2CF0C918CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61123303-1217-4491-B7E0-ED2CF0C918CE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
88 changes: 77 additions & 11 deletions Lexer/Automaton/CharSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ public class CharSet: IEnumerable<CharRange>
{
private readonly List<CharRange> list = new List<CharRange>();

public int Length { get { return list.Sum(r => r.Count()); } }

public bool Contains(char c)
{
var index = 0;
while (index < list.Count && list[index].From < c)
while (index < list.Count && c > list[index].To)
index++;
if (index >= list.Count)
return false;
Expand All @@ -24,7 +26,7 @@ public bool Contains(char c)
public bool Contains(char from, char to)
{
var index = 0;
while (index < list.Count && list[index].From < from)
while (index < list.Count && from > list[index].To)
index++;
if (index >= list.Count)
return false;
Expand All @@ -35,7 +37,7 @@ public bool Contains(char from, char to)
public void Add(char c)
{
var index = 0;
while (index < list.Count && list[index].From < c)
while (index < list.Count && c > list[index].To)
index++;
if (index >= list.Count)
{
Expand All @@ -47,16 +49,23 @@ public void Add(char c)
var range = list[index];
if(c >= range.From && c <= range.To)
return; //already included
//c must be greater than range.To
list.Insert(index+1, new CharRange(c));
TryMergeRange(index+1);
if (c > range.To)
{
list.Insert(index + 1, new CharRange(c));
TryMergeRange(index + 1);
}
else // c < range.From
{
list.Insert(index, new CharRange(c));
TryMergeRange(index);
}
}
}

public void Add(char from, char to)
{
var index = 0;
while (index < list.Count && list[index].From < from)
while (index < list.Count && from > list[index].To)
index++;
if (index >= list.Count)
{
Expand All @@ -66,14 +75,71 @@ public void Add(char from, char to)
else
{
var fromIndex = index;
while (index < list.Count && list[index].To < to)
while (index < list.Count && from > list[index].To)
index++;
list.RemoveRange(fromIndex, index-fromIndex+1);
if(index < list.Count)
{
if (from > list[index].From)
{
from = list[index].From;
list.RemoveRange(fromIndex, index - fromIndex + 1);
}
}
list.Insert(fromIndex, new CharRange(from, to));
TryMergeRange(fromIndex);
}
}


public void Remove(char c)
{
var index = 0;
while (index < list.Count && c > list[index].To)
index++;
if (index >= list.Count)
return;
var range = list[index];
if (c < range.From || c > range.To)
return;
list.RemoveAt(index);
if (range.From == range.To)
;
else if (range.From == c)
list.Insert(index, new CharRange((char)(c+1), range.To));
else if (range.To == c)
list.Insert(index, new CharRange(range.From, (char)(c - 1)));
else
{
list.Insert(index, new CharRange(range.From, (char)(c - 1)));
list.Insert(index+1, new CharRange((char)(c + 1), range.To));
}
}

public void Remove(char from, char to)
{
var index = 0;
while (index < list.Count && from > list[index].To)
index++;
if (index >= list.Count)
return;
var range = list[index];
if (from > range.To)
return; //nothing to do
list.RemoveAt(index);
if (range.From <= from - 1)
{
list.Insert(index, new CharRange(range.From, (char)(from - 1)));
index++;
while (index < list.Count && to < list[index].From)
list.RemoveAt(index);
if (index < list.Count)
{
range = list[index];
list.RemoveAt(index);
list.Insert(index, new CharRange((char)(to + 1), range.To));
}
}
}

private void TryMergeRange(int index)
{
var current = list[index];
Expand All @@ -98,7 +164,7 @@ private void TryMergeRange(int index)
}
}
}

public IEnumerator<CharRange> GetEnumerator()
{
return list.GetEnumerator();
Expand Down
15 changes: 0 additions & 15 deletions Lexer/Features/CharSet_add.feature

This file was deleted.

2 changes: 1 addition & 1 deletion Lexer/Lexer.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net471</TargetFramework>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Main/Main.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>net471</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Lexer\Lexer.csproj" />
Expand Down
17 changes: 17 additions & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net471</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Features\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Lexer\Lexer.csproj" />
</ItemGroup>
</Project>
Loading

0 comments on commit 79b3e1d

Please sign in to comment.