2021-03-11 12:56:25 +13:00
|
|
|
|
using System;
|
2021-03-11 16:18:05 +13:00
|
|
|
|
using System.Collections.Generic;
|
2021-03-15 17:28:20 +13:00
|
|
|
|
using System.Collections;
|
2021-03-11 12:56:25 +13:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2021-03-15 17:28:20 +13:00
|
|
|
|
using System.Collections.ObjectModel;
|
2021-03-15 20:58:52 +13:00
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2021-03-11 12:56:25 +13:00
|
|
|
|
|
|
|
|
|
namespace Assignment_1
|
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
2021-03-12 11:00:29 +13:00
|
|
|
|
[Flags]
|
2021-03-11 16:18:05 +13:00
|
|
|
|
enum VariableFlags
|
|
|
|
|
{
|
|
|
|
|
Empty = 0,
|
2021-03-11 16:39:15 +13:00
|
|
|
|
Reserved = 1,
|
2021-03-15 20:58:52 +13:00
|
|
|
|
NoPrint = 2,
|
|
|
|
|
Static = 4,
|
|
|
|
|
Undef = 8
|
2021-03-11 16:18:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This captures the end-point of each part of an expression (in the stream), to validate the syntax
|
|
|
|
|
/// Optionally also captures the parsed string for each expression.
|
|
|
|
|
///
|
|
|
|
|
/// For this program, a word is considered to be any non-whitespace value bounded by whitespace or the array boundary.
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
static Dictionary<string, Tuple<string, VariableFlags>> Symbols = new Dictionary<string, Tuple<string, VariableFlags>>
|
|
|
|
|
{
|
2021-03-11 16:39:15 +13:00
|
|
|
|
{ "SPACE", new Tuple<string, VariableFlags>(" ", VariableFlags.Reserved | VariableFlags.NoPrint) },
|
|
|
|
|
{ "TAB", new Tuple<string, VariableFlags>("\t", VariableFlags.Reserved | VariableFlags.NoPrint) },
|
|
|
|
|
{ "NEWLINE", new Tuple<string, VariableFlags>("\n", VariableFlags.Reserved | VariableFlags.NoPrint) },
|
|
|
|
|
{ "CARRIAGE_RETURN", new Tuple<string, VariableFlags>("\r", VariableFlags.Reserved | VariableFlags.NoPrint) }
|
2021-03-11 16:18:05 +13:00
|
|
|
|
};
|
2021-03-12 12:46:00 +13:00
|
|
|
|
|
|
|
|
|
static List<char> ForbiddenChars = new List<char>
|
|
|
|
|
{
|
|
|
|
|
'$', '\\', '\"', '\''
|
|
|
|
|
};
|
2021-03-15 15:03:36 +13:00
|
|
|
|
|
|
|
|
|
|
2021-03-11 12:56:25 +13:00
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
2021-03-11 19:22:49 +13:00
|
|
|
|
Console.WriteLine("┌──────────────────────────────────────────┐");
|
|
|
|
|
Console.WriteLine("│ 159.341 2021 Semester 1, Assignment 1 │");
|
|
|
|
|
Console.WriteLine("│ Submitted by Brychan Dempsey, 14299890 │");
|
|
|
|
|
Console.WriteLine("└──────────────────────────────────────────┘");
|
2021-03-11 12:56:25 +13:00
|
|
|
|
MemoryStream sourceStream = new MemoryStream(1024); // Creates a memory stream to retain source while being interpreted.
|
2021-03-11 16:18:05 +13:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-11 12:56:25 +13:00
|
|
|
|
Parser parser = new Parser();
|
|
|
|
|
bool dynamicInput = false;
|
|
|
|
|
// From https://stackoverflow.com/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected
|
|
|
|
|
// Reading from pipes is equivalent to reading user input, though the input is redirected
|
|
|
|
|
if (Console.IsInputRedirected)
|
|
|
|
|
{
|
|
|
|
|
sourceStream.Write(Encoding.UTF8.GetBytes(Console.In.ReadToEnd()));
|
|
|
|
|
sourceStream.Position = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-11 19:22:49 +13:00
|
|
|
|
sourceStream.Write(Encoding.UTF8.GetBytes("{ \r\n"));
|
2021-03-11 12:56:25 +13:00
|
|
|
|
sourceStream.Position = 0;
|
|
|
|
|
dynamicInput = true;
|
|
|
|
|
}
|
2021-03-11 16:18:05 +13:00
|
|
|
|
parser.StartParsing(sourceStream, dynamicInput);
|
2021-03-11 19:22:49 +13:00
|
|
|
|
|
|
|
|
|
Console.ReadLine();
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
|
|
|
|
public class Parser
|
|
|
|
|
{
|
2021-03-11 16:18:05 +13:00
|
|
|
|
public enum statements
|
2021-03-11 12:56:25 +13:00
|
|
|
|
{
|
2021-03-11 16:18:05 +13:00
|
|
|
|
exit,
|
|
|
|
|
append,
|
|
|
|
|
list,
|
|
|
|
|
print,
|
|
|
|
|
printlength,
|
|
|
|
|
printwords,
|
|
|
|
|
printwordcount,
|
|
|
|
|
set,
|
2021-03-12 12:14:51 +13:00
|
|
|
|
reverse,
|
2021-03-15 13:46:54 +13:00
|
|
|
|
h,
|
|
|
|
|
writeout
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
2021-03-11 16:18:05 +13:00
|
|
|
|
public void StartParsing(Stream source, bool dynamicInput = false)
|
2021-03-11 12:56:25 +13:00
|
|
|
|
{
|
2021-03-11 19:22:49 +13:00
|
|
|
|
if ((byte)source.ReadByte() == '{')
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (dynamicInput)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Enter a command: ");
|
|
|
|
|
string s = Console.ReadLine();
|
|
|
|
|
long pos = source.Position;
|
|
|
|
|
source.Write(Encoding.UTF8.GetBytes(s));
|
|
|
|
|
source.Position = pos;
|
|
|
|
|
int g = 0;
|
|
|
|
|
}
|
|
|
|
|
// parse the statement or list of statements;
|
|
|
|
|
// This is done by reading the next word
|
|
|
|
|
SkipWhitespace(source);
|
2021-03-15 13:46:54 +13:00
|
|
|
|
long initPos = source.Position;
|
2021-03-12 11:00:29 +13:00
|
|
|
|
long position = FindNextWord(source, out string word);
|
2021-03-15 20:58:52 +13:00
|
|
|
|
|
2021-03-11 19:22:49 +13:00
|
|
|
|
object statementType;
|
2021-03-15 20:58:52 +13:00
|
|
|
|
try
|
2021-03-11 19:22:49 +13:00
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
if (Enum.TryParse(typeof(statements), word, out statementType))
|
|
|
|
|
{
|
|
|
|
|
// By turning the result of the command into an action,
|
|
|
|
|
// we can defer processing the final result until the end of this control flow
|
|
|
|
|
// I.e. "I don't know what action to do, but I will need it, when I know where this statement ends"
|
|
|
|
|
|
|
|
|
|
// In some ways, it makes more sense. The action is determined by the interpreter's result
|
|
|
|
|
Action result = () => { };
|
|
|
|
|
source.Position = position;
|
|
|
|
|
switch ((statements)statementType)
|
|
|
|
|
{
|
|
|
|
|
case statements.exit:
|
|
|
|
|
result = Exit();
|
|
|
|
|
break;
|
|
|
|
|
case statements.append:
|
|
|
|
|
result = AppendSet(source);
|
|
|
|
|
break;
|
|
|
|
|
case statements.list:
|
|
|
|
|
result = List();
|
|
|
|
|
break;
|
|
|
|
|
case statements.print:
|
|
|
|
|
result = Print(source, 0);
|
|
|
|
|
break;
|
|
|
|
|
case statements.printlength:
|
|
|
|
|
result = Print(source, 1);
|
|
|
|
|
break;
|
|
|
|
|
case statements.printwordcount:
|
|
|
|
|
result = Print(source, 2);
|
|
|
|
|
break;
|
|
|
|
|
case statements.printwords:
|
|
|
|
|
result = Print(source, 3);
|
|
|
|
|
break;
|
|
|
|
|
case statements.set:
|
|
|
|
|
result = AppendSet(source, false);
|
|
|
|
|
break;
|
|
|
|
|
case statements.reverse:
|
|
|
|
|
result = Reverse(source);
|
|
|
|
|
break;
|
|
|
|
|
case statements.h:
|
|
|
|
|
Console.WriteLine("Commands are: ");
|
|
|
|
|
foreach (var item in Enum.GetValues(typeof(statements)))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("\t{0}", ((statements)item).ToString());
|
|
|
|
|
}
|
|
|
|
|
// Ignore these as actual commands
|
|
|
|
|
source.Position = initPos;
|
|
|
|
|
source.SetLength(initPos);
|
|
|
|
|
break;
|
|
|
|
|
case statements.writeout:
|
|
|
|
|
// Writes the full command history to the stream.
|
|
|
|
|
Console.WriteLine("Writing input commands to {0}...");
|
|
|
|
|
source.Position = initPos;
|
|
|
|
|
source.SetLength(initPos);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// Do a check semicolons etc
|
|
|
|
|
if (IsNextEoS(source))
|
|
|
|
|
{
|
|
|
|
|
result();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ParserException("expected a semi-colon", 0, source.Position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
2021-03-11 19:22:49 +13:00
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
// Statement parse failed,
|
|
|
|
|
// Ensure stream gets trimmed back to the correct position
|
|
|
|
|
Console.WriteLine("Failed parsing statement");
|
|
|
|
|
source.Position = initPos;
|
|
|
|
|
source.SetLength(initPos);
|
2021-03-11 19:22:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-15 20:58:52 +13:00
|
|
|
|
// Throwing a parserexception will return us to this point immediately. From here, the line is automatically restored,
|
|
|
|
|
// and the excepion printed to the console window.
|
|
|
|
|
// This means that each function does not need to keep track of our current position in the stream
|
|
|
|
|
catch (ParserException e)
|
2021-03-15 13:46:54 +13:00
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
if (e.Importance > 3)
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException("A critical error occurred.");
|
|
|
|
|
}
|
|
|
|
|
if (e.LinePosition > 0)
|
|
|
|
|
{
|
|
|
|
|
WriteDebugLine(initPos, e.LinePosition, e.Message, source);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e.LinePosition + ": " + e.Message);
|
|
|
|
|
source.Position = initPos;
|
|
|
|
|
source.SetLength(initPos);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 13:46:54 +13:00
|
|
|
|
}
|
2021-03-15 20:58:52 +13:00
|
|
|
|
|
2021-03-11 19:22:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 16:18:05 +13:00
|
|
|
|
#region Function Handling
|
2021-03-15 20:58:52 +13:00
|
|
|
|
|
|
|
|
|
private string ValidateKey(Stream source, bool checkExist)
|
2021-03-11 12:56:25 +13:00
|
|
|
|
{
|
2021-03-11 16:18:05 +13:00
|
|
|
|
string key;
|
2021-03-12 11:00:29 +13:00
|
|
|
|
long keyEndPos = FindIdentifier(source, out key);
|
2021-03-15 20:58:52 +13:00
|
|
|
|
if (keyEndPos < 0 || key.Length == 0)
|
2021-03-11 12:56:25 +13:00
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
throw new ParserException("Could not identify object", 0, source.Position);
|
|
|
|
|
}
|
|
|
|
|
else if (checkExist && !Symbols.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
throw new ParserException("Key not found", 0, source.Position);
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
if (Symbols.ContainsKey(key) && Symbols[key].Item2.HasFlag(VariableFlags.Reserved))
|
|
|
|
|
{
|
|
|
|
|
throw new ParserException("Cannot assign a value to a reserved constant", 0, keyEndPos - (key.Length + 1));
|
|
|
|
|
}
|
2021-03-12 11:00:29 +13:00
|
|
|
|
source.Position = keyEndPos;
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
2021-03-15 20:58:52 +13:00
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string ValidateValue(Stream source)
|
|
|
|
|
{
|
2021-03-11 16:18:05 +13:00
|
|
|
|
string value;
|
2021-03-15 20:58:52 +13:00
|
|
|
|
long valuePos = FindExpression(source, out value);
|
2021-03-12 11:00:29 +13:00
|
|
|
|
if (valuePos < 0)
|
2021-03-11 12:56:25 +13:00
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
throw new ParserException("Could not evaluate expression", 0, source.Position);
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
2021-03-11 16:18:05 +13:00
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-12 11:00:29 +13:00
|
|
|
|
source.Position = valuePos;
|
2021-03-11 16:18:05 +13:00
|
|
|
|
}
|
2021-03-15 20:58:52 +13:00
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the 'append x y [ + z];' case &
|
|
|
|
|
/// And the 'set x y [ + z];' case
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source"></param>
|
|
|
|
|
/// <returns>An Action that will add the key to the dictionary</returns>
|
|
|
|
|
Action AppendSet(Stream source, bool appendMode=true)
|
|
|
|
|
{
|
|
|
|
|
string key = ValidateKey(source, appendMode);
|
|
|
|
|
string value = ValidateValue(source);
|
|
|
|
|
if (appendMode)
|
2021-03-11 16:18:05 +13:00
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
return () => Symbols[key] = new Tuple<string, VariableFlags>(Symbols[key].Item1 + value, Symbols[key].Item2);
|
2021-03-11 16:18:05 +13:00
|
|
|
|
}
|
2021-03-15 20:58:52 +13:00
|
|
|
|
else
|
2021-03-11 16:18:05 +13:00
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
return () => Symbols.Add(key, new Tuple<string, VariableFlags>(value, VariableFlags.Empty));
|
2021-03-11 16:18:05 +13:00
|
|
|
|
}
|
2021-03-15 20:58:52 +13:00
|
|
|
|
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
2021-03-15 15:03:36 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates and prints a list of all defined variables
|
|
|
|
|
/// </summary>
|
2021-03-15 20:58:52 +13:00
|
|
|
|
/// <param name="printUnprint">List values normally excluded from printing</param>
|
|
|
|
|
Action List(bool printUnprint = false)
|
2021-03-11 16:39:15 +13:00
|
|
|
|
{
|
2021-03-15 17:28:20 +13:00
|
|
|
|
int keyWidth = 10;
|
|
|
|
|
int valueWidth = 50;
|
|
|
|
|
int flagWidth = 9;
|
2021-03-15 20:58:52 +13:00
|
|
|
|
StringBuilder consoleOutput = new StringBuilder();
|
|
|
|
|
consoleOutput.Append(string.Format("┌" + new string('─', keyWidth) + "┬" + new string('─', valueWidth) + "┬" + new string('─', flagWidth) + "┐\n"));
|
|
|
|
|
consoleOutput.Append(string.Format("│{0}│{1}│{2}│\n", CenterString("Symbol", keyWidth), CenterString("Value", valueWidth), CenterString("Flags", flagWidth)));
|
2021-03-15 17:28:20 +13:00
|
|
|
|
// Figure out how many symbols are eligible for printing
|
|
|
|
|
List<string> eligibleKeys = new List<string>(Symbols.Count);
|
|
|
|
|
foreach (var item in Symbols.Keys)
|
|
|
|
|
{
|
|
|
|
|
if (!Symbols[item].Item2.HasFlag(VariableFlags.NoPrint) || (Symbols[item].Item2.HasFlag(VariableFlags.NoPrint) && printUnprint))
|
2021-03-11 16:39:15 +13:00
|
|
|
|
{
|
2021-03-15 17:28:20 +13:00
|
|
|
|
eligibleKeys.Add(item);
|
2021-03-11 16:39:15 +13:00
|
|
|
|
}
|
2021-03-15 17:28:20 +13:00
|
|
|
|
}
|
|
|
|
|
// Control printing based on how many keys are available
|
|
|
|
|
if (eligibleKeys.Count > 0)
|
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
consoleOutput.Append(string.Format("├" + new string('─', keyWidth) + "┼" + new string('─', valueWidth) + "┼" + new string('─', flagWidth) + "┤\n"));
|
2021-03-15 17:28:20 +13:00
|
|
|
|
for (int i = 0; i < eligibleKeys.Count; i++)
|
2021-03-11 16:39:15 +13:00
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
string entryFormat = "│{0," + -1*keyWidth + "}│{1," + -1*valueWidth + "}│{2," + -1*flagWidth + "}│\n";
|
2021-03-15 17:28:20 +13:00
|
|
|
|
|
2021-03-15 20:58:52 +13:00
|
|
|
|
consoleOutput.Append(string.Format(entryFormat, eligibleKeys[i], Symbols[eligibleKeys[i]].Item1.Replace("\r", "\\r").Replace("\n", "\\n").Replace("\t", "\\t"), Convert.ToString((byte)Symbols[eligibleKeys[i]].Item2, 2).PadLeft(8, '0')));
|
2021-03-15 17:28:20 +13:00
|
|
|
|
if (i + 1 < eligibleKeys.Count)
|
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
consoleOutput.Append(string.Format("├" + new string('─', keyWidth) + "┼" + new string('─', valueWidth) + "┼" + new string('─', flagWidth) + "┤\n"));
|
2021-03-15 17:28:20 +13:00
|
|
|
|
}
|
2021-03-11 16:39:15 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-15 20:58:52 +13:00
|
|
|
|
consoleOutput.Append(string.Format("└" + new string('─', keyWidth) + "┴" + new string('─', valueWidth) + "┴" + new string('─', flagWidth) + "┘\n"));
|
|
|
|
|
|
|
|
|
|
return () => Console.WriteLine(consoleOutput.ToString());
|
2021-03-11 16:53:20 +13:00
|
|
|
|
}
|
2021-03-15 20:58:52 +13:00
|
|
|
|
Action Exit()
|
2021-03-11 16:53:20 +13:00
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
// Should do some save command here
|
|
|
|
|
return () => Environment.Exit(0);
|
2021-03-11 16:53:20 +13:00
|
|
|
|
}
|
2021-03-15 20:58:52 +13:00
|
|
|
|
Action Print(Stream source, int mode=0)
|
2021-03-11 16:53:20 +13:00
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
StringBuilder outputString = new StringBuilder();
|
|
|
|
|
string expression = ValidateValue(source);
|
2021-03-11 16:53:20 +13:00
|
|
|
|
if (mode == 0)
|
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
outputString.Append(expression + Environment.NewLine);
|
2021-03-11 16:53:20 +13:00
|
|
|
|
}
|
|
|
|
|
else if (mode == 1)
|
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
outputString.Append("Length of the expression is: ");
|
|
|
|
|
outputString.Append(expression.Length + Environment.NewLine);
|
2021-03-11 16:53:20 +13:00
|
|
|
|
}
|
|
|
|
|
else if (mode >= 2)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
string[] words = expression.Split(' ');
|
|
|
|
|
if (mode == 3)
|
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
outputString.Append("Wordcount is: ");
|
|
|
|
|
outputString.Append(words.Length + Environment.NewLine);
|
2021-03-11 16:53:20 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Words are:");
|
|
|
|
|
foreach (string word in words)
|
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
outputString.Append(word + Environment.NewLine);
|
2021-03-11 16:53:20 +13:00
|
|
|
|
}
|
2021-03-15 20:58:52 +13:00
|
|
|
|
}
|
2021-03-11 17:18:02 +13:00
|
|
|
|
}
|
2021-03-15 20:58:52 +13:00
|
|
|
|
return () => Console.WriteLine(outputString.ToString());
|
2021-03-11 17:18:02 +13:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 20:58:52 +13:00
|
|
|
|
Action Reverse(Stream source)
|
2021-03-11 17:47:38 +13:00
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
string key = ValidateKey(source, true);
|
|
|
|
|
string ToReverse = Symbols[key].Item1;
|
2021-03-11 17:47:38 +13:00
|
|
|
|
string[] words = ToReverse.Split(' ');
|
|
|
|
|
StringBuilder reversed = new StringBuilder();
|
2021-03-15 20:58:52 +13:00
|
|
|
|
for (int i = words.Length-1; i >= 0; i--)
|
2021-03-11 17:47:38 +13:00
|
|
|
|
{
|
|
|
|
|
reversed.Append(words[i]);
|
|
|
|
|
reversed.Append(' ');
|
|
|
|
|
}
|
2021-03-15 20:58:52 +13:00
|
|
|
|
|
|
|
|
|
return () => Symbols[key] = new Tuple<string, VariableFlags>(reversed.ToString(), Symbols[key].Item2);
|
2021-03-11 17:47:38 +13:00
|
|
|
|
}
|
2021-03-12 11:29:46 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes the debug info to the screen in the form:<br/>
|
|
|
|
|
/// line read from stream (lineStart) to line end<br/>
|
|
|
|
|
/// <whitespace@caratPos> ^ <errorMessage>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="lineStart"></param>
|
|
|
|
|
/// <param name="caratPos"></param>
|
|
|
|
|
/// <param name="errorMessage"></param>
|
|
|
|
|
/// <param name="source"></param>
|
|
|
|
|
void WriteDebugLine(long lineStart, long caratPos, string errorMessage, Stream source)
|
|
|
|
|
{
|
|
|
|
|
source.Position = lineStart;
|
|
|
|
|
string fullLine = GetNextLine(source);
|
2021-03-12 12:14:51 +13:00
|
|
|
|
string errorMSG = new string (' ', (caratPos - lineStart) >= 0 ? (int)(caratPos - lineStart):0) + "^ " + errorMessage;
|
2021-03-12 11:29:46 +13:00
|
|
|
|
Console.WriteLine(fullLine);
|
|
|
|
|
Console.WriteLine(errorMSG);
|
|
|
|
|
source.SetLength(source.Position);
|
|
|
|
|
}
|
2021-03-12 12:14:51 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the starting point of the expression at expected line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="source"></param>
|
|
|
|
|
/// <param name="word"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
long GetLineStart(Stream source, string word)
|
|
|
|
|
{
|
|
|
|
|
// Decrement and check the previous value is 32 (space), before incrementing back to our current pos
|
|
|
|
|
// Don't need to guard against oob - implied it's within bounds by the set command being at least 3 bytes long
|
|
|
|
|
source.Position--;
|
|
|
|
|
if (source.ReadByte() == 32)
|
|
|
|
|
{
|
|
|
|
|
source.Position--;
|
|
|
|
|
}
|
|
|
|
|
//source.Position++;
|
|
|
|
|
return source.Position - word.Length;
|
|
|
|
|
}
|
2021-03-11 17:47:38 +13:00
|
|
|
|
|
2021-03-11 16:18:05 +13:00
|
|
|
|
#endregion
|
|
|
|
|
#region Data Handling
|
|
|
|
|
// Data Handling
|
|
|
|
|
/// <summary>
|
2021-03-15 15:03:36 +13:00
|
|
|
|
/// Parses & evaluates the expression from the stream, moving the stream to the end of the last value
|
2021-03-11 16:18:05 +13:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <param name="expression"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
long FindExpression(Stream s, out string expression)
|
2021-03-11 12:56:25 +13:00
|
|
|
|
{
|
2021-03-15 15:03:36 +13:00
|
|
|
|
// Expressions are one or more occurances of a variable name or literal definition.
|
|
|
|
|
// To make logical sense, there needs to be an operator between them. Typically, for strings, this is
|
|
|
|
|
// the append operator: +
|
|
|
|
|
// Variable symbols should be evaluated immediately.
|
|
|
|
|
// Start by ensuring we don't try reading past the end of the stream
|
|
|
|
|
// Also check for the EoS
|
2021-03-15 16:42:37 +13:00
|
|
|
|
long realStart = s.Position;
|
2021-03-15 15:03:36 +13:00
|
|
|
|
string result = "";
|
|
|
|
|
while (s.Position < s.Length && !IsNextEoS(s))
|
|
|
|
|
{
|
2021-03-15 16:42:37 +13:00
|
|
|
|
long currStart = s.Position;
|
2021-03-15 15:03:36 +13:00
|
|
|
|
if (IsNextEoS(s, '+'))
|
2021-03-11 16:18:05 +13:00
|
|
|
|
{
|
2021-03-15 15:03:36 +13:00
|
|
|
|
s.Position = FindNextWord(s, out _);
|
2021-03-11 16:18:05 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-15 16:42:37 +13:00
|
|
|
|
string value;
|
|
|
|
|
long firstPos = s.Position;
|
|
|
|
|
long val = FindValue(s, out value);
|
|
|
|
|
if (val == -1)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Could not parse value");
|
|
|
|
|
}
|
|
|
|
|
s.Position = val;
|
|
|
|
|
result += value;
|
2021-03-11 16:18:05 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
expression = result;
|
2021-03-15 16:42:37 +13:00
|
|
|
|
return s.Position;
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
2021-03-15 15:03:36 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks ahead to see if the next non-whitespace character is the EoS indicator (';')
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <param name="EoSChar"></param>
|
|
|
|
|
/// <returns>true if the next char is <paramref name="EoSChar"/>, else false</returns>
|
|
|
|
|
static bool IsNextEoS(Stream s, char EoSChar = ';')
|
|
|
|
|
{
|
2021-03-15 16:42:37 +13:00
|
|
|
|
long pos = s.Position;
|
2021-03-15 15:03:36 +13:00
|
|
|
|
char readChar = PeekChar(s);
|
2021-03-15 16:42:37 +13:00
|
|
|
|
while (readChar != 0 && char.IsWhiteSpace(readChar))
|
2021-03-15 15:03:36 +13:00
|
|
|
|
{
|
2021-03-15 16:42:37 +13:00
|
|
|
|
readChar = ReadChar(s);
|
2021-03-15 15:03:36 +13:00
|
|
|
|
}
|
2021-03-15 16:42:37 +13:00
|
|
|
|
s.Position = pos;
|
2021-03-15 15:03:36 +13:00
|
|
|
|
if (readChar == EoSChar) return true;
|
|
|
|
|
else return false;
|
|
|
|
|
}
|
2021-03-11 12:56:25 +13:00
|
|
|
|
|
2021-03-11 16:18:05 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the next value in the stream
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <param name="returnedValue"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
long FindValue(Stream s, out string returnedValue)
|
2021-03-11 12:56:25 +13:00
|
|
|
|
{
|
2021-03-11 16:18:05 +13:00
|
|
|
|
SkipWhitespace(s);
|
2021-03-15 16:42:37 +13:00
|
|
|
|
char result = PeekChar(s);
|
2021-03-11 16:18:05 +13:00
|
|
|
|
if (result == '\"')
|
2021-03-11 12:56:25 +13:00
|
|
|
|
{
|
2021-03-15 16:42:37 +13:00
|
|
|
|
// The first char is a ", i.e. the start of a literal - search as if it were a literal.
|
2021-03-11 16:18:05 +13:00
|
|
|
|
return FindLiteral(s, out returnedValue);
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
2021-03-11 16:18:05 +13:00
|
|
|
|
else
|
2021-03-11 12:56:25 +13:00
|
|
|
|
{
|
2021-03-11 16:18:05 +13:00
|
|
|
|
string keyValue;
|
2021-03-11 19:22:49 +13:00
|
|
|
|
long t = FindExistingIdentifier(s, out keyValue);
|
2021-03-15 16:42:37 +13:00
|
|
|
|
// Set the key value to result + this read string
|
|
|
|
|
//keyValue = result + keyValue;
|
2021-03-12 11:00:29 +13:00
|
|
|
|
|
|
|
|
|
if (!Symbols.ContainsKey(keyValue))
|
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
throw new ParserException("Could not find key: " + keyValue, 0);
|
2021-03-12 11:00:29 +13:00
|
|
|
|
}
|
2021-03-11 16:18:05 +13:00
|
|
|
|
returnedValue = Symbols[keyValue].Item1;
|
|
|
|
|
return t;
|
|
|
|
|
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
2021-03-11 16:18:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long FindIdentifier(Stream s, out string returnedKey)
|
2021-03-11 19:22:49 +13:00
|
|
|
|
{
|
|
|
|
|
long wordEnd = FindNextWord(s, out returnedKey);
|
|
|
|
|
return wordEnd;
|
|
|
|
|
}
|
|
|
|
|
long FindExistingIdentifier(Stream s, out string returnedKey)
|
2021-03-11 16:18:05 +13:00
|
|
|
|
{
|
|
|
|
|
string identifier;
|
|
|
|
|
long wordEnd = FindNextWord(s, out identifier);
|
2021-03-15 16:42:37 +13:00
|
|
|
|
if (identifier.Length > 1 && identifier.EndsWith(';'))
|
2021-03-12 11:10:24 +13:00
|
|
|
|
{
|
|
|
|
|
// Remove the trailing semicolon from the parse & backtrack the identifier length one spot
|
|
|
|
|
identifier = identifier.TrimEnd(';');
|
|
|
|
|
wordEnd--;
|
2021-03-15 16:42:37 +13:00
|
|
|
|
s.Position--;
|
2021-03-12 11:10:24 +13:00
|
|
|
|
}
|
2021-03-11 16:18:05 +13:00
|
|
|
|
// Lookup the value in the symbol table
|
2021-03-11 19:22:49 +13:00
|
|
|
|
returnedKey = identifier;
|
2021-03-11 16:18:05 +13:00
|
|
|
|
return wordEnd;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 16:42:37 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the end of the complete literal definition, returning the stream to the original position
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <param name="returnedLiteral"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-03-11 16:18:05 +13:00
|
|
|
|
long FindLiteral(Stream s, out string returnedLiteral)
|
|
|
|
|
{
|
2021-03-15 16:42:37 +13:00
|
|
|
|
long pos = s.Position;
|
2021-03-11 16:18:05 +13:00
|
|
|
|
// Is a literal. Now we must parse until we find the end of the literal
|
2021-03-15 16:42:37 +13:00
|
|
|
|
// Remove the first char, if it is a literal definition.
|
|
|
|
|
if (PeekChar(s) == '\"') ReadChar(s);
|
2021-03-11 16:18:05 +13:00
|
|
|
|
string resultLiteral;
|
|
|
|
|
long resultPosition = FindNextOccurance(s, (c, s) =>
|
2021-03-11 12:56:25 +13:00
|
|
|
|
{
|
2021-03-11 16:18:05 +13:00
|
|
|
|
if (c == '\"')
|
|
|
|
|
{
|
|
|
|
|
long pos = s.Position--;
|
2021-03-15 15:03:36 +13:00
|
|
|
|
if (ReadChar(s) == '\\')
|
2021-03-11 16:18:05 +13:00
|
|
|
|
{
|
|
|
|
|
// TODO: handle the \\ escape
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}, out resultLiteral);
|
|
|
|
|
if (resultPosition > -1)
|
|
|
|
|
{
|
|
|
|
|
returnedLiteral = resultLiteral;
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
throw new ParserException("Could not parse the literal");
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
2021-03-15 16:42:37 +13:00
|
|
|
|
s.Position = pos;
|
2021-03-11 16:18:05 +13:00
|
|
|
|
return resultPosition;
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
2021-03-11 16:18:05 +13:00
|
|
|
|
#endregion
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 16:18:05 +13:00
|
|
|
|
|
2021-03-15 15:03:36 +13:00
|
|
|
|
#region HelperFunctions
|
2021-03-11 16:18:05 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads the memory stream as a UTF-8 encoded string until the next occurance of '\n' or '\r\n' (consuming, and excluded)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
static string GetNextLine(Stream s)
|
|
|
|
|
{
|
|
|
|
|
string nextLine;
|
|
|
|
|
FindNextOccurance(s, '\n', out nextLine);
|
|
|
|
|
return nextLine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-03-15 15:03:36 +13:00
|
|
|
|
/// Finds the end-boundary of the next word in the stream, and returns the stream to the original position
|
2021-03-11 16:18:05 +13:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <param name="nextWord"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
static long FindNextWord(Stream s, out string nextWord)
|
|
|
|
|
{
|
2021-03-15 16:42:37 +13:00
|
|
|
|
StringBuilder newWord = new StringBuilder();
|
|
|
|
|
// Record our current position
|
|
|
|
|
long start = s.Position;
|
|
|
|
|
// Check if the character at the current pos is whitespace, if so, keep advancing until it isn't
|
|
|
|
|
char currentChar = ReadChar(s);
|
|
|
|
|
while (s.Position < s.Length && char.IsWhiteSpace(currentChar))
|
2021-03-12 12:14:51 +13:00
|
|
|
|
{
|
2021-03-15 16:42:37 +13:00
|
|
|
|
currentChar = ReadChar(s);
|
|
|
|
|
}
|
|
|
|
|
// Add the last read value to the SB
|
|
|
|
|
newWord.Append(currentChar);
|
|
|
|
|
// Start a second loop, this time checking we're not a whitespace char
|
|
|
|
|
while (s.Position < s.Length)
|
|
|
|
|
{
|
|
|
|
|
currentChar = ReadChar(s);
|
2021-03-15 20:58:52 +13:00
|
|
|
|
if (char.IsWhiteSpace(currentChar) || currentChar == ';')
|
2021-03-15 16:42:37 +13:00
|
|
|
|
{
|
2021-03-15 20:58:52 +13:00
|
|
|
|
s.Position--;
|
2021-03-15 16:42:37 +13:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
newWord.Append(currentChar);
|
|
|
|
|
}
|
2021-03-12 12:14:51 +13:00
|
|
|
|
}
|
2021-03-15 16:42:37 +13:00
|
|
|
|
nextWord = newWord.ToString();
|
|
|
|
|
|
|
|
|
|
long endPos = s.Position;
|
|
|
|
|
s.Position = start;
|
|
|
|
|
return endPos;
|
2021-03-11 16:18:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds and returns the position of the next occurance of the Func returning true.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
2021-03-15 15:03:36 +13:00
|
|
|
|
/// <param name="p">A 'predicate'-like Func</param>
|
|
|
|
|
/// <param name="result">Returns the string captured while searching for the next char</param>
|
2021-03-11 16:18:05 +13:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
static long FindNextOccurance(Stream s, Func<char, Stream, bool> p, out string result)
|
|
|
|
|
{
|
|
|
|
|
long start = s.Position;
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
bool charFound = false;
|
2021-03-15 15:03:36 +13:00
|
|
|
|
while (s.Position < s.Length && !charFound)
|
2021-03-11 16:18:05 +13:00
|
|
|
|
{
|
2021-03-15 15:03:36 +13:00
|
|
|
|
char nextChar = ReadChar(s);
|
2021-03-11 19:22:49 +13:00
|
|
|
|
if (nextChar == 0)
|
|
|
|
|
{
|
|
|
|
|
charFound = true;
|
|
|
|
|
}
|
|
|
|
|
else if (p(nextChar, s))
|
2021-03-11 16:18:05 +13:00
|
|
|
|
{
|
|
|
|
|
charFound = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sb.Append(nextChar);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = sb.ToString();
|
|
|
|
|
long newPosition = s.Position;
|
|
|
|
|
s.Position = start;
|
2021-03-11 19:22:49 +13:00
|
|
|
|
return newPosition--;
|
2021-03-11 16:18:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the next position of the character
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <param name="c"></param>
|
|
|
|
|
/// <param name="result">Captures the string read in searching for the character</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
static long FindNextOccurance(Stream s, char c, out string result)
|
|
|
|
|
{
|
|
|
|
|
return FindNextOccurance(s, (streamChar, s) => streamChar == c, out result);
|
|
|
|
|
}
|
2021-03-15 15:03:36 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads the next UTF-8 encoded character in the stream, and advances the stream by the amount of characters read
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
static char ReadChar(Stream s)
|
2021-03-11 16:18:05 +13:00
|
|
|
|
{
|
|
|
|
|
// As UTF-8 allows codepoints to span multiple bytes, reading a single byte as a character will not always give the expected
|
|
|
|
|
// value.
|
|
|
|
|
// Fortunately, the standard ASCII table is 7-bits long. The 8th bit is used to determine the character size
|
|
|
|
|
int readAmount = 0;
|
|
|
|
|
int firstChar = s.ReadByte();
|
2021-03-11 19:22:49 +13:00
|
|
|
|
if (firstChar == -1)
|
|
|
|
|
{
|
|
|
|
|
return (char)0;
|
|
|
|
|
}
|
2021-03-11 16:18:05 +13:00
|
|
|
|
if ((firstChar >> 3) == 0x1E) // 11110xxx implies a 4-byte length character
|
|
|
|
|
{
|
|
|
|
|
readAmount = 3;
|
|
|
|
|
}
|
|
|
|
|
else if ((firstChar >> 4) == 0xE) // 1110xxxx, 3-byte
|
|
|
|
|
{
|
|
|
|
|
readAmount = 2;
|
|
|
|
|
}
|
|
|
|
|
else if ((firstChar >> 5) == 0x6) // 110xxxxx, 2-byte
|
|
|
|
|
{
|
|
|
|
|
readAmount = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] charBytes = new byte[readAmount + 1];
|
|
|
|
|
charBytes[0] = (byte)firstChar;
|
|
|
|
|
for (int i = 1; i < readAmount; i++)
|
|
|
|
|
{
|
|
|
|
|
int nextChar = s.ReadByte();
|
|
|
|
|
if (nextChar >> 6 != 2) throw new Exception("Character is not a valid UTF-8 code point!");
|
|
|
|
|
charBytes[i] = (byte)nextChar;
|
|
|
|
|
}
|
2021-03-11 19:22:49 +13:00
|
|
|
|
s.Position += readAmount;
|
2021-03-11 16:18:05 +13:00
|
|
|
|
string converted = Encoding.UTF8.GetString(charBytes);
|
|
|
|
|
return converted[0];
|
|
|
|
|
}
|
2021-03-15 15:03:36 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads the next character in the stream, and returns the position to the original position
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
static char PeekChar(Stream s)
|
|
|
|
|
{
|
|
|
|
|
long curr = s.Position;
|
|
|
|
|
char c = ReadChar(s);
|
|
|
|
|
s.Position = curr;
|
|
|
|
|
return c;
|
|
|
|
|
}
|
2021-03-11 16:18:05 +13:00
|
|
|
|
|
2021-03-15 15:03:36 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Skips whitespace characters
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
2021-03-11 16:18:05 +13:00
|
|
|
|
static void SkipWhitespace(Stream s)
|
|
|
|
|
{
|
2021-03-15 15:03:36 +13:00
|
|
|
|
char c = PeekChar(s);
|
|
|
|
|
while (s.Position < s.Length && char.IsWhiteSpace(c))
|
2021-03-11 16:18:05 +13:00
|
|
|
|
{
|
2021-03-15 16:42:37 +13:00
|
|
|
|
ReadChar(s); // move by the size of that character
|
2021-03-15 15:03:36 +13:00
|
|
|
|
c = PeekChar(s);
|
2021-03-11 16:18:05 +13:00
|
|
|
|
}
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
2021-03-12 12:46:00 +13:00
|
|
|
|
|
|
|
|
|
static string CenterString(string source, int totalPadding, char paddingChar=' ')
|
|
|
|
|
{
|
|
|
|
|
if (source.Length >= totalPadding) return source;
|
|
|
|
|
int rightHalf = (int)Math.Ceiling(source.Length / 2.0);
|
|
|
|
|
int leftHalfPad = (int)Math.Floor(totalPadding / 2.0);
|
|
|
|
|
int rightHalfPad = (int)Math.Ceiling(totalPadding / 2.0);
|
|
|
|
|
string t = "{0," + leftHalfPad + "}{1," + -1 * rightHalfPad + "}";
|
2021-03-15 17:28:20 +13:00
|
|
|
|
string result = string.Format(t, source[..rightHalf], source[rightHalf..]);
|
2021-03-12 12:46:00 +13:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-03-15 15:03:36 +13:00
|
|
|
|
#endregion
|
2021-03-15 20:58:52 +13:00
|
|
|
|
public class ParserException : Exception
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Importance is used to signify how the parser should respond to the error.
|
|
|
|
|
/// A code of 3 or greater is a critical error; the application will throw the error further up the call and exit.
|
|
|
|
|
/// 0 implies the line may be retried.
|
|
|
|
|
/// 1 should imply the current block is not valid and should be retried.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int Importance = 0;
|
|
|
|
|
public long LinePosition = -1;
|
|
|
|
|
public ParserException(string message, int importance, long linePos) : base(message)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public ParserException(string message, int importance) : base(message)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public ParserException(string message) : base(message)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-11 12:56:25 +13:00
|
|
|
|
}
|
|
|
|
|
}
|