chore: canonicalize method names of token stream.

This commit is contained in:
redxef 2024-10-26 23:45:13 +02:00
parent 7dc3dd01ae
commit 953927bc27
Signed by: redxef
GPG key ID: 7DAC3AA211CBD921
3 changed files with 61 additions and 47 deletions

View file

@ -269,7 +269,7 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
} }
public Expression parse() { public Expression parse() {
Token<string> token = _sts.get(); Token<string> token = _sts.Get();
switch (token) { switch (token) {
case GroupingToken gt: case GroupingToken gt:
return parse_grouping(gt, gt.closing_value); return parse_grouping(gt, gt.closing_value);
@ -287,14 +287,14 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
Debug.Assert(start.value == end.value); Debug.Assert(start.value == end.value);
Debug.Assert("'\"".Contains(start.value)); Debug.Assert("'\"".Contains(start.value));
string r = ""; string r = "";
while (_sts.available() > 0) { while (_sts.Available() > 0) {
Token<string> t = _sts.get(); Token<string> t = _sts.Get();
if (t.value == end.value) { if (t.value == end.value) {
break; break;
} }
r += t.value; r += t.value;
} }
_sts.commit(); _sts.Commit();
return new String(r); return new String(r);
} }
@ -303,13 +303,13 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
return parse_string(start, end); return parse_string(start, end);
} }
IList<Expression> expressions = new List<Expression>(); IList<Expression> expressions = new List<Expression>();
while (_sts.available() > 0) { while (_sts.Available() > 0) {
Token<string> t = _sts.get(); Token<string> t = _sts.Get();
if (t.value == end.value) { if (t.value == end.value) {
_sts.commit(); _sts.Commit();
break; break;
} }
_sts.rewind(1); _sts.Rewind(1);
expressions.Add(parse()); expressions.Add(parse());
} }
return new List(expressions); return new List(expressions);
@ -318,7 +318,7 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
Expression parse_atom(AtomToken at) { Expression parse_atom(AtomToken at) {
int parsed_value; int parsed_value;
if (int.TryParse(at.value, out parsed_value)) { if (int.TryParse(at.value, out parsed_value)) {
_sts.commit(); _sts.Commit();
return new Integer(parsed_value); return new Integer(parsed_value);
} }
if (at.value.Equals("t")) { if (at.value.Equals("t")) {
@ -327,19 +327,19 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
if (at.value.Equals("nil")) { if (at.value.Equals("nil")) {
return new Boolean(false); return new Boolean(false);
} }
_sts.commit(); _sts.Commit();
return new Symbol(at.value); return new Symbol(at.value);
} }
Expression parse_operator(OperatorToken ot) { Expression parse_operator(OperatorToken ot) {
string v = ot.value; string v = ot.value;
while (_sts.available() > 0) { while (_sts.Available() > 0) {
Token<string> t = _sts.get(); Token<string> t = _sts.Get();
if (t is OperatorToken ot_) { if (t is OperatorToken ot_) {
v += ot_.value; v += ot_.value;
continue; continue;
} }
_sts.rewind(1); _sts.Rewind(1);
break; break;
} }
return new Symbol(v); return new Symbol(v);

View file

@ -24,10 +24,10 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
class SpaceToken : Token<string> { class SpaceToken : Token<string> {
private SpaceToken(string value) : base(value) {} private SpaceToken(string value) : base(value) {}
private static IToken<string>? take(CharStream program) { private static IToken<string>? take(CharStream program) {
if (program.available() == 0) { if (program.Available() == 0) {
return null; return null;
} }
if (program.get() == ' ') { if (program.Get() == ' ') {
return new SpaceToken(" "); return new SpaceToken(" ");
} }
return null; return null;
@ -37,10 +37,10 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
class GroupingToken: Token<string> { class GroupingToken: Token<string> {
private GroupingToken(string value) : base(value) {} private GroupingToken(string value) : base(value) {}
private static IToken<string>? take(CharStream program) { private static IToken<string>? take(CharStream program) {
if (program.available() == 0) { if (program.Available() == 0) {
return null; return null;
} }
char t = program.get(); char t = program.Get();
if ("()\"'".Contains(t)) { if ("()\"'".Contains(t)) {
return new GroupingToken(t.ToString()); return new GroupingToken(t.ToString());
} }
@ -61,13 +61,13 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
private AtomToken(string value) : base(value) {} private AtomToken(string value) : base(value) {}
private static IToken<string>? take(CharStream program) { private static IToken<string>? take(CharStream program) {
string value = ""; string value = "";
while (program.available() > 0) { while (program.Available() > 0) {
char t = program.get(); char t = program.Get();
if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".Contains(t)) { if (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".Contains(t)) {
if (value.Equals("")) { if (value.Equals("")) {
return null; return null;
} }
program.rewind(1); program.Rewind(1);
return new AtomToken(value); return new AtomToken(value);
} }
value += t; value += t;
@ -79,10 +79,10 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
class OperatorToken : Token<string> { class OperatorToken : Token<string> {
private OperatorToken(string value) : base(value) {} private OperatorToken(string value) : base(value) {}
private static IToken<string>? take(CharStream program) { private static IToken<string>? take(CharStream program) {
if (program.available() == 0) { if (program.Available() == 0) {
return null; return null;
} }
return new OperatorToken(program.get().ToString()); return new OperatorToken(program.Get().ToString());
//char t = program.get(); //char t = program.get();
//if ("+-*/%".Contains(t)) { //if ("+-*/%".Contains(t)) {
// return new OperatorToken(t.ToString()); // return new OperatorToken(t.ToString());
@ -108,12 +108,12 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
IList<Token<string>> result = new List<Token<string>>(); IList<Token<string>> result = new List<Token<string>>();
int prev_avail = 0; int prev_avail = 0;
while (true) { while (true) {
if (prev_avail == program.available() && prev_avail == 0) { if (prev_avail == program.Available() && prev_avail == 0) {
break; break;
} else if (prev_avail == program.available()) { } else if (prev_avail == program.Available()) {
throw new ApplicationException("Program is invalid"); throw new ApplicationException("Program is invalid");
} }
prev_avail = program.available(); prev_avail = program.Available();
foreach (Type c in _classes) { foreach (Type c in _classes) {
Token<string>? t = (Token<string>?) c.GetMethod( Token<string>? t = (Token<string>?) c.GetMethod(
"take", "take",
@ -127,10 +127,10 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
new object[]{program} new object[]{program}
); );
if (t == null) { if (t == null) {
program.rewind(); program.Rewind();
continue; continue;
} }
program.commit(); program.Commit();
result.Add(t); result.Add(t);
break; break;
} }

View file

@ -1,47 +1,61 @@
namespace Jellyfin.Plugin.SmartPlaylist.Util { namespace Jellyfin.Plugin.SmartPlaylist.Util {
public interface IStream<T> { public interface IStream<T> {
int available(); int Available();
T get(); T Get();
int commit(); int Commit();
int rewind(); int Rewind();
int rewind(int n); int Rewind(int n);
int Consumed();
IStream<T> Copy();
} }
public class Stream<T> : IStream<T> { public class Stream<T> : IStream<T> {
private readonly IList<T> _items; private readonly IList<T> _items;
private int _cursor; private int _cursor;
private int _ephemeral_cursor; private int _ephemeralCursor;
protected Stream(IList<T> items) { protected Stream(IList<T> items) {
_items = items; _items = items;
_cursor = 0; _cursor = 0;
_ephemeral_cursor = 0; _ephemeralCursor = 0;
} }
public int available() { private Stream(IList<T> items, int cursor, int ephemeralCursor) {
return _items.Count - _ephemeral_cursor; _items = items;
_cursor = cursor;
_ephemeralCursor = ephemeralCursor;
} }
public T get() {
return _items[_ephemeral_cursor++]; public int Available() {
return _items.Count - _ephemeralCursor;
} }
public int commit() { public T Get() {
int diff = _ephemeral_cursor - _cursor; return _items[_ephemeralCursor++];
_cursor = _ephemeral_cursor; }
public int Commit() {
int diff = Consumed();
_cursor = _ephemeralCursor;
return diff; return diff;
} }
public int rewind() { public int Rewind() {
int diff = _ephemeral_cursor - _cursor; int diff = Consumed();
_ephemeral_cursor = _cursor; _ephemeralCursor = _cursor;
return diff; return diff;
} }
public int rewind(int n) { public int Rewind(int n) {
int diff = _ephemeral_cursor - _cursor; int diff = Consumed();
if (diff < n) { if (diff < n) {
n = diff; n = diff;
} }
_ephemeral_cursor -= n; _ephemeralCursor -= n;
return n; return n;
} }
public int Consumed() {
return _ephemeralCursor - _cursor;
}
public IStream<T> Copy() {
return new Stream<T>(_items, _cursor, _ephemeralCursor);
}
} }
} }