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

Commit 79b3e1d

Browse files
committed
start creating tests and features
1 parent b6b57cc commit 79b3e1d

10 files changed

+453
-35
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
/Lexer/obj
44
/Main/bin
55
/Main/obj
6+
/TestResults
7+
/Tests/obj
8+
/Tests/bin
9+
/.vs

CQLLexer.sln

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lexer", "Lexer\Lexer.csproj
44
EndProject
55
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Main", "Main\Main.csproj", "{FFBF79FD-6B1E-4E02-B086-8BCD2ABAC14B}"
66
EndProject
7+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{61123303-1217-4491-B7E0-ED2CF0C918CE}"
8+
EndProject
79
Global
810
GlobalSection(SolutionConfigurationPlatforms) = preSolution
911
Debug|Any CPU = Debug|Any CPU
@@ -18,5 +20,9 @@ Global
1820
{FFBF79FD-6B1E-4E02-B086-8BCD2ABAC14B}.Debug|Any CPU.Build.0 = Debug|Any CPU
1921
{FFBF79FD-6B1E-4E02-B086-8BCD2ABAC14B}.Release|Any CPU.ActiveCfg = Release|Any CPU
2022
{FFBF79FD-6B1E-4E02-B086-8BCD2ABAC14B}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{61123303-1217-4491-B7E0-ED2CF0C918CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{61123303-1217-4491-B7E0-ED2CF0C918CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{61123303-1217-4491-B7E0-ED2CF0C918CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{61123303-1217-4491-B7E0-ED2CF0C918CE}.Release|Any CPU.Build.0 = Release|Any CPU
2127
EndGlobalSection
2228
EndGlobal

Lexer/Automaton/CharSet.cs

+77-11
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ public class CharSet: IEnumerable<CharRange>
1010
{
1111
private readonly List<CharRange> list = new List<CharRange>();
1212

13+
public int Length { get { return list.Sum(r => r.Count()); } }
14+
1315
public bool Contains(char c)
1416
{
1517
var index = 0;
16-
while (index < list.Count && list[index].From < c)
18+
while (index < list.Count && c > list[index].To)
1719
index++;
1820
if (index >= list.Count)
1921
return false;
@@ -24,7 +26,7 @@ public bool Contains(char c)
2426
public bool Contains(char from, char to)
2527
{
2628
var index = 0;
27-
while (index < list.Count && list[index].From < from)
29+
while (index < list.Count && from > list[index].To)
2830
index++;
2931
if (index >= list.Count)
3032
return false;
@@ -35,7 +37,7 @@ public bool Contains(char from, char to)
3537
public void Add(char c)
3638
{
3739
var index = 0;
38-
while (index < list.Count && list[index].From < c)
40+
while (index < list.Count && c > list[index].To)
3941
index++;
4042
if (index >= list.Count)
4143
{
@@ -47,16 +49,23 @@ public void Add(char c)
4749
var range = list[index];
4850
if(c >= range.From && c <= range.To)
4951
return; //already included
50-
//c must be greater than range.To
51-
list.Insert(index+1, new CharRange(c));
52-
TryMergeRange(index+1);
52+
if (c > range.To)
53+
{
54+
list.Insert(index + 1, new CharRange(c));
55+
TryMergeRange(index + 1);
56+
}
57+
else // c < range.From
58+
{
59+
list.Insert(index, new CharRange(c));
60+
TryMergeRange(index);
61+
}
5362
}
5463
}
5564

5665
public void Add(char from, char to)
5766
{
5867
var index = 0;
59-
while (index < list.Count && list[index].From < from)
68+
while (index < list.Count && from > list[index].To)
6069
index++;
6170
if (index >= list.Count)
6271
{
@@ -66,14 +75,71 @@ public void Add(char from, char to)
6675
else
6776
{
6877
var fromIndex = index;
69-
while (index < list.Count && list[index].To < to)
78+
while (index < list.Count && from > list[index].To)
7079
index++;
71-
list.RemoveRange(fromIndex, index-fromIndex+1);
80+
if(index < list.Count)
81+
{
82+
if (from > list[index].From)
83+
{
84+
from = list[index].From;
85+
list.RemoveRange(fromIndex, index - fromIndex + 1);
86+
}
87+
}
7288
list.Insert(fromIndex, new CharRange(from, to));
7389
TryMergeRange(fromIndex);
7490
}
7591
}
76-
92+
93+
public void Remove(char c)
94+
{
95+
var index = 0;
96+
while (index < list.Count && c > list[index].To)
97+
index++;
98+
if (index >= list.Count)
99+
return;
100+
var range = list[index];
101+
if (c < range.From || c > range.To)
102+
return;
103+
list.RemoveAt(index);
104+
if (range.From == range.To)
105+
;
106+
else if (range.From == c)
107+
list.Insert(index, new CharRange((char)(c+1), range.To));
108+
else if (range.To == c)
109+
list.Insert(index, new CharRange(range.From, (char)(c - 1)));
110+
else
111+
{
112+
list.Insert(index, new CharRange(range.From, (char)(c - 1)));
113+
list.Insert(index+1, new CharRange((char)(c + 1), range.To));
114+
}
115+
}
116+
117+
public void Remove(char from, char to)
118+
{
119+
var index = 0;
120+
while (index < list.Count && from > list[index].To)
121+
index++;
122+
if (index >= list.Count)
123+
return;
124+
var range = list[index];
125+
if (from > range.To)
126+
return; //nothing to do
127+
list.RemoveAt(index);
128+
if (range.From <= from - 1)
129+
{
130+
list.Insert(index, new CharRange(range.From, (char)(from - 1)));
131+
index++;
132+
while (index < list.Count && to < list[index].From)
133+
list.RemoveAt(index);
134+
if (index < list.Count)
135+
{
136+
range = list[index];
137+
list.RemoveAt(index);
138+
list.Insert(index, new CharRange((char)(to + 1), range.To));
139+
}
140+
}
141+
}
142+
77143
private void TryMergeRange(int index)
78144
{
79145
var current = list[index];
@@ -98,7 +164,7 @@ private void TryMergeRange(int index)
98164
}
99165
}
100166
}
101-
167+
102168
public IEnumerator<CharRange> GetEnumerator()
103169
{
104170
return list.GetEnumerator();

Lexer/Features/CharSet_add.feature

-15
This file was deleted.

Lexer/Lexer.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>netstandard2.0</TargetFramework>
3+
<TargetFramework>net471</TargetFramework>
44
</PropertyGroup>
55
</Project>

Main/Main.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
4-
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<TargetFramework>net471</TargetFramework>
55
</PropertyGroup>
66
<ItemGroup>
77
<ProjectReference Include="..\Lexer\Lexer.csproj" />

Tests/Tests.csproj

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net471</TargetFramework>
4+
<IsPackable>false</IsPackable>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
8+
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
9+
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
10+
</ItemGroup>
11+
<ItemGroup>
12+
<Folder Include="Features\" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\Lexer\Lexer.csproj" />
16+
</ItemGroup>
17+
</Project>

0 commit comments

Comments
 (0)