Skip to content

Commit e33f016

Browse files
committed
Version 0.2.0. Added support for significant whitespace parsing.
1 parent b24bf4c commit e33f016

File tree

4 files changed

+130
-35
lines changed

4 files changed

+130
-35
lines changed

Platform.Communication.Protocol.Lino.Tests/ParserTests.cs

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,63 @@ public static void SignificantWhitespaceTest()
3636
{
3737
var source = @"
3838
users
39-
user1
40-
id
41-
43
42-
name
43-
first
44-
John
45-
last
46-
Williams
47-
location
48-
New York
49-
age
50-
23
51-
user2
52-
id
53-
56
54-
name
55-
first
56-
Igor
57-
middle
58-
Petrovich
59-
last
60-
Ivanov
61-
location
62-
Moscow
63-
age
64-
20";
65-
66-
67-
39+
user1
40+
id
41+
43
42+
name
43+
first
44+
John
45+
last
46+
Williams
47+
location
48+
New York
49+
age
50+
23
51+
user2
52+
id
53+
56
54+
name
55+
first
56+
Igor
57+
middle
58+
Petrovich
59+
last
60+
Ivanov
61+
location
62+
Moscow
63+
age
64+
20";
65+
var target = @"(users)
66+
(users user1)
67+
((users user1) id)
68+
(((users user1) id) 43)
69+
((users user1) name)
70+
(((users user1) name) first)
71+
((((users user1) name) first) John)
72+
(((users user1) name) last)
73+
((((users user1) name) last) Williams)
74+
((users user1) location)
75+
(((users user1) location) (New York))
76+
((users user1) age)
77+
(((users user1) age) 23)
78+
(users user2)
79+
((users user2) id)
80+
(((users user2) id) 56)
81+
((users user2) name)
82+
(((users user2) name) first)
83+
((((users user2) name) first) Igor)
84+
(((users user2) name) middle)
85+
((((users user2) name) middle) Petrovich)
86+
(((users user2) name) last)
87+
((((users user2) name) last) Ivanov)
88+
((users user2) location)
89+
(((users user2) location) Moscow)
90+
((users user2) age)
91+
(((users user2) age) 20)";
92+
var parser = new Parser();
93+
var links = parser.Parse(source);
94+
var formattedLinks = links.Format();
95+
Assert.Equal(formattedLinks, target);
6896
}
6997
}
7098
}

Platform.Communication.Protocol.Lino/Link.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public IList<Link> Values
3333
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3434
public Link(IList<Link> values) : this(null, values) { }
3535

36+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
37+
public Link(params Link[] values) : this(null, values) { }
38+
3639
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3740
public Link(string id) : this(id, null) { }
3841

@@ -57,6 +60,62 @@ public string GetValuesString()
5760
return sb.ToString();
5861
}
5962

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+
97+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
98+
public Link Simplify()
99+
{
100+
if (Values.IsNullOrEmpty())
101+
{
102+
return this;
103+
}
104+
else if (Values.Count == 1)
105+
{
106+
return Values[0];
107+
}
108+
else
109+
{
110+
var newValues = new Link[Values.Count];
111+
for (int i = 0; i < Values.Count; i++)
112+
{
113+
newValues[i] = Values[i].Simplify();
114+
}
115+
return new Link(Id, newValues);
116+
}
117+
}
118+
60119
[MethodImpl(MethodImplOptions.AggressiveInlining)]
61120
public static string GetValueString(Link value) => value.ToLinkOrIdString();
62121

Platform.Communication.Protocol.Lino/Parser.peg

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
@namespace Platform.Communication.Protocol.Lino
22
@classname Parser
3-
links <IList<Link>> = _ list:linkAndWhitespace* { list }
4-
linkAndWhitespace <Link> = l:anyLink _ { l }
3+
@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 } }
58
identityOrLink <Link> = l:multiLineAnyLink { l } / i:identity { i }
69
anyLink <Link> = multiLineAnyLink / singleLineAnyLink
710
multiLineAnyLink <Link> = multiLinePointLink / multiLineValueLink / multiLineLink
8-
singleLineAnyLink <Link> = singleLineLink / singleLineValueLink / singleLinePointLink
11+
singleLineAnyLink <Link> = singleLineLink / v:singleLineValueLink { v.Values.Count == 1 ? v.Values.First() : v } / singleLinePointLink
912
multiLineValueAndWhitespace <Link> = value:identityOrLink _ { value }
1013
multiLineValues <IList<Link>> = _ list:multiLineValueAndWhitespace+ { list }
1114
singleLineValueAndWhitespace <Link> = value:identityOrLink __ { value }
@@ -18,5 +21,10 @@ pointLink <Link> = id:(identity) { new Link(id) }
1821
singleLinePointLink<Link> = __ l:pointLink __ { l }
1922
multiLinePointLink<Link> = "(" _ l:pointLink _ ")" { l }
2023
identity <string> = "" [0-9a-zA-Zа-яёА-ЯЁ]+
24+
INDENTATION = spaces:" "* &{ spaces.Count == state["Indentation"] }
25+
INDENT = #{ state["Indentation"] += 4; }
26+
UNDENT = #{ state["Indentation"] -= 4; }
27+
eol = __ ("" [\r\n]+ / eof)
28+
eof = !.
2129
__ = [ \t]*
2230
_ = [ \t\n\r]*

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

Lines changed: 3 additions & 3 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.1.0</VersionPrefix>
7+
<VersionPrefix>0.2.0</VersionPrefix>
88
<Authors>Konstantin Diachenko</Authors>
99
<TargetFrameworks>net471;netstandard2.0;netstandard2.1</TargetFrameworks>
1010
<AssemblyName>Platform.Communication.Protocol.Lino</AssemblyName>
@@ -24,8 +24,7 @@
2424
<IncludeSymbols>true</IncludeSymbols>
2525
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2626
<LangVersion>latest</LangVersion>
27-
<PackageReleaseNotes>Added single line links definitions to support the notation with less parentheses.
28-
Issues fix.</PackageReleaseNotes>
27+
<PackageReleaseNotes>Added support for significant whitespace parsing.</PackageReleaseNotes>
2928
</PropertyGroup>
3029

3130
<ItemGroup Condition="$(TargetFramework.StartsWith('net4')) AND '$(MSBuildRuntimeType)' == 'Core' AND '$(OS)' != 'Windows_NT'">
@@ -37,6 +36,7 @@ Issues fix.</PackageReleaseNotes>
3736
</ItemGroup>
3837

3938
<ItemGroup>
39+
<PackageReference Include="Microsoft.CSharp" Version="4.6.0" />
4040
<PackageReference Include="Pegasus" Version="4.1.0" />
4141
<PackageReference Include="Platform.Collections" Version="0.1.0" />
4242
</ItemGroup>

0 commit comments

Comments
 (0)