public object Parse(string source) { NodesStack = new Stack(); LRParser = new Parser(new StringReader(source), LanguageGrammar); parsertools.parser = LRParser; LRParser.TrimReductions = false; 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: prev_node = LRParser.TokenSyntaxNode; Error er = new UnexpectedToken(this,parsertools.symbol_collection_to_string(this.LRParser.GetExpectedTokens())); 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("PascalABCParser", new Exception("ParseMessage.InternalError"))); return null; case ParseMessage.NotLoadedError: errors.Add(new CompilerInternalError("PascalABCParser", 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;*/ } } }
public bool Parse(StringReader sourceReader) { m_parser = ParserFactory.CreateParser(sourceReader); m_parser.TrimReductions = true; m_context = new ParserContext(m_parser); while (true) { switch (m_parser.Parse()) { case ParseMessage.LexicalError: m_errorString = string.Format("Lexical Error. Line {0}. Token {1} was not expected.", m_parser.LineNumber, m_parser.TokenText); return(false); case ParseMessage.SyntaxError: StringBuilder text = new StringBuilder(); foreach (Symbol tokenSymbol in m_parser.GetExpectedTokens()) { text.Append(' '); text.Append(tokenSymbol.ToString()); } m_errorString = string.Format("Syntax Error. Line {0}. Expecting: {1}.", m_parser.LineNumber, text.ToString()); return(false); case ParseMessage.Reduction: m_parser.TokenSyntaxNode = m_context.CreateASTNode(); break; case ParseMessage.Accept: m_AST = m_parser.TokenSyntaxNode as ASTNode; m_errorString = null; return(true); case ParseMessage.InternalError: m_errorString = "Internal Error. Something is horribly wrong."; return(false); case ParseMessage.NotLoadedError: m_errorString = "Grammar Table is not loaded."; return(false); case ParseMessage.CommentError: m_errorString = "Comment Error. Unexpected end of input."; return(false); case ParseMessage.CommentBlockRead: case ParseMessage.CommentLineRead: // don't do anything break; } } }
/// <summary> /// Runs a custom "C++ Header" grammar on a preprocessed and transformed file to produce /// a tree of source elements in memory. /// </summary> /// <param name="file">The preprocessed source file.</param> public NonTerminal Parse(string file) { using (var stream = new StreamReader(file)) { var parser = new Parser(stream, grammar); parser.TrimReductions = true; while (true) { var result = parser.Parse(); switch (result) { case ParseMessage.InternalError: throw new InvalidOperationException("Internal error in parser"); case ParseMessage.LexicalError: throw new InvalidOperationException(string.Format("Lexical error: (line:{0}) {1}", parser.TokenLineNumber, parser.TokenText)); case ParseMessage.SyntaxError: throw new InvalidOperationException(string.Format("Syntax error: (line:{0}) {1}\n -- Expected: {2}", parser.LineNumber, parser.LineText, string.Join<Symbol>(", ", parser.GetExpectedTokens()))); case ParseMessage.TokenRead: var terminal = new Terminal(parser.TokenSymbol, parser.TokenText); parser.TokenSyntaxNode = terminal; break; case ParseMessage.Reduction: var nonTerminal = new NonTerminal(parser.ReductionRule); parser.TokenSyntaxNode = nonTerminal; for (int i = 0; i < parser.ReductionCount; i++) nonTerminal.Add(parser.GetReductionSyntaxNode(i)); break; case ParseMessage.Accept: return parser.TokenSyntaxNode as NonTerminal; } } } }
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 bool Parse(TextReader sourceReader) { Debug.Assert(sourceReader != null); m_parser = ParserFactory.CreateParser(sourceReader); m_parser.TrimReductions = true; while (true) { switch (m_parser.Parse()) { case ParseMessage.LexicalError: ErrorString = string.Format("Lexical Error. Line {0}. Token {1} was not expected.", m_parser.LineNumber, m_parser.TokenText); return false; case ParseMessage.SyntaxError: StringBuilder text = new StringBuilder(); foreach (Symbol tokenSymbol in m_parser.GetExpectedTokens()) text.AppendFormat(" {0}", tokenSymbol); ErrorString = string.Format("Syntax Error. Line {0}. Expecting: {1}.", m_parser.LineNumber, text); return false; case ParseMessage.Reduction: NonTerminalNode nonTerminal = SyntaxRuleFactory.CreateNode(m_parser.ReductionRule); m_parser.TokenSyntaxNode = nonTerminal; for (int i = 0; i < m_parser.ReductionCount; i++) nonTerminal.AppendChildNode(m_parser.GetReductionSyntaxNode(i) as SyntaxNode); // post parsing syntax check (used to do things like segregate the difference between HAVING and WHERE // expressions in terms of the validtity of aggregate expressions) nonTerminal.CheckSyntax(); break; case ParseMessage.TokenRead: m_parser.TokenSyntaxNode = new TerminalNode(m_parser); break; case ParseMessage.Accept: SyntaxTree = m_parser.TokenSyntaxNode as NonTerminalNode; ErrorString = null; return true; case ParseMessage.InternalError: ErrorString = "Internal Error. Something is horribly wrong."; return false; case ParseMessage.NotLoadedError: ErrorString = "Grammar Table is not loaded."; return false; case ParseMessage.CommentError: ErrorString = "Comment Error. Unexpected end of input."; return false; case ParseMessage.CommentBlockRead: case ParseMessage.CommentLineRead: // don't do anything break; } } }
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; } }
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(StringReader sourceReader, out Document document) { _parser = ParserFactory.CreateParser(sourceReader); _parser.TrimReductions = true; _context = new ParserContext(_parser); document = null; while (true) { switch (_parser.Parse()) { case ParseMessage.LexicalError: _errorString = string.Format("Lexical Error. Line {0}. Token {1} was not expected.", _parser.LineNumber, _parser.TokenText); return false; case ParseMessage.SyntaxError: StringBuilder text = new StringBuilder(); foreach (Symbol tokenSymbol in _parser.GetExpectedTokens()) { text.Append(' '); text.Append(tokenSymbol.ToString()); } _errorString = string.Format("Syntax Error. Line {0}. Expecting: {1}.", _parser.LineNumber, text); return false; case ParseMessage.Reduction: _parser.TokenSyntaxNode = _context.CreateASTNode(); break; case ParseMessage.Accept: var result = _parser.TokenSyntaxNode as BlockNodeList; if (result == null) { result = new BlockNodeList(_parser.TokenSyntaxNode as BlockNode); } document = new Document { Children = result }; _errorString = null; return true; case ParseMessage.TokenRead: //=== Make sure that we store token string for needed tokens. _parser.TokenSyntaxNode = _context.GetTokenText(); break; case ParseMessage.InternalError: _errorString = "Internal Error. Something is horribly wrong."; return false; case ParseMessage.NotLoadedError: _errorString = "Grammar Table is not loaded."; return false; case ParseMessage.CommentError: _errorString = "Comment Error. Unexpected end of input."; return false; case ParseMessage.CommentBlockRead: case ParseMessage.CommentLineRead: // don't do anything break; } } }
private ComplexSyntaxNode ParseImpl(TextReader xpidlTextReader) { var goldParser = new GoldParser.Parser(xpidlTextReader, m_Grammar) { TrimReductions = true, IgnoreNestedComments = true }; goldParser.AddCommentSymbols( new Regex(@"^\/\*$", RegexOptions.Singleline), // /* new Regex(@"^\*\/$", RegexOptions.Singleline)); // */ goldParser.AddCommentSymbols( new Regex(@"^\%\{\s*C\+\+$", RegexOptions.Singleline), // %{ C++ new Regex(@"^\%\}(\s*C\+\+)?$", RegexOptions.Singleline)); // %} C++ var rootSyntaxNode = new ComplexSyntaxNode(null); while (true) { ParseMessage parseMessage = goldParser.Parse(); switch (parseMessage) { case ParseMessage.Empty: break; // Comment or inline C header case ParseMessage.CommentLineRead: case ParseMessage.CommentBlockRead: var commentSyntaxNode = new CommentSyntaxNode(goldParser.CommentText); if (goldParser.TokenSyntaxNode == null) { rootSyntaxNode.AddChildNode(commentSyntaxNode); } else { ((SyntaxNode)goldParser.TokenSyntaxNode).AttachCommentNode(commentSyntaxNode); } break; // Read valid token case ParseMessage.TokenRead: var simpleSyntaxNode = new SimpleSyntaxNode(goldParser.TokenSymbol, goldParser.TokenText); goldParser.TokenSyntaxNode = simpleSyntaxNode; break; // Can create new xpidl-node case ParseMessage.Reduction: var complexSyntaxNode = new ComplexSyntaxNode(goldParser.ReductionRule); for (Int32 i = 0; i < goldParser.ReductionCount; ++i) { var syntaxNode = (SyntaxNode)goldParser.GetReductionSyntaxNode(i); complexSyntaxNode.AddChildNode(syntaxNode); if (i == (goldParser.ReductionCount - 1)) { complexSyntaxNode.ReattachCommentNodes(syntaxNode); } else { syntaxNode.DetachCommentNodes(complexSyntaxNode); } } goldParser.TokenSyntaxNode = complexSyntaxNode; break; // Parsing successfully completed case ParseMessage.Accept: var acceptedSyntaxNode = (SyntaxNode)goldParser.TokenSyntaxNode; Debug.Assert(acceptedSyntaxNode != null); rootSyntaxNode.AddChildNode(acceptedSyntaxNode); acceptedSyntaxNode.DetachCommentNodes(rootSyntaxNode); return rootSyntaxNode; // Grammar table is not loaded case ParseMessage.NotLoadedError: throw new XpidlParserException("Grammar not loaded"); // Unexpected end of input case ParseMessage.CommentError: throw new XpidlParserException("Comment error"); // Invalid token case ParseMessage.LexicalError: throw new XpidlParserSyntaxException("Can not recognize token", goldParser.TokenText, goldParser.LineNumber, goldParser.LinePosition); // Unexpected token case ParseMessage.SyntaxError: throw new XpidlParserSyntaxException("Unexpected token", goldParser.TokenText, goldParser.LineNumber, goldParser.LinePosition); // Fatal internal error case ParseMessage.InternalError: throw new XpidlParserException("Internal parser error"); } } }