示例#1
0
        public override GrammarParseResult VisitStatement(StatementContext context)
        {
            if (context == null)
            {
                return(GrammarParseResult.Unsuccessful(context.GetText()));
            }

            Debug.WriteLine($"VisitStatement \"{context.GetText()}\"");

            string labelStr = context.label()?.STRING()?.GetText();

            //Successful if any of the 3 possible productions are successful
            var expression = context.expression();

            if (expression != null)
            {
                GrammarParseResult result = _expressionVisitor.VisitExpression(expression);
                if (result.IsSuccessful)
                {
                    result.Label = labelStr;
                    return(result);
                }
            }

            var command = context.command();

            if (command != null)
            {
                GrammarParseResult result = VisitCommand(command);
                if (result.IsSuccessful)
                {
                    result.Label = labelStr;
                    return(result);
                }
            }

            var action = context.action();

            if (action != null)
            {
                GrammarParseResult result = VisitAction(action);
                if (result.IsSuccessful)
                {
                    result.Label = labelStr;
                    return(result);
                }
            }

            return(GrammarParseResult.Unsuccessful(context.GetText()));
        }
示例#2
0
        /// <summary>
        /// Handling an Array Inizializer block
        /// I'm unsure why this is not an ExpressionCOntext, but whatever
        /// </summary>
        /// <param name="inCodeBlock"></param>
        /// <param name="inBlockStatement"></param>
        private void HandleBlockStatementStatement(CodeBlock inParentCodeBlock, StatementContext inStatement)
        {
            if (inStatement.expression().Length > 1)
            {
                throw new NotImplementedException("Not done yet");
            }
            var tmpStatement = new StatementCode();

            if (inStatement.IF() != null)
            {
                if (inStatement.statement().Length > 2)
                {
                    throw new NotImplementedException("statement.statement length bigger than 1");
                }
                tmpStatement.StatementType       = StatementTypeEnum.If;
                tmpStatement.StatementCodeBlocks = new List <CodeBlock>()
                {
                    new CodeBlock()
                };
                HandleExpressionContext(tmpStatement.StatementCodeBlocks[0], inStatement.parExpression().expression(), null);
                tmpStatement.InnerContent = new CodeBlock();
                HandleBlockStatementStatement(tmpStatement.InnerContent, inStatement.statement()[0]);

                if (inStatement.ELSE() != null)
                {
                    inParentCodeBlock.CodeEntries.Add(tmpStatement);
                    tmpStatement = new StatementCode();

                    tmpStatement.StatementType       = StatementTypeEnum.Else;
                    tmpStatement.StatementCodeBlocks = new List <CodeBlock>()
                    {
                        new CodeBlock()
                    };
                    HandleExpressionContext(tmpStatement.StatementCodeBlocks[0], inStatement.parExpression().expression(), null);
                    tmpStatement.InnerContent = new CodeBlock();
                    HandleBlockStatementStatement(tmpStatement.InnerContent, inStatement.statement().Last());
                }
            }
            else if (inStatement.ASSERT() != null)
            {
                tmpStatement.StatementType       = StatementTypeEnum.Assert;
                tmpStatement.StatementCodeBlocks = new List <CodeBlock>()
                {
                };
                foreach (var tmpStatementExpression in inStatement.expression())
                {
                    var tmpBlock = new CodeBlock();
                    HandleExpressionContext(tmpBlock, tmpStatementExpression);
                    tmpStatement.StatementCodeBlocks.Add(tmpBlock);
                }
            }
            else if (inStatement.IDENTIFIER() != null)
            {
                throw new NotImplementedException("Not done yet");
            }
            else if (inStatement.RETURN() != null)
            {
                var tmpReturnCodeEntry = new ReturnCodeEntry();
                HandleExpressionContext(tmpReturnCodeEntry, inStatement.expression()[0], null);
                inParentCodeBlock.CodeEntries.Add(tmpReturnCodeEntry);
                return;
            }
            else if (inStatement.THROW() != null)
            {
                tmpStatement.StatementType       = StatementTypeEnum.Throw;
                tmpStatement.StatementCodeBlocks = new List <CodeBlock>()
                {
                };
                var tmpBlock = new CodeBlock();
                HandleExpressionContext(tmpBlock, inStatement.expression()[0]);
                tmpStatement.StatementCodeBlocks.Add(tmpBlock);
                return;
            }
            else if (inStatement.SEMI() != null)
            {
                //Semicolon, so it is a simple Statement
                //tmpStatement.StatementCodeBlocks = new List<CodeBlock>() { new CodeBlock() };
                if (inStatement.expression()[0].GetText() + ";" != inStatement.GetText())
                {
                    throw new NotImplementedException("Unhandlet Statement in Semi");
                }

                HandleExpressionContext(inParentCodeBlock, inStatement.expression()[0], null);
                return;
            }
            else if (inStatement.block() != null)
            {
                foreach (var tmpCode in inStatement.block().blockStatement())
                {
                    HandloBlockStatementContent(inParentCodeBlock, tmpCode);
                }
                return;
            }
            else if (inStatement.forControl() != null)
            {
                var tmpForControl = inStatement.forControl();
                tmpStatement.StatementType       = StatementTypeEnum.For;
                tmpStatement.StatementCodeBlocks = new List <CodeBlock>()
                {
                    new CodeBlock(), new CodeBlock(), new CodeBlock()
                };
                tmpStatement.InnerContent = new CodeBlock();
                HandleBlockStatementStatement(tmpStatement.InnerContent, inStatement.statement()[0]);

                if (tmpForControl.forInit() != null)
                {
                    if (tmpForControl.forInit().localVariableDeclaration() != null)
                    {
                        HandleLocalVariableDeclarationContext(tmpStatement.StatementCodeBlocks[0], tmpForControl.forInit().localVariableDeclaration());
                    }
                    if (tmpForControl.forInit().expressionList() != null)
                    {
                        foreach (var tmpExpr in tmpForControl.forInit().expressionList().expression())
                        {
                            HandleExpressionContext(tmpStatement.StatementCodeBlocks[0], tmpExpr);
                        }
                    }
                }
                if (tmpForControl.expression() != null)
                {
                    HandleExpressionContext(tmpStatement.StatementCodeBlocks[1], tmpForControl.expression());
                }
                if (tmpForControl.expressionList() != null)
                {
                    foreach (var tmpExpr in tmpForControl.expressionList().expression())
                    {
                        HandleExpressionContext(tmpStatement.StatementCodeBlocks[2], tmpExpr);
                    }
                }
                if (tmpForControl.enhancedForControl() != null)
                {
                    throw new NotImplementedException("for Inline Variable Declaration missing yet");
                    HandleExpressionContext(tmpStatement.StatementCodeBlocks[1], tmpForControl.expression());
                }
            }
            else if (inStatement.WHILE() != null)
            {
                if (inStatement.statement().Length > 2)
                {
                    throw new NotImplementedException("statement.statement length bigger than 1");
                }
                tmpStatement.StatementType       = StatementTypeEnum.While;
                tmpStatement.StatementCodeBlocks = new List <CodeBlock>()
                {
                    new CodeBlock()
                };
                HandleExpressionContext(tmpStatement.StatementCodeBlocks[0], inStatement.parExpression().expression(), null);
                tmpStatement.InnerContent = new CodeBlock();
                HandleBlockStatementStatement(tmpStatement.InnerContent, inStatement.statement()[0]);
            }
            else
            {
                throw new NotImplementedException("Not done yet");
            }

            if (inStatement.statement().Length > 0)
            {
                var tmpinnercount = (inStatement.ELSE() != null ? 1 : 0)
                                    + (inStatement.IF() != null ? 1 : 0)
                                    + (inStatement.FOR() != null ? 1 : 0)
                                    + (inStatement.WHILE() != null ? 1 : 0);

                if (inStatement.statement().Length != tmpinnercount)
                {
                    throw new NotImplementedException("Statement inner Statement length not matching");
                }
            }

            inParentCodeBlock.CodeEntries.Add(tmpStatement);
        }
示例#3
0
        void AddBracesAndLog(StatementContext context, string text)
        {
            var column       = context.Start.Column;
            var columnSpaces = GetTokenSpaces(column);

            rewriter.Replace(context.Start, context.Stop, $"{{\r\n{columnSpaces}\t" + context.GetText() + GetLog(column, text) + "}");
        }