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); } }
private void Fail(GOLD.ParseMessage msg) { switch (msg) { case GOLD.ParseMessage.LexicalError: //Cannot recognize token throw new LexicalErrorException(parser.CurrentToken().Data, new SourceCodePosition(parser)); case GOLD.ParseMessage.SyntaxError: //Expecting a different token throw new SyntaxErrorException(parser.CurrentToken().Data, parser.ExpectedSymbols().Text(), new SourceCodePosition(parser)); case GOLD.ParseMessage.InternalError: //INTERNAL ERROR! Something is horribly wrong. throw new InternalErrorException(new SourceCodePosition(parser)); case GOLD.ParseMessage.NotLoadedError: //This error occurs if the CGT was not loaded. throw new NotLoadedErrorException(new SourceCodePosition(parser)); case GOLD.ParseMessage.GroupError: //GROUP ERROR! Unexpected end of file throw new GroupErrorException(new SourceCodePosition(parser)); } }
// Encapsulation of call to GOLD.Parser.Parse() and analysing callbacks from production building 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. bool done = false; //Controls when we leave the loop bool accepted = false; //Was the parse successful? m_coreParser.Open(reader); m_coreParser.TrimReductions = false; //Please read about this feature before enabling while (!done) { GOLD.ParseMessage response = m_coreParser.Parse(); switch (response) { case GOLD.ParseMessage.LexicalError: //Cannot recognize token OnLexicalError(); done = true; break; case GOLD.ParseMessage.SyntaxError: //Expecting a different token OnSyntaxError(); done = true; break; case GOLD.ParseMessage.Reduction: OnReduction(); break; case GOLD.ParseMessage.Accept: //Accepted! OnAccept(); 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. OnInternalError(); done = true; break; case GOLD.ParseMessage.NotLoadedError: //This error occurs if the CGT was not loaded. OnNotLoadedError(); done = true; break; case GOLD.ParseMessage.GroupError: //GROUP ERROR! Unexpected end of file OnGroupError(); done = true; break; } } // End of while return(accepted); } // End of method Parse()