Skip to content

Commit 4c36c8e

Browse files
committed
Version 0.2.1.
Using a more reliable way to convert groups to links. Added LinksGroup struct and ILinksGroupListExtensions class. Link struct: * Added Combine method. * Removed AddDependency method.
1 parent c0c87b3 commit 4c36c8e

File tree

5 files changed

+117
-40
lines changed

5 files changed

+117
-40
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections.Generic;
2+
using System.Runtime.CompilerServices;
3+
4+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
5+
6+
namespace Platform.Communication.Protocol.Lino
7+
{
8+
public static class ILinksGroupListExtensions
9+
{
10+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11+
public static List<Link> ToLinksList(this IList<LinksGroup> groups)
12+
{
13+
var list = new List<Link>();
14+
for (var i = 0; i < groups.Count; i++)
15+
{
16+
groups[i].AppendToLinksList(list);
17+
}
18+
return list;
19+
}
20+
}
21+
}

Platform.Communication.Protocol.Lino/Link.cs

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,40 +60,6 @@ public string GetValuesString()
6060
return sb.ToString();
6161
}
6262

63-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
64-
public Link AddDependency(Link dependency)
65-
{
66-
if (Values.IsNullOrEmpty())
67-
{
68-
return new Link(new Link(dependency), this);
69-
}
70-
else
71-
{
72-
var firstValue = Values[0];
73-
if (firstValue.Id == null)
74-
{
75-
var newValues = new List<Link>();
76-
newValues.Add(firstValue.AddDependency(dependency));
77-
newValues.AddSkipFirst(Values);
78-
return new Link(newValues);
79-
}
80-
else
81-
{
82-
if (Values.Count > 1)
83-
{
84-
return new Link(new Link(dependency), new Link(Values));
85-
}
86-
else
87-
{
88-
var newValues = new List<Link>();
89-
newValues.Add(new Link(dependency));
90-
newValues.AddAll(Values);
91-
return new Link(newValues);
92-
}
93-
}
94-
}
95-
}
96-
9763
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9864
public Link Simplify()
9965
{
@@ -116,6 +82,9 @@ public Link Simplify()
11682
}
11783
}
11884

85+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
86+
public Link Combine(Link other) => new Link(this, other);
87+
11988
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12089
public static string GetValueString(Link value) => value.ToLinkOrIdString();
12190

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
4+
using Platform.Collections;
5+
using Platform.Collections.Lists;
6+
7+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
8+
9+
namespace Platform.Communication.Protocol.Lino
10+
{
11+
public struct LinksGroup : IEquatable<LinksGroup>
12+
{
13+
public Link Link
14+
{
15+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
16+
get;
17+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18+
set;
19+
}
20+
21+
public IList<LinksGroup> Groups
22+
{
23+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
24+
get;
25+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
26+
set;
27+
}
28+
29+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
30+
public LinksGroup(Link link, IList<LinksGroup> groups)
31+
{
32+
Link = link;
33+
Groups = groups;
34+
}
35+
36+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
37+
public LinksGroup(Link link) : this(link, null) { }
38+
39+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40+
public static implicit operator List<Link>(LinksGroup value) => value.ToLinksList();
41+
42+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
43+
public List<Link> ToLinksList()
44+
{
45+
var list = new List<Link>();
46+
AppendToLinksList(list);
47+
return list;
48+
}
49+
50+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
51+
public void AppendToLinksList(List<Link> list) => AppendToLinksList(list, Link, this);
52+
53+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
54+
public static void AppendToLinksList(List<Link> list, Link dependency, LinksGroup group)
55+
{
56+
list.Add(dependency);
57+
var groups = group.Groups;
58+
if (!groups.IsNullOrEmpty())
59+
{
60+
for (int i = 0; i < groups.Count; i++)
61+
{
62+
var innerGroup = groups[i];
63+
AppendToLinksList(list, dependency.Combine(innerGroup.Link), innerGroup);
64+
}
65+
}
66+
}
67+
68+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
69+
public override bool Equals(object obj) => obj is LinksGroup linksGroup ? Equals(linksGroup) : false;
70+
71+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
72+
public override int GetHashCode() => (Link, Groups.GenerateHashCode()).GetHashCode();
73+
74+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
75+
public bool Equals(LinksGroup other) => Link == other.Link && Groups.EqualTo(other.Groups);
76+
77+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
78+
public static bool operator ==(LinksGroup left, LinksGroup right) => left.Equals(right);
79+
80+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
81+
public static bool operator !=(LinksGroup left, LinksGroup right) => !(left == right);
82+
}
83+
}

Platform.Communication.Protocol.Lino/Parser.peg

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
@namespace Platform.Communication.Protocol.Lino
22
@classname Parser
33
@using System.Linq
4-
document <IList<Link>> = #{ state["Indentation"] = 0; } _ l:links eof { l.SelectMany(x => x).Select(x => x.Simplify()).ToList() }
5-
links <IList<IList<Link>>> = list:line+ { list }
6-
line <IList<Link>> = INDENTATION l:element { l }
7-
element <IList<Link>> = e:anyLink eol INDENT l:links UNDENT { new Link[] { e }.Concat(l.SelectMany(x => x).Select(y => y.AddDependency(e))).ToList() } / e:anyLink eol { new Link[] { e } }
4+
document <IList<Link>> = #{ state["Indentation"] = 0; } _ l:links eof { l.ToLinksList() }
5+
links <IList<LinksGroup>> = list:line+ { list }
6+
line <LinksGroup> = INDENTATION l:element { l }
7+
element <LinksGroup> = e:anyLink eol INDENT l:links UNDENT { new LinksGroup(e, l) } / e:anyLink eol { new LinksGroup(e) }
88
identityOrLink <Link> = l:multiLineAnyLink { l } / i:identity { i }
99
anyLink <Link> = multiLineAnyLink / singleLineAnyLink
1010
multiLineAnyLink <Link> = multiLinePointLink / multiLineValueLink / multiLineLink

Platform.Communication.Protocol.Lino/Platform.Communication.Protocol.Lino.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<Description>LinksPlatform's Platform.Communication.Protocol.Lino Class Library</Description>
55
<Copyright>Konstantin Diachenko</Copyright>
66
<AssemblyTitle>Platform.Communication.Protocol.Lino</AssemblyTitle>
7-
<VersionPrefix>0.2.0</VersionPrefix>
7+
<VersionPrefix>0.2.1</VersionPrefix>
88
<Authors>Konstantin Diachenko</Authors>
99
<TargetFrameworks>net471;netstandard2.0;netstandard2.1</TargetFrameworks>
1010
<AssemblyName>Platform.Communication.Protocol.Lino</AssemblyName>
@@ -24,7 +24,11 @@
2424
<IncludeSymbols>true</IncludeSymbols>
2525
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2626
<LangVersion>latest</LangVersion>
27-
<PackageReleaseNotes>Added support for significant whitespace parsing.</PackageReleaseNotes>
27+
<PackageReleaseNotes>Using a more reliable way to convert groups to links.
28+
Added LinksGroup struct and ILinksGroupListExtensions class.
29+
Link struct:
30+
* Added Combine method.
31+
* Removed AddDependency method.</PackageReleaseNotes>
2832
</PropertyGroup>
2933

3034
<ItemGroup Condition="$(TargetFramework.StartsWith('net4')) AND '$(MSBuildRuntimeType)' == 'Core' AND '$(OS)' != 'Windows_NT'">

0 commit comments

Comments
 (0)