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 FailMessage = "Lexical Error:\n" + "Position: " + parser.CurrentPosition().Line + ", " + parser.CurrentPosition().Column + "\n" + "Read: " + parser.CurrentToken().Data; done = true; break; case GOLD.ParseMessage.SyntaxError: //Expecting a different token FailMessage = "Syntax Error:\n" + "Position: " + parser.CurrentPosition().Line + ", " + 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); }
public bool Parse(TextReader reader) { 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 FailMessage = "Lexical Error:\n" + "Position: " + parser.CurrentPosition().Line + ", " + parser.CurrentPosition().Column + "\n" + "Read: " + parser.CurrentToken().Data; done = true; break; case GOLD.ParseMessage.SyntaxError: //Expecting a different token FailMessage = "Syntax Error:\n" + "Position: " + parser.CurrentPosition().Line + ", " + 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); }
public override bool Parse(string source, out IReduction root) { //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? root = null; StringReader reader = new StringReader(source); 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 failLineNumber = parser.CurrentPosition().Line; failColumnNumber = parser.CurrentPosition().Column; failLength = parser.CurrentToken().Data.ToString().Length; failMessage = "Lexical Error:" + Environment.NewLine + "Line " + (failLineNumber + 1) + ", Column " + (failColumnNumber + 1) + Environment.NewLine + "Read: " + parser.CurrentToken().Data; done = true; break; case GOLD.ParseMessage.SyntaxError: //Expecting a different token failLineNumber = parser.CurrentPosition().Line; failColumnNumber = parser.CurrentPosition().Column; failLength = parser.CurrentToken().Data.ToString().Length; failMessage = "Syntax Error:" + Environment.NewLine + "Line " + (failLineNumber + 1) + ", Column " + (failColumnNumber + 1) + Environment.NewLine + "Read: " + parser.CurrentToken().Data + Environment.NewLine + "Expecting one of: " + 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 = new CookV5Reduction((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. failLineNumber = 0; failColumnNumber = 0; failLength = 0; failMessage = "Tables not loaded"; done = true; break; case GOLD.ParseMessage.GroupError: //GROUP ERROR! Unexpected end of file failLineNumber = 0; failColumnNumber = 0; failLength = 0; failMessage = "COMMENT ERROR! Unexpected end of file"; done = true; break; } } //while return(accepted); }
} // End of method Parse() // Hooks to override if required to handle parsing events protected void OnLexicalError() { FailMessage = "Lexical Error:\n" + "Position: " + m_coreParser.CurrentPosition().Line + ", " + m_coreParser.CurrentPosition().Column + "\n" + "Read: " + m_coreParser.CurrentToken().Data; }
private object Parse(string txt) { lock (this) { object result = null; txt = txt.Replace("\r\n", "\n") + "\n"; parser.Open(ref txt); parser.TrimReductions = false; bool done = false; while (!done) { GOLD.ParseMessage response = parser.Parse(); switch (response) { case GOLD.ParseMessage.LexicalError: case GOLD.ParseMessage.SyntaxError: case GOLD.ParseMessage.InternalError: case GOLD.ParseMessage.NotLoadedError: case GOLD.ParseMessage.GroupError: Fail(response); break; case GOLD.ParseMessage.Reduction: // Reduction parser.CurrentReduction = GrammarTableMap.CreateNewASTObject(parser.CurrentReduction as GOLD.Reduction, parser.CurrentPosition()); break; case GOLD.ParseMessage.Accept: //Accepted! result = parser.CurrentReduction; done = true; break; case GOLD.ParseMessage.TokenRead: //You don't have to do anything here. break; } } return(result); } }
public SourceCodePosition(GOLD.Parser p) { this.Line = p.CurrentPosition().Line + 1; this.Column = p.CurrentPosition().Column; }