Exemple #1
0
        public static PipelineNode GetPipeline(Parser theParser)
        {
            PipelineNode pipeline = null;

            if (theParser != null)
            {
                object objLeft = theParser.GetReductionSyntaxNode(0);
                object objRight = theParser.GetReductionSyntaxNode(2);

                if (objLeft is PipelineNode)
                {
                    pipeline = (PipelineNode)objLeft;
                    pipeline.AddItem(objRight as ASTNode);
                }
                else if (objRight is PipelineNode)
                {
                    pipeline = (PipelineNode)objRight;
                    pipeline.Insert(0, objLeft as ASTNode);
                }
            }

            if (pipeline == null)
            {
                pipeline = new PipelineNode(theParser);
                if (theParser != null)
                {
                    pipeline.AddItemFromParser(theParser, 0);
                    pipeline.AddItemFromParser(theParser, 2);
                }
            }

            return pipeline;
        }
Exemple #2
0
        public static ParamsListNode GetParamsList(Parser theParser)
        {
            ParamsListNode paramsList = null;

            if (theParser != null)
            {
                object objLeft = theParser.GetReductionSyntaxNode(0);
                object objRight = theParser.GetReductionSyntaxNode(1);

                if (objLeft is ParamsListNode)
                {
                    paramsList = (ParamsListNode)objLeft;
                    paramsList.AddParam(objRight);
                }
                else if (objRight is ParamsListNode)
                {
                    paramsList = (ParamsListNode)objRight;
                    paramsList.Insert(0, objLeft);
                }
            }

            if (paramsList == null)
            {
                paramsList = new ParamsListNode(theParser);

                if (theParser != null)
                {
                    paramsList.AddParamFromParser(theParser, 0);
                    paramsList.AddParamFromParser(theParser, 1);
                }
            }

            return paramsList;
        }
Exemple #3
0
        public static ParamsListNode GetParamsListFromRight(Parser theParser)
        {
            object objRight = theParser.GetReductionSyntaxNode(1);

            ParamsListNode paramsList = null;
            if (objRight is ParamsListNode)
            {
                paramsList = (ParamsListNode)objRight;
            }
            else
            {
                paramsList = new ParamsListNode(theParser);
                paramsList.AddParamFromParser(theParser, 1);
            }

            return paramsList;
        }
Exemple #4
0
        /// <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 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;
                }
            }
        }
		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");
				}
			}
		}