public void Parse(string code) { this.Structures = new List<LogicStructure>(); this.Functions = new List<LogicFunction>(); var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Protogame.Scripting.LogicControl.Grammar.egt"); var parser = new Parser(); if (stream != null) { using (var reader = new BinaryReader(stream)) { parser.LoadTables(reader); } } else { throw new InvalidOperationException(); } parser.Open(ref code); Reduction root = null; var done = false; while (!done) { var response = parser.Parse(); switch (response) { case GOLD.ParseMessage.LexicalError: throw new Exception("Lexical Error:\n" + "Position: " + parser.CurrentPosition().Line + ", " + parser.CurrentPosition().Column + "\n" + "Read: " + parser.CurrentToken().Data); case GOLD.ParseMessage.SyntaxError: throw new Exception("Syntax Error:\n" + "Position: " + parser.CurrentPosition().Line + ", " + parser.CurrentPosition().Column + "\n" + "Read: " + parser.CurrentToken().Data + "\n" + "Expecting: " + parser.ExpectedSymbols().Text()); case GOLD.ParseMessage.Accept: root = (GOLD.Reduction)parser.CurrentReduction; done = true; break; case GOLD.ParseMessage.InternalError: throw new Exception("Internal error"); case GOLD.ParseMessage.NotLoadedError: throw new Exception("Tables not loaded"); case GOLD.ParseMessage.GroupError: throw new Exception("Runaway group"); } } this.TraverseNodes(root); }
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. GOLD.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 GOLD.ParseMessage.LexicalError: //Cannot recognize token done = true; break; case GOLD.ParseMessage.SyntaxError: //Expecting a different token done = true; break; case GOLD.ParseMessage.Reduction: //Create a customized object to store the reduction parser.CurrentReduction = CreateNewObject(parser.CurrentReduction as GOLD.Reduction); break; case GOLD.ParseMessage.Accept: //Accepted! //program = parser.CurrentReduction //The root node! done = true; accepted = true; break; case GOLD.ParseMessage.TokenRead: //You don't have to do anything here. break; case GOLD.ParseMessage.InternalError: //INTERNAL ERROR! Something is horribly wrong. done = true; break; case GOLD.ParseMessage.NotLoadedError: //This error occurs if the CGT was not loaded. done = true; break; case GOLD.ParseMessage.GroupError: //GROUP ERROR! Unexpected end of file done = true; break; } } //while return(accepted); }
//public bool Parse(TextReader reader) public bool Parse(string path) { parser.Restart(); //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. GOLD.ParseMessage response; bool done; //Controls when we leave the loop bool accepted = false; //Was the parse successful? FailMessage = null; using (TextReader reader = File.OpenText(path)) { parser.Open(reader); parser.TrimReductions = true; //Please read about this feature before enabling done = false; while (!done) { response = parser.Parse(); switch (response) { case GOLD.ParseMessage.LexicalError: //Cannot recognize token FailMessage = "Lexical Error in " + path + ":\n" + "Position: Ln " + parser.CurrentPosition().Line + ", Col " + parser.CurrentPosition().Column + "\n" + "Read: " + parser.CurrentToken().Data; done = true; break; case GOLD.ParseMessage.SyntaxError: //Expecting a different token FailMessage = "Syntax Error in " + path + ":\n" + "Position: Ln " + parser.CurrentPosition().Line + ", Col " + parser.CurrentPosition().Column + "\n" + "Read: " + parser.CurrentToken().Data + "\n" + "Expecting: " + parser.ExpectedSymbols().Text(); done = true; break; case GOLD.ParseMessage.Reduction: //For this project, we will let the parser build a tree of Reduction objects //parser.CurrentReduction = CreateNewObject(parser.CurrentReduction); break; case GOLD.ParseMessage.Accept: //Accepted! Root = (GOLD.Reduction)parser.CurrentReduction; //The root node! done = true; accepted = true; break; case GOLD.ParseMessage.TokenRead: //You don't have to do anything here. break; case GOLD.ParseMessage.InternalError: //INTERNAL ERROR! Something is horribly wrong. done = true; break; case GOLD.ParseMessage.NotLoadedError: //This error occurs if the CGT was not loaded. FailMessage = "Tables not loaded"; done = true; break; case GOLD.ParseMessage.GroupError: //GROUP ERROR! Unexpected end of file FailMessage = "Runaway group"; done = true; break; } } //while } return(accepted); }