public TerminalNode(GoldParser.Parser theParser) { m_symbol = theParser.TokenSymbol; m_text = theParser.TokenSymbol.ToString(); m_lineNumber = theParser.LineNumber; m_linePosition = theParser.LinePosition; }
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 override SourceContext GetTokenSourceContext(Parser parser) { int LineNum = parser.TokenLineNumber + LineCorrection; SourceContext sc = new SourceContext(LineNum, parser.TokenLinePosition, LineNum + GetLinesCount(parser.TokenText), parser.TokenLinePosition + parser.TokenLength - 1, parser.TokenCharPosition, parser.TokenCharPosition + parser.TokenLength); if (scm != null) sc = scm.GetSourceContext(sc); return sc; }
internal TerminalNode(Parser parser) { Debug.Assert(parser != null); Symbol = parser.TokenSymbol; Text = parser.TokenText; LineNumber = parser.LineNumber; LinePosition = parser.LinePosition; }
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; } } }
public GlifInterpreter(string filename) { var streamReader = new StreamReader(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\" + filename); _fileText = streamReader.ReadToEnd(); streamReader.Close(); var currentAssembly = Assembly.GetExecutingAssembly(); var stream = currentAssembly.GetManifestResourceStream("SP2.Glif.Interpreter.res.glifgrammar.cgt"); if (stream != null) { var binaryReader = new BinaryReader(stream); _grammar = new Grammar(binaryReader); } else { throw new FileNotFoundException("Grammar file not found!"); } _parser = new Parser(new StringReader(_fileText), _grammar) {TrimReductions = true}; _context = new GlifContext(_parser); }
/// <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 GPBParser(Stream stream) { LRParser = null; LanguageGrammar = new GoldParser.Grammar(new BinaryReader(stream)); }
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 NonTerminalNode(Parser theParser) { m_rule = theParser.ReductionRule; }
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; } }
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"); } } }
// constructor public ParserContext(GoldParser.Parser prser) { m_parser = prser; }
public NonTerminalNode(GoldParser.Parser theParser) { m_rule = theParser.ReductionRule; }
public GPBParser(GoldParser.Grammar grammar) { LRParser = null; LanguageGrammar = grammar; }
public GPBParser() { LRParser = null; LanguageGrammar = null; }
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; } } }
public Tokenizer(ShaderLanguageData languageData) { GoldParser = new GoldParser.Parser(grammar); this.languageData = languageData; }