diff --git a/@DescribeCompilerCLI/Datnik.cs b/@DescribeCompilerCLI/Datnik.cs index 7288bedf..f89a606c 100644 --- a/@DescribeCompilerCLI/Datnik.cs +++ b/@DescribeCompilerCLI/Datnik.cs @@ -8,7 +8,7 @@ namespace DescribeCompilerCLI { - internal class Datnik + public class Datnik { /// /// The output directory for the ext and extone commands diff --git a/@DescribeCompilerCLI/FunctionsMessages.cs b/@DescribeCompilerCLI/FunctionsMessages.cs index caca064c..facde1fb 100644 --- a/@DescribeCompilerCLI/FunctionsMessages.cs +++ b/@DescribeCompilerCLI/FunctionsMessages.cs @@ -1,10 +1,5 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using static System.Net.Mime.MediaTypeNames; namespace DescribeCompilerCLI { @@ -14,14 +9,14 @@ namespace DescribeCompilerCLI //minimalist - Bulbhead, Graceful, Modular, Ogre, Slant, Small //Small Slant, Standard, Twisted, ANSI Shadow - internal static class Messages + public static class Messages { //settings - static bool ONE_BASED_ARG_INDEX = true; - static ConsoleColor INFO_COLOR = ConsoleColor.DarkGray; - static ConsoleColor TEXT_COLOR = ConsoleColor.White; - static ConsoleColor ERROR_COLOR = ConsoleColor.Red; - static ConsoleColor MOREINFO_COLOR = ConsoleColor.Green; + public static bool ONE_BASED_ARG_INDEX = true; + public static ConsoleColor INFO_COLOR = ConsoleColor.DarkGray; + public static ConsoleColor TEXT_COLOR = ConsoleColor.White; + public static ConsoleColor ERROR_COLOR = ConsoleColor.Red; + public static ConsoleColor MOREINFO_COLOR = ConsoleColor.Green; static string thisName; static Messages() @@ -31,35 +26,35 @@ static Messages() //themes - internal static void SetDefaultTheme() + public static void SetDefaultTheme() { INFO_COLOR = ConsoleColor.DarkGray; TEXT_COLOR = ConsoleColor.White; ERROR_COLOR = ConsoleColor.Red; MOREINFO_COLOR = ConsoleColor.Green; } - internal static void SetVioletTheme() + public static void SetVioletTheme() { INFO_COLOR = ConsoleColor.DarkMagenta; TEXT_COLOR = ConsoleColor.White; ERROR_COLOR = ConsoleColor.Red; MOREINFO_COLOR = ConsoleColor.DarkGray; } - internal static void SetCyanTheme() + public static void SetCyanTheme() { INFO_COLOR = ConsoleColor.DarkMagenta; TEXT_COLOR = ConsoleColor.Cyan; ERROR_COLOR = ConsoleColor.Red; MOREINFO_COLOR = ConsoleColor.DarkCyan; } - internal static void SetLightBlueTheme() + public static void SetLightBlueTheme() { INFO_COLOR = ConsoleColor.White; TEXT_COLOR = ConsoleColor.Blue; ERROR_COLOR = ConsoleColor.Red; MOREINFO_COLOR = ConsoleColor.DarkCyan; } - internal static void SetDarkBlueTheme() + public static void SetDarkBlueTheme() { INFO_COLOR = ConsoleColor.DarkGray; TEXT_COLOR = ConsoleColor.DarkBlue; @@ -69,16 +64,16 @@ internal static void SetDarkBlueTheme() //presets - internal static void presetConsole() + public static void presetConsole() { Console.BufferHeight = Int16.MaxValue - 1; Console.ForegroundColor = ConsoleColor.DarkGray; } - internal static void printLogo3() + public static void printLogo3() { printLogo3(TEXT_COLOR); } - internal static void printLogo3(ConsoleColor logoColor) + public static void printLogo3(ConsoleColor logoColor) { Log += @" /\\\\ /\\\\ /\\\\ /\\\\ /\\\\ /\\\\ /\\\\ /\\\\ " + Environment.NewLine; Log += @" / \\\\ / \\\\ / \\\\ / \\\\ / \\\\ _\ \\\\ / \\\\ / \\\\ " + Environment.NewLine; @@ -100,11 +95,11 @@ internal static void printLogo3(ConsoleColor logoColor) Console.WriteLine(); Console.ForegroundColor = col; } - internal static void printLogo2() + public static void printLogo2() { printLogo2(TEXT_COLOR); } - internal static void printLogo2(ConsoleColor logoColor) + public static void printLogo2(ConsoleColor logoColor) { Log += @" /\\\ /\\\ /\\\ /\\\ /\\\ /\\\ /\\\ /\\\ " + Environment.NewLine; Log += @" / \\\ / \\\ / \\\ / \\\ / \\\ _\ \\\ / \\\ / \\\ " + Environment.NewLine; @@ -126,11 +121,11 @@ internal static void printLogo2(ConsoleColor logoColor) Console.WriteLine(); Console.ForegroundColor = col; } - internal static void printLogo3Bicolor() + public static void printLogo3Bicolor() { printLogo3Bicolor(TEXT_COLOR, INFO_COLOR); } - internal static void printLogo3Bicolor(ConsoleColor colorA, ConsoleColor colorB) + public static void printLogo3Bicolor(ConsoleColor colorA, ConsoleColor colorB) { Log += @" /\\\\ /\\\\ /\\\\ /\\\\ /\\\\ /\\\\ /\\\\ /\\\\ " + Environment.NewLine; Log += @" / \\\\ / \\\\ / \\\\ / \\\\ / \\\\ _\ \\\\ / \\\\ / \\\\ " + Environment.NewLine; @@ -257,7 +252,7 @@ internal static void printLogo3Bicolor(ConsoleColor colorA, ConsoleColor colorB) //errors - internal static void printCmdLine(string[] args) + public static void printCmdLine(string[] args) { string s = "> "; @@ -279,7 +274,7 @@ internal static void printCmdLine(string[] args) ConsoleLogInfo(s); } - internal static void printExtTemplatesSuccess(string path) + public static void printExtTemplatesSuccess(string path) { //add to log Log += "Templates outputted to \"" + path + "\"" + Environment.NewLine; @@ -292,7 +287,7 @@ internal static void printExtTemplatesSuccess(string path) Console.ForegroundColor = TEXT_COLOR; Console.ReadKey(); } - internal static void printCompilationSuccess() + public static void printCompilationSuccess() { //add to log Log += "Task completed successfully. Press any key to exit." + Environment.NewLine; @@ -303,7 +298,7 @@ internal static void printCompilationSuccess() Console.ForegroundColor = TEXT_COLOR; Console.ReadKey(); } - internal static void printHelpMessage() + public static void printHelpMessage() { ConsoleLogInfo("-----------------------------------------------------------------"); ConsoleLog("usage: " + thisName + " help | -h"); @@ -360,7 +355,7 @@ internal static void printHelpMessage() Console.ForegroundColor = TEXT_COLOR; Console.ReadKey(); } - internal static void printWarning(string message) + public static void printWarning(string message) { Console.ForegroundColor = INFO_COLOR; Log += "Warning: " + message + Environment.NewLine; @@ -371,7 +366,7 @@ internal static void printWarning(string message) Console.ForegroundColor = TEXT_COLOR; Console.ReadKey(); } - internal static void printNoArgumentsError() + public static void printNoArgumentsError() { Console.ForegroundColor = ERROR_COLOR; Log += "No arguments or invalid argument count." + Environment.NewLine; @@ -387,7 +382,7 @@ internal static void printNoArgumentsError() Console.ForegroundColor = TEXT_COLOR; Console.ReadKey(); } - internal static void printArgumentError(string arg, int argIndex) + public static void printArgumentError(string arg, int argIndex) { Console.ForegroundColor = ERROR_COLOR; if (ONE_BASED_ARG_INDEX) argIndex++; @@ -404,7 +399,7 @@ internal static void printArgumentError(string arg, int argIndex) Console.ForegroundColor = TEXT_COLOR; Console.ReadKey(); } - internal static void printArgumentError(string arg, int argIndex, string message) + public static void printArgumentError(string arg, int argIndex, string message) { Console.ForegroundColor = ERROR_COLOR; if (ONE_BASED_ARG_INDEX) argIndex++; @@ -419,7 +414,7 @@ internal static void printArgumentError(string arg, int argIndex, string message Console.ForegroundColor = TEXT_COLOR; Console.ReadKey(); } - internal static void printFatalError(string message) + public static void printFatalError(string message) { Console.ForegroundColor = ERROR_COLOR; Log += "Fatal error: " + message + Environment.NewLine; @@ -435,31 +430,31 @@ internal static void printFatalError(string message) //log - internal static string Log + public static string Log { get; private set; } - internal static void ConsoleLog(string text) + public static void ConsoleLog(string text) { Log += text + Environment.NewLine; Console.WriteLine(text); } - internal static void ConsoleLogInfo(string text) + public static void ConsoleLogInfo(string text) { Log += text + Environment.NewLine; Console.ForegroundColor = INFO_COLOR; Console.WriteLine(text); Console.ForegroundColor = TEXT_COLOR; } - internal static void ConsoleLogError(string text) + public static void ConsoleLogError(string text) { Log += text + Environment.NewLine; Console.ForegroundColor = ERROR_COLOR; Console.WriteLine(text); Console.ForegroundColor = TEXT_COLOR; } - internal static void ConsoleLogParseInfo(string text) + public static void ConsoleLogParseInfo(string text) { Log += text + Environment.NewLine; Console.ForegroundColor = MOREINFO_COLOR; diff --git a/@DescribeCompilerDB/@DescribeCompilerDB.7z b/@DescribeCompilerDB/@DescribeCompilerDB.7z new file mode 100644 index 00000000..eb69bb80 Binary files /dev/null and b/@DescribeCompilerDB/@DescribeCompilerDB.7z differ diff --git a/@GoldParserEngine/GoldParserEngine/Egt/EgtContentReader.cs b/@GoldParserEngine/GoldParserEngine/Egt/EgtContentReader.cs index c6061d35..69473b0c 100644 --- a/@GoldParserEngine/GoldParserEngine/Egt/EgtContentReader.cs +++ b/@GoldParserEngine/GoldParserEngine/Egt/EgtContentReader.cs @@ -1,273 +1,270 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.IO; -using GoldParser; +using System.Text; namespace GoldParser.Egt { - /// - /// Reads .EGT file containing grammar tables for the gold parser - /// engine v 5.0 - /// - /// My reader is slightly different from the original - the - /// original will show position when error occurs, however I find - /// that unnecessairy in a binary file, where you most likely won't - /// go in with a hex editor but debug the code anyway. - /// - public static class EgtContentReager - { - /// - /// Read EGT file containing grammar tables for the GOLD - /// parser engine v 5.0 - /// - /// Path to the EGT file - /// EgtFile object containing List of Egt Records - public static EgtFile ReadFile(string filepath) - { - try - { - Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read); - Utf16Reader reader = new Utf16Reader(stream, Encoding.Unicode); - EgtFile file = readEgtFile(reader); - return file; - } - catch (Exception ex) - { - throw new EgtException(ex.Message, ex, "ReadEgtFile"); - } - } + /// + /// Reads .EGT file containing grammar tables for the gold parser + /// engine v 5.0 + /// + /// My reader is slightly different from the original - the + /// original will show position when error occurs, however I find + /// that unnecessairy in a binary file, where you most likely won't + /// go in with a hex editor but debug the code anyway. + /// + public static class EgtContentReager + { + /// + /// Read EGT file containing grammar tables for the GOLD + /// parser engine v 5.0 + /// + /// Path to the EGT file + /// EgtFile object containing List of Egt Records + public static EgtFile ReadFile(string filepath) + { + try + { + Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read); + Utf16Reader reader = new Utf16Reader(stream, Encoding.Unicode); + EgtFile file = readEgtFile(reader); + return file; + } + catch (Exception ex) + { + throw new EgtException(ex.Message, ex, "ReadEgtFile"); + } + } - /// - /// Read EGT file containing grammar tables for the GOLD - /// parser engine v 5.0 - /// - /// - /// Reader from a stream that contains EGT file data - /// - /// EgtFile object containing List of Egt Records - public static EgtFile ReadFile(BinaryReader binreader) - { - try - { - Utf16Reader reader = new Utf16Reader(binreader.BaseStream, Encoding.Unicode); - EgtFile file = readEgtFile(reader); - return file; - } - catch (Exception ex) - { - throw new EgtException(ex.Message, ex, "ReadEgtFile"); - } - } + /// + /// Read EGT file containing grammar tables for the GOLD + /// parser engine v 5.0 + /// + /// + /// Reader from a stream that contains EGT file data + /// + /// EgtFile object containing List of Egt Records + public static EgtFile ReadFile(BinaryReader binreader) + { + try + { + Utf16Reader reader = new Utf16Reader(binreader.BaseStream, Encoding.Unicode); + EgtFile file = readEgtFile(reader); + return file; + } + catch (Exception ex) + { + throw new EgtException(ex.Message, ex, "ReadEgtFile"); + } + } - /// - /// Read EGT file containing grammar tables for the GOLD - /// parser engine v 5.0 - /// - /// Path to the EGT file - /// List of Egt Records - public static List ReadFileRecords(string filepath) - { - try + /// + /// Read EGT file containing grammar tables for the GOLD + /// parser engine v 5.0 + /// + /// Path to the EGT file + /// List of Egt Records + public static List ReadFileRecords(string filepath) + { + try { - Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read); - Utf16Reader reader = new Utf16Reader(stream, Encoding.Unicode); - List records = readEgtFileRecords(reader); - return records; - } - catch (Exception ex) + Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read); + Utf16Reader reader = new Utf16Reader(stream, Encoding.Unicode); + List records = readEgtFileRecords(reader); + return records; + } + catch (Exception ex) { - throw new EgtException(ex.Message, ex, "ReadEgtFile"); - } - } + throw new EgtException(ex.Message, ex, "ReadEgtFile"); + } + } - /// - /// Read EGT file containing grammar tables for the GOLD - /// parser engine v 5.0 - /// - /// - /// Reader from a stream that contains EGT file data - /// - /// List of Egt Records - public static List ReadFileRecords(BinaryReader binreader) - { - try - { - Utf16Reader reader = new Utf16Reader(binreader.BaseStream, Encoding.Unicode); - List records = readEgtFileRecords(reader); - return records; - } - catch (Exception ex) - { - throw new EgtException(ex.Message, ex, "ReadEgtFile"); - } - } + /// + /// Read EGT file containing grammar tables for the GOLD + /// parser engine v 5.0 + /// + /// + /// Reader from a stream that contains EGT file data + /// + /// List of Egt Records + public static List ReadFileRecords(BinaryReader binreader) + { + try + { + Utf16Reader reader = new Utf16Reader(binreader.BaseStream, Encoding.Unicode); + List records = readEgtFileRecords(reader); + return records; + } + catch (Exception ex) + { + throw new EgtException(ex.Message, ex, "ReadEgtFile"); + } + } - private static EgtFile readEgtFile(Utf16Reader reader) - { - //create CGTStructure from the file. this happens in the CGTReader class - //in the main dir in "Reader.cs". First we read the file header like so - string header = reader.ReadUnicodeString(); + private static EgtFile readEgtFile(Utf16Reader reader) + { + //create CGTStructure from the file. this happens in the CGTReader class + //in the main dir in "Reader.cs". First we read the file header like so + string header = reader.ReadUnicodeString(); - //If the header don't start with "GOLD", then it is not valid - if (!header.StartsWith("GOLD")) return null; + //If the header don't start with "GOLD", then it is not valid + if (!header.StartsWith("GOLD")) return null; - //after reading the header, we read records till the end of the stream - List records = new List(); - while (reader.BaseStream.Position != reader.BaseStream.Length) - { - //we read record header first, and it must be 77 which is 'M' - byte entriesHeader = reader.ReadByte(); - if (entriesHeader != 77) return null; + //after reading the header, we read records till the end of the stream + List records = new List(); + while (reader.BaseStream.Position != reader.BaseStream.Length) + { + //we read record header first, and it must be 77 which is 'M' + byte entriesHeader = reader.ReadByte(); + if (entriesHeader != 77) return null; - //next is count of entries in the record - ushort entriesCount = reader.ReadUInt16(); + //next is count of entries in the record + ushort entriesCount = reader.ReadUInt16(); - //then we deal with the entries by using the EntryFactory class - //in the structure namespace - List entries = new List(); - for (int i = 0; i < entriesCount; i++) - { - byte entryType = reader.ReadByte(); - switch (entryType) - { - case 69: // 'E' - var eentry = new EgtEntry(); - entries.Add(eentry); - break; - case 98: // 'b' - byte b = reader.ReadByte(); - var bentry = new EgtEntry(EgtEntryType.Byte, b); - entries.Add(bentry); - break; - case 66: // 'B' - bool a = reader.ReadBoolean(); - var aentry = new EgtEntry(EgtEntryType.Boolean, a); - entries.Add(aentry); - break; - case 73: // 'I' - ushort v = reader.RawReadUInt16(); - var ientry = new EgtEntry(EgtEntryType.UInt16, v); - entries.Add(ientry); - break; - case 83: // 'S' - string s = reader.ReadUnicodeString(); - var sentry = new EgtEntry(EgtEntryType.String, s); - entries.Add(sentry); - break; - default: //Unknown - var rentry = new EgtEntry(EgtEntryType.Error, string.Empty); - entries.Add(rentry); - break; - } - } + //then we deal with the entries by using the EntryFactory class + //in the structure namespace + List entries = new List(); + for (int i = 0; i < entriesCount; i++) + { + byte entryType = reader.ReadByte(); + switch (entryType) + { + case 69: // 'E' + var eentry = new EgtEntry(); + entries.Add(eentry); + break; + case 98: // 'b' + byte b = reader.ReadByte(); + var bentry = new EgtEntry(EgtEntryType.Byte, b); + entries.Add(bentry); + break; + case 66: // 'B' + bool a = reader.ReadBoolean(); + var aentry = new EgtEntry(EgtEntryType.Boolean, a); + entries.Add(aentry); + break; + case 73: // 'I' + ushort v = reader.RawReadUInt16(); + var ientry = new EgtEntry(EgtEntryType.UInt16, v); + entries.Add(ientry); + break; + case 83: // 'S' + string s = reader.ReadUnicodeString(); + var sentry = new EgtEntry(EgtEntryType.String, s); + entries.Add(sentry); + break; + default: //Unknown + var rentry = new EgtEntry(EgtEntryType.Error, string.Empty); + entries.Add(rentry); + break; + } + } - EgtRecord r = new EgtRecord(entriesCount, entries); - records.Add(r); - } - return new EgtFile(records, header); - } - private static List readEgtFileRecords(Utf16Reader reader) - { - //create CGTStructure from the file. this happens in the CGTReader class - //in the main dir in "Reader.cs". First we read the file header like so - string header = reader.ReadUnicodeString(); + EgtRecord r = new EgtRecord(entriesCount, entries); + records.Add(r); + } + return new EgtFile(records, header); + } + private static List readEgtFileRecords(Utf16Reader reader) + { + //create CGTStructure from the file. this happens in the CGTReader class + //in the main dir in "Reader.cs". First we read the file header like so + string header = reader.ReadUnicodeString(); - //If the header don't start with "GOLD", then it is not valid - if (!header.StartsWith("GOLD")) return null; + //If the header don't start with "GOLD", then it is not valid + if (!header.StartsWith("GOLD")) return null; - //after reading the header, we read records till the end of the stream - List records = new List(); - while (reader.BaseStream.Position != reader.BaseStream.Length) - { - //we read record header first, and it must be 77 which is 'M' - byte entriesHeader = reader.ReadByte(); - if (entriesHeader != 77) return null; + //after reading the header, we read records till the end of the stream + List records = new List(); + while (reader.BaseStream.Position != reader.BaseStream.Length) + { + //we read record header first, and it must be 77 which is 'M' + byte entriesHeader = reader.ReadByte(); + if (entriesHeader != 77) return null; - //next is count of entries in the record - ushort entriesCount = reader.ReadUInt16(); + //next is count of entries in the record + ushort entriesCount = reader.ReadUInt16(); - //then we deal with the entries by using the EntryFactory class - //in the structure namespace - List entries = new List(); - for (int i = 0; i < entriesCount; i++) - { - byte entryType = reader.ReadByte(); - switch (entryType) - { - case 69: // 'E' - var eentry = new EgtEntry(); - entries.Add(eentry); - break; - case 98: // 'b' - byte b = reader.ReadByte(); - var bentry = new EgtEntry(EgtEntryType.Byte, b); - entries.Add(bentry); - break; - case 66: // 'B' - bool a = reader.ReadBoolean(); - var aentry = new EgtEntry(EgtEntryType.Boolean, a); - entries.Add(aentry); - break; - case 73: // 'I' - ushort v = reader.RawReadUInt16(); - var ientry = new EgtEntry(EgtEntryType.UInt16, v); - entries.Add(ientry); - break; - case 83: // 'S' - string s = reader.ReadUnicodeString(); - var sentry = new EgtEntry(EgtEntryType.String, s); - entries.Add(sentry); - break; - default: //Unknown - var rentry = new EgtEntry(EgtEntryType.Error, string.Empty); - entries.Add(rentry); - break; - } - } + //then we deal with the entries by using the EntryFactory class + //in the structure namespace + List entries = new List(); + for (int i = 0; i < entriesCount; i++) + { + byte entryType = reader.ReadByte(); + switch (entryType) + { + case 69: // 'E' + var eentry = new EgtEntry(); + entries.Add(eentry); + break; + case 98: // 'b' + byte b = reader.ReadByte(); + var bentry = new EgtEntry(EgtEntryType.Byte, b); + entries.Add(bentry); + break; + case 66: // 'B' + bool a = reader.ReadBoolean(); + var aentry = new EgtEntry(EgtEntryType.Boolean, a); + entries.Add(aentry); + break; + case 73: // 'I' + ushort v = reader.RawReadUInt16(); + var ientry = new EgtEntry(EgtEntryType.UInt16, v); + entries.Add(ientry); + break; + case 83: // 'S' + string s = reader.ReadUnicodeString(); + var sentry = new EgtEntry(EgtEntryType.String, s); + entries.Add(sentry); + break; + default: //Unknown + var rentry = new EgtEntry(EgtEntryType.Error, string.Empty); + entries.Add(rentry); + break; + } + } - EgtRecord r = new EgtRecord(entriesCount, entries); - records.Add(r); - } - return records; - } + EgtRecord r = new EgtRecord(entriesCount, entries); + records.Add(r); + } + return records; + } - //Read a uint in little endian. This is the format already used - //by the .NET BinaryReader. However, it is good to specificially - //define this given byte order can change depending on platform. - private static ushort RawReadUInt16(Utf16Reader reader) - { - int b0 = reader.ReadByte(); - int b1 = reader.ReadByte(); + //Read a uint in little endian. This is the format already used + //by the .NET BinaryReader. However, it is good to specificially + //define this given byte order can change depending on platform. + private static ushort RawReadUInt16(Utf16Reader reader) + { + int b0 = reader.ReadByte(); + int b1 = reader.ReadByte(); - return (ushort)((b1 << 8) + b0); - } - private static string RawReadCString(Utf16Reader reader) - { - string text = string.Empty; - bool done = false; + return (ushort)((b1 << 8) + b0); + } + private static string RawReadCString(Utf16Reader reader) + { + string text = string.Empty; + bool done = false; - while (!done) - { - ushort char16 = RawReadUInt16(reader); - if (char16 == 0) - { - done = true; - } - else - { - text += (char)char16; - } - } + while (!done) + { + ushort char16 = RawReadUInt16(reader); + if (char16 == 0) + { + done = true; + } + else + { + text += (char)char16; + } + } - return text; - } - } + return text; + } + } } \ No newline at end of file diff --git a/@GoldParserEngine/GoldParserEngine/Egt/EgtEntry.cs b/@GoldParserEngine/GoldParserEngine/Egt/EgtEntry.cs index c2b44ebf..18c495ba 100644 --- a/@GoldParserEngine/GoldParserEngine/Egt/EgtEntry.cs +++ b/@GoldParserEngine/GoldParserEngine/Egt/EgtEntry.cs @@ -1,83 +1,79 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace GoldParser.Egt { - public enum EgtEntryType - { - Empty = 69, - UInt16 = 73, - String = 83, - Boolean = 66, - Byte = 98, - Error = 0 - } - public class EgtEntry - { - private EgtEntryType _type; - private object _value; + public enum EgtEntryType + { + Empty = 69, + UInt16 = 73, + String = 83, + Boolean = 66, + Byte = 98, + Error = 0 + } + public class EgtEntry + { + private EgtEntryType _type; + private object _value; - public EgtEntry() - { - _type = EgtEntryType.Empty; - _value = null; - } - public EgtEntry(EgtEntryType type, object value) - { - _type = type; - _value = value; - } + public EgtEntry() + { + _type = EgtEntryType.Empty; + _value = null; + } + public EgtEntry(EgtEntryType type, object value) + { + _type = type; + _value = value; + } - public EgtEntryType Type - { - get - { - return _type; - } - } - public bool IsEmpty() + public EgtEntryType Type + { + get + { + return _type; + } + } + public bool IsEmpty() + { + return _type == EgtEntryType.Empty; + } + public bool IsError() + { + return _type == EgtEntryType.Error; + } + public int? GetInt() { - return _type == EgtEntryType.Empty; + if (_value != null && _value is ushort) + { + return (UInt16)_value; + } + return null; + } + public bool? GetBool() + { + if (_value != null && _value is bool) + { + return (bool)_value; + } + return null; } - public bool IsError() - { - return _type == EgtEntryType.Error; - } - public int? GetInt() - { - if (_value != null && _value is ushort) - { - return (UInt16)_value; - } - return null; - } - public bool? GetBool() - { - if(_value != null && _value is bool) + public byte? GetByte() + { + if (_value != null && _value is byte) + { + return (byte)_value; + } + return null; + } + public string GetString() + { + if (_value != null && _value is string) { - return (bool)_value; + return (string)_value; } - return null; - } - public byte? GetByte() - { - if (_value != null && _value is byte) - { - return (byte)_value; - } - return null; - } - public string GetString() - { - if (_value != null && _value is string) - { - return (string)_value; - } - return null; - } - } + return null; + } + } } diff --git a/@GoldParserEngine/GoldParserEngine/Egt/EgtException.cs b/@GoldParserEngine/GoldParserEngine/Egt/EgtException.cs index 7a4e9d68..3bc63cad 100644 --- a/@GoldParserEngine/GoldParserEngine/Egt/EgtException.cs +++ b/@GoldParserEngine/GoldParserEngine/Egt/EgtException.cs @@ -1,22 +1,18 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace GoldParser.Egt { public class EgtException : Exception { - public string Method; + public string Method; - public EgtException(string message) : base(message) - { - Method = ""; - } - public EgtException(string message, Exception inner, string method) : base(message, inner) - { - Method = method; - } - } + public EgtException(string message) : base(message) + { + Method = ""; + } + public EgtException(string message, Exception inner, string method) : base(message, inner) + { + Method = method; + } + } } diff --git a/@GoldParserEngine/GoldParserEngine/Egt/EgtFile.cs b/@GoldParserEngine/GoldParserEngine/Egt/EgtFile.cs index 7c9e8925..688839a3 100644 --- a/@GoldParserEngine/GoldParserEngine/Egt/EgtFile.cs +++ b/@GoldParserEngine/GoldParserEngine/Egt/EgtFile.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; namespace GoldParser.Egt { diff --git a/@GoldParserEngine/GoldParserEngine/Egt/EgtRecord.cs b/@GoldParserEngine/GoldParserEngine/Egt/EgtRecord.cs index ae2e6452..3c17b71e 100644 --- a/@GoldParserEngine/GoldParserEngine/Egt/EgtRecord.cs +++ b/@GoldParserEngine/GoldParserEngine/Egt/EgtRecord.cs @@ -1,64 +1,60 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; namespace GoldParser.Egt { - /* + /* * Disasmembler * GOLD Engine, Version=5.0.4337.25397, Culture=neutral, PublicKeyToken=null * GOLD.Parser.LoadTables(string Path) * ->LoadTables(new BinaryReader(File.Open(Path, FileMode.Open, FileAccess.Read))); */ - //records appear in order as follows. - //No record groups appear twice. - //This is for my own custom grammar, though. - // - //"value":"0x70" Property 8 records - //"value":"0x74" TableCounts 1 record - //"value":"0x49" InitialStates 1 record - //"value":"0x63" CharRanges 19 records - //"value":"0x53" Symbol 42 records - //"value":"0x67" Group 2 records - //"value":"0x52" Production 38 records - //"value":"0x44" DFAState 39 records - //"value":"0x4C" LRState 53 records - //GroupNesting is not used + //records appear in order as follows. + //No record groups appear twice. + //This is for my own custom grammar, though. + // + //"value":"0x70" Property 8 records + //"value":"0x74" TableCounts 1 record + //"value":"0x49" InitialStates 1 record + //"value":"0x63" CharRanges 19 records + //"value":"0x53" Symbol 42 records + //"value":"0x67" Group 2 records + //"value":"0x52" Production 38 records + //"value":"0x44" DFAState 39 records + //"value":"0x4C" LRState 53 records + //GroupNesting is not used - public enum EgtRecordType : byte - { - InitialStates = 0x49, //73 in dec - Symbol = 0x53, //83 in dec - Production = 0x52, //82 in dec - DFAState = 0x44, //68 in dec - LRState = 0x4C, //76 in dec - Property = 0x70, //112 in dec - CharRanges = 0x63, //99 in dec - Group = 0x67, //103 in dec - GroupNesting = 0x6E, //110 in dec - TableCounts = 0x74 //116 in dec - } - public class EgtRecord - { - private int _entriesCount; - public List Entries; + public enum EgtRecordType : byte + { + InitialStates = 0x49, //73 in dec + Symbol = 0x53, //83 in dec + Production = 0x52, //82 in dec + DFAState = 0x44, //68 in dec + LRState = 0x4C, //76 in dec + Property = 0x70, //112 in dec + CharRanges = 0x63, //99 in dec + Group = 0x67, //103 in dec + GroupNesting = 0x6E, //110 in dec + TableCounts = 0x74 //116 in dec + } + public class EgtRecord + { + private int _entriesCount; + public List Entries; - public int EntriesCount + public int EntriesCount { - get + get { - return _entriesCount; + return _entriesCount; } } - public EgtRecord(int ecount, List entries) - { - _entriesCount = ecount; - Entries = entries; - } - } + public EgtRecord(int ecount, List entries) + { + _entriesCount = ecount; + Entries = entries; + } + } } diff --git a/@GoldParserEngine/GoldParserEngine/Egt/Utf16Reader.cs b/@GoldParserEngine/GoldParserEngine/Egt/Utf16Reader.cs index 72bcf4c8..3926d3d2 100644 --- a/@GoldParserEngine/GoldParserEngine/Egt/Utf16Reader.cs +++ b/@GoldParserEngine/GoldParserEngine/Egt/Utf16Reader.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using System.IO; +using System.IO; using System.Text; namespace GoldParser.Egt diff --git a/@GoldParserEngine/GoldParserEngine/GoldParserEngine.csproj b/@GoldParserEngine/GoldParserEngine/GoldParserEngine.csproj index e74357b5..2aea9109 100644 --- a/@GoldParserEngine/GoldParserEngine/GoldParserEngine.csproj +++ b/@GoldParserEngine/GoldParserEngine/GoldParserEngine.csproj @@ -71,10 +71,6 @@ - - - - diff --git a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarCharacterSet.cs b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarCharacterSet.cs index d812a8bc..98873ecd 100644 --- a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarCharacterSet.cs +++ b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarCharacterSet.cs @@ -7,115 +7,115 @@ namespace GoldParser.Grammar /// public class GrammarCharacterSet { - private readonly int _tableIndex; + private readonly int _tableIndex; - /// - /// The ranges in this set - /// - public List Ranges - { - get; - set; - } + /// + /// The ranges in this set + /// + public List Ranges + { + get; + set; + } - /// - /// Get one of the ranges in this set - /// - /// The index of the range - /// A character range - public GrammarCharacterRange this[int index] - { - get - { - return Ranges[index]; - } - set - { - Ranges[index] = value; - } - } + /// + /// Get one of the ranges in this set + /// + /// The index of the range + /// A character range + public GrammarCharacterRange this[int index] + { + get + { + return Ranges[index]; + } + set + { + Ranges[index] = value; + } + } - /// - /// Returns the index of the Set in the CharacterSet Table. - /// - public int TableIndex - { - get { return _tableIndex; } - } + /// + /// Returns the index of the Set in the CharacterSet Table. + /// + public int TableIndex + { + get { return _tableIndex; } + } - /// - /// Ctor. - /// - public GrammarCharacterSet(int tableIndex) + /// + /// Ctor. + /// + public GrammarCharacterSet(int tableIndex) { - Ranges = new List(); - _tableIndex = tableIndex; - } + Ranges = new List(); + _tableIndex = tableIndex; + } - /// - /// Add a range to the current CharacterSet - /// - /// The range to add - public void Add(GrammarCharacterRange range) - { - Ranges.Add(range); - } + /// + /// Add a range to the current CharacterSet + /// + /// The range to add + public void Add(GrammarCharacterRange range) + { + Ranges.Add(range); + } - /// - /// Wether the current CharacterSet contains the specified character - /// in some of its ranges - /// - /// the char code of the character - /// true if the CharacterSet contains it - public bool Contains(int charCode) - { - bool flag = false; - int x = 0; - while (x < Ranges.Count && flag == false) - { - GrammarCharacterRange characterRange = Ranges[x]; - flag = (charCode >= characterRange.Start && charCode <= characterRange.End); - x++; - } - return flag; - } - } + /// + /// Wether the current CharacterSet contains the specified character + /// in some of its ranges + /// + /// the char code of the character + /// true if the CharacterSet contains it + public bool Contains(int charCode) + { + bool flag = false; + int x = 0; + while (x < Ranges.Count && flag == false) + { + GrammarCharacterRange characterRange = Ranges[x]; + flag = (charCode >= characterRange.Start && charCode <= characterRange.End); + x++; + } + return flag; + } + } - /// - /// Stores a range of characters. If start and end are the same, - /// it is a single character, while otherwise it is a true range. - /// - public class GrammarCharacterRange - { - /// - /// Char-code. First character in the range - /// - public int Start - { - get; - set; - } + /// + /// Stores a range of characters. If start and end are the same, + /// it is a single character, while otherwise it is a true range. + /// + public class GrammarCharacterRange + { + /// + /// Char-code. First character in the range + /// + public int Start + { + get; + set; + } - /// - /// Char-code. Last character in the range - /// - public int End - { - get; - set; - } + /// + /// Char-code. Last character in the range + /// + public int End + { + get; + set; + } - /// - /// Ctor. - /// - /// first character - /// last character - public GrammarCharacterRange(int start, int end) - { - Start = start; - End = end; - } - } + /// + /// Ctor. + /// + /// first character + /// last character + public GrammarCharacterRange(int start, int end) + { + Start = start; + End = end; + } + } } diff --git a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarContentReader.cs b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarContentReader.cs index 8b6f3641..ff7a54ce 100644 --- a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarContentReader.cs +++ b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarContentReader.cs @@ -1,201 +1,197 @@ using GoldParser.Egt; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace GoldParser.Grammar { public class GrammarContentReader { - public static GrammarTables ReadGrammar(EgtFile file) - { - List records = file.Records; - GrammarTables g = ReadGrammar(records); - return g; - } - public static GrammarTables ReadGrammar(List records) - { - GrammarTables grammar = new GrammarTables(); - grammar.Properties = new GrammarProperties(); - grammar.TableCounts = new GrammarTableCounts(); - grammar.InitialStates = new GrammarInitialStates(); + public static GrammarTables ReadGrammar(EgtFile file) + { + List records = file.Records; + GrammarTables g = ReadGrammar(records); + return g; + } + public static GrammarTables ReadGrammar(List records) + { + GrammarTables grammar = new GrammarTables(); + grammar.Properties = new GrammarProperties(); + grammar.TableCounts = new GrammarTableCounts(); + grammar.InitialStates = new GrammarInitialStates(); - for (int i = 0; i < records.Count; i++) - { - EgtRecord record = records[i]; - EgtRecordType type = (EgtRecordType)record.Entries[0].GetByte(); - switch (type) - { - case EgtRecordType.Property: - loadProperty(grammar, record); - continue; - case EgtRecordType.TableCounts: - loadTableCounts(grammar, record); - continue; - case EgtRecordType.InitialStates: - loadInitialStates(grammar, record); - continue; - case EgtRecordType.CharRanges: - loadCharRange(grammar, record); - continue; - case EgtRecordType.Symbol: - loadSymbol(grammar, record); - continue; - case EgtRecordType.Group: - loadGroup(grammar, record); - continue; - case EgtRecordType.Production: - loadProduction(grammar, record); - continue; - case EgtRecordType.DFAState: - loadDfaState(grammar, record); - continue; - case EgtRecordType.LRState: - loadLrState(grammar, record); - continue; - } - } + for (int i = 0; i < records.Count; i++) + { + EgtRecord record = records[i]; + EgtRecordType type = (EgtRecordType)record.Entries[0].GetByte(); + switch (type) + { + case EgtRecordType.Property: + loadProperty(grammar, record); + continue; + case EgtRecordType.TableCounts: + loadTableCounts(grammar, record); + continue; + case EgtRecordType.InitialStates: + loadInitialStates(grammar, record); + continue; + case EgtRecordType.CharRanges: + loadCharRange(grammar, record); + continue; + case EgtRecordType.Symbol: + loadSymbol(grammar, record); + continue; + case EgtRecordType.Group: + loadGroup(grammar, record); + continue; + case EgtRecordType.Production: + loadProduction(grammar, record); + continue; + case EgtRecordType.DFAState: + loadDfaState(grammar, record); + continue; + case EgtRecordType.LRState: + loadLrState(grammar, record); + continue; + } + } - grammar.TablesLoaded = true; - return grammar; - } + grammar.TablesLoaded = true; + return grammar; + } - static void loadProperty(GrammarTables grammar, EgtRecord record) - { - int index = record.Entries[1].GetInt().Value; - string name = record.Entries[2].GetString(); - string value = record.Entries[3].GetString(); + static void loadProperty(GrammarTables grammar, EgtRecord record) + { + int index = record.Entries[1].GetInt().Value; + string name = record.Entries[2].GetString(); + string value = record.Entries[3].GetString(); - if (grammar.Properties == null) grammar.Properties = new GrammarProperties(); - grammar.Properties.SetValue(index, name, value); - } - static void loadTableCounts(GrammarTables grammar, EgtRecord record) - { - grammar.TableCounts.SymbolTableCount = record.Entries[1].GetInt().Value; - grammar.TableCounts.CharsetTableCount = record.Entries[2].GetInt().Value; - grammar.TableCounts.ProductionTableCount = record.Entries[3].GetInt().Value; - grammar.TableCounts.DfaTableCount = record.Entries[4].GetInt().Value; - grammar.TableCounts.LrStatesTableCount = record.Entries[5].GetInt().Value; - grammar.TableCounts.GroupTableCount = record.Entries[6].GetInt().Value; + if (grammar.Properties == null) grammar.Properties = new GrammarProperties(); + grammar.Properties.SetValue(index, name, value); + } + static void loadTableCounts(GrammarTables grammar, EgtRecord record) + { + grammar.TableCounts.SymbolTableCount = record.Entries[1].GetInt().Value; + grammar.TableCounts.CharsetTableCount = record.Entries[2].GetInt().Value; + grammar.TableCounts.ProductionTableCount = record.Entries[3].GetInt().Value; + grammar.TableCounts.DfaTableCount = record.Entries[4].GetInt().Value; + grammar.TableCounts.LrStatesTableCount = record.Entries[5].GetInt().Value; + grammar.TableCounts.GroupTableCount = record.Entries[6].GetInt().Value; - grammar.CharacterSets = new GrammarCharacterSet[grammar.TableCounts.CharsetTableCount]; - grammar.Symbols = new GrammarSymbol[grammar.TableCounts.SymbolTableCount]; - grammar.Groups = new GrammarGroup[grammar.TableCounts.GroupTableCount]; - grammar.Productions = new GrammarProduction[grammar.TableCounts.ProductionTableCount]; - grammar.FAStates = new GrammarFAState[grammar.TableCounts.DfaTableCount]; - grammar.LRStates = new GrammarLRState[grammar.TableCounts.LrStatesTableCount]; - } - static void loadInitialStates(GrammarTables grammar, EgtRecord record) - { - grammar.InitialStates.DfaInitialState = record.Entries[1].GetInt().Value; - grammar.InitialStates.LrInitialState = record.Entries[2].GetInt().Value; - } - static void loadCharRange(GrammarTables grammar, EgtRecord record) - { - int rangesIndex = record.Entries[1].GetInt().Value; - //record.Entries[2] is skipped - //record.Entries[3] is skipped - //record.Entries[4] is skipped + grammar.CharacterSets = new GrammarCharacterSet[grammar.TableCounts.CharsetTableCount]; + grammar.Symbols = new GrammarSymbol[grammar.TableCounts.SymbolTableCount]; + grammar.Groups = new GrammarGroup[grammar.TableCounts.GroupTableCount]; + grammar.Productions = new GrammarProduction[grammar.TableCounts.ProductionTableCount]; + grammar.FAStates = new GrammarFAState[grammar.TableCounts.DfaTableCount]; + grammar.LRStates = new GrammarLRState[grammar.TableCounts.LrStatesTableCount]; + } + static void loadInitialStates(GrammarTables grammar, EgtRecord record) + { + grammar.InitialStates.DfaInitialState = record.Entries[1].GetInt().Value; + grammar.InitialStates.LrInitialState = record.Entries[2].GetInt().Value; + } + static void loadCharRange(GrammarTables grammar, EgtRecord record) + { + int rangesIndex = record.Entries[1].GetInt().Value; + //record.Entries[2] is skipped + //record.Entries[3] is skipped + //record.Entries[4] is skipped - grammar.CharacterSets[rangesIndex] = new GrammarCharacterSet(rangesIndex); - for (int k = 5; k < record.EntriesCount; k += 2) - { - int start = record.Entries[k].GetInt().Value; - int end = record.Entries[k + 1].GetInt().Value; - GrammarCharacterRange range = new GrammarCharacterRange(start, end); - grammar.CharacterSets[rangesIndex].Add(range); - } - } - static void loadSymbol(GrammarTables grammar, EgtRecord record) - { - int symbolTableIndex = record.Entries[1].GetInt().Value; - string symbolname = record.Entries[2].GetString(); - GrammarSymbolType symboltype = (GrammarSymbolType)record.Entries[3].GetInt().Value; - GrammarSymbol symbol = new GrammarSymbol(symbolname, symboltype, symbolTableIndex); - grammar.Symbols[symbolTableIndex] = symbol; - } - static void loadGroup(GrammarTables grammar, EgtRecord record) - { - GrammarGroup group = new GrammarGroup(); - int groupTableIndex = record.Entries[1].GetInt().Value; + grammar.CharacterSets[rangesIndex] = new GrammarCharacterSet(rangesIndex); + for (int k = 5; k < record.EntriesCount; k += 2) + { + int start = record.Entries[k].GetInt().Value; + int end = record.Entries[k + 1].GetInt().Value; + GrammarCharacterRange range = new GrammarCharacterRange(start, end); + grammar.CharacterSets[rangesIndex].Add(range); + } + } + static void loadSymbol(GrammarTables grammar, EgtRecord record) + { + int symbolTableIndex = record.Entries[1].GetInt().Value; + string symbolname = record.Entries[2].GetString(); + GrammarSymbolType symboltype = (GrammarSymbolType)record.Entries[3].GetInt().Value; + GrammarSymbol symbol = new GrammarSymbol(symbolname, symboltype, symbolTableIndex); + grammar.Symbols[symbolTableIndex] = symbol; + } + static void loadGroup(GrammarTables grammar, EgtRecord record) + { + GrammarGroup group = new GrammarGroup(); + int groupTableIndex = record.Entries[1].GetInt().Value; - group.TableIndex = groupTableIndex; - group.Name = record.Entries[2].GetString(); - group.ContainerSymbol = grammar.Symbols[record.Entries[3].GetInt().Value]; - group.StartSymbol = grammar.Symbols[record.Entries[4].GetInt().Value]; - group.EndSymbol = grammar.Symbols[record.Entries[5].GetInt().Value]; - group.Advance = (GrammarGroupAdvanceMode)record.Entries[6].GetInt().Value; - group.Ending = (GrammarGroupEndingMode)record.Entries[7].GetInt().Value; - //record.Entries[8] is skipped - int num = record.Entries[9].GetInt().Value; - for (int j = 1; j <= num; j++) - { - int nest = record.Entries[9 + j].GetInt().Value; - group.Nesting.Add(nest); - } - group.ContainerSymbol.Group = group; - group.StartSymbol.Group = group; - group.EndSymbol.Group = group; + group.TableIndex = groupTableIndex; + group.Name = record.Entries[2].GetString(); + group.ContainerSymbol = grammar.Symbols[record.Entries[3].GetInt().Value]; + group.StartSymbol = grammar.Symbols[record.Entries[4].GetInt().Value]; + group.EndSymbol = grammar.Symbols[record.Entries[5].GetInt().Value]; + group.Advance = (GrammarGroupAdvanceMode)record.Entries[6].GetInt().Value; + group.Ending = (GrammarGroupEndingMode)record.Entries[7].GetInt().Value; + //record.Entries[8] is skipped + int num = record.Entries[9].GetInt().Value; + for (int j = 1; j <= num; j++) + { + int nest = record.Entries[9 + j].GetInt().Value; + group.Nesting.Add(nest); + } + group.ContainerSymbol.Group = group; + group.StartSymbol.Group = group; + group.EndSymbol.Group = group; - grammar.Groups[groupTableIndex] = group; - } - static void loadProduction(GrammarTables grammar, EgtRecord record) - { - int prodTableIndex = record.Entries[1].GetInt().Value; - int symbol = record.Entries[2].GetInt().Value; - //record.Entries[3] is skipped + grammar.Groups[groupTableIndex] = group; + } + static void loadProduction(GrammarTables grammar, EgtRecord record) + { + int prodTableIndex = record.Entries[1].GetInt().Value; + int symbol = record.Entries[2].GetInt().Value; + //record.Entries[3] is skipped - grammar.Productions[prodTableIndex] = new GrammarProduction(grammar.Symbols[symbol], prodTableIndex); - for (int l = 4; l < record.EntriesCount; l++) - { - int x = record.Entries[l].GetInt().Value; - grammar.Productions[prodTableIndex].Handle.Add(grammar.Symbols[x]); - } - } - static void loadDfaState(GrammarTables grammar, EgtRecord record) - { - int dfaIndex = record.Entries[1].GetInt().Value; - bool flag = record.Entries[2].GetBool().Value; - int symbolIndex = record.Entries[3].GetInt().Value; - //record.Entries[4] is skipped + grammar.Productions[prodTableIndex] = new GrammarProduction(grammar.Symbols[symbol], prodTableIndex); + for (int l = 4; l < record.EntriesCount; l++) + { + int x = record.Entries[l].GetInt().Value; + grammar.Productions[prodTableIndex].Handle.Add(grammar.Symbols[x]); + } + } + static void loadDfaState(GrammarTables grammar, EgtRecord record) + { + int dfaIndex = record.Entries[1].GetInt().Value; + bool flag = record.Entries[2].GetBool().Value; + int symbolIndex = record.Entries[3].GetInt().Value; + //record.Entries[4] is skipped - if (flag) - { - grammar.FAStates[dfaIndex] = new GrammarFAState(grammar.Symbols[symbolIndex], dfaIndex); - } - else - { - grammar.FAStates[dfaIndex] = new GrammarFAState(dfaIndex); - } - for (int m = 5; m < record.EntriesCount; m += 3) - { - int y = record.Entries[m].GetInt().Value; - int target = record.Entries[m + 1].GetInt().Value; - //record.Entries[m + 2] is skipped - GrammarFAEdge edge = new GrammarFAEdge(grammar.CharacterSets[y], target); - grammar.FAStates[dfaIndex].Edges.Add(edge); - } - } - static void loadLrState(GrammarTables grammar, EgtRecord record) - { - int z = record.Entries[1].GetInt().Value; - //record.Entries[2] is skipped - grammar.LRStates[z] = new GrammarLRState(z); + if (flag) + { + grammar.FAStates[dfaIndex] = new GrammarFAState(grammar.Symbols[symbolIndex], dfaIndex); + } + else + { + grammar.FAStates[dfaIndex] = new GrammarFAState(dfaIndex); + } + for (int m = 5; m < record.EntriesCount; m += 3) + { + int y = record.Entries[m].GetInt().Value; + int target = record.Entries[m + 1].GetInt().Value; + //record.Entries[m + 2] is skipped + GrammarFAEdge edge = new GrammarFAEdge(grammar.CharacterSets[y], target); + grammar.FAStates[dfaIndex].Edges.Add(edge); + } + } + static void loadLrState(GrammarTables grammar, EgtRecord record) + { + int z = record.Entries[1].GetInt().Value; + //record.Entries[2] is skipped + grammar.LRStates[z] = new GrammarLRState(z); - for (int o = 3; o < record.EntriesCount; o += 4) - { - int gsymbol = record.Entries[o].GetInt().Value; - int type = record.Entries[o + 1].GetInt().Value; - int gvalue = record.Entries[o + 2].GetInt().Value; - //record.Entries[o + 3] is skipped - grammar.LRStates[z].Add( - new GrammarLRAction( - grammar.Symbols[gsymbol], - (LRActionType)type, - gvalue)); - } - } - } + for (int o = 3; o < record.EntriesCount; o += 4) + { + int gsymbol = record.Entries[o].GetInt().Value; + int type = record.Entries[o + 1].GetInt().Value; + int gvalue = record.Entries[o + 2].GetInt().Value; + //record.Entries[o + 3] is skipped + grammar.LRStates[z].Add( + new GrammarLRAction( + grammar.Symbols[gsymbol], + (LRActionType)type, + gvalue)); + } + } + } } diff --git a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarException.cs b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarException.cs index 86a00da5..5b988781 100644 --- a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarException.cs +++ b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarException.cs @@ -1,22 +1,18 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace GoldParser.Grammar { - public class GrammarException : Exception - { - public string Method; + public class GrammarException : Exception + { + public string Method; - public GrammarException(string message) : base(message) - { - Method = ""; - } - public GrammarException(string message, Exception inner, string method) : base(message, inner) - { - Method = method; - } - } + public GrammarException(string message) : base(message) + { + Method = ""; + } + public GrammarException(string message, Exception inner, string method) : base(message, inner) + { + Method = method; + } + } } diff --git a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarFAState.cs b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarFAState.cs index 8c4eee25..9e3955a3 100644 --- a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarFAState.cs +++ b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarFAState.cs @@ -7,88 +7,88 @@ namespace GoldParser.Grammar /// public class GrammarFAState { - private readonly int _tableIndex; + private readonly int _tableIndex; - /// - /// All the possible transitions (aka edges) from this state - /// - public List Edges + /// + /// All the possible transitions (aka edges) from this state + /// + public List Edges { - get; - set; + get; + set; } - /// - /// A symbol that is accepted in this state. If not null, we - /// could stop parsing and produce a token of that symbol's kind. - /// - public GrammarSymbol Accept - { - get; - set; - } + /// + /// A symbol that is accepted in this state. If not null, we + /// could stop parsing and produce a token of that symbol's kind. + /// + public GrammarSymbol Accept + { + get; + set; + } - /// - /// Returns the index of the state in the FAState Table. - /// - public int TableIndex - { - get { return _tableIndex; } - } + /// + /// Returns the index of the state in the FAState Table. + /// + public int TableIndex + { + get { return _tableIndex; } + } - /// - /// Ctor. - /// - public GrammarFAState(int tableIndex) - { - Accept = null; - Edges = new List(); - _tableIndex = tableIndex; - } + /// + /// Ctor. + /// + public GrammarFAState(int tableIndex) + { + Accept = null; + Edges = new List(); + _tableIndex = tableIndex; + } - /// - /// Ctor. - /// - public GrammarFAState(GrammarSymbol accept, int tableIndex) - { - Accept = accept; - Edges = new List(); - _tableIndex = tableIndex; - } - } + /// + /// Ctor. + /// + public GrammarFAState(GrammarSymbol accept, int tableIndex) + { + Accept = accept; + Edges = new List(); + _tableIndex = tableIndex; + } + } - /// - /// Represents a DFA transition, aka edge - /// + /// + /// Represents a DFA transition, aka edge + /// public class GrammarFAEdge { - /// - /// The character set that will allow the transition - /// - public GrammarCharacterSet CharacterSet; + /// + /// The character set that will allow the transition + /// + public GrammarCharacterSet CharacterSet; - /// - /// The new state to switch to - /// - public int Target; + /// + /// The new state to switch to + /// + public int Target; - /// - /// Ctor. - /// - public GrammarFAEdge() - { - } + /// + /// Ctor. + /// + public GrammarFAEdge() + { + } - /// - /// Ctor. - /// - public GrammarFAEdge(GrammarCharacterSet charSet, int target) - { - CharacterSet = charSet; - Target = target; - } - } + /// + /// Ctor. + /// + public GrammarFAEdge(GrammarCharacterSet charSet, int target) + { + CharacterSet = charSet; + Target = target; + } + } } // Characters is CharacterSet \ No newline at end of file diff --git a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarGroup.cs b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarGroup.cs index c7bd3ea6..7c018e70 100644 --- a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarGroup.cs +++ b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarGroup.cs @@ -6,110 +6,110 @@ namespace GoldParser.Grammar /// Lexer specific stuff /// public enum GrammarGroupAdvanceMode - { - Token, - Character - } + { + Token, + Character + } - /// - /// Closed GroupEndingMode means to add the group ending - /// token to the data, while Open means to discard it. - /// - public enum GrammarGroupEndingMode - { - Open, - Closed - } + /// + /// Closed GroupEndingMode means to add the group ending + /// token to the data, while Open means to discard it. + /// + public enum GrammarGroupEndingMode + { + Open, + Closed + } - /// - /// Groups are a special mode in the Lexer that starts with a Token - /// of a given symbol type, ends with one, and everything else is ignored - /// and all is outputted as a single token - /// - public class GrammarGroup + /// + /// Groups are a special mode in the Lexer that starts with a Token + /// of a given symbol type, ends with one, and everything else is ignored + /// and all is outputted as a single token + /// + public class GrammarGroup { - /// - /// The index of the group in the Group table - /// - public int TableIndex - { - get; - set; - } + /// + /// The index of the group in the Group table + /// + public int TableIndex + { + get; + set; + } - /// - /// The name of the group - /// - public string Name - { - get; - set; - } + /// + /// The name of the group + /// + public string Name + { + get; + set; + } - /// - /// The symbol that represent the group - For example Comment - /// - public GrammarSymbol ContainerSymbol - { - get; - set; - } + /// + /// The symbol that represent the group - For example Comment + /// + public GrammarSymbol ContainerSymbol + { + get; + set; + } - /// - /// The starting symbol - For example CommentStart - "/*" - /// - public GrammarSymbol StartSymbol - { - get; - set; - } + /// + /// The starting symbol - For example CommentStart - "/*" + /// + public GrammarSymbol StartSymbol + { + get; + set; + } - /// - /// The ending symbol - For example CommentEnd - "*/" - /// - public GrammarSymbol EndSymbol - { - get; - set; - } + /// + /// The ending symbol - For example CommentEnd - "*/" + /// + public GrammarSymbol EndSymbol + { + get; + set; + } - /// - /// The Lexer advance mode for the group - /// - public GrammarGroupAdvanceMode Advance - { - get; - set; - } + /// + /// The Lexer advance mode for the group + /// + public GrammarGroupAdvanceMode Advance + { + get; + set; + } - /// - /// The Lexer ending mode for the group - /// - public GrammarGroupEndingMode Ending - { - get; - set; - } + /// + /// The Lexer ending mode for the group + /// + public GrammarGroupEndingMode Ending + { + get; + set; + } - /// - /// List of the indexes (in the Group table) of all the - /// groups that can be nested inside of this one - /// - public List Nesting - { - get; - private set; - } + /// + /// List of the indexes (in the Group table) of all the + /// groups that can be nested inside of this one + /// + public List Nesting + { + get; + private set; + } - /// - /// Ctor. - /// - public GrammarGroup() - { - Advance = GrammarGroupAdvanceMode.Character; - Ending = GrammarGroupEndingMode.Closed; - Nesting = new List(); - } - } + /// + /// Ctor. + /// + public GrammarGroup() + { + Advance = GrammarGroupAdvanceMode.Character; + Ending = GrammarGroupEndingMode.Closed; + Nesting = new List(); + } + } } diff --git a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarInitialStates.cs b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarInitialStates.cs index 71e6040d..f31aef02 100644 --- a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarInitialStates.cs +++ b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarInitialStates.cs @@ -4,34 +4,34 @@ /// Stores the initial state numbers for the DFA and the LALR /// public class GrammarInitialStates - { - /// - /// The initial state of the DFA - /// - public int DfaInitialState - { - get; - set; - } + { + /// + /// The initial state of the DFA + /// + public int DfaInitialState + { + get; + set; + } - /// - /// The initial state of the LALR - /// - public int LrInitialState - { - get; - set; - } + /// + /// The initial state of the LALR + /// + public int LrInitialState + { + get; + set; + } - /// - /// Ctor. - /// - public GrammarInitialStates() - { - DfaInitialState = 0; - LrInitialState = 0; - } - } + /// + /// Ctor. + /// + public GrammarInitialStates() + { + DfaInitialState = 0; + LrInitialState = 0; + } + } } \ No newline at end of file diff --git a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarLRState.cs b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarLRState.cs index afd91501..9e2d618e 100644 --- a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarLRState.cs +++ b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarLRState.cs @@ -2,172 +2,172 @@ namespace GoldParser.Grammar { - /// - /// Types of LALR parser actions - /// + /// + /// Types of LALR parser actions + /// public enum LRActionType - { - Shift = 1, - Reduce, - Goto, - Accept, - Error - } - - /// - /// Represents a LALR parser state - /// - public class GrammarLRState { - /// - /// Getss the index of the symbol in the LR State Table. - /// - public int TableIndex - { - get; - private set; - } - - /// - /// All the possible actions in this state - /// - public List Actions + Shift = 1, + Reduce, + Goto, + Accept, + Error + } + + /// + /// Represents a LALR parser state + /// + public class GrammarLRState + { + /// + /// Getss the index of the symbol in the LR State Table. + /// + public int TableIndex + { + get; + private set; + } + + /// + /// All the possible actions in this state + /// + public List Actions + { + get; + private set; + } + + /// + /// Get action by its index + /// + /// The action's index in this state's action list + public GrammarLRAction this[int index] + { + get + { + return Actions[index]; + } + set + { + Actions[index] = value; + } + } + + /// + /// Get action by a symbol that triggers it + /// + /// The symbol + public GrammarLRAction this[GrammarSymbol sym] + { + get + { + int x = IndexOf(sym); + if (x != -1) + { + return Actions[x]; + } + else + { + return null; + } + } + set + { + int x = IndexOf(sym); + if (x != -1) + { + Actions[x] = value; + } + } + } + + + /// + /// Ctor. + /// + public GrammarLRState(int tableIndex) + { + TableIndex = tableIndex; + Actions = new List(); + } + + + + /// + /// Get the index of an action via a symbol that triggers it + /// + /// The symbol + public int IndexOf(GrammarSymbol item) + { + for (int i = 0; i < Actions.Count; i++) + { + if (item == Actions[i].Symbol) return i; + } + return -1; + } + + /// + /// Add an action + /// + /// The action to add + public void Add(GrammarLRAction Action) + { + Actions.Add(Action); + } + + /// + /// Count the actions + /// + /// + public int Count() { - get; - private set; + return Actions.Count; } + } - /// - /// Get action by its index - /// - /// The action's index in this state's action list - public GrammarLRAction this[int index] - { - get - { - return Actions[index]; - } - set - { - Actions[index] = value; - } - } - - /// - /// Get action by a symbol that triggers it - /// - /// The symbol - public GrammarLRAction this[GrammarSymbol sym] - { - get - { - int x = IndexOf(sym); - if (x != -1) - { - return Actions[x]; - } - else - { - return null; - } - } - set - { - int x = IndexOf(sym); - if (x != -1) - { - Actions[x] = value; - } - } - } - - - /// - /// Ctor. - /// - public GrammarLRState(int tableIndex) + /// + /// Represents a LALR parser action + /// + public class GrammarLRAction + { + /// + /// The Symbol that triggers this action to be chosen to be + /// executed + /// + public GrammarSymbol Symbol { - TableIndex = tableIndex; - Actions = new List(); + get; + set; } + /// + /// The type of the action + /// + public LRActionType Type + { + get; + set; + } + /// + /// The value of the action. The value means a different thing + /// depending on the type of action - if SHIFT, it is the index + /// of the new state. If REDUCE, it is the index of the production. + /// + public int Value + { + get; + set; + } - /// - /// Get the index of an action via a symbol that triggers it - /// - /// The symbol - public int IndexOf(GrammarSymbol item) - { - for (int i = 0; i < Actions.Count; i++) - { - if (item == Actions[i].Symbol) return i; - } - return -1; - } - - /// - /// Add an action - /// - /// The action to add - public void Add(GrammarLRAction Action) - { - Actions.Add(Action); - } - - /// - /// Count the actions - /// - /// - public int Count() - { - return Actions.Count; - } - } - - /// - /// Represents a LALR parser action - /// - public class GrammarLRAction - { - /// - /// The Symbol that triggers this action to be chosen to be - /// executed - /// - public GrammarSymbol Symbol - { - get; - set; - } - - /// - /// The type of the action - /// - public LRActionType Type - { - get; - set; - } - - /// - /// The value of the action. The value means a different thing - /// depending on the type of action - if SHIFT, it is the index - /// of the new state. If REDUCE, it is the index of the production. - /// - public int Value - { - get; - set; - } - - /// - /// Ctor. - /// - public GrammarLRAction(GrammarSymbol symbol, LRActionType type, int value) - { - Symbol = symbol; - Type = type; - Value = value; - } - } + /// + /// Ctor. + /// + public GrammarLRAction(GrammarSymbol symbol, LRActionType type, int value) + { + Symbol = symbol; + Type = type; + Value = value; + } + } } diff --git a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarProduction.cs b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarProduction.cs index 2df0e019..d90e6bfc 100644 --- a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarProduction.cs +++ b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarProduction.cs @@ -11,28 +11,28 @@ public class GrammarProduction /// /// The index of this production in the Production table /// - public int TableIndex - { - get; - set; + public int TableIndex + { + get; + set; } /// /// The symbol at the left hand side of the production /// - public GrammarSymbol Head - { - get; - set; + public GrammarSymbol Head + { + get; + set; } /// /// The symbols of the right hand side of the production (after the "->") /// - public List Handle - { - get; - set; + public List Handle + { + get; + set; } diff --git a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarSymbol.cs b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarSymbol.cs index e35a1580..9daa94af 100644 --- a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarSymbol.cs +++ b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarSymbol.cs @@ -4,15 +4,15 @@ /// Types of symbols. /// public enum GrammarSymbolType - { - Nonterminal, - Terminal, - Noise, - End, - GroupStart, - GroupEnd, - Error = 7 - } + { + Nonterminal, + Terminal, + Noise, + End, + GroupStart, + GroupEnd, + Error = 7 + } /// /// A Grammar Symbol. diff --git a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarTableCounts.cs b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarTableCounts.cs index 0fa2b7a4..7db9b350 100644 --- a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarTableCounts.cs +++ b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarTableCounts.cs @@ -8,73 +8,73 @@ /// public class GrammarTableCounts { - /// - /// Number of symbol entries - /// - public int SymbolTableCount - { - get; - set; - } + /// + /// Number of symbol entries + /// + public int SymbolTableCount + { + get; + set; + } - /// - /// Number of character set entries - /// - public int CharsetTableCount - { - get; - set; - } + /// + /// Number of character set entries + /// + public int CharsetTableCount + { + get; + set; + } - /// - /// Number of production entries - /// - public int ProductionTableCount - { - get; - set; - } + /// + /// Number of production entries + /// + public int ProductionTableCount + { + get; + set; + } - /// - /// Number of DFA entries - /// - public int DfaTableCount - { - get; - set; - } + /// + /// Number of DFA entries + /// + public int DfaTableCount + { + get; + set; + } - /// - /// Number of LR state entries - /// - public int LrStatesTableCount - { - get; - set; - } + /// + /// Number of LR state entries + /// + public int LrStatesTableCount + { + get; + set; + } - /// - /// Number of group entries - /// - public int GroupTableCount - { - get; - set; - } + /// + /// Number of group entries + /// + public int GroupTableCount + { + get; + set; + } - /// - /// Ctor. - /// - public GrammarTableCounts() - { - SymbolTableCount = 0; - CharsetTableCount = 0; - ProductionTableCount = 0; - DfaTableCount = 0; - LrStatesTableCount = 0; - GroupTableCount = 0; - } - } + /// + /// Ctor. + /// + public GrammarTableCounts() + { + SymbolTableCount = 0; + CharsetTableCount = 0; + ProductionTableCount = 0; + DfaTableCount = 0; + LrStatesTableCount = 0; + GroupTableCount = 0; + } + } } \ No newline at end of file diff --git a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarTables.cs b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarTables.cs index eaac902c..8c65b161 100644 --- a/@GoldParserEngine/GoldParserEngine/Grammar/GrammarTables.cs +++ b/@GoldParserEngine/GoldParserEngine/Grammar/GrammarTables.cs @@ -1,111 +1,109 @@ -using System.Linq; - -namespace GoldParser.Grammar +namespace GoldParser.Grammar { /// /// A class to hold all the grammar. /// Originally not in the parser. /// public class GrammarTables - { - /// - /// The properties or metadata about the grammar - /// - public GrammarProperties Properties + { + /// + /// The properties or metadata about the grammar + /// + public GrammarProperties Properties { - get; - set; + get; + set; } - /// - /// Table counts in the grammar - /// - public GrammarTableCounts TableCounts - { - get; - set; - } + /// + /// Table counts in the grammar + /// + public GrammarTableCounts TableCounts + { + get; + set; + } - /// - /// The DFA and LALR initial states - /// - public GrammarInitialStates InitialStates - { - get; - set; - } + /// + /// The DFA and LALR initial states + /// + public GrammarInitialStates InitialStates + { + get; + set; + } - /// - /// The character set table - /// - public GrammarCharacterSet[] CharacterSets - { - get; - set; - } + /// + /// The character set table + /// + public GrammarCharacterSet[] CharacterSets + { + get; + set; + } - /// - /// The symbol table - /// - public GrammarSymbol[] Symbols - { - get; - set; - } + /// + /// The symbol table + /// + public GrammarSymbol[] Symbols + { + get; + set; + } - /// - /// The group table - /// - public GrammarGroup[] Groups - { - get; - set; - } + /// + /// The group table + /// + public GrammarGroup[] Groups + { + get; + set; + } - /// - /// The production table - /// - public GrammarProduction[] Productions - { - get; - set; - } + /// + /// The production table + /// + public GrammarProduction[] Productions + { + get; + set; + } - /// - /// The FA aka DFA state table - /// - public GrammarFAState[] FAStates - { - get; - set; - } + /// + /// The FA aka DFA state table + /// + public GrammarFAState[] FAStates + { + get; + set; + } - /// - /// The LR aka LALR state table - /// - public GrammarLRState[] LRStates - { - get; - set; - } + /// + /// The LR aka LALR state table + /// + public GrammarLRState[] LRStates + { + get; + set; + } - /// - /// Wether a grammar has been loaded - /// - public bool TablesLoaded + /// + /// Wether a grammar has been loaded + /// + public bool TablesLoaded { - get; - internal set; + get; + internal set; } - /// - /// Ctor. - /// - public GrammarTables() - { - TablesLoaded = false; - } - } + /// + /// Ctor. + /// + public GrammarTables() + { + TablesLoaded = false; + } + } } \ No newline at end of file diff --git a/@GoldParserEngine/GoldParserEngine/Helpers/EgtToJSON.cs b/@GoldParserEngine/GoldParserEngine/Helpers/EgtToJSON.cs index 9a07db3d..fdbbbf32 100644 --- a/@GoldParserEngine/GoldParserEngine/Helpers/EgtToJSON.cs +++ b/@GoldParserEngine/GoldParserEngine/Helpers/EgtToJSON.cs @@ -1,7 +1,7 @@ -using System; +using GoldParser.Egt; +using System; using System.Collections.Generic; using System.IO; -using GoldParser.Egt; namespace GoldParser.Helpers @@ -68,7 +68,7 @@ public static string ReadFile(string path) return json; } - catch(Exception ex) + catch (Exception ex) { throw new EgtException(ex.Message, ex, "ReadFile"); } @@ -83,11 +83,11 @@ public static string ReadRecords(List records) { try { - return "{" + Environment.NewLine + - readRecords(records, _indent) + + return "{" + Environment.NewLine + + readRecords(records, _indent) + Environment.NewLine + "}"; } - catch(Exception ex) + catch (Exception ex) { throw new EgtException(ex.Message, ex, "ReadRecords"); } @@ -201,7 +201,7 @@ static string readEntry(EgtEntry entry, string indent = "") { string result = indent + _indent + "\"type\":\"" + entry.Type.ToString() + "\""; result += "," + Environment.NewLine; - switch(entry.Type) + switch (entry.Type) { case EgtEntryType.Error: result += indent + _indent + "\"value\":\"\""; diff --git a/@GoldParserEngine/GoldParserEngine/Helpers/EgtToText.cs b/@GoldParserEngine/GoldParserEngine/Helpers/EgtToText.cs index 8990568e..39c2356d 100644 --- a/@GoldParserEngine/GoldParserEngine/Helpers/EgtToText.cs +++ b/@GoldParserEngine/GoldParserEngine/Helpers/EgtToText.cs @@ -147,7 +147,7 @@ static string readRecords(List records) static string readRecord(EgtRecord record) { string result = record.EntriesCount.ToString() + - Environment.NewLine + + Environment.NewLine + readEntries(record.Entries); return result; } diff --git a/@GoldParserEngine/GoldParserEngine/Helpers/GrammarToJSON.cs b/@GoldParserEngine/GoldParserEngine/Helpers/GrammarToJSON.cs index 9ade2033..3b19d63f 100644 --- a/@GoldParserEngine/GoldParserEngine/Helpers/GrammarToJSON.cs +++ b/@GoldParserEngine/GoldParserEngine/Helpers/GrammarToJSON.cs @@ -1,11 +1,8 @@ -using System; +using GoldParser.Egt; +using GoldParser.Grammar; +using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using GoldParser.Egt; -using GoldParser.Grammar; namespace GoldParser.Helpers { @@ -343,7 +340,7 @@ public static string faStateToJSON(GrammarFAState state, string indent = "") json += indent + _indent + "\"accept-symbol-index\":" + state.Accept.TableIndex.ToString() + "," + Environment.NewLine; } - if(state.Edges.Count == 0) + if (state.Edges.Count == 0) { json += indent + _indent + "\"edges\":[]" + Environment.NewLine; } diff --git a/@GoldParserEngine/GoldParserEngine/Helpers/GrammarToText.cs b/@GoldParserEngine/GoldParserEngine/Helpers/GrammarToText.cs index 7a92a845..d7ffc40a 100644 --- a/@GoldParserEngine/GoldParserEngine/Helpers/GrammarToText.cs +++ b/@GoldParserEngine/GoldParserEngine/Helpers/GrammarToText.cs @@ -1,10 +1,8 @@ -using System; +using GoldParser.Egt; +using GoldParser.Grammar; +using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using GoldParser.Egt; -using GoldParser.Grammar; namespace GoldParser.Helpers { @@ -190,7 +188,7 @@ public static string characterSetToText(GrammarCharacterSet charset) public static string characterRangeToText(GrammarCharacterRange range) { string text = ""; - if(range.Start == range.End) + if (range.Start == range.End) { text = range.Start.ToString(); } @@ -205,7 +203,7 @@ public static string characterRangeToText(GrammarCharacterRange range) public static string readSymbols(GrammarSymbol[] symbols) { string text = "SYMBOLS" + Environment.NewLine; - text += "Index".PadRight(_columnWidth, ' ') + "Name".PadRight(_columnWidth, ' ') + + text += "Index".PadRight(_columnWidth, ' ') + "Name".PadRight(_columnWidth, ' ') + "Type".PadRight(_columnWidth, ' ') + "Group".PadRight(_columnWidth, ' ') + Environment.NewLine; text += "-".PadRight(_columnWidth * 4, '-') + Environment.NewLine; @@ -373,7 +371,7 @@ public static string faStateToText(GrammarFAState state) if (i < state.Edges.Count - 1) edges += ", "; } } - text += edges; + text += edges; return text; } @@ -412,7 +410,7 @@ public static string LrStateToText(GrammarLRState state) for (int i = 0; i < state.Actions.Count(); i++) { text += lrActionToText(state.Actions[i]); - if(i < state.Actions.Count() - 1) text += Environment.NewLine; + if (i < state.Actions.Count() - 1) text += Environment.NewLine; } return text; diff --git a/@GoldParserEngine/GoldParserEngine/Helpers/Logging.cs b/@GoldParserEngine/GoldParserEngine/Helpers/Logging.cs index cb9bd0c4..7e52694a 100644 --- a/@GoldParserEngine/GoldParserEngine/Helpers/Logging.cs +++ b/@GoldParserEngine/GoldParserEngine/Helpers/Logging.cs @@ -1,72 +1,68 @@ using GoldParser.Grammar; using GoldParser.ParseTree; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace GoldParser { public static class Logging { - public static string LOG; + public static string LOG; - static Logging() + static Logging() { - LOG = ""; + LOG = ""; } - public static void Log(string s) + public static void Log(string s) { - Console.WriteLine(s); - LOG += s + Environment.NewLine; + Console.WriteLine(s); + LOG += s + Environment.NewLine; } - public static string GetTokenLogParagraph(Token t) - { - string s = "TOKEN" + Environment.NewLine; - s += "Data: " + t.Data.ToString() + Environment.NewLine; - s += "Type: " + t.SymbolType.ToString() + Environment.NewLine; - s += "State: " + t.State.ToString() + Environment.NewLine; - s += "Position: " + t.Position.Line.ToString() + "," + t.Position.Column.ToString(); - s += Environment.NewLine + Environment.NewLine; + public static string GetTokenLogParagraph(Token t) + { + string s = "TOKEN" + Environment.NewLine; + s += "Data: " + t.Data.ToString() + Environment.NewLine; + s += "Type: " + t.SymbolType.ToString() + Environment.NewLine; + s += "State: " + t.State.ToString() + Environment.NewLine; + s += "Position: " + t.Position.Line.ToString() + "," + t.Position.Column.ToString(); + s += Environment.NewLine + Environment.NewLine; - return s; - } - public static string GetTokenLogLine(Token t) - { - string s = - "TOKEN: Data: \"" + t.Data.ToString() + - "\"; Type: {" + t.SymbolType.ToString() + - "}; State: " + t.State.ToString() + - "; Position: " + t.Position.Line.ToString() + "," + t.Position.Column.ToString(); + return s; + } + public static string GetTokenLogLine(Token t) + { + string s = + "TOKEN: Data: \"" + t.Data.ToString() + + "\"; Type: {" + t.SymbolType.ToString() + + "}; State: " + t.State.ToString() + + "; Position: " + t.Position.Line.ToString() + "," + t.Position.Column.ToString(); - return s; - } - public static string GetSymbolLogLine(GrammarSymbol symbol) - { - string group = "null"; - if (symbol.Group != null) group = symbol.Group.TableIndex.ToString(); + return s; + } + public static string GetSymbolLogLine(GrammarSymbol symbol) + { + string group = "null"; + if (symbol.Group != null) group = symbol.Group.TableIndex.ToString(); - string s = - "SYMBOL: Name: \"" + symbol.Name.ToString() + - "\"; Type: {" + symbol.Type.ToString() + - "}; Table Index: " + symbol.TableIndex.ToString() + - "; Group Index: " + group; + string s = + "SYMBOL: Name: \"" + symbol.Name.ToString() + + "\"; Type: {" + symbol.Type.ToString() + + "}; Table Index: " + symbol.TableIndex.ToString() + + "; Group Index: " + group; - return s; - } - public static string GetLRActionLogLine(GrammarLRAction action) - { - if (action == null) return "LR_ACTION: null"; + return s; + } + public static string GetLRActionLogLine(GrammarLRAction action) + { + if (action == null) return "LR_ACTION: null"; - string s = - "LR_ACTION: Type: {" + action.Type.ToString() + - "}; Value: " + action.Value.ToString(); + string s = + "LR_ACTION: Type: {" + action.Type.ToString() + + "}; Value: " + action.Value.ToString(); - return s; - } - } + return s; + } + } } diff --git a/@GoldParserEngine/GoldParserEngine/LoggedParser/Parser.cs b/@GoldParserEngine/GoldParserEngine/LoggedParser/Parser.cs deleted file mode 100644 index 41e2f272..00000000 --- a/@GoldParserEngine/GoldParserEngine/LoggedParser/Parser.cs +++ /dev/null @@ -1,340 +0,0 @@ -using GoldParser.Grammar; -using GoldParser.ParseTree; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace GoldParser.LoggedParser -{ - public enum ParseMessage - { - TokenRead, - Reduction, - Accept, - NotLoadedError, - LexicalError, - SyntaxError, - GroupError, - InternalError - } - public class Parser : ParserLexer - { - enum ParseResult - { - Accept = 1, - Shift, - ReduceNormal, - ReduceEliminated, - SyntaxError, - InternalError - } - - private bool _trimReductions; - private bool _haveReduction; - private int _CurrentLALR; - - private Stack _stack; - private Stack _inputTokens; - private Position _currentPosition; - List _expectedSymbols; - - - - - /// - /// Determines if reductions will be trimmed in cases where a production contains a single element. - /// - public bool TrimReductions - { - get - { - return _trimReductions; - } - set - { - _trimReductions = value; - } - } - - /// - /// When the Parse() method returns a Reduce, this property will contain the current Reduction. - /// - public Reduction CurrentReduction - { - get - { - if (_haveReduction && _stack.Peek().Data is Reduction) - { - return (Reduction)_stack.Peek().Data; - } - else - { - return null; - } - } - set - { - if (_haveReduction) - { - _stack.Peek().Data = value; - } - } - } - - /// - /// If the Parse() function returns TokenRead, this method will return that last read token. - /// - /// - public Token CurrentToken - { - get - { - return _inputTokens.Peek(); - } - } - - /// - /// - /// - public Position CurrentPosition - { - get - { - return _currentPosition; - } - } - - /// - /// If the Parse() method returns a SyntaxError, - /// this method will contain a list of the symbols the grammar expected to see. - /// - /// - public List ExpectedSymbols - { - get - { - return _expectedSymbols; - } - } - - - - /// - /// Ctor. - /// - public Parser() - { - _expectedSymbols = new List(); - _stack = new Stack(); - _inputTokens = new Stack(); - - _currentPosition = new Position(); - _currentPosition.Line = 0; - _currentPosition.Column = 0; - - _haveReduction = false; - _tablesLoaded = false; - _trimReductions = false; - } - - /// - /// Opens a string for parsing. - /// - /// The string to be parsed - /// - public bool Open(string text) - { - bool opened = Open(new StringReader(text)); - Logging.Log("Lexer Opened string for parsing"); - return opened; - } - - /// - /// Opens a text stream for parsing. - /// - /// The TextReader stream to be parsed - /// - public bool Open(TextReader reader) - { - Restart(); - _Source = reader; - Logging.Log("Lexer Opened reader for parsing - \"_Source = reader\""); - - Token token = new Token(); - token.State = _grammar.InitialStates.DfaInitialState; - _stack.Push(token); - return true; - } - - /// - /// Restarts the parser. Loaded tables are retained. - /// - public new void Restart() - { - base.Restart(); - - _CurrentLALR = _grammar.InitialStates.LrInitialState; - - _currentPosition.Line = 0; - _currentPosition.Column = 0; - _haveReduction = false; - - _expectedSymbols.Clear(); - _inputTokens.Clear(); - _stack.Clear(); - } - - /// - /// Removes the next token from the input queue. - /// - /// - public Token PopToken() - { - return _inputTokens.Pop(); - } - - /// - /// Pushes the token onto the top of the input stack. This token will be analyzed next. - /// - /// - public void PushToken(ref Token token) - { - _inputTokens.Push(token); - } - - /// - /// Performs a parse action on the input. - /// - /// ParseMessage - public ParseMessage Parse() - { - if (!_tablesLoaded) return ParseMessage.NotLoadedError; - while (true) - { - if (_inputTokens.Count == 0) - { - Token t = produceToken(); - _inputTokens.Push(t); - return ParseMessage.TokenRead; - } - - Token token = _inputTokens.Peek(); - _currentPosition.Copy(token.Position); - - if (_GroupStack.Count != 0) return ParseMessage.GroupError; - if (token.SymbolType == GrammarSymbolType.Error) return ParseMessage.LexicalError; - if (token.SymbolType == GrammarSymbolType.Noise) - { - _inputTokens.Pop(); - continue; - } - - ParseResult r = ParseLALR(ref token); - switch (r) - { - case ParseResult.Accept: return ParseMessage.Accept; - case ParseResult.ReduceNormal: return ParseMessage.Reduction; - case ParseResult.SyntaxError: return ParseMessage.SyntaxError; - case ParseResult.InternalError: return ParseMessage.InternalError; - case ParseResult.Shift: _inputTokens.Pop(); break; - } - } - } - private ParseResult ParseLALR(ref Token token) - { - GrammarLRState lRState = _grammar.LRStates[_CurrentLALR]; - GrammarLRAction lRAction = lRState[token.Symbol]; - - if (lRAction == null) - { - _expectedSymbols.Clear(); - try - { - for (int i = 0; i < lRState.Count(); i++) - { - GrammarLRAction lra = lRState[i]; - switch (lra.Symbol.Type) - { - case GrammarSymbolType.Terminal: - case GrammarSymbolType.End: - case GrammarSymbolType.GroupStart: - case GrammarSymbolType.GroupEnd: - _expectedSymbols.Add(lra.Symbol); - break; - } - } - } - catch { } - return ParseResult.SyntaxError; - } - - _haveReduction = false; - switch (lRAction.Type) - { - case LRActionType.Accept: - _haveReduction = true; - return ParseResult.Accept; - - case LRActionType.Shift: - _CurrentLALR = lRAction.Value; - token.State = _CurrentLALR; //checked(_CurrentLALR); - _stack.Push(token); //_Stack.Push(ref token); - return ParseResult.Shift; - - case LRActionType.Reduce: - - GrammarProduction production = _grammar.Productions[lRAction.Value]; - - ParseResult result; - Token t; - if (_trimReductions && production.ContainsOneNonTerminal()) - { - t = _stack.Pop(); - t.Symbol = production.Head; - result = ParseResult.ReduceEliminated; - } - else - { - _haveReduction = true; - Reduction reduction = new Reduction(production.Handle.Count()); - reduction.Production = production; - for (int i = production.Handle.Count() - 1; i >= 0; i -= 1) - { - reduction[i] = _stack.Pop(); - } - t = new Token(production.Head, reduction); - result = ParseResult.ReduceNormal; - } - - int stateindex = _stack.Peek().State; - GrammarLRState state = _grammar.LRStates[stateindex]; - int actionindex = state.IndexOf(production.Head); //action index is returned - - if (actionindex == -1) { return ParseResult.InternalError; } - - GrammarLRAction lra = state[actionindex]; - _CurrentLALR = lra.Value; - t.State = _CurrentLALR; - _stack.Push(t); - - return result; - - default: - return ParseResult.InternalError; - } - } - } -} -//_InputTokens.Top() is _InputTokens.Peek() -//_ExpectedSymbols is _expectedSymbols -//ExpectedSymbols() is ExpectedSymbols.get -//PushInput(Token) is PushToken(Token) -//DiscardCurrentToken() is PopToken() -//Clear() seems pointless, when we have Restart() that does the same - -//EnqueueInput(Token) has been removed as it is not used, isn't clear to me if useful, -//and @meziantou has omitted it as well - https://github.com/meziantou/GoldParser-Engine/ -//It will require custom stack class to be implemented, hurting code readability \ No newline at end of file diff --git a/@GoldParserEngine/GoldParserEngine/LoggedParser/ParserBase.cs b/@GoldParserEngine/GoldParserEngine/LoggedParser/ParserBase.cs deleted file mode 100644 index 38a48c7e..00000000 --- a/@GoldParserEngine/GoldParserEngine/LoggedParser/ParserBase.cs +++ /dev/null @@ -1,210 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using GoldParser.Grammar; - - -namespace GoldParser.LoggedParser -{ - public class ParserBase - { - private const string _aboutString = "GOLD Parser Engine; Version 5.0; (Refactored)"; - - protected GrammarTables _grammar; - protected bool _tablesLoaded; - - private TextReader _source; - private string _sourcePath; - - - - /// - /// Library name and version. - /// - public string About - { - get - { - return _aboutString; - } - } - - - - /// - /// Check if parse tables were loaded. - /// - internal bool TablesLoaded - { - get - { - return _tablesLoaded; - } - set - { - _tablesLoaded = value; - } - } - - /// - /// Returns information about the current grammar. - /// - internal GrammarProperties GrammarInfo - { - get - { - return _grammar.Properties; - } - } - - /// - /// Returns a list of Symbols recognized by the grammar. - /// - internal GrammarSymbol[] SymbolTable - { - get - { - return _grammar.Symbols; - } - } - - /// - /// Returns a list of Productions recognized by the grammar. - /// - internal GrammarProduction[] ProductionTable - { - get - { - return _grammar.Productions; - } - } - - - - /// - /// Check if file for parsing have been opened - /// - internal bool HaveSourcePath - { - get - { - if(string.IsNullOrEmpty(_sourcePath)) - { - return false; - } - return true; - } - } - - /// - /// Path of source file to be parsed - /// - internal string SourcePath - { - get - { - return _sourcePath; - } - set - { - _sourcePath = value; - } - } - - - - /// - /// Ctor. - /// - public ParserBase() - { - _tablesLoaded = false; - } - - - - /// - /// Opens a string for parsing. - /// - /// The string to be parsed - /// true if successful - internal bool OpenFile(string text) - { - try - { - StringReader reader = new StringReader(text); - return OpenFile(reader); - } - catch (Exception ex) - { - throw new ParserException(ex.Message, ex, "OpenFile"); - } - } - - /// - /// Opens a text stream for parsing. - /// - /// The TextReader stream to be parsed - /// Always true - public bool OpenFile(TextReader Reader) - { - _source = Reader; - return true; - } - - - - /// - /// Loads parse tables from the specified filename. - /// Only EGT (version 5.0) is supported. - /// - /// - /// Path to an EGT file containing grammar tables - /// - /// true if successful - public bool LoadTables(string filepath) - { - try - { - Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read); - BinaryReader reader = new BinaryReader(stream); - return LoadTables(reader); - } - catch (Exception ex) - { - _tablesLoaded = false; - throw new ParserException(ex.Message, ex, "LoadTables"); - } - } - - /// - /// Loads parse tables from the specified BinaryReader. - /// Only EGT (version 5.0) is supported. - /// - /// - /// A binary reader to the data of EGT file - /// containing grammar tables - /// - /// true if successful - public bool LoadTables(BinaryReader reader) - { - try - { - List records = Egt.EgtContentReager.ReadFileRecords(reader); - _grammar = GrammarContentReader.ReadGrammar(records); - reader.Close(); - _tablesLoaded = true; - return true; - } - catch (ParserException ex) - { - _tablesLoaded = false; - throw new ParserException(ex.Message, ex, "LoadTables"); - } - } - } -} \ No newline at end of file diff --git a/@GoldParserEngine/GoldParserEngine/LoggedParser/ParserException.cs b/@GoldParserEngine/GoldParserEngine/LoggedParser/ParserException.cs deleted file mode 100644 index 9731649e..00000000 --- a/@GoldParserEngine/GoldParserEngine/LoggedParser/ParserException.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace GoldParser.LoggedParser -{ - public class ParserException : Exception - { - internal string Method; - - internal ParserException(string message) : base(message) - { - Method = ""; - } - internal ParserException(string message, Exception inner, string method) : base(message, inner) - { - Method = method; - } - } -} diff --git a/@GoldParserEngine/GoldParserEngine/LoggedParser/ParserLexer.cs b/@GoldParserEngine/GoldParserEngine/LoggedParser/ParserLexer.cs deleted file mode 100644 index 81685702..00000000 --- a/@GoldParserEngine/GoldParserEngine/LoggedParser/ParserLexer.cs +++ /dev/null @@ -1,345 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using GoldParser.ParseTree; -using GoldParser.Grammar; -using System.IO; - -namespace GoldParser.LoggedParser -{ - public class ParserLexer : ParserBase - { - protected TextReader _Source; - protected Stack _GroupStack; - private Position _SysPosition; - private string _LookaheadBuffer; - - - - /// - /// Ctor. - /// - public ParserLexer() : base() - { - _GroupStack = new Stack(); - _SysPosition = new Position(); - _LookaheadBuffer = ""; - Logging.Log("Lexer Created"); - } - - /// - /// Restarts the Lexer. Loaded tables are retained. - /// - public void Restart() - { - _GroupStack.Clear(); - _SysPosition.Column = 0; - _SysPosition.Line = 0; - _LookaheadBuffer = ""; - Logging.Log("Lexer Restarted"); - } - - - - /// - /// This function creates a token and also takes into account the current - /// lexing mode of the parser. In particular, it contains the group logic. - /// - /// A stack is used to track the current "group". This replaces the comment - /// level counter. Also, text is appended to the token on the top of the - /// stack. This allows the group text to returned in one chunk. - /// - /// - protected Token produceToken() - { - while (true) - { - Token token = produceTokenDFA(); - - //The logic - to determine if a group should be nested - requires that the top of the stack - //and the symbol's linked group need to be looked at. Both of these can be unset. So, this section - //sets a Boolean and avoids errors. We will use this boolean in the logic chain below. - bool nestGroup = false; - if (token.SymbolType == GrammarSymbolType.GroupStart) - { - nestGroup = _GroupStack.Count == 0 || - _GroupStack.Peek().SymbolGroup.Nesting.Contains(token.SymbolGroup.TableIndex); - } - - //TOKEN IS GROUP START-ING - //If a token is one that is a start of a group, we can start a - //group by pushing it on the group stack. We are allowed to do so, - //if the group stack is empty(we are not in a group), or group - //stack is not empty(we are in a group) and that groups' Nesting - //allows for current group we are starting to be nested in. - if (nestGroup) - { - trimBuffer((token.Data as string).Length); - _GroupStack.Push(token); - } - - //TOKEN IS NOT GROUP-START OR IS NOT NESTABLE GROUP-START - //WE ARE NOT IN A GROUP - else if (_GroupStack.Count == 0) - { - trimBuffer((token.Data as string).Length); - return token; - } - - //TOKEN IS NOT GROUP-START OR IS NOT NESTABLE GROUP-START - //WE ARE NOT-NOT IN A GROUP - //TOKEN IS GROUP END-ING - else if (_GroupStack.Peek().SymbolGroup.EndSymbol == token.Symbol) - { - //End the current group - Token pop = _GroupStack.Pop(); - - //if we have GrammarGroupEndingMode.Closed then add the end - //token, otherwise don't - if (pop.SymbolGroup.Ending == GrammarGroupEndingMode.Closed) - { - pop.Data = (pop.Data as string) + (token.Data as string); - trimBuffer((token.Data as string).Length); - } - - //We are out of the group. Return pop'd token (which contains all the group text) - if (_GroupStack.Count == 0) - { - pop.Symbol = pop.SymbolGroup.ContainerSymbol; //Change symbol to symbol of the group - return pop; - } - else - { - Token top = _GroupStack.Peek(); - top.Data = (top.Data as string) + (pop.Data as string); - } - } - - //TOKEN IS NOT GROUP-START OR IS NOT NESTABLE GROUP-START - //TOKEN IS GROUP END-ING - //WE ARE NOT-NOT IN A GROUP - //TOKEN IS EOF - "End Of File" - else if (token.SymbolType == GrammarSymbolType.End) - { - return token; - } - - //TOKEN IS NOT GROUP-START OR IS NOT NESTABLE GROUP-START - //TOKEN IS NOT GROUP END-ING - //WE ARE NOT-NOT IN A GROUP - //TOKEN IS NOT EOF - "End Of File" - // - //we are in group and none of the above, so if that group is TokenAdvance - //we add the text of the token to the Data of previous one, otherwise, if - //we have CharacterAdvance, we add just the first character. we then continue - //on with the loop - else - { - Token previousToken = _GroupStack.Peek(); - if (previousToken.SymbolGroup.Advance == GrammarGroupAdvanceMode.Token) - { - previousToken.Data = (previousToken.Data as string) + (token.Data as string); - trimBuffer((token.Data as string).Length); - } - else - { - previousToken.Data = (previousToken.Data as string) + (token.Data as string)[0]; - trimBuffer(1); - } - } - } - } - - /// - /// This function implements the DFA for the parser's lexer. - /// It generates a token which is used by the LALR state machine. - /// - /// //https://github.com/meziantou/GoldParser-Engine/blob/master/GoldParserEngine/GoldParserEngine/Parser.cs - Token produceTokenDFA() - { - Token result = new Token(); - int currentDfa = _grammar.InitialStates.DfaInitialState; - int lastAcceptState = -1; //Next byte in the input Stream - int lastAcceptPosition = -1; //We have not yet accepted a character string - int currentPosition = 1; - bool done = false; - - string ch = getCharacter(1); // Get first symbol from the lookahead buffer - if (ch != "" && Convert.ToInt32(ch[0]) != 65535) // End of file is not reached - { - while (done == false) - { - // This code searches all the branches of the current DFA state - // for the next character in the input Stream. If found the - // target state is returned. - ch = getCharacter(currentPosition); //Get first x symbols from the lookahead buffer - int target = -1; - //if ch is "", then End of file reached, so do not match - if (!string.IsNullOrEmpty(ch)) - { - GrammarFAState currentFAState = _grammar.FAStates[currentDfa]; - for (int n = 0; n < currentFAState.Edges.Count(); n++) - { - GrammarFAEdge fAEdge = currentFAState.Edges[n]; - //Look for character in the Character Set Table - if (fAEdge.CharacterSet.Contains(Convert.ToInt32(ch[0]))) - { - target = fAEdge.Target; - break; - } - } - } - - - // This block-if statement checks whether an edge was found from the current state. If so, the state and current - // position advance. Otherwise it is time to exit the main loop and report the token found (if there was one). - // If the LastAcceptState is -1, then we never found a match and the Error Token is created. Otherwise, a new - // token is created using the Symbol in the Accept State and all the characters that comprise it. - - if (target > -1) //that means that target state is found - { - // This code checks whether the target state accepts a token. - // If so, it sets the appropiate variables so when the - // algorithm in done, it can return the proper token and - // number of characters. - - if (_grammar.FAStates[target].Accept != null) - { - lastAcceptState = target; - lastAcceptPosition = currentPosition; - } - currentDfa = target; - currentPosition++; - } - else - { - done = true; - if (lastAcceptState == -1) // Lexer cannot recognize symbol - { - result.Symbol = getFirstOfType(_grammar, GrammarSymbolType.Error); - result.Data = getString(1); - } - else //Lexer can recognize symbol - { - result.Symbol = _grammar.FAStates[lastAcceptState].Accept; - result.Data = getString(lastAcceptPosition); - //Data contains the total number of accept characters - } - } - } - } - else // End of file reached, create End Token - { - result.Data = ""; - result.Symbol = getFirstOfType(_grammar, GrammarSymbolType.End); - } - - //Set the new token's position information - //Notice, this is a copy, not a linking of an instance. We don't want the user - //to be able to alter the main value indirectly. - result.Position.Copy(_SysPosition); - return result; - } - - /// - /// Get a character from _LookaheadBuffer, and return it as string. Uses one-based indexing! - /// If _LookaheadBuffer is too short, read more chars from _Source TextReader - /// returns _LookaheadBuffer[charIndex - 1] - /// - /// - /// - string getCharacter(int charIndex) - { - //Check if we must read characters from the Stream - if (charIndex > _LookaheadBuffer.Length) - { - for (int i = 0; i < charIndex - _LookaheadBuffer.Length; i++) - { - int x = _Source.Read(); - char c = (char)x;//char c = Strings.ChrW(x); - _LookaheadBuffer += c.ToString(); - } - } - - //If the buffer is still smaller than the index, we have reached - //the end of the text. In this case, return a null string - the DFA - //code will understand. - if (charIndex <= _LookaheadBuffer.Length) - { - return _LookaheadBuffer[charIndex - 1].ToString(); - } - return ""; - } - - /// - /// Get first "count" number of symbols from _LookaheadBuffer. - /// - /// Return Count characters from the lookahead buffer. DO NOT CONSUME - /// This is used to create the text stored in a token. It is disgarded - /// separately. Because of the design of the DFA algorithm, count should - /// never exceed the buffer length. The If-Statement below is fault-tolerate - /// programming, but not necessary. - /// - /// - /// - string getString(int length) - { - - if (length > _LookaheadBuffer.Length) - { - length = _LookaheadBuffer.Length; - } - return _LookaheadBuffer.Substring(0, length); - } - - /// - /// ConsumeBuffer increments _SysPosition according to the characters in _LookaheadBuffer - /// and removes those characters from _LookaheadBuffer - /// - /// Number of characters to advance the _SysPosition position - void trimBuffer(int count) - { - if (count > _LookaheadBuffer.Length) return; - - // Count Carriage Returns and increment the internal column and line - // numbers. This is done for the Developer and is not necessary for the - // DFA algorithm. - for (int n = 0; n <= count - 1; n++) - { - switch (_LookaheadBuffer[n]) - { - case '\n': - _SysPosition.Line++; - _SysPosition.Column = 0; - break; - - case '\r': //carriage return - break; //Ignore, LF is used to inc line to be UNIX friendly - - case '\v': //vertical tab - case '\f': //form feed - default: - _SysPosition.Column++; - break; - } - } - - _LookaheadBuffer = _LookaheadBuffer.Remove(0, count); - } - - GrammarSymbol getFirstOfType(GrammarTables grammar, GrammarSymbolType type) - { - return grammar.Symbols.FirstOrDefault(_ => _.Type == type); - } - } -} -//Top() is Peek() -//LookaheadDFA() is produceTokenDFA() -//Lookahead(int charIndex) is getCharacter(int charIndex) -//LookaheadBuffer(int count) is getString(int length) -//ConsumeBuffer(int charCount) is trimBuffer(int count) - -//TODO: Look at GrammarLists functionality I am omitting -//TODO: Add documentation comments on each grammar object \ No newline at end of file diff --git a/@GoldParserEngine/GoldParserEngine/ParseTree/Position.cs b/@GoldParserEngine/GoldParserEngine/ParseTree/Position.cs index b15a9faa..d008c27b 100644 --- a/@GoldParserEngine/GoldParserEngine/ParseTree/Position.cs +++ b/@GoldParserEngine/GoldParserEngine/ParseTree/Position.cs @@ -1,48 +1,42 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace GoldParser.ParseTree +namespace GoldParser.ParseTree { - /// - /// Represents a position of a symbol in a text - /// + /// + /// Represents a position of a symbol in a text + /// public class Position { - /// - /// The line in the text - /// - public int Line + /// + /// The line in the text + /// + public int Line { - get; - set; + get; + set; } - /// - /// The column in the text - /// - public int Column - { - get; - set; - } + /// + /// The column in the text + /// + public int Column + { + get; + set; + } - internal Position() - { - Line = 0; - Column = 0; - } + internal Position() + { + Line = 0; + Column = 0; + } - /// - /// Copy values from another position object. - /// - /// The position to be copied - internal void Copy(Position pos) - { - Column = pos.Column; - Line = pos.Line; - } - } + /// + /// Copy values from another position object. + /// + /// The position to be copied + internal void Copy(Position pos) + { + Column = pos.Column; + Line = pos.Line; + } + } } diff --git a/@GoldParserEngine/GoldParserEngine/ParseTree/Reduction.cs b/@GoldParserEngine/GoldParserEngine/ParseTree/Reduction.cs index 05b06bcc..6660a03f 100644 --- a/@GoldParserEngine/GoldParserEngine/ParseTree/Reduction.cs +++ b/@GoldParserEngine/GoldParserEngine/ParseTree/Reduction.cs @@ -1,102 +1,98 @@ using GoldParser.Grammar; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace GoldParser.ParseTree { public class Reduction { - private GrammarProduction _production; - private List _tokens = new List(); - private object _Tag; + private GrammarProduction _production; + private List _tokens = new List(); + private object _Tag; - /// - /// Returns the token with the specified index. - /// - /// - public Token this[int index] - { - get - { - return _tokens[index]; - } - internal set - { - _tokens[index] = value; - } - } - - /// - /// Returns the parent production. - /// - public GrammarProduction Production - { - get - { - return _production; - } - internal set - { - _production = value; - } - } + /// + /// Returns the token with the specified index. + /// + /// + public Token this[int index] + { + get + { + return _tokens[index]; + } + internal set + { + _tokens[index] = value; + } + } - /// - /// Returns/sets any additional user-defined data to this object. - /// - public object Tag - { - get - { - return _Tag; - } - set - { - _Tag = value; - } - } + /// + /// Returns the parent production. + /// + public GrammarProduction Production + { + get + { + return _production; + } + internal set + { + _production = value; + } + } + /// + /// Returns/sets any additional user-defined data to this object. + /// + public object Tag + { + get + { + return _Tag; + } + set + { + _Tag = value; + } + } - public Reduction(int size) - { - ReDimension(size); - } + public Reduction(int size) + { + ReDimension(size); + } - public object GetTokenData(int index) - { - return _tokens[index].Data; - } - public void SetTokenData(int index, object value) - { - _tokens[index].Data = value; - } - public void ReDimension(int size) - { - for (int i = 0; i <= size - 1; i++) - { - _tokens.Add(null); - } - } + public object GetTokenData(int index) + { + return _tokens[index].Data; - public void Add(Token item) - { - _tokens.Add(item); - } - public int Count() - { - return _tokens.Count; - } - public void Clear() - { - _tokens = new List(); - } - } + } + public void SetTokenData(int index, object value) + { + _tokens[index].Data = value; + } + public void ReDimension(int size) + { + for (int i = 0; i <= size - 1; i++) + { + _tokens.Add(null); + } + } + + public void Add(Token item) + { + _tokens.Add(item); + } + public int Count() + { + return _tokens.Count; + } + public void Clear() + { + _tokens = new List(); + } + } } //_Parent is _production //Parent is Production \ No newline at end of file diff --git a/@GoldParserEngine/GoldParserEngine/ParseTree/Token.cs b/@GoldParserEngine/GoldParserEngine/ParseTree/Token.cs index afd5fb88..8b1b2446 100644 --- a/@GoldParserEngine/GoldParserEngine/ParseTree/Token.cs +++ b/@GoldParserEngine/GoldParserEngine/ParseTree/Token.cs @@ -1,19 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using GoldParser.Grammar; +using GoldParser.Grammar; namespace GoldParser.ParseTree { - public enum TokenType + public enum TokenType { - Terminal, - NonTerminal + Terminal, + NonTerminal } public class Token - { + { private GrammarSymbol _symbol; private Position _position; private object _data; @@ -21,82 +16,82 @@ public class Token private int _state; - public GrammarSymbol Symbol - { - get - { - return _symbol; - } - internal set - { - _symbol = value; - } - } - public Position Position - { - get + public GrammarSymbol Symbol + { + get + { + return _symbol; + } + internal set + { + _symbol = value; + } + } + public Position Position + { + get + { + return _position; + } + } + public object Data + { + get + { + return _data; + } + set { - return _position; + _data = value; } - } - public object Data - { - get - { - return _data; - } - set - { - _data = value; - } - } + } - internal int State - { - get - { - return _state; - } - set - { - _state = value; - } - } + internal int State + { + get + { + return _state; + } + set + { + _state = value; + } + } - public Token() - { - _position = new Position(); - _symbol = null; - _data = null; - _state = 0; - } - public Token(GrammarSymbol sym, object dat) - { - _position = new Position(); - _symbol = sym; - _data = dat; - _state = 0; - } + public Token() + { + _position = new Position(); + _symbol = null; + _data = null; + _state = 0; + } + public Token(GrammarSymbol sym, object dat) + { + _position = new Position(); + _symbol = sym; + _data = dat; + _state = 0; + } - public GrammarSymbolType SymbolType - { - get + public GrammarSymbolType SymbolType + { + get { - return _symbol.Type; - } - } - internal GrammarGroup SymbolGroup - { - get + return _symbol.Type; + } + } + internal GrammarGroup SymbolGroup + { + get { - return _symbol.Group; - } - } + return _symbol.Group; + } + } - } + } } diff --git a/@GoldParserEngine/GoldParserEngine/Parser/Parser.cs b/@GoldParserEngine/GoldParserEngine/Parser/Parser.cs index 57709d7c..d2b842cd 100644 --- a/@GoldParserEngine/GoldParserEngine/Parser/Parser.cs +++ b/@GoldParserEngine/GoldParserEngine/Parser/Parser.cs @@ -1,329 +1,326 @@ using GoldParser.Grammar; using GoldParser.ParseTree; -using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace GoldParser.Parser { - public enum ParseMessage - { - TokenRead, - Reduction, - Accept, - NotLoadedError, - LexicalError, - SyntaxError, - GroupError, - InternalError - } - public class Parser : ParserLexer + public enum ParseMessage { - enum ParseResult - { - Accept = 1, - Shift, - ReduceNormal, - ReduceEliminated, - SyntaxError, - InternalError - } - - private bool _trimReductions; - private bool _haveReduction; - private int _CurrentLALR; - - private Stack _stack; - private Stack _inputTokens; - private Position _currentPosition; - List _expectedSymbols; - - - - - /// - /// Determines if reductions will be trimmed in cases where a production contains a single element. - /// - public bool TrimReductions - { - get - { - return _trimReductions; - } - set - { - _trimReductions = value; - } - } - - /// - /// When the Parse() method returns a Reduce, this property will contain the current Reduction. - /// - public Reduction CurrentReduction - { - get - { - if (_haveReduction && _stack.Peek().Data is Reduction) - { - return (Reduction)_stack.Peek().Data; - } - else - { - return null; - } - } - set - { - if (_haveReduction) - { - _stack.Peek().Data = value; - } - } - } - - /// - /// If the Parse() function returns TokenRead, this method will return that last read token. - /// - /// - public Token CurrentToken - { - get + TokenRead, + Reduction, + Accept, + NotLoadedError, + LexicalError, + SyntaxError, + GroupError, + InternalError + } + public class Parser : ParserLexer + { + enum ParseResult + { + Accept = 1, + Shift, + ReduceNormal, + ReduceEliminated, + SyntaxError, + InternalError + } + + private bool _trimReductions; + private bool _haveReduction; + private int _CurrentLALR; + + private Stack _stack; + private Stack _inputTokens; + private Position _currentPosition; + List _expectedSymbols; + + + + + /// + /// Determines if reductions will be trimmed in cases where a production contains a single element. + /// + public bool TrimReductions + { + get + { + return _trimReductions; + } + set + { + _trimReductions = value; + } + } + + /// + /// When the Parse() method returns a Reduce, this property will contain the current Reduction. + /// + public Reduction CurrentReduction + { + get + { + if (_haveReduction && _stack.Peek().Data is Reduction) + { + return (Reduction)_stack.Peek().Data; + } + else + { + return null; + } + } + set + { + if (_haveReduction) + { + _stack.Peek().Data = value; + } + } + } + + /// + /// If the Parse() function returns TokenRead, this method will return that last read token. + /// + /// + public Token CurrentToken + { + get + { + return _inputTokens.Peek(); + } + } + + /// + /// + /// + public Position CurrentPosition + { + get + { + return _currentPosition; + } + } + + /// + /// If the Parse() method returns a SyntaxError, + /// this method will contain a list of the symbols the grammar expected to see. + /// + /// + public List ExpectedSymbols + { + get + { + return _expectedSymbols; + } + } + + + + /// + /// Ctor. + /// + public Parser() + { + _expectedSymbols = new List(); + _stack = new Stack(); + _inputTokens = new Stack(); + + _currentPosition = new Position(); + _currentPosition.Line = 0; + _currentPosition.Column = 0; + + _haveReduction = false; + _tablesLoaded = false; + _trimReductions = false; + } + + /// + /// Opens a string for parsing. + /// + /// The string to be parsed + /// + public bool Open(string text) + { + return Open(new StringReader(text)); + } + + /// + /// Opens a text stream for parsing. + /// + /// The TextReader stream to be parsed + /// + public bool Open(TextReader reader) + { + Restart(); + _Source = reader; + + Token token = new Token(); + token.State = _grammar.InitialStates.DfaInitialState; + _stack.Push(token); + return true; + } + + /// + /// Restarts the parser. Loaded tables are retained. + /// + public new void Restart() + { + base.Restart(); + + _CurrentLALR = _grammar.InitialStates.LrInitialState; + + _currentPosition.Line = 0; + _currentPosition.Column = 0; + _haveReduction = false; + + _expectedSymbols.Clear(); + _inputTokens.Clear(); + _stack.Clear(); + } + + /// + /// Removes the next token from the input queue. + /// + /// + public Token PopToken() + { + return _inputTokens.Pop(); + } + + /// + /// Pushes the token onto the top of the input stack. This token will be analyzed next. + /// + /// + public void PushToken(ref Token token) + { + _inputTokens.Push(token); + } + + /// + /// Performs a parse action on the input. + /// + /// ParseMessage + public ParseMessage Parse() + { + if (!_tablesLoaded) return ParseMessage.NotLoadedError; + while (true) { - return _inputTokens.Peek(); - } - } - - /// - /// - /// - public Position CurrentPosition - { - get + if (_inputTokens.Count == 0) + { + Token t = produceToken(); + _inputTokens.Push(t); + return ParseMessage.TokenRead; + } + + Token token = _inputTokens.Peek(); + _currentPosition.Copy(token.Position); + + if (_GroupStack.Count != 0) return ParseMessage.GroupError; + if (token.SymbolType == GrammarSymbolType.Error) return ParseMessage.LexicalError; + if (token.SymbolType == GrammarSymbolType.Noise) + { + _inputTokens.Pop(); + continue; + } + + ParseResult r = ParseLALR(ref token); + switch (r) + { + case ParseResult.Accept: return ParseMessage.Accept; + case ParseResult.ReduceNormal: return ParseMessage.Reduction; + case ParseResult.SyntaxError: return ParseMessage.SyntaxError; + case ParseResult.InternalError: return ParseMessage.InternalError; + case ParseResult.Shift: _inputTokens.Pop(); break; + } + } + } + private ParseResult ParseLALR(ref Token token) + { + GrammarLRState lRState = _grammar.LRStates[_CurrentLALR]; + GrammarLRAction lRAction = lRState[token.Symbol]; + + if (lRAction == null) + { + _expectedSymbols.Clear(); + try + { + for (int i = 0; i < lRState.Count(); i++) + { + GrammarLRAction lra = lRState[i]; + switch (lra.Symbol.Type) + { + case GrammarSymbolType.Terminal: + case GrammarSymbolType.End: + case GrammarSymbolType.GroupStart: + case GrammarSymbolType.GroupEnd: + _expectedSymbols.Add(lra.Symbol); + break; + } + } + } + catch { } + return ParseResult.SyntaxError; + } + + _haveReduction = false; + switch (lRAction.Type) { - return _currentPosition; + case LRActionType.Accept: + _haveReduction = true; + return ParseResult.Accept; + + case LRActionType.Shift: + _CurrentLALR = lRAction.Value; + token.State = _CurrentLALR; //checked(_CurrentLALR); + _stack.Push(token); //_Stack.Push(ref token); + return ParseResult.Shift; + + case LRActionType.Reduce: + + GrammarProduction production = _grammar.Productions[lRAction.Value]; + + ParseResult result; + Token t; + if (_trimReductions && production.ContainsOneNonTerminal()) + { + t = _stack.Pop(); + t.Symbol = production.Head; + result = ParseResult.ReduceEliminated; + } + else + { + _haveReduction = true; + Reduction reduction = new Reduction(production.Handle.Count()); + reduction.Production = production; + for (int i = production.Handle.Count() - 1; i >= 0; i -= 1) + { + reduction[i] = _stack.Pop(); + } + t = new Token(production.Head, reduction); + result = ParseResult.ReduceNormal; + } + + int stateindex = _stack.Peek().State; + GrammarLRState state = _grammar.LRStates[stateindex]; + int actionindex = state.IndexOf(production.Head); //action index is returned + + if (actionindex == -1) { return ParseResult.InternalError; } + + GrammarLRAction lra = state[actionindex]; + _CurrentLALR = lra.Value; + t.State = _CurrentLALR; + _stack.Push(t); + + return result; + + default: + return ParseResult.InternalError; } - } - - /// - /// If the Parse() method returns a SyntaxError, - /// this method will contain a list of the symbols the grammar expected to see. - /// - /// - public List ExpectedSymbols - { - get - { - return _expectedSymbols; - } - } - - - - /// - /// Ctor. - /// - public Parser() - { - _expectedSymbols = new List(); - _stack = new Stack(); - _inputTokens = new Stack(); - - _currentPosition = new Position(); - _currentPosition.Line = 0; - _currentPosition.Column = 0; - - _haveReduction = false; - _tablesLoaded = false; - _trimReductions = false; - } - - /// - /// Opens a string for parsing. - /// - /// The string to be parsed - /// - public bool Open(string text) - { - return Open(new StringReader(text)); - } - - /// - /// Opens a text stream for parsing. - /// - /// The TextReader stream to be parsed - /// - public bool Open(TextReader reader) - { - Restart(); - _Source = reader; - - Token token = new Token(); - token.State = _grammar.InitialStates.DfaInitialState; - _stack.Push(token); - return true; - } - - /// - /// Restarts the parser. Loaded tables are retained. - /// - public new void Restart() - { - base.Restart(); - - _CurrentLALR = _grammar.InitialStates.LrInitialState; - - _currentPosition.Line = 0; - _currentPosition.Column = 0; - _haveReduction = false; - - _expectedSymbols.Clear(); - _inputTokens.Clear(); - _stack.Clear(); - } - - /// - /// Removes the next token from the input queue. - /// - /// - public Token PopToken() - { - return _inputTokens.Pop(); - } - - /// - /// Pushes the token onto the top of the input stack. This token will be analyzed next. - /// - /// - public void PushToken(ref Token token) - { - _inputTokens.Push(token); - } - - /// - /// Performs a parse action on the input. - /// - /// ParseMessage - public ParseMessage Parse() - { - if (!_tablesLoaded) return ParseMessage.NotLoadedError; - while (true) - { - if (_inputTokens.Count == 0) - { - Token t = produceToken(); - _inputTokens.Push(t); - return ParseMessage.TokenRead; - } - - Token token = _inputTokens.Peek(); - _currentPosition.Copy(token.Position); - - if (_GroupStack.Count != 0) return ParseMessage.GroupError; - if (token.SymbolType == GrammarSymbolType.Error) return ParseMessage.LexicalError; - if (token.SymbolType == GrammarSymbolType.Noise) - { - _inputTokens.Pop(); - continue; - } - - ParseResult r = ParseLALR(ref token); - switch (r) - { - case ParseResult.Accept: return ParseMessage.Accept; - case ParseResult.ReduceNormal: return ParseMessage.Reduction; - case ParseResult.SyntaxError: return ParseMessage.SyntaxError; - case ParseResult.InternalError: return ParseMessage.InternalError; - case ParseResult.Shift: _inputTokens.Pop(); break; - } - } - } - private ParseResult ParseLALR(ref Token token) - { - GrammarLRState lRState = _grammar.LRStates[_CurrentLALR]; - GrammarLRAction lRAction = lRState[token.Symbol]; - - if (lRAction == null) - { - _expectedSymbols.Clear(); - try - { - for (int i = 0; i < lRState.Count(); i++) - { - GrammarLRAction lra = lRState[i]; - switch (lra.Symbol.Type) - { - case GrammarSymbolType.Terminal: - case GrammarSymbolType.End: - case GrammarSymbolType.GroupStart: - case GrammarSymbolType.GroupEnd: - _expectedSymbols.Add(lra.Symbol); - break; - } - } - } - catch { } - return ParseResult.SyntaxError; - } - - _haveReduction = false; - switch (lRAction.Type) - { - case LRActionType.Accept: - _haveReduction = true; - return ParseResult.Accept; - - case LRActionType.Shift: - _CurrentLALR = lRAction.Value; - token.State = _CurrentLALR; //checked(_CurrentLALR); - _stack.Push(token); //_Stack.Push(ref token); - return ParseResult.Shift; - - case LRActionType.Reduce: - - GrammarProduction production = _grammar.Productions[lRAction.Value]; - - ParseResult result; - Token t; - if (_trimReductions && production.ContainsOneNonTerminal()) - { - t = _stack.Pop(); - t.Symbol = production.Head; - result = ParseResult.ReduceEliminated; - } - else - { - _haveReduction = true; - Reduction reduction = new Reduction(production.Handle.Count()); - reduction.Production = production; - for (int i = production.Handle.Count() - 1; i >= 0; i -= 1) - { - reduction[i] = _stack.Pop(); - } - t = new Token(production.Head, reduction); - result = ParseResult.ReduceNormal; - } - - int stateindex = _stack.Peek().State; - GrammarLRState state = _grammar.LRStates[stateindex]; - int actionindex = state.IndexOf(production.Head); //action index is returned - - if (actionindex == -1) { return ParseResult.InternalError; } - - GrammarLRAction lra = state[actionindex]; - _CurrentLALR = lra.Value; - t.State = _CurrentLALR; - _stack.Push(t); - - return result; - - default: - return ParseResult.InternalError; - } - } - } + } + } } //_InputTokens.Top() is _InputTokens.Peek() //_ExpectedSymbols is _expectedSymbols diff --git a/@GoldParserEngine/GoldParserEngine/Parser/ParserBase.cs b/@GoldParserEngine/GoldParserEngine/Parser/ParserBase.cs index 782f9318..a937acaf 100644 --- a/@GoldParserEngine/GoldParserEngine/Parser/ParserBase.cs +++ b/@GoldParserEngine/GoldParserEngine/Parser/ParserBase.cs @@ -1,44 +1,40 @@ -using System; +using GoldParser.Grammar; +using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using GoldParser.Grammar; namespace GoldParser.Parser { - public class ParserBase + public class ParserBase { private const string _aboutString = "GOLD Parser Engine; Version 5.0; (Refactored)"; protected GrammarTables _grammar; protected bool _tablesLoaded; - private TextReader _source; - private string _sourcePath; + private TextReader _source; + private string _sourcePath; - /// - /// Library name and version. - /// - public string About - { - get - { - return _aboutString; - } - } + /// + /// Library name and version. + /// + public string About + { + get + { + return _aboutString; + } + } - /// - /// Check if parse tables were loaded. - /// - internal bool TablesLoaded + /// + /// Check if parse tables were loaded. + /// + internal bool TablesLoaded { get { @@ -50,10 +46,10 @@ internal bool TablesLoaded } } - /// - /// Returns information about the current grammar. - /// - internal GrammarProperties GrammarInfo + /// + /// Returns information about the current grammar. + /// + internal GrammarProperties GrammarInfo { get { @@ -61,150 +57,150 @@ internal GrammarProperties GrammarInfo } } - /// - /// Returns a list of Symbols recognized by the grammar. - /// - internal GrammarSymbol[] SymbolTable + /// + /// Returns a list of Symbols recognized by the grammar. + /// + internal GrammarSymbol[] SymbolTable { get { - return _grammar.Symbols; - } + return _grammar.Symbols; + } } - /// - /// Returns a list of Productions recognized by the grammar. - /// - internal GrammarProduction[] ProductionTable + /// + /// Returns a list of Productions recognized by the grammar. + /// + internal GrammarProduction[] ProductionTable { get { - return _grammar.Productions; - } + return _grammar.Productions; + } } - /// - /// Check if file for parsing have been opened - /// - internal bool HaveSourcePath + /// + /// Check if file for parsing have been opened + /// + internal bool HaveSourcePath { - get + get { - if(string.IsNullOrEmpty(_sourcePath)) + if (string.IsNullOrEmpty(_sourcePath)) { - return false; + return false; } - return true; + return true; + } + } + + /// + /// Path of source file to be parsed + /// + internal string SourcePath + { + get + { + return _sourcePath; + } + set + { + _sourcePath = value; } } - /// - /// Path of source file to be parsed - /// - internal string SourcePath - { - get - { - return _sourcePath; - } - set - { - _sourcePath = value; - } - } - - - - /// - /// Ctor. - /// - public ParserBase() + + + /// + /// Ctor. + /// + public ParserBase() { - _tablesLoaded = false; - } + _tablesLoaded = false; + } - /// - /// Opens a string for parsing. - /// - /// The string to be parsed - /// true if successful - internal bool OpenFile(string text) - { - try + /// + /// Opens a string for parsing. + /// + /// The string to be parsed + /// true if successful + internal bool OpenFile(string text) + { + try { - StringReader reader = new StringReader(text); - return OpenFile(reader); - } - catch (Exception ex) + StringReader reader = new StringReader(text); + return OpenFile(reader); + } + catch (Exception ex) + { + throw new ParserException(ex.Message, ex, "OpenFile"); + } + } + + /// + /// Opens a text stream for parsing. + /// + /// The TextReader stream to be parsed + /// Always true + public bool OpenFile(TextReader Reader) + { + _source = Reader; + return true; + } + + + + /// + /// Loads parse tables from the specified filename. + /// Only EGT (version 5.0) is supported. + /// + /// + /// Path to an EGT file containing grammar tables + /// + /// true if successful + public bool LoadTables(string filepath) + { + try { - throw new ParserException(ex.Message, ex, "OpenFile"); + Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read); + BinaryReader reader = new BinaryReader(stream); + return LoadTables(reader); } - } - - /// - /// Opens a text stream for parsing. - /// - /// The TextReader stream to be parsed - /// Always true - public bool OpenFile(TextReader Reader) - { - _source = Reader; - return true; - } - - - - /// - /// Loads parse tables from the specified filename. - /// Only EGT (version 5.0) is supported. - /// - /// - /// Path to an EGT file containing grammar tables - /// - /// true if successful - public bool LoadTables(string filepath) - { - try - { - Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read); - BinaryReader reader = new BinaryReader(stream); - return LoadTables(reader); - } - catch (Exception ex) - { - _tablesLoaded = false; - throw new ParserException(ex.Message, ex, "LoadTables"); - } - } - - /// - /// Loads parse tables from the specified BinaryReader. - /// Only EGT (version 5.0) is supported. - /// - /// - /// A binary reader to the data of EGT file - /// containing grammar tables - /// - /// true if successful - public bool LoadTables(BinaryReader reader) - { - try - { - List records = Egt.EgtContentReager.ReadFileRecords(reader); - _grammar = GrammarContentReader.ReadGrammar(records); - reader.Close(); - _tablesLoaded = true; - return true; - } - catch (ParserException ex) - { - _tablesLoaded = false; - throw new ParserException(ex.Message, ex, "LoadTables"); - } - } - } + catch (Exception ex) + { + _tablesLoaded = false; + throw new ParserException(ex.Message, ex, "LoadTables"); + } + } + + /// + /// Loads parse tables from the specified BinaryReader. + /// Only EGT (version 5.0) is supported. + /// + /// + /// A binary reader to the data of EGT file + /// containing grammar tables + /// + /// true if successful + public bool LoadTables(BinaryReader reader) + { + try + { + List records = Egt.EgtContentReager.ReadFileRecords(reader); + _grammar = GrammarContentReader.ReadGrammar(records); + reader.Close(); + _tablesLoaded = true; + return true; + } + catch (ParserException ex) + { + _tablesLoaded = false; + throw new ParserException(ex.Message, ex, "LoadTables"); + } + } + } } \ No newline at end of file diff --git a/@GoldParserEngine/GoldParserEngine/Parser/ParserException.cs b/@GoldParserEngine/GoldParserEngine/Parser/ParserException.cs index f057d7f0..8300db40 100644 --- a/@GoldParserEngine/GoldParserEngine/Parser/ParserException.cs +++ b/@GoldParserEngine/GoldParserEngine/Parser/ParserException.cs @@ -1,22 +1,18 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace GoldParser.Parser { - public class ParserException : Exception - { - internal string Method; + public class ParserException : Exception + { + internal string Method; - internal ParserException(string message) : base(message) - { - Method = ""; - } - internal ParserException(string message, Exception inner, string method) : base(message, inner) - { - Method = method; - } - } + internal ParserException(string message) : base(message) + { + Method = ""; + } + internal ParserException(string message, Exception inner, string method) : base(message, inner) + { + Method = method; + } + } } diff --git a/@GoldParserEngine/GoldParserEngine/Parser/ParserLexer.cs b/@GoldParserEngine/GoldParserEngine/Parser/ParserLexer.cs index 78bf1209..77373d48 100644 --- a/@GoldParserEngine/GoldParserEngine/Parser/ParserLexer.cs +++ b/@GoldParserEngine/GoldParserEngine/Parser/ParserLexer.cs @@ -1,338 +1,336 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using GoldParser.Grammar; using GoldParser.ParseTree; -using GoldParser.Grammar; +using System; +using System.Collections.Generic; using System.IO; +using System.Linq; namespace GoldParser.Parser { public class ParserLexer : ParserBase { - protected TextReader _Source; - protected Stack _GroupStack; - private Position _SysPosition; - private string _LookaheadBuffer; - - - - /// - /// Ctor. - /// - public ParserLexer() : base() - { - _GroupStack = new Stack(); - _SysPosition = new Position(); - _LookaheadBuffer = ""; - } - - /// - /// Restarts the Lexer. Loaded tables are retained. - /// - public void Restart() - { - _GroupStack.Clear(); - _SysPosition.Column = 0; - _SysPosition.Line = 0; - _LookaheadBuffer = ""; - } - - - - - /// - /// This function creates a token and also takes into account the current - /// lexing mode of the parser. In particular, it contains the group logic. - /// - /// A stack is used to track the current "group". This replaces the comment - /// level counter. Also, text is appended to the token on the top of the - /// stack. This allows the group text to returned in one chunk. - /// - /// - protected Token produceToken() - { - while (true) - { - Token token = produceTokenDFA(); - - //The logic - to determine if a group should be nested - requires that the top of the stack - //and the symbol's linked group need to be looked at. Both of these can be unset. So, this section - //sets a Boolean and avoids errors. We will use this boolean in the logic chain below. - bool nestGroup = false; - if (token.SymbolType == GrammarSymbolType.GroupStart) - { - nestGroup = _GroupStack.Count == 0 || - _GroupStack.Peek().SymbolGroup.Nesting.Contains(token.SymbolGroup.TableIndex); - } - - //TOKEN IS GROUP START-ING - //If a token is one that is a start of a group, we can start a - //group by pushing it on the group stack. We are allowed to do so, - //if the group stack is empty(we are not in a group), or group - //stack is not empty(we are in a group) and that groups' Nesting - //allows for current group we are starting to be nested in. - if (nestGroup) - { - trimBuffer((token.Data as string).Length); - _GroupStack.Push(token); - } - - //TOKEN IS NOT GROUP-START OR IS NOT NESTABLE GROUP-START - //WE ARE NOT IN A GROUP - else if (_GroupStack.Count == 0) - { - trimBuffer((token.Data as string).Length); - return token; - } - - //TOKEN IS NOT GROUP-START OR IS NOT NESTABLE GROUP-START - //WE ARE NOT-NOT IN A GROUP - //TOKEN IS GROUP END-ING - else if (_GroupStack.Peek().SymbolGroup.EndSymbol == token.Symbol) - { - //End the current group - Token pop = _GroupStack.Pop(); - - //if we have GrammarGroupEndingMode.Closed then add the end - //token, otherwise don't - if (pop.SymbolGroup.Ending == GrammarGroupEndingMode.Closed) - { - pop.Data = (pop.Data as string) + (token.Data as string); - trimBuffer((token.Data as string).Length); - } - - //We are out of the group. Return pop'd token (which contains all the group text) - if (_GroupStack.Count == 0) - { - pop.Symbol = pop.SymbolGroup.ContainerSymbol; //Change symbol to symbol of the group - return pop; - } - else - { - Token top = _GroupStack.Peek(); - top.Data = (top.Data as string) + (pop.Data as string); - } - } - - //TOKEN IS NOT GROUP-START OR IS NOT NESTABLE GROUP-START - //TOKEN IS GROUP END-ING - //WE ARE NOT-NOT IN A GROUP - //TOKEN IS EOF - "End Of File" - else if (token.SymbolType == GrammarSymbolType.End) - { - return token; - } - - //TOKEN IS NOT GROUP-START OR IS NOT NESTABLE GROUP-START - //TOKEN IS NOT GROUP END-ING - //WE ARE NOT-NOT IN A GROUP - //TOKEN IS NOT EOF - "End Of File" - // - //we are in group and none of the above, so if that group is TokenAdvance - //we add the text of the token to the Data of previous one, otherwise, if - //we have CharacterAdvance, we add just the first character. we then continue - //on with the loop - else - { - Token previousToken = _GroupStack.Peek(); - if (previousToken.SymbolGroup.Advance == GrammarGroupAdvanceMode.Token) - { - previousToken.Data = (previousToken.Data as string) + (token.Data as string); - trimBuffer((token.Data as string).Length); - } - else - { - previousToken.Data = (previousToken.Data as string) + (token.Data as string)[0]; - trimBuffer(1); - } - } - } - } - - /// - /// This function implements the DFA for the parser's lexer. - /// It generates a token which is used by the LALR state machine. - /// - /// //https://github.com/meziantou/GoldParser-Engine/blob/master/GoldParserEngine/GoldParserEngine/Parser.cs - Token produceTokenDFA() - { - Token result = new Token(); - int currentDfa = _grammar.InitialStates.DfaInitialState; - int lastAcceptState = -1; //Next byte in the input Stream - int lastAcceptPosition = -1; //We have not yet accepted a character string - int currentPosition = 1; - bool done = false; - - string ch = getCharacter(1); // Get first symbol from the lookahead buffer - if (ch != "" && Convert.ToInt32(ch[0]) != 65535) // End of file is not reached - { - while (done == false) - { - // This code searches all the branches of the current DFA state - // for the next character in the input Stream. If found the - // target state is returned. - ch = getCharacter(currentPosition); //Get first x symbols from the lookahead buffer - int target = -1; - //if ch is "", then End of file reached, so do not match - if (!string.IsNullOrEmpty(ch)) - { - GrammarFAState currentFAState = _grammar.FAStates[currentDfa]; - for (int n = 0; n < currentFAState.Edges.Count(); n++) - { - GrammarFAEdge fAEdge = currentFAState.Edges[n]; - //Look for character in the Character Set Table - if (fAEdge.CharacterSet.Contains(Convert.ToInt32(ch[0]))) - { - target = fAEdge.Target; - break; - } - } - } - - - // This block-if statement checks whether an edge was found from the current state. If so, the state and current - // position advance. Otherwise it is time to exit the main loop and report the token found (if there was one). - // If the LastAcceptState is -1, then we never found a match and the Error Token is created. Otherwise, a new - // token is created using the Symbol in the Accept State and all the characters that comprise it. - - if (target > -1) //that means that target state is found - { - // This code checks whether the target state accepts a token. - // If so, it sets the appropiate variables so when the - // algorithm in done, it can return the proper token and - // number of characters. - - if (_grammar.FAStates[target].Accept != null) - { - lastAcceptState = target; - lastAcceptPosition = currentPosition; - } - currentDfa = target; - currentPosition++; - } - else - { - done = true; - if (lastAcceptState == -1) // Lexer cannot recognize symbol - { - result.Symbol = getFirstOfType(_grammar, GrammarSymbolType.Error); - result.Data = getString(1); - } - else //Lexer can recognize symbol - { - result.Symbol = _grammar.FAStates[lastAcceptState].Accept; - result.Data = getString(lastAcceptPosition); - //Data contains the total number of accept characters - } - } - } - } - else // End of file reached, create End Token - { - result.Data = ""; - result.Symbol = getFirstOfType(_grammar, GrammarSymbolType.End); - } - - //Set the new token's position information - //Notice, this is a copy, not a linking of an instance. We don't want the user - //to be able to alter the main value indirectly. - result.Position.Copy(_SysPosition); - return result; - } - - /// - /// Get a character from _LookaheadBuffer, and return it as string. Uses one-based indexing! - /// If _LookaheadBuffer is too short, read more chars from _Source TextReader - /// returns _LookaheadBuffer[charIndex - 1] - /// - /// - /// - string getCharacter(int charIndex) - { - //Check if we must read characters from the Stream - if (charIndex > _LookaheadBuffer.Length) - { - for (int i = 0; i < charIndex - _LookaheadBuffer.Length; i++) - { - int x = _Source.Read(); - char c = (char)x;//char c = Strings.ChrW(x); - _LookaheadBuffer += c.ToString(); - } - } - - //If the buffer is still smaller than the index, we have reached - //the end of the text. In this case, return a null string - the DFA - //code will understand. - if (charIndex <= _LookaheadBuffer.Length) - { - return _LookaheadBuffer[charIndex - 1].ToString(); - } - return ""; - } - - /// - /// Get first "count" number of symbols from _LookaheadBuffer. - /// - /// Return Count characters from the lookahead buffer. DO NOT CONSUME - /// This is used to create the text stored in a token. It is disgarded - /// separately. Because of the design of the DFA algorithm, count should - /// never exceed the buffer length. The If-Statement below is fault-tolerate - /// programming, but not necessary. - /// - /// - /// - string getString(int length) - { - - if (length > _LookaheadBuffer.Length) - { - length = _LookaheadBuffer.Length; - } - return _LookaheadBuffer.Substring(0, length); - } - - /// - /// ConsumeBuffer increments _SysPosition according to the characters in _LookaheadBuffer - /// and removes those characters from _LookaheadBuffer - /// - /// Number of characters to advance the _SysPosition position - void trimBuffer(int count) - { - if (count > _LookaheadBuffer.Length) return; - - // Count Carriage Returns and increment the internal column and line - // numbers. This is done for the Developer and is not necessary for the - // DFA algorithm. - for (int n = 0; n <= count - 1; n++) - { - switch (_LookaheadBuffer[n]) - { - case '\n': - _SysPosition.Line++; - _SysPosition.Column = 0; - break; - - case '\r': //carriage return - break; //Ignore, LF is used to inc line to be UNIX friendly - - case '\v': //vertical tab - case '\f': //form feed - default: - _SysPosition.Column++; - break; - } - } - - _LookaheadBuffer = _LookaheadBuffer.Remove(0, count); - } - - GrammarSymbol getFirstOfType(GrammarTables grammar, GrammarSymbolType type) + protected TextReader _Source; + protected Stack _GroupStack; + private Position _SysPosition; + private string _LookaheadBuffer; + + + + /// + /// Ctor. + /// + public ParserLexer() : base() + { + _GroupStack = new Stack(); + _SysPosition = new Position(); + _LookaheadBuffer = ""; + } + + /// + /// Restarts the Lexer. Loaded tables are retained. + /// + public void Restart() + { + _GroupStack.Clear(); + _SysPosition.Column = 0; + _SysPosition.Line = 0; + _LookaheadBuffer = ""; + } + + + + + /// + /// This function creates a token and also takes into account the current + /// lexing mode of the parser. In particular, it contains the group logic. + /// + /// A stack is used to track the current "group". This replaces the comment + /// level counter. Also, text is appended to the token on the top of the + /// stack. This allows the group text to returned in one chunk. + /// + /// + protected Token produceToken() + { + while (true) + { + Token token = produceTokenDFA(); + + //The logic - to determine if a group should be nested - requires that the top of the stack + //and the symbol's linked group need to be looked at. Both of these can be unset. So, this section + //sets a Boolean and avoids errors. We will use this boolean in the logic chain below. + bool nestGroup = false; + if (token.SymbolType == GrammarSymbolType.GroupStart) + { + nestGroup = _GroupStack.Count == 0 || + _GroupStack.Peek().SymbolGroup.Nesting.Contains(token.SymbolGroup.TableIndex); + } + + //TOKEN IS GROUP START-ING + //If a token is one that is a start of a group, we can start a + //group by pushing it on the group stack. We are allowed to do so, + //if the group stack is empty(we are not in a group), or group + //stack is not empty(we are in a group) and that groups' Nesting + //allows for current group we are starting to be nested in. + if (nestGroup) + { + trimBuffer((token.Data as string).Length); + _GroupStack.Push(token); + } + + //TOKEN IS NOT GROUP-START OR IS NOT NESTABLE GROUP-START + //WE ARE NOT IN A GROUP + else if (_GroupStack.Count == 0) + { + trimBuffer((token.Data as string).Length); + return token; + } + + //TOKEN IS NOT GROUP-START OR IS NOT NESTABLE GROUP-START + //WE ARE NOT-NOT IN A GROUP + //TOKEN IS GROUP END-ING + else if (_GroupStack.Peek().SymbolGroup.EndSymbol == token.Symbol) + { + //End the current group + Token pop = _GroupStack.Pop(); + + //if we have GrammarGroupEndingMode.Closed then add the end + //token, otherwise don't + if (pop.SymbolGroup.Ending == GrammarGroupEndingMode.Closed) + { + pop.Data = (pop.Data as string) + (token.Data as string); + trimBuffer((token.Data as string).Length); + } + + //We are out of the group. Return pop'd token (which contains all the group text) + if (_GroupStack.Count == 0) + { + pop.Symbol = pop.SymbolGroup.ContainerSymbol; //Change symbol to symbol of the group + return pop; + } + else + { + Token top = _GroupStack.Peek(); + top.Data = (top.Data as string) + (pop.Data as string); + } + } + + //TOKEN IS NOT GROUP-START OR IS NOT NESTABLE GROUP-START + //TOKEN IS GROUP END-ING + //WE ARE NOT-NOT IN A GROUP + //TOKEN IS EOF - "End Of File" + else if (token.SymbolType == GrammarSymbolType.End) + { + return token; + } + + //TOKEN IS NOT GROUP-START OR IS NOT NESTABLE GROUP-START + //TOKEN IS NOT GROUP END-ING + //WE ARE NOT-NOT IN A GROUP + //TOKEN IS NOT EOF - "End Of File" + // + //we are in group and none of the above, so if that group is TokenAdvance + //we add the text of the token to the Data of previous one, otherwise, if + //we have CharacterAdvance, we add just the first character. we then continue + //on with the loop + else + { + Token previousToken = _GroupStack.Peek(); + if (previousToken.SymbolGroup.Advance == GrammarGroupAdvanceMode.Token) + { + previousToken.Data = (previousToken.Data as string) + (token.Data as string); + trimBuffer((token.Data as string).Length); + } + else + { + previousToken.Data = (previousToken.Data as string) + (token.Data as string)[0]; + trimBuffer(1); + } + } + } + } + + /// + /// This function implements the DFA for the parser's lexer. + /// It generates a token which is used by the LALR state machine. + /// + /// //https://github.com/meziantou/GoldParser-Engine/blob/master/GoldParserEngine/GoldParserEngine/Parser.cs + Token produceTokenDFA() + { + Token result = new Token(); + int currentDfa = _grammar.InitialStates.DfaInitialState; + int lastAcceptState = -1; //Next byte in the input Stream + int lastAcceptPosition = -1; //We have not yet accepted a character string + int currentPosition = 1; + bool done = false; + + string ch = getCharacter(1); // Get first symbol from the lookahead buffer + if (ch != "" && Convert.ToInt32(ch[0]) != 65535) // End of file is not reached + { + while (done == false) + { + // This code searches all the branches of the current DFA state + // for the next character in the input Stream. If found the + // target state is returned. + ch = getCharacter(currentPosition); //Get first x symbols from the lookahead buffer + int target = -1; + //if ch is "", then End of file reached, so do not match + if (!string.IsNullOrEmpty(ch)) + { + GrammarFAState currentFAState = _grammar.FAStates[currentDfa]; + for (int n = 0; n < currentFAState.Edges.Count(); n++) + { + GrammarFAEdge fAEdge = currentFAState.Edges[n]; + //Look for character in the Character Set Table + if (fAEdge.CharacterSet.Contains(Convert.ToInt32(ch[0]))) + { + target = fAEdge.Target; + break; + } + } + } + + + // This block-if statement checks whether an edge was found from the current state. If so, the state and current + // position advance. Otherwise it is time to exit the main loop and report the token found (if there was one). + // If the LastAcceptState is -1, then we never found a match and the Error Token is created. Otherwise, a new + // token is created using the Symbol in the Accept State and all the characters that comprise it. + + if (target > -1) //that means that target state is found + { + // This code checks whether the target state accepts a token. + // If so, it sets the appropiate variables so when the + // algorithm in done, it can return the proper token and + // number of characters. + + if (_grammar.FAStates[target].Accept != null) + { + lastAcceptState = target; + lastAcceptPosition = currentPosition; + } + currentDfa = target; + currentPosition++; + } + else + { + done = true; + if (lastAcceptState == -1) // Lexer cannot recognize symbol + { + result.Symbol = getFirstOfType(_grammar, GrammarSymbolType.Error); + result.Data = getString(1); + } + else //Lexer can recognize symbol + { + result.Symbol = _grammar.FAStates[lastAcceptState].Accept; + result.Data = getString(lastAcceptPosition); + //Data contains the total number of accept characters + } + } + } + } + else // End of file reached, create End Token + { + result.Data = ""; + result.Symbol = getFirstOfType(_grammar, GrammarSymbolType.End); + } + + //Set the new token's position information + //Notice, this is a copy, not a linking of an instance. We don't want the user + //to be able to alter the main value indirectly. + result.Position.Copy(_SysPosition); + return result; + } + + /// + /// Get a character from _LookaheadBuffer, and return it as string. Uses one-based indexing! + /// If _LookaheadBuffer is too short, read more chars from _Source TextReader + /// returns _LookaheadBuffer[charIndex - 1] + /// + /// + /// + string getCharacter(int charIndex) + { + //Check if we must read characters from the Stream + if (charIndex > _LookaheadBuffer.Length) + { + for (int i = 0; i < charIndex - _LookaheadBuffer.Length; i++) + { + int x = _Source.Read(); + char c = (char)x;//char c = Strings.ChrW(x); + _LookaheadBuffer += c.ToString(); + } + } + + //If the buffer is still smaller than the index, we have reached + //the end of the text. In this case, return a null string - the DFA + //code will understand. + if (charIndex <= _LookaheadBuffer.Length) + { + return _LookaheadBuffer[charIndex - 1].ToString(); + } + return ""; + } + + /// + /// Get first "count" number of symbols from _LookaheadBuffer. + /// + /// Return Count characters from the lookahead buffer. DO NOT CONSUME + /// This is used to create the text stored in a token. It is disgarded + /// separately. Because of the design of the DFA algorithm, count should + /// never exceed the buffer length. The If-Statement below is fault-tolerate + /// programming, but not necessary. + /// + /// + /// + string getString(int length) + { + + if (length > _LookaheadBuffer.Length) + { + length = _LookaheadBuffer.Length; + } + return _LookaheadBuffer.Substring(0, length); + } + + /// + /// ConsumeBuffer increments _SysPosition according to the characters in _LookaheadBuffer + /// and removes those characters from _LookaheadBuffer + /// + /// Number of characters to advance the _SysPosition position + void trimBuffer(int count) + { + if (count > _LookaheadBuffer.Length) return; + + // Count Carriage Returns and increment the internal column and line + // numbers. This is done for the Developer and is not necessary for the + // DFA algorithm. + for (int n = 0; n <= count - 1; n++) + { + switch (_LookaheadBuffer[n]) + { + case '\n': + _SysPosition.Line++; + _SysPosition.Column = 0; + break; + + case '\r': //carriage return + break; //Ignore, LF is used to inc line to be UNIX friendly + + case '\v': //vertical tab + case '\f': //form feed + default: + _SysPosition.Column++; + break; + } + } + + _LookaheadBuffer = _LookaheadBuffer.Remove(0, count); + } + + GrammarSymbol getFirstOfType(GrammarTables grammar, GrammarSymbolType type) { - return grammar.Symbols.FirstOrDefault(_ => _.Type == type); - } - } + return grammar.Symbols.FirstOrDefault(_ => _.Type == type); + } + } } //Top() is Peek() //LookaheadDFA() is produceTokenDFA() diff --git a/@GoldParserEngine/GoldParserEngine/Properties/AssemblyInfo.cs b/@GoldParserEngine/GoldParserEngine/Properties/AssemblyInfo.cs index ed263dac..e64c92df 100644 --- a/@GoldParserEngine/GoldParserEngine/Properties/AssemblyInfo.cs +++ b/@GoldParserEngine/GoldParserEngine/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following diff --git a/Documentation/v 9.2/next/ConfigFile.txt b/Documentation/v 9.2/next/ConfigFile.txt new file mode 100644 index 00000000..c1184ea8 --- /dev/null +++ b/Documentation/v 9.2/next/ConfigFile.txt @@ -0,0 +1 @@ +Have an option to load settings from a config file instead of the console arguments \ No newline at end of file diff --git a/Documentation/v 9.2/fileOrFolder.txt b/Documentation/v 9.2/next/fileOrFolder.txt similarity index 100% rename from Documentation/v 9.2/fileOrFolder.txt rename to Documentation/v 9.2/next/fileOrFolder.txt diff --git a/Documentation/v 9.2/makeSureDatnikRestarts.txt b/Documentation/v 9.2/next/makeSureDatnikRestarts.txt similarity index 100% rename from Documentation/v 9.2/makeSureDatnikRestarts.txt rename to Documentation/v 9.2/next/makeSureDatnikRestarts.txt diff --git a/Documentation/v 9.2/quirkDoubleTranslatorInitWhenLoadingExternal.txt b/Documentation/v 9.2/next/quirkDoubleTranslatorInitWhenLoadingExternal.txt similarity index 100% rename from Documentation/v 9.2/quirkDoubleTranslatorInitWhenLoadingExternal.txt rename to Documentation/v 9.2/next/quirkDoubleTranslatorInitWhenLoadingExternal.txt diff --git a/Documentation/v 9.2/quirkGenericFileFolderErrorLogCLI.txt b/Documentation/v 9.2/next/quirkGenericFileFolderErrorLogCLI.txt similarity index 100% rename from Documentation/v 9.2/quirkGenericFileFolderErrorLogCLI.txt rename to Documentation/v 9.2/next/quirkGenericFileFolderErrorLogCLI.txt diff --git a/Documentation/v 9.2/quirkLowVerbosityFolderLogAsFile.txt b/Documentation/v 9.2/next/quirkLowVerbosityFolderLogAsFile.txt similarity index 100% rename from Documentation/v 9.2/quirkLowVerbosityFolderLogAsFile.txt rename to Documentation/v 9.2/next/quirkLowVerbosityFolderLogAsFile.txt