2021-05-11 01:43:08 +10:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2021-06-30 07:49:58 +10:00
|
|
|
|
namespace UnityExplorer.CSConsole.Lexers
|
2021-05-11 01:43:08 +10:00
|
|
|
|
{
|
|
|
|
|
public class KeywordLexer : Lexer
|
|
|
|
|
{
|
|
|
|
|
// system blue
|
|
|
|
|
protected override Color HighlightColor => new Color(0.33f, 0.61f, 0.83f, 1.0f);
|
|
|
|
|
|
2021-05-12 20:48:56 +10:00
|
|
|
|
public static readonly HashSet<string> keywords = new HashSet<string>
|
2021-05-11 01:43:08 +10:00
|
|
|
|
{
|
|
|
|
|
// reserved keywords
|
2021-06-05 19:36:09 +10:00
|
|
|
|
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue",
|
2021-05-11 01:43:08 +10:00
|
|
|
|
"decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally",
|
2021-06-05 19:36:09 +10:00
|
|
|
|
"fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock",
|
|
|
|
|
"long", "namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", "public",
|
|
|
|
|
"readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct", "switch",
|
|
|
|
|
"this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using", "virtual", "void",
|
2021-05-11 01:43:08 +10:00
|
|
|
|
"volatile", "while",
|
|
|
|
|
// contextual keywords
|
|
|
|
|
"add", "and", "alias", "ascending", "async", "await", "by", "descending", "dynamic", "equals", "from", "get",
|
|
|
|
|
"global", "group", "init", "into", "join", "let", "managed", "nameof", "not", "notnull", "on",
|
|
|
|
|
"or", "orderby", "partial", "record", "remove", "select", "set", "unmanaged", "value", "var", "when", "where",
|
|
|
|
|
"where", "with", "yield", "nint", "nuint"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public override bool TryMatchCurrent(LexerBuilder lexer)
|
|
|
|
|
{
|
|
|
|
|
var prev = lexer.Previous;
|
|
|
|
|
var first = lexer.Current;
|
|
|
|
|
|
|
|
|
|
// check for keywords
|
|
|
|
|
if (lexer.IsDelimiter(prev, true) && char.IsLetter(first))
|
|
|
|
|
{
|
|
|
|
|
// can be a keyword...
|
|
|
|
|
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
sb.Append(lexer.Current);
|
|
|
|
|
while (!lexer.EndOfInput && char.IsLetter(lexer.PeekNext()))
|
|
|
|
|
sb.Append(lexer.Current);
|
|
|
|
|
|
2021-06-01 16:00:52 +10:00
|
|
|
|
// next must be whitespace or delimiter
|
|
|
|
|
if (!lexer.EndOfInput && !(char.IsWhiteSpace(lexer.Current) || lexer.IsDelimiter(lexer.Current)))
|
|
|
|
|
return false;
|
|
|
|
|
|
2021-05-11 01:43:08 +10:00
|
|
|
|
if (keywords.Contains(sb.ToString()))
|
|
|
|
|
{
|
|
|
|
|
if (!lexer.EndOfInput)
|
|
|
|
|
lexer.RollbackBy(1);
|
|
|
|
|
lexer.Commit();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|