//Generated by the GOLD Parser Builder namespace FSO.Client.UI.Framework.Parser { using System.IO; using System.Collections.Generic; using GOLDEngine; class UIScriptParser { private Parser parser = new Parser(); private Dictionary DataMap = new Dictionary(); private enum SymbolIndex { @Eof = 0, // (EOF) @Error = 1, // (Error) @Comment = 2, // Comment @Newline = 3, // NewLine @Whitespace = 4, // Whitespace @Num = 5, // '#' @Ltexclamminusminus = 6, // '' @Divgt = 8, // '/>' @Lt = 9, // '<' @Ltdiv = 10, // '' @Beginliteral = 13, // BeginLiteral @Charname = 14, // CharName @Charnumber = 15, // CharNumber @Endliteral = 16, // EndLiteral @Id = 17, // ID @Stringliteral = 18, // StringLiteral @Attribute = 19, // @Attributes = 20, // @Content = 21, // @Endtag = 22, // @Object = 23, // @Objects = 24, // @Optionalid = 25, // @Starttag = 26, // @Text = 27, // @Unarytag = 28, // @Word = 29 // } private enum ProductionIndex { @Objects = 0, // ::= @Objects2 = 1, // ::= @Object_Beginliteral_Endliteral = 2, // ::= BeginLiteral EndLiteral @Object = 3, // ::= @Object2 = 4, // ::= @Starttag_Lt_Id_Gt = 5, // ::= '<' ID '>' @Endtag_Ltdiv_Id_Gt = 6, // ::= '' @Unarytag_Lt_Id_Divgt = 7, // ::= '<' ID '/>' @Optionalid_Stringliteral = 8, // ::= StringLiteral @Optionalid = 9, // ::= @Content = 10, // ::= @Content2 = 11, // ::= @Attributes = 12, // ::= @Attributes2 = 13, // ::= @Attribute_Id_Eq_Stringliteral = 14, // ::= ID '=' StringLiteral @Attribute_Id_Eq_Id = 15, // ::= ID '=' ID @Text = 16, // ::= @Text2 = 17, // ::= @Word_Id = 18, // ::= ID @Word_Eq = 19, // ::= '=' @Word_Charname = 20, // ::= CharName @Word_Charnumber = 21 // ::= CharNumber } public object program; //You might derive a specific object public void Setup() { //This procedure can be called to load the parse tables. The class can //read tables using a BinaryReader. parser.LoadTables(new BinaryReader(new FileStream("Content/UIScript.egt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))); } public bool Parse(TextReader reader) { //This procedure starts the GOLD Parser Engine and handles each of the //messages it returns. Each time a reduction is made, you can create new //custom object and reassign the .CurrentReduction property. Otherwise, //the system will use the Reduction object that was returned. // //The resulting tree will be a pure representation of the language //and will be ready to implement. GOLDEngine.ParseMessage response; bool done; //Controls when we leave the loop bool accepted = false; //Was the parse successful? parser.Open(reader); parser.TrimReductions = false; //Please read about this feature before enabling done = false; while (!done) { response = parser.Parse(); switch (response) { case GOLDEngine.ParseMessage.LexicalError: //Cannot recognize token done = true; break; case GOLDEngine.ParseMessage.SyntaxError: //Expecting a different token done = true; break; case GOLDEngine.ParseMessage.Reduction: //Create a customized object to store the reduction CreateNewObject(parser.CurrentReduction as GOLDEngine.Reduction); break; case GOLDEngine.ParseMessage.Accept: //Accepted! //program = parser.CurrentReduction //The root node! done = true; accepted = true; break; case GOLDEngine.ParseMessage.TokenRead: //You don't have to do anything here. break; case GOLDEngine.ParseMessage.InternalError: //INTERNAL ERROR! Something is horribly wrong. done = true; break; case GOLDEngine.ParseMessage.NotLoadedError: //This error occurs if the CGT was not loaded. done = true; break; case GOLDEngine.ParseMessage.GroupError: //GROUP ERROR! Unexpected end of file done = true; break; } } //while return accepted; } private object CreateNewObject(GOLDEngine.Reduction r) { object result = r.ToText(); for (int i = 0; i < r.Count; i++) { if (!DataMap.ContainsKey(r[i])) { DataMap[r[i]] = r[i].ToText(); } } switch ((ProductionIndex)r.Production.TableIndex()) { case ProductionIndex.Objects: var newResult = new List(); newResult.Add((UINode)DataMap[r[0]]); newResult.AddRange((List)DataMap[r[1]]); result = newResult; program = result; // ::= break; case ProductionIndex.Objects2: // ::= var newResult2 = new List(); newResult2.Add((UINode)DataMap[r[0]]); result = newResult2; program = result; break; case ProductionIndex.Object_Beginliteral_Endliteral: // ::= BeginLiteral EndLiteral result = UIGroup.FromReduction(r, DataMap); break; case ProductionIndex.Object: // ::= result = DataMap[r[0]]; break; case ProductionIndex.Object2: // ::= break; case ProductionIndex.Starttag_Lt_Id_Gt: // ::= '<' ID '>' var node = new UINode(); node.Name = (string)DataMap[r[1]]; node.ID = (string)DataMap[r[2]]; var atts = (List>)DataMap[r[3]]; foreach (var att in atts) { node[att.Key] = att.Value; } result = node; break; case ProductionIndex.Endtag_Ltdiv_Id_Gt: // ::= '' break; case ProductionIndex.Unarytag_Lt_Id_Divgt: // ::= '<' ID '/>' break; case ProductionIndex.Optionalid_Stringliteral: // ::= StringLiteral result = GetStringLiteral((string)DataMap[r[0]]); break; case ProductionIndex.Optionalid: // ::= result = null; break; case ProductionIndex.Content: // ::= var newResult3 = new List(); newResult3.AddRange((List)DataMap[r[0]]); result = newResult3; break; case ProductionIndex.Content2: // ::= var newResult4 = new List(); newResult4.Add((UINode)DataMap[r[0]]); result = newResult4; break; case ProductionIndex.Attributes: // ::= var attributeList = new List>(); attributeList.Add((KeyValuePair)DataMap[r[0]]); if (r.Count > 1) { attributeList.AddRange((List>)DataMap[r[1]]); } result = attributeList; break; case ProductionIndex.Attributes2: // ::= result = new List>(); break; case ProductionIndex.Attribute_Id_Eq_Stringliteral: // ::= ID '=' StringLiteral result = new KeyValuePair((string)DataMap[r[0]], GetStringLiteral((string)DataMap[r[2]])); break; case ProductionIndex.Attribute_Id_Eq_Id: // ::= ID '=' ID result = new KeyValuePair((string)DataMap[r[0]], (string)DataMap[r[2]]); break; case ProductionIndex.Text: // ::= break; case ProductionIndex.Text2: // ::= break; case ProductionIndex.Word_Id: // ::= ID break; case ProductionIndex.Word_Eq: // ::= '=' break; case ProductionIndex.Word_Charname: // ::= CharName break; case ProductionIndex.Word_Charnumber: // ::= CharNumber break; } //switch DataMap[r] = result; return result; } private string GetStringLiteral(string input) { if (input.StartsWith("\"")) { input = input.Substring(1); } if (input.EndsWith("\"")) { input = input.Substring(0, input.Length - 1); } return input; } }; //MyParser }