48 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
{
// 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
public override IEnumerable<char> Delimiters => symbols;
2021-05-10 15:58:49 +10:00
private readonly HashSet<char> symbols = new HashSet<char>
2021-05-10 15:58:49 +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;
if (symbols.Contains(lexer.Current))
2021-05-10 15:58:49 +10:00
{
do
{
lexer.Commit();
lexer.PeekNext();
}
while (symbols.Contains(lexer.Current));
2021-05-10 15:58:49 +10:00
return true;
}
return false;
}
}
}