Files
Convention-CSharp/Convention/[Symbolization]/Runner/SymbolizationReader.cs

138 lines
4.0 KiB
C#
Raw Normal View History

2025-07-03 21:50:26 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Convention.Symbolization.Internal
{
public class SymbolizationReader
{
2025-07-04 21:39:02 +08:00
#region Read Script Words
2025-07-03 21:50:26 +08:00
private string buffer = "";
2025-07-04 21:39:02 +08:00
private int lineContext = 1;
2025-07-07 00:28:37 +08:00
private int wordContext = 0;
2025-07-03 21:50:26 +08:00
private bool isOutOfStringline = true;
2025-07-07 00:28:37 +08:00
public List<Variable> ScriptWords = new();
2025-07-03 21:50:26 +08:00
2025-07-07 00:28:37 +08:00
private readonly static HashSet<char> ControllerCharSet = new()
2025-07-03 21:50:26 +08:00
{
'{','}','(',')','[',']',
'+','-','*','/','%',
'=',
'!','<','>','&','|',
'^',
':',',','.','?',/*'\'','"',*/
'@','#','$',
2025-07-04 21:39:02 +08:00
';'
2025-07-03 21:50:26 +08:00
};
public void ReadChar(char ch)
{
if (isOutOfStringline)
{
if (ControllerCharSet.Contains(ch))
CompleteSingleSymbol(ch);
2025-07-04 21:39:02 +08:00
else if (ch == '\n')
CompleteLine();
else if (char.IsWhiteSpace(ch) || ch == '\r' || ch == '\t')
2025-07-03 21:50:26 +08:00
CompleteWord();
else if (buffer.Length == 0 && ch == '"')
BeginString();
else if (char.IsLetter(ch) || char.IsDigit(ch))
PushChar(ch);
else
2025-07-04 21:39:02 +08:00
throw new NotImplementedException($"{lineContext + 1} line, {buffer} + <{ch}> not implemented");
2025-07-03 21:50:26 +08:00
}
else
{
if ((buffer.Length > 0 && buffer.Last() == '\\') || ch != '"')
PushChar(ch);
else if (ch == '"')
EndString();
else
2025-07-04 21:39:02 +08:00
throw new NotImplementedException($"{lineContext + 1} line, \"{buffer}\" + \'{ch}\' not implemented");
2025-07-03 21:50:26 +08:00
}
}
void CompleteSingleSymbol(char ch)
{
CompleteWord();
buffer = ch.ToString();
CompleteWord();
}
void PushChar(char ch)
{
buffer += ch;
}
void CompleteWord()
{
if (buffer.Length > 0)
{
2025-07-04 21:39:02 +08:00
if (Internal.Keyword.Keywords.TryGetValue(buffer, out var keyword))
{
2025-07-07 00:28:37 +08:00
ScriptWords.Add(keyword.CloneVariable(buffer, lineContext, wordContext));
2025-07-04 21:39:02 +08:00
}
else
{
2025-07-07 00:28:37 +08:00
ScriptWords.Add(new ScriptWordVariable(buffer, lineContext, wordContext + 1));
2025-07-04 21:39:02 +08:00
}
2025-07-03 21:50:26 +08:00
buffer = "";
2025-07-07 00:28:37 +08:00
wordContext++;
2025-07-03 21:50:26 +08:00
}
}
void CompleteLine()
{
CompleteWord();
2025-07-07 00:28:37 +08:00
lineContext++;
wordContext = 0;
2025-07-03 21:50:26 +08:00
}
void BeginString()
{
if (buffer.Length > 0)
throw new InvalidGrammarException($"String must start with nothing: {buffer}");
isOutOfStringline = false;
}
void EndString()
{
isOutOfStringline = true;
CompleteWord();
}
2025-07-04 21:39:02 +08:00
#endregion
#region Read Scope Words
2025-07-07 00:28:37 +08:00
public Dictionary<Keyword, List<Variable>> ScopeWords = new();
public Keyword currentKey = null;
public bool isNextKeyword = true;
2025-07-07 00:28:37 +08:00
public void ReadWord(Variable word)
2025-07-04 21:39:02 +08:00
{
2025-07-07 00:28:37 +08:00
if (isNextKeyword)
2025-07-04 21:39:02 +08:00
{
2025-07-07 00:28:37 +08:00
if (word is Keyword cky)
2025-07-04 21:39:02 +08:00
{
2025-07-07 00:28:37 +08:00
currentKey = cky;
ScopeWords.Add(currentKey, new());
isNextKeyword = false;
2025-07-04 21:39:02 +08:00
}
2025-07-07 00:28:37 +08:00
else
{
throw new InvalidGrammarException(
$"Line {word.SymbolInfo.LineIndex}, Word {word.SymbolInfo.WordIndex}: Expected a keyword, but got {word}"
);
}
}
else
{
ScopeWords[currentKey].Add(word);
isNextKeyword = currentKey.ControlScope(word) == false;
2025-07-04 21:39:02 +08:00
}
}
#endregion
2025-07-03 21:50:26 +08:00
}
}