2024-06-27 01:47:44 +02:00
|
|
|
using System.Diagnostics;
|
2024-11-07 00:48:56 +01:00
|
|
|
using Jellyfin.Plugin.SmartPlaylist.Lisp;
|
2024-06-27 01:47:44 +02:00
|
|
|
|
|
|
|
namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
|
|
|
|
public class Parser {
|
|
|
|
private StringTokenStream _sts;
|
|
|
|
public Parser(StringTokenStream tokens) {
|
|
|
|
_sts = tokens;
|
|
|
|
}
|
2024-10-27 00:51:54 +02:00
|
|
|
public Parser(string s) {
|
|
|
|
_sts = StringTokenStream.generate(s);
|
|
|
|
}
|
2024-06-27 01:47:44 +02:00
|
|
|
|
|
|
|
public Expression parse() {
|
2024-10-26 23:45:13 +02:00
|
|
|
Token<string> token = _sts.Get();
|
2024-06-27 01:47:44 +02:00
|
|
|
switch (token) {
|
|
|
|
case GroupingToken gt:
|
|
|
|
return parse_grouping(gt, gt.closing_value);
|
|
|
|
case AtomToken at:
|
|
|
|
return parse_atom(at);
|
|
|
|
case SpaceToken sp:
|
|
|
|
return parse();
|
|
|
|
}
|
|
|
|
return parse();
|
|
|
|
}
|
|
|
|
|
2024-11-07 00:48:56 +01:00
|
|
|
Expression parse_string(GroupingToken start, GroupingToken? end) {
|
|
|
|
Debug.Assert(end != null);
|
2024-06-29 18:29:40 +02:00
|
|
|
Debug.Assert(start.value == end.value);
|
2024-11-07 00:48:56 +01:00
|
|
|
Debug.Assert("\"".Contains(start.value));
|
2024-06-27 01:47:44 +02:00
|
|
|
string r = "";
|
2024-10-26 23:45:13 +02:00
|
|
|
while (_sts.Available() > 0) {
|
|
|
|
Token<string> t = _sts.Get();
|
2024-06-29 18:29:40 +02:00
|
|
|
if (t.value == end.value) {
|
2024-06-27 01:47:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
r += t.value;
|
|
|
|
}
|
2024-10-26 23:45:13 +02:00
|
|
|
_sts.Commit();
|
2024-06-27 01:47:44 +02:00
|
|
|
return new String(r);
|
|
|
|
}
|
|
|
|
|
2024-11-07 00:48:56 +01:00
|
|
|
Expression parse_grouping(GroupingToken start, GroupingToken? end) {
|
|
|
|
Debug.Assert(end != null);
|
|
|
|
if ("\"".Contains(start.value)) {
|
2024-06-29 18:29:40 +02:00
|
|
|
return parse_string(start, end);
|
|
|
|
}
|
2024-06-27 01:47:44 +02:00
|
|
|
IList<Expression> expressions = new List<Expression>();
|
2024-10-26 23:45:13 +02:00
|
|
|
while (_sts.Available() > 0) {
|
|
|
|
Token<string> t = _sts.Get();
|
2024-06-29 18:29:40 +02:00
|
|
|
if (t.value == end.value) {
|
2024-10-26 23:45:13 +02:00
|
|
|
_sts.Commit();
|
2024-06-27 01:47:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2024-10-26 23:45:13 +02:00
|
|
|
_sts.Rewind(1);
|
2024-06-27 01:47:44 +02:00
|
|
|
expressions.Add(parse());
|
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
return Cons.FromList(expressions);
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Expression parse_atom(AtomToken at) {
|
|
|
|
int parsed_value;
|
|
|
|
if (int.TryParse(at.value, out parsed_value)) {
|
2024-10-26 23:45:13 +02:00
|
|
|
_sts.Commit();
|
2024-06-27 01:47:44 +02:00
|
|
|
return new Integer(parsed_value);
|
|
|
|
}
|
|
|
|
if (at.value.Equals("t")) {
|
2024-11-07 00:48:56 +01:00
|
|
|
return Boolean.TRUE;
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
|
|
|
if (at.value.Equals("nil")) {
|
2024-11-07 00:48:56 +01:00
|
|
|
return Boolean.FALSE;
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-10-26 23:45:13 +02:00
|
|
|
_sts.Commit();
|
2024-06-27 01:47:44 +02:00
|
|
|
return new Symbol(at.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|