2021-05-10 15:58:49 +10:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2021-05-12 20:48:56 +10:00
|
|
|
|
namespace UnityExplorer.UI.CSConsole.Lexers
|
2021-05-10 15:58:49 +10:00
|
|
|
|
{
|
|
|
|
|
public class SymbolLexer : Lexer
|
|
|
|
|
{
|
2021-05-11 01:43:08 +10:00
|
|
|
|
// silver
|
|
|
|
|
protected override Color HighlightColor => new Color(0.6f, 0.6f, 0.6f);
|
2021-05-10 15:58:49 +10:00
|
|
|
|
|
|
|
|
|
// all symbols are delimiters
|
2021-05-11 01:43:08 +10:00
|
|
|
|
public override IEnumerable<char> Delimiters => symbols;
|
2021-05-10 15:58:49 +10:00
|
|
|
|
|
2021-05-11 19:15:46 +10:00
|
|
|
|
public static bool IsSymbol(char c) => symbols.Contains(c);
|
|
|
|
|
|
|
|
|
|
public static readonly HashSet<char> symbols = new HashSet<char>
|
2021-05-10 15:58:49 +10:00
|
|
|
|
{
|
2021-05-11 01:43:08 +10:00
|
|
|
|
'[', '{', '(', // open
|
|
|
|
|
']', '}', ')', // close
|
|
|
|
|
'.', ',', ';', ':', '?', '@', // special
|
|
|
|
|
|
|
|
|
|
// operators
|
|
|
|
|
'+', '-', '*', '/', '%', '&', '|', '^', '~', '=', '<', '>', '!',
|
2021-05-10 15:58:49 +10:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public override bool TryMatchCurrent(LexerBuilder lexer)
|
|
|
|
|
{
|
|
|
|
|
// previous character must be delimiter, whitespace, or alphanumeric.
|
|
|
|
|
if (!lexer.IsDelimiter(lexer.Previous, true, true))
|
|
|
|
|
return false;
|
|
|
|
|
|
2021-05-11 19:15:46 +10:00
|
|
|
|
if (IsSymbol(lexer.Current))
|
2021-05-10 15:58:49 +10:00
|
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
lexer.Commit();
|
|
|
|
|
lexer.PeekNext();
|
|
|
|
|
}
|
2021-05-11 19:15:46 +10:00
|
|
|
|
while (IsSymbol(lexer.Current));
|
2021-05-10 15:58:49 +10:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|