45 lines
1.3 KiB
C#
Raw Normal View History

2021-05-10 15:58:49 +10:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace UnityExplorer.UI.CSharpConsole.Lexers
{
public class SymbolLexer : Lexer
{
protected override Color HighlightColor => Color.white;
// all symbols are delimiters
public override IEnumerable<char> Delimiters => uniqueSymbols;
// all symbol combinations are made of valid individual symbols.
private readonly HashSet<char> uniqueSymbols = new HashSet<char>
{
'[', ']', '{', '}', '(', ')', ',', '.', ';', ':',
'+', '-', '*', '/', '%', '&', '|', '^', '~', '=',
'<', '>', '?', '!', '@'
};
public override bool TryMatchCurrent(LexerBuilder lexer)
{
// previous character must be delimiter, whitespace, or alphanumeric.
if (!lexer.IsDelimiter(lexer.Previous, true, true))
return false;
if (uniqueSymbols.Contains(lexer.Current))
{
do
{
lexer.Commit();
lexer.PeekNext();
}
while (uniqueSymbols.Contains(lexer.Current));
return true;
}
return false;
}
}
}