Skip to content

Commit

Permalink
Add support for empty link (with no id and values)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konard committed Dec 1, 2024
1 parent 5aeb5ce commit d746b3e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
17 changes: 13 additions & 4 deletions csharp/Platform.Protocols.Lino/Link.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ public Link(params Link<TLinkAddress>[] values) : this(default!, values) { }
public Link(TLinkAddress id) : this(id, default!) { }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string ToString() => Values.IsNullOrEmpty() ? $"({EscapeReference(Id.ToString())})" : GetLinkValuesString();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private string GetLinkValuesString() => Id == null ? $"({GetValuesString()})" : $"({EscapeReference(Id.ToString())}: {GetValuesString()})";
public override string ToString() => Id == null ?
$"({GetValuesString()})" :
Values.IsNullOrEmpty() ?
$"({EscapeReference(Id.ToString())})" :
$"({EscapeReference(Id.ToString())}: {GetValuesString()})";

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string GetValuesString()
{
if (Values.IsNullOrEmpty())
{
return "";
}
var sb = new StringBuilder();
for (int i = 0; i < Values.Count; i++)
{
Expand Down Expand Up @@ -80,6 +85,10 @@ public Link<TLinkAddress> Simplify()

public static string EscapeReference(string reference)
{
if (string.IsNullOrWhiteSpace(reference))
{
return "";
}
if (
reference.Contains(":") ||
reference.Contains("(") ||
Expand Down
6 changes: 3 additions & 3 deletions csharp/Platform.Protocols.Lino/Parser.peg
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ anyLink <Link<string>> = ml:multiLineAnyLink eol { ml } / sl:singleLineAnyLink {
multiLineAnyLink <Link<string>> = multiLinePointLink / multiLineValueLink / multiLineLink
singleLineAnyLink <Link<string>> = fl:singleLineLink eol { fl } / pl:singleLinePointLink eol { pl } / vl:singleLineValueLink eol { vl }
multiLineValueAndWhitespace <Link<string>> = value:referenceOrLink _ { value }
multiLineValues <IList<Link<string>>> = _ list:multiLineValueAndWhitespace+ { list }
multiLineValues <IList<Link<string>>> = _ list:multiLineValueAndWhitespace* { list }
singleLineValueAndWhitespace <Link<string>> = __ value:referenceOrLink { value }
singleLineValues <IList<Link<string>>> = list:singleLineValueAndWhitespace+ { list }
singleLineLink <Link<string>> = __ id:(reference) __ ":" v:singleLineValues { new Link<string>(id, v) }
multiLineLink <Link<string>> = "(" _ id:(reference) _ ":" v:multiLineValues ")" { new Link<string>(id, v) }
multiLineLink <Link<string>> = "(" _ id:(reference) _ ":" v:multiLineValues _ ")" { new Link<string>(id, v) }
singleLineValueLink <Link<string>> = v:singleLineValues { new Link<string>(v) }
multiLineValueLink <Link<string>> = "(" v:multiLineValues ")" { new Link<string>(v) }
multiLineValueLink <Link<string>> = "(" v:multiLineValues _ ")" { new Link<string>(v) }
pointLink <Link<string>> = id:(reference) { new Link<string>(id) }
singleLinePointLink<Link<string>> = __ l:pointLink { l }
multiLinePointLink<Link<string>> = "(" _ l:pointLink _ ")" { l }
Expand Down

0 comments on commit d746b3e

Please sign in to comment.