Reformatting and reimperilment escape codes

This commit is contained in:
Stone_Red
2023-09-27 17:17:28 +02:00
parent 79df5c9b97
commit ce3715c1b5
9 changed files with 591 additions and 543 deletions
+18 -20
View File
@@ -4,14 +4,15 @@ using System.IO;
using YesNt.Interpreter.Runtime; using YesNt.Interpreter.Runtime;
namespace YesNt.CodeEditor namespace YesNt.CodeEditor;
internal class TextEditor
{ {
internal class TextEditor
{
private readonly InputHandler inputHandler; private readonly InputHandler inputHandler;
private readonly SyntaxHighlighter syntaxHighlighter; private readonly SyntaxHighlighter syntaxHighlighter;
private readonly List<string> debugOutput = new(); private readonly List<string> debugOutput = new();
private readonly Point oldSize = new Point(0, 0);
public YesNtInterpreter YesNtInterpreter { get; } = new(); public YesNtInterpreter YesNtInterpreter { get; } = new();
public int LineOffset { get; set; } = 0; public int LineOffset { get; set; } = 0;
public List<string> Lines { get; } = new(); public List<string> Lines { get; } = new();
@@ -177,6 +178,14 @@ namespace YesNt.CodeEditor
} }
} }
public int GetSpacing()
{
int padding = (Console.WindowHeight + LineOffset - 3).ToString().Length;
padding = Math.Max(padding, Lines.Count.ToString().Length);
padding += 1;
return padding;
}
private void YesNtInterpreter_OnDebugOutput(string output) private void YesNtInterpreter_OnDebugOutput(string output)
{ {
debugOutput.Add(output); debugOutput.Add(output);
@@ -225,16 +234,6 @@ namespace YesNt.CodeEditor
} }
} }
public int GetSpacing()
{
int padding = (Console.WindowHeight + LineOffset - 3).ToString().Length;
padding = Math.Max(padding, Lines.Count.ToString().Length);
padding += 1;
return padding;
}
private readonly Point oldSize = new Point(0, 0);
private bool SizeChanged() private bool SizeChanged()
{ {
if (oldSize.X != Console.WindowWidth || oldSize.Y != Console.WindowHeight) if (oldSize.X != Console.WindowWidth || oldSize.Y != Console.WindowHeight)
@@ -245,10 +244,10 @@ namespace YesNt.CodeEditor
} }
return false; return false;
} }
} }
internal class Point internal class Point
{ {
public int X { get; set; } public int X { get; set; }
public int Y { get; set; } public int Y { get; set; }
@@ -257,12 +256,11 @@ namespace YesNt.CodeEditor
X = x; X = x;
Y = y; Y = y;
} }
} }
internal enum Mode internal enum Mode
{ {
Edit, Edit,
Command, Command,
Debug Debug
}
} }
+9 -5
View File
@@ -1,10 +1,10 @@
using System; using System;
using System.Text; using System.Text;
namespace YesNt.CodeEditor namespace YesNt.CodeEditor;
internal class InputHandler
{ {
internal class InputHandler
{
private readonly TextEditor textEditor; private readonly TextEditor textEditor;
public InputHandler(TextEditor textEditor) public InputHandler(TextEditor textEditor)
@@ -126,8 +126,13 @@ namespace YesNt.CodeEditor
textEditor.Lines[textEditor.CursorPosition.Y] = lineBuilder.ToString(); textEditor.Lines[textEditor.CursorPosition.Y] = lineBuilder.ToString();
if (keyInfo.Key == ConsoleKey.Backspace) if (keyInfo.Key is ConsoleKey.Backspace or ConsoleKey.Delete)
{ {
if (keyInfo.Key == ConsoleKey.Delete)
{
textEditor.CursorPosition.X++;
}
if (textEditor.CursorPosition.X > 0) if (textEditor.CursorPosition.X > 0)
{ {
if (textEditor.Lines[textEditor.CursorPosition.Y][textEditor.CursorPosition.X - 1] == ' ' && textEditor.CursorPosition.X > textEditor.Lines[textEditor.CursorPosition.Y].TrimEnd().Length) if (textEditor.Lines[textEditor.CursorPosition.Y][textEditor.CursorPosition.X - 1] == ' ' && textEditor.CursorPosition.X > textEditor.Lines[textEditor.CursorPosition.Y].TrimEnd().Length)
@@ -309,5 +314,4 @@ namespace YesNt.CodeEditor
Console.SetCursorPosition(0, Console.WindowHeight - 1); Console.SetCursorPosition(0, Console.WindowHeight - 1);
Console.Write(input + new string(' ', Console.WindowWidth - input.Length - 1)); Console.Write(input + new string(' ', Console.WindowWidth - input.Length - 1));
} }
}
} }
+4 -11
View File
@@ -1,11 +1,4 @@
namespace YesNt.CodeEditor using YesNt.CodeEditor;
{
internal static class Program TextEditor textEditor = args.Length > 0 ? new TextEditor(args[0]) : new TextEditor();
{ textEditor.Run();
private static void Main(string[] args)
{
TextEditor textEditor = args.Length > 0 ? new TextEditor(args[0]) : new TextEditor();
textEditor.Run();
}
}
}
+6 -7
View File
@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using YesNt.Interpreter.Enums; using YesNt.Interpreter.Enums;
@@ -11,10 +12,12 @@ namespace YesNt.CodeEditor;
internal partial class SyntaxHighlighter internal partial class SyntaxHighlighter
{ {
private readonly ReadOnlyCollection<StatementInformation> statementInformation; private readonly ReadOnlyCollection<StatementInformation> statementInformation;
private readonly string[] replacementValues;
public SyntaxHighlighter(ReadOnlyCollection<StatementInformation> statementInformation) public SyntaxHighlighter(ReadOnlyCollection<StatementInformation> statementInformation)
{ {
this.statementInformation = statementInformation; this.statementInformation = statementInformation;
replacementValues = StringExtentions.ReplacementRules.Values.ToArray();
} }
public static string Base64Encode(string plainText) public static string Base64Encode(string plainText)
@@ -39,10 +42,9 @@ internal partial class SyntaxHighlighter
} }
else else
{ {
MatchCollection matches = EscapeSequenceRegex().Matches(input); for (int i = 0; i < replacementValues.Length; i++)
for (int i = 0; i < matches.Count; i++)
{ {
input = AddColorInformation(input, matches[i].Value, ConsoleColor.DarkYellow, SearchMode.Contains); input = AddColorInformation(input, replacementValues[i], ConsoleColor.Blue, SearchMode.Contains);
} }
foreach (StatementInformation statement in statementInformation) foreach (StatementInformation statement in statementInformation)
@@ -110,7 +112,7 @@ internal partial class SyntaxHighlighter
} }
} }
matches = VariableRegex().Matches(input); MatchCollection matches = VariableRegex().Matches(input);
for (int i = 0; i < matches.Count; i++) for (int i = 0; i < matches.Count; i++)
{ {
input = AddColorInformation(input, matches[i].Value, ConsoleColor.Cyan, SearchMode.Contains); input = AddColorInformation(input, matches[i].Value, ConsoleColor.Cyan, SearchMode.Contains);
@@ -171,9 +173,6 @@ internal partial class SyntaxHighlighter
return reult; return reult;
} }
[GeneratedRegex("!!.")]
private static partial Regex EscapeSequenceRegex();
[GeneratedRegex("^<[a-zA-Z0-9]+")] [GeneratedRegex("^<[a-zA-Z0-9]+")]
private static partial Regex VariableDeclarationRegex(); private static partial Regex VariableDeclarationRegex();
+1 -2
View File
@@ -38,7 +38,6 @@ public class CodeFlowTests
[TestMethod] [TestMethod]
public void CalculationsTest() public void CalculationsTest()
{ {
Assert.Inconclusive(); YesNtAssert.IsLineEqual("10 * 10 !calc", 100.ToString());
YesNtAssert.IsLineEqual("10 * 10 !calc", 20.ToString());
} }
} }
+22
View File
@@ -60,4 +60,26 @@ internal static class YesNtAssert
Assert.AreEqual(expected, debugEventArgs.CurrentLine); Assert.AreEqual(expected, debugEventArgs.CurrentLine);
} }
public static void IsLineNotEqual(string line, string expected, int timeout = 1000)
{
AutoResetEvent onDone = new AutoResetEvent(false);
List<string> lines = new List<string>()
{
line
};
DebugEventArgs debugEventArgs = new DebugEventArgs();
yesNtInterpreter.OnLineExecuted += (er) =>
{
debugEventArgs = er ?? debugEventArgs;
_ = onDone.Set();
};
yesNtInterpreter.Execute(lines, true);
_ = onDone.WaitOne(TimeSpan.FromMilliseconds(timeout));
Assert.AreNotEqual(expected, debugEventArgs.CurrentLine);
}
} }
@@ -38,23 +38,6 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
RuntimeInfo.CurrentLine = args.FromSafeString(); RuntimeInfo.CurrentLine = args.FromSafeString();
} }
[Statement("!!", SearchMode.Contains, SpaceAround.None, ConsoleColor.DarkYellow, Priority = Priority.PreProcessing, KeepStatementInArgs = true)]
public void DontEvaluate(string args)
{
int index;
while ((index = args.IndexOf("!!")) != -1)
{
args = args.Remove(index, 2);
if (index < args.Length)
{
char charToEscape = args[index];
args = args.Remove(index, 1);
args = args.Insert(index, charToEscape.ToString().ToSafeString());
}
}
RuntimeInfo.CurrentLine = args;
}
[Statement("!task", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)] [Statement("!task", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)]
public void RunTask(string line) public void RunTask(string line)
{ {
@@ -77,9 +60,15 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
[Statement("slp", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta)] [Statement("slp", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta)]
public void Sleep(string args) public void Sleep(string args)
{ {
_ = int.TryParse(args, out int millisecondsTimeout); if (int.TryParse(args, out int millisecondsTimeout))
{
ConsoleExtentions.Sleep(millisecondsTimeout, RuntimeInfo); ConsoleExtentions.Sleep(millisecondsTimeout, RuntimeInfo);
} }
else
{
RuntimeInfo.Exit($"\"{args}\" is not a valid time-out value", true);
}
}
[Statement("len", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta)] [Statement("len", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta)]
public void Length(string args) public void Length(string args)
@@ -91,6 +91,11 @@ internal partial class VariableStatements : StatementRuntimeInformation
MatchCollection matches = VariableStatementRegex().Matches(RuntimeInfo.CurrentLine); MatchCollection matches = VariableStatementRegex().Matches(RuntimeInfo.CurrentLine);
if (matches.Count <= 0)
{
RuntimeInfo.Exit("Invalid syntax", true);
}
for (int i = 0; i < matches.Count; i++) for (int i = 0; i < matches.Count; i++)
{ {
string varName = matches[i].Value.Replace(">", string.Empty); string varName = matches[i].Value.Replace(">", string.Empty);
@@ -1,11 +1,34 @@
using System; using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization; using System.Globalization;
using System.Linq;
using System.Text; using System.Text;
namespace YesNt.Interpreter.Utilities; namespace YesNt.Interpreter.Utilities;
public static class StringExtentions public static class StringExtentions
{ {
private static readonly Dictionary<string, string> reverseReplacementRules;
public static Dictionary<string, string> ReplacementRules { get; } = new()
{
{"~", "~til" },
{" ", "~spc" },
{"%", "~per" },
{"<", "~let" },
{">", "~grt" },
{",", "~com" },
{"!", "~exm" },
{"|", "~pip" }
};
[SuppressMessage("Minor Code Smell", "S3963:\"static\" fields should be initialized inline", Justification = "Doesn't work because it throws a TypeInitializationException")]
static StringExtentions()
{
reverseReplacementRules = ReplacementRules.ToDictionary(x => x.Value, x => x.Key);
}
public static string ToSafeString(this string input) public static string ToSafeString(this string input)
{ {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@@ -13,16 +36,32 @@ public static class StringExtentions
{ {
_ = output.Append($"\v{c}\v"); _ = output.Append($"\v{c}\v");
} }
return output
.ToString() return ReplaceOnce(output.ToString(), ReplacementRules);
.Replace(' ', '~');
} }
public static string FromSafeString(this string input) public static string FromSafeString(this string input)
{ {
return input return ReplaceOnce(input.Replace("\v", ""), reverseReplacementRules);
.Replace("\v", "") }
.Replace('~', ' ');
public static string ReplaceOnce(string input, Dictionary<string, string> replacementRules)
{
IEnumerable<KeyValuePair<string, string>> matches = replacementRules.Where(rule => input.Contains(rule.Key));
if (!matches.Any())
{
return input;
}
KeyValuePair<string, string> match = matches.First();
int startIndex = input.IndexOf(match.Key);
int endIndex = startIndex + match.Key.Length;
string before = ReplaceOnce(input[..startIndex], replacementRules);
string replaced = match.Value;
string after = ReplaceOnce(input[endIndex..], replacementRules);
return before + replaced + after;
} }
public static bool ToStandardizedNumber(this string input, out double result) public static bool ToStandardizedNumber(this string input, out double result)