From 87dd4358d8e677eb7b318dd399ed50732fc74569 Mon Sep 17 00:00:00 2001 From: redxef Date: Wed, 18 Dec 2024 00:51:40 +0100 Subject: [PATCH] feat: parser, ignore comments. --- .../Lisp/Compiler/Parser.cs | 16 ++++++++++++++++ .../Lisp/Compiler/TokenStream.cs | 4 +++- Tests/Tests.cs | 10 ++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Jellyfin.Plugin.SmartPlaylist/Lisp/Compiler/Parser.cs b/Jellyfin.Plugin.SmartPlaylist/Lisp/Compiler/Parser.cs index 3452fee..97e6918 100644 --- a/Jellyfin.Plugin.SmartPlaylist/Lisp/Compiler/Parser.cs +++ b/Jellyfin.Plugin.SmartPlaylist/Lisp/Compiler/Parser.cs @@ -40,6 +40,19 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler { return new String(r); } + Expression parse_comment(GroupingToken start, GroupingToken? end) { + Debug.Assert(end != null); + Debug.Assert(";".Contains(start.value)); + while (_sts.Available() > 0) { + Token t = _sts.Get(); + if (t.value == end.value) { + break; + } + } + _sts.Commit(); + return parse(); + } + Expression parse_quote(GroupingToken start, GroupingToken? end) { Debug.Assert(end == null); Debug.Assert("'".Contains(start.value)); @@ -53,6 +66,9 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler { if ("'".Contains(start.value)) { return parse_quote(start, end); } + if (";".Contains(start.value)) { + return parse_comment(start, end); + } Debug.Assert(end != null); IList expressions = new List(); while (_sts.Available() > 0) { diff --git a/Jellyfin.Plugin.SmartPlaylist/Lisp/Compiler/TokenStream.cs b/Jellyfin.Plugin.SmartPlaylist/Lisp/Compiler/TokenStream.cs index d29f90c..d334f0d 100644 --- a/Jellyfin.Plugin.SmartPlaylist/Lisp/Compiler/TokenStream.cs +++ b/Jellyfin.Plugin.SmartPlaylist/Lisp/Compiler/TokenStream.cs @@ -43,7 +43,7 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler { return null; } char t = program.Get(); - if ("()\"'".Contains(t)) { + if ("()\"';".Contains(t)) { return new GroupingToken(t.ToString()); } return null; @@ -55,6 +55,8 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler { return null; } else if (_value == "'") { return null; + } else if (_value == ";") { + return new GroupingToken("\n"); } return new GroupingToken(_value); } diff --git a/Tests/Tests.cs b/Tests/Tests.cs index 71ea7d5..da690c0 100644 --- a/Tests/Tests.cs +++ b/Tests/Tests.cs @@ -76,6 +76,16 @@ namespace Tests sts = StringTokenStream.generate(program); p = new Parser(sts); Assert.Equal(program, string.Format("{0}", p.parse())); + + program = """ + (begin ;this too + ;;; this is a comment + t + ) + """; + sts = StringTokenStream.generate(program); + p = new Parser(sts); + Assert.Equal("(begin t)", string.Format("{0}", p.parse())); } [Fact]