Skip to content

Commit

Permalink
Version 0.2.0. Added support for significant whitespace parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Konard committed Dec 3, 2019
1 parent b24bf4c commit e33f016
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 35 deletions.
86 changes: 57 additions & 29 deletions Platform.Communication.Protocol.Lino.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,63 @@ public static void SignificantWhitespaceTest()
{
var source = @"
users
user1
id
43
name
first
John
last
Williams
location
New York
age
23
user2
id
56
name
first
Igor
middle
Petrovich
last
Ivanov
location
Moscow
age
20";



user1
id
43
name
first
John
last
Williams
location
New York
age
23
user2
id
56
name
first
Igor
middle
Petrovich
last
Ivanov
location
Moscow
age
20";
var target = @"(users)
(users user1)
((users user1) id)
(((users user1) id) 43)
((users user1) name)
(((users user1) name) first)
((((users user1) name) first) John)
(((users user1) name) last)
((((users user1) name) last) Williams)
((users user1) location)
(((users user1) location) (New York))
((users user1) age)
(((users user1) age) 23)
(users user2)
((users user2) id)
(((users user2) id) 56)
((users user2) name)
(((users user2) name) first)
((((users user2) name) first) Igor)
(((users user2) name) middle)
((((users user2) name) middle) Petrovich)
(((users user2) name) last)
((((users user2) name) last) Ivanov)
((users user2) location)
(((users user2) location) Moscow)
((users user2) age)
(((users user2) age) 20)";
var parser = new Parser();
var links = parser.Parse(source);
var formattedLinks = links.Format();
Assert.Equal(formattedLinks, target);
}
}
}
59 changes: 59 additions & 0 deletions Platform.Communication.Protocol.Lino/Link.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public IList<Link> Values
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Link(IList<Link> values) : this(null, values) { }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Link(params Link[] values) : this(null, values) { }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Link(string id) : this(id, null) { }

Expand All @@ -57,6 +60,62 @@ public string GetValuesString()
return sb.ToString();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Link AddDependency(Link dependency)
{
if (Values.IsNullOrEmpty())
{
return new Link(new Link(dependency), this);
}
else
{
var firstValue = Values[0];
if (firstValue.Id == null)
{
var newValues = new List<Link>();
newValues.Add(firstValue.AddDependency(dependency));
newValues.AddSkipFirst(Values);
return new Link(newValues);
}
else
{
if (Values.Count > 1)
{
return new Link(new Link(dependency), new Link(Values));
}
else
{
var newValues = new List<Link>();
newValues.Add(new Link(dependency));
newValues.AddAll(Values);
return new Link(newValues);
}
}
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Link Simplify()
{
if (Values.IsNullOrEmpty())
{
return this;
}
else if (Values.Count == 1)
{
return Values[0];
}
else
{
var newValues = new Link[Values.Count];
for (int i = 0; i < Values.Count; i++)
{
newValues[i] = Values[i].Simplify();
}
return new Link(Id, newValues);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string GetValueString(Link value) => value.ToLinkOrIdString();

Expand Down
14 changes: 11 additions & 3 deletions Platform.Communication.Protocol.Lino/Parser.peg
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
@namespace Platform.Communication.Protocol.Lino
@classname Parser
links <IList<Link>> = _ list:linkAndWhitespace* { list }
linkAndWhitespace <Link> = l:anyLink _ { l }
@using System.Linq
document <IList<Link>> = #{ state["Indentation"] = 0; } _ l:links eof { l.SelectMany(x => x).Select(x => x.Simplify()).ToList() }
links <IList<IList<Link>>> = list:line+ { list }
line <IList<Link>> = INDENTATION l:element { l }
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 } }
identityOrLink <Link> = l:multiLineAnyLink { l } / i:identity { i }
anyLink <Link> = multiLineAnyLink / singleLineAnyLink
multiLineAnyLink <Link> = multiLinePointLink / multiLineValueLink / multiLineLink
singleLineAnyLink <Link> = singleLineLink / singleLineValueLink / singleLinePointLink
singleLineAnyLink <Link> = singleLineLink / v:singleLineValueLink { v.Values.Count == 1 ? v.Values.First() : v } / singleLinePointLink
multiLineValueAndWhitespace <Link> = value:identityOrLink _ { value }
multiLineValues <IList<Link>> = _ list:multiLineValueAndWhitespace+ { list }
singleLineValueAndWhitespace <Link> = value:identityOrLink __ { value }
Expand All @@ -18,5 +21,10 @@ pointLink <Link> = id:(identity) { new Link(id) }
singleLinePointLink<Link> = __ l:pointLink __ { l }
multiLinePointLink<Link> = "(" _ l:pointLink _ ")" { l }
identity <string> = "" [0-9a-zA-Zа-яёА-ЯЁ]+
INDENTATION = spaces:" "* &{ spaces.Count == state["Indentation"] }
INDENT = #{ state["Indentation"] += 4; }
UNDENT = #{ state["Indentation"] -= 4; }
eol = __ ("" [\r\n]+ / eof)
eof = !.
__ = [ \t]*
_ = [ \t\n\r]*
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>LinksPlatform's Platform.Communication.Protocol.Lino Class Library</Description>
<Copyright>Konstantin Diachenko</Copyright>
<AssemblyTitle>Platform.Communication.Protocol.Lino</AssemblyTitle>
<VersionPrefix>0.1.0</VersionPrefix>
<VersionPrefix>0.2.0</VersionPrefix>
<Authors>Konstantin Diachenko</Authors>
<TargetFrameworks>net471;netstandard2.0;netstandard2.1</TargetFrameworks>
<AssemblyName>Platform.Communication.Protocol.Lino</AssemblyName>
Expand All @@ -24,8 +24,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<LangVersion>latest</LangVersion>
<PackageReleaseNotes>Added single line links definitions to support the notation with less parentheses.
Issues fix.</PackageReleaseNotes>
<PackageReleaseNotes>Added support for significant whitespace parsing.</PackageReleaseNotes>
</PropertyGroup>

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

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.6.0" />
<PackageReference Include="Pegasus" Version="4.1.0" />
<PackageReference Include="Platform.Collections" Version="0.1.0" />
</ItemGroup>
Expand Down

0 comments on commit e33f016

Please sign in to comment.