public object Parse(string source) { LRParser = new Parser(new StringReader(source), LanguageGrammar); parsertools.parser = LRParser; LRParser.TrimReductions = true; try { while (true) { switch (LRParser.Parse()) { case ParseMessage.LexicalError: errors.Add(new TokenReadError(this)); LRParser.PopInputToken(); if (errors.Count > max_errors) return null; break; case ParseMessage.SyntaxError: if ((LRParser.TokenSyntaxNode as syntax_tree_node) != null) prev_node = LRParser.TokenSyntaxNode; Error er = new PABCNETUnexpectedToken(this); Symbol sym = LRParser.PopInputToken(); if (sym.SymbolType == SymbolType.End && errors.Count > 0) return null; errors.Add(er); if (errors.Count > max_errors) return null; break; case ParseMessage.Reduction: LRParser.TokenSyntaxNode = CreateNonTerminalObject(); break; case ParseMessage.Accept: return LRParser.TokenSyntaxNode; case ParseMessage.TokenRead: LRParser.TokenSyntaxNode = CreateTerminalObject(); break; case ParseMessage.InternalError: errors.Add(new CompilerInternalError("PABCPreprocessor2", new Exception("ParseMessage.InternalError"))); return null; case ParseMessage.NotLoadedError: errors.Add(new CompilerInternalError("PABCPreprocessor2", new Exception("ParseMessage.NotLoadedError"))); return null; case ParseMessage.CommentError: errors.Add(new UnexpectedToken(this, "(EOF)")); return null; case ParseMessage.CommentBlockRead: { SourceContext sc1 = parsertools.GetTokenSourceContext(this.LRParser); string comment = this.LRParser.CommentText; SourceContext sc2 = new SourceContext(sc1, parsertools.GetTokenSourceContext(this.LRParser)); if (comment[0] == '{' && comment[1] == '$') prepr.Errors.Add(new SyntaxErrorInDirective(prepr.CurrentFileName, sc2, comment)); //if (prepr.sm.AllowWrite()) //prepr.WriteToThread(sc2, comment.Replace(Environment.NewLine, " ")); //prepr.WriteToStream(sc2, comment.Replace("\r\n", " ")); } break; case ParseMessage.CommentLineRead: { SourceContext sc1 = parsertools.GetTokenSourceContext(this.LRParser); string comment = this.LRParser.CommentText; SourceContext sc2 = new SourceContext(sc1, sc1); if (prepr.sm.AllowWrite()) prepr.WriteToStream(sc1, comment.Replace("\r\n", " ")); //prepr.WriteToStream(sc1, comment); } break; } } } catch (GoldParser.UnexpectedEOFinParseCommentBlock) { throw new TokenReadError(this); } catch (Exception e) { if (errors.Count > 0) return null; else throw; } }
public object Parse(string source) { if_part.Clear(); first_if.Clear(); LRParser = new Parser(new StringReader(source), LanguageGrammar); parsertools.parser = LRParser; LRParser.TrimReductions = true; try { while (true) { switch (LRParser.Parse()) { case ParseMessage.LexicalError: errors.Add(new TokenReadError(this)); LRParser.PopInputToken(); if (errors.Count > max_errors) return null; break; case ParseMessage.SyntaxError: if ((LRParser.TokenSyntaxNode as syntax_tree_node)!= null) prev_node = LRParser.TokenSyntaxNode; Error er = new PABCNETUnexpectedToken(this); Symbol sym = LRParser.PopInputToken(); if (sym.SymbolType == SymbolType.End && errors.Count > 0) return null; errors.Add(er); if (errors.Count > max_errors) return null; break; case ParseMessage.Reduction: LRParser.TokenSyntaxNode = CreateNonTerminalObject(); break; case ParseMessage.Accept: return LRParser.TokenSyntaxNode; case ParseMessage.TokenRead: LRParser.TokenSyntaxNode = CreateTerminalObject(); break; case ParseMessage.InternalError: errors.Add(new CompilerInternalError("CParser", new Exception("ParseMessage.InternalError"))); return null; case ParseMessage.NotLoadedError: errors.Add(new CompilerInternalError("CParser", new Exception("ParseMessage.NotLoadedError"))); return null; case ParseMessage.CommentError: errors.Add(new UnexpectedToken(this,"(EOF)")); return null; /*case ParseMessage.CommentBlockRead: break; case ParseMessage.CommentLineRead: break;*/ } } } catch (GoldParser.UnexpectedEOFinParseCommentBlock) { throw new TokenReadError(this); } catch (Exception e) { if (errors.Count > 0) return null; else throw; } }