mirror of
https://github.com/GrahamKracker/UnityExplorer.git
synced 2025-07-01 11:12:49 +08:00
Rewrite lexer from scratch
This commit is contained in:
59
src/UI/CSConsole/Lexer/StringLexer.cs
Normal file
59
src/UI/CSConsole/Lexer/StringLexer.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityExplorer.UI.CSharpConsole.Lexers
|
||||
{
|
||||
public class StringLexer : Lexer
|
||||
{
|
||||
protected override Color HighlightColor => new Color(0.79f, 0.52f, 0.32f, 1.0f);
|
||||
|
||||
public override IEnumerable<char> Delimiters => new[] { '"' };
|
||||
|
||||
public override bool TryMatchCurrent(LexerBuilder lexer)
|
||||
{
|
||||
if (lexer.Current != '"')
|
||||
return false;
|
||||
|
||||
if (lexer.Previous == '@')
|
||||
{
|
||||
// verbatim string, continue until un-escaped quote.
|
||||
while (!lexer.EndOfInput)
|
||||
{
|
||||
lexer.Commit();
|
||||
if (lexer.PeekNext() == '"')
|
||||
{
|
||||
lexer.Commit();
|
||||
// possibly the end, check for escaped quotes.
|
||||
// commit the character and flip the escape bool for each quote.
|
||||
bool escaped = false;
|
||||
while (lexer.PeekNext() == '"')
|
||||
{
|
||||
lexer.Commit();
|
||||
escaped = !escaped;
|
||||
}
|
||||
// if the last quote wasnt escaped, that was the end of the string.
|
||||
if (!escaped)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// normal string
|
||||
// continue until a quote which is not escaped, or end of input
|
||||
|
||||
while (!lexer.EndOfInput)
|
||||
{
|
||||
lexer.Commit();
|
||||
if (lexer.PeekNext() == '"' && lexer.Previous != '\\')
|
||||
{
|
||||
lexer.Commit();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user