From d746b3e691dc2a9853d5937d62d7ccaeb937deec Mon Sep 17 00:00:00 2001 From: Konstantin Diachenko Date: Sun, 1 Dec 2024 13:27:14 +0530 Subject: [PATCH] Add support for empty link (with no id and values) --- csharp/Platform.Protocols.Lino/Link.cs | 17 +++++++++++++---- csharp/Platform.Protocols.Lino/Parser.peg | 6 +++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/csharp/Platform.Protocols.Lino/Link.cs b/csharp/Platform.Protocols.Lino/Link.cs index 4bd4025..d7ba455 100644 --- a/csharp/Platform.Protocols.Lino/Link.cs +++ b/csharp/Platform.Protocols.Lino/Link.cs @@ -30,14 +30,19 @@ public Link(params Link[] 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++) { @@ -80,6 +85,10 @@ public Link Simplify() public static string EscapeReference(string reference) { + if (string.IsNullOrWhiteSpace(reference)) + { + return ""; + } if ( reference.Contains(":") || reference.Contains("(") || diff --git a/csharp/Platform.Protocols.Lino/Parser.peg b/csharp/Platform.Protocols.Lino/Parser.peg index 814f298..3ca28f7 100644 --- a/csharp/Platform.Protocols.Lino/Parser.peg +++ b/csharp/Platform.Protocols.Lino/Parser.peg @@ -11,13 +11,13 @@ anyLink > = ml:multiLineAnyLink eol { ml } / sl:singleLineAnyLink { multiLineAnyLink > = multiLinePointLink / multiLineValueLink / multiLineLink singleLineAnyLink > = fl:singleLineLink eol { fl } / pl:singleLinePointLink eol { pl } / vl:singleLineValueLink eol { vl } multiLineValueAndWhitespace > = value:referenceOrLink _ { value } -multiLineValues >> = _ list:multiLineValueAndWhitespace+ { list } +multiLineValues >> = _ list:multiLineValueAndWhitespace* { list } singleLineValueAndWhitespace > = __ value:referenceOrLink { value } singleLineValues >> = list:singleLineValueAndWhitespace+ { list } singleLineLink > = __ id:(reference) __ ":" v:singleLineValues { new Link(id, v) } -multiLineLink > = "(" _ id:(reference) _ ":" v:multiLineValues ")" { new Link(id, v) } +multiLineLink > = "(" _ id:(reference) _ ":" v:multiLineValues _ ")" { new Link(id, v) } singleLineValueLink > = v:singleLineValues { new Link(v) } -multiLineValueLink > = "(" v:multiLineValues ")" { new Link(v) } +multiLineValueLink > = "(" v:multiLineValues _ ")" { new Link(v) } pointLink > = id:(reference) { new Link(id) } singleLinePointLink> = __ l:pointLink { l } multiLinePointLink> = "(" _ l:pointLink _ ")" { l }