2020-10-28 06:39:26 +11:00
|
|
|
|
using System.Collections.Generic;
|
2020-10-26 01:07:59 +11:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2020-11-05 17:33:04 +11:00
|
|
|
|
namespace UnityExplorer.Console.Lexer
|
2020-10-26 01:07:59 +11:00
|
|
|
|
{
|
2020-11-11 00:16:01 +11:00
|
|
|
|
// I use two different KeywordMatch instances (valid and invalid).
|
|
|
|
|
// This class just contains common implementations.
|
|
|
|
|
public class KeywordMatch : Matcher
|
2020-10-26 01:07:59 +11:00
|
|
|
|
{
|
2020-11-11 00:16:01 +11:00
|
|
|
|
public string[] Keywords;
|
2020-10-26 01:07:59 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
public override Color HighlightColor => highlightColor;
|
2020-10-26 01:07:59 +11:00
|
|
|
|
public Color highlightColor;
|
|
|
|
|
|
|
|
|
|
private readonly HashSet<string> shortlist = new HashSet<string>();
|
|
|
|
|
private readonly Stack<string> removeList = new Stack<string>();
|
|
|
|
|
|
2020-11-11 00:16:01 +11:00
|
|
|
|
public override bool IsImplicitMatch(CSharpLexer lexer)
|
2020-10-26 01:07:59 +11:00
|
|
|
|
{
|
2020-11-13 21:39:25 +11:00
|
|
|
|
if (!char.IsWhiteSpace(lexer.Previous) &&
|
|
|
|
|
!lexer.IsSpecialSymbol(lexer.Previous, DelimiterType.End))
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2020-10-26 01:07:59 +11:00
|
|
|
|
return false;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
2020-10-26 01:07:59 +11:00
|
|
|
|
|
|
|
|
|
shortlist.Clear();
|
|
|
|
|
|
|
|
|
|
int currentIndex = 0;
|
|
|
|
|
char currentChar = lexer.ReadNext();
|
|
|
|
|
|
2020-11-11 00:16:01 +11:00
|
|
|
|
for (int i = 0; i < Keywords.Length; i++)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2020-11-11 00:16:01 +11:00
|
|
|
|
if (Keywords[i][0] == currentChar)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2020-11-11 00:16:01 +11:00
|
|
|
|
shortlist.Add(Keywords[i]);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-26 01:07:59 +11:00
|
|
|
|
|
|
|
|
|
if (shortlist.Count == 0)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2020-10-26 01:07:59 +11:00
|
|
|
|
return false;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
2020-10-26 01:07:59 +11:00
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
if (lexer.EndOfStream)
|
|
|
|
|
{
|
|
|
|
|
RemoveLongStrings(currentIndex + 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentChar = lexer.ReadNext();
|
|
|
|
|
currentIndex++;
|
|
|
|
|
|
|
|
|
|
if (char.IsWhiteSpace(currentChar) ||
|
2020-11-11 00:16:01 +11:00
|
|
|
|
lexer.IsSpecialSymbol(currentChar, DelimiterType.Start))
|
2020-10-26 01:07:59 +11:00
|
|
|
|
{
|
|
|
|
|
RemoveLongStrings(currentIndex);
|
|
|
|
|
lexer.Rollback(1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (string keyword in shortlist)
|
|
|
|
|
{
|
2020-10-27 00:54:08 +11:00
|
|
|
|
if (currentIndex >= keyword.Length || keyword[currentIndex] != currentChar)
|
2020-10-26 01:07:59 +11:00
|
|
|
|
{
|
|
|
|
|
removeList.Push(keyword);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (removeList.Count > 0)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2020-10-26 01:07:59 +11:00
|
|
|
|
shortlist.Remove(removeList.Pop());
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
2020-10-26 01:07:59 +11:00
|
|
|
|
}
|
|
|
|
|
while (shortlist.Count > 0);
|
|
|
|
|
|
|
|
|
|
return shortlist.Count > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RemoveLongStrings(int length)
|
|
|
|
|
{
|
|
|
|
|
foreach (string keyword in shortlist)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2020-10-26 01:07:59 +11:00
|
|
|
|
if (keyword.Length > length)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2020-10-26 01:07:59 +11:00
|
|
|
|
removeList.Push(keyword);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-26 01:07:59 +11:00
|
|
|
|
|
|
|
|
|
while (removeList.Count > 0)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2020-10-26 01:07:59 +11:00
|
|
|
|
shortlist.Remove(removeList.Pop());
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
2020-10-26 01:07:59 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|