Exemplo n.º 1
0
            public override object VisitIfElseBody([NotNull] LGTemplateParser.IfElseBodyContext context)
            {
                var ifRules = context.ifElseTemplateBody().ifConditionRule();

                for (var idx = 0; idx < ifRules.Length; idx++)
                {
                    var conditionNode = ifRules[idx].ifCondition();
                    var ifExpr        = conditionNode.IF() != null;
                    var elseIfExpr    = conditionNode.ELSEIF() != null;
                    var elseExpr      = conditionNode.ELSE() != null;

                    var node = ifExpr ? conditionNode.IF() :
                               elseIfExpr?conditionNode.ELSEIF() :
                                   conditionNode.ELSE();

                    if (node.GetText().Count(u => u == ' ') > 1 ||
                        (idx > 0 && ifExpr) ||
                        (idx > 0 && idx < ifRules.Length - 1 && !elseIfExpr))
                    {
                        return(null);
                    }

                    if (!elseExpr && (ifRules[idx].ifCondition().expression().Length == 1))
                    {
                        FillInExpression(conditionNode.expression(0));
                    }

                    if (ifRules[idx].normalTemplateBody() != null)
                    {
                        Visit(ifRules[idx].normalTemplateBody());
                    }
                }

                return(null);
            }
Exemplo n.º 2
0
        public override object VisitIfElseBody([NotNull] LGTemplateParser.IfElseBodyContext context)
        {
            var ifRules = context.ifElseTemplateBody().ifConditionRule();

            foreach (var ifRule in ifRules)
            {
                if (EvalCondition(ifRule.ifCondition()) && ifRule.normalTemplateBody() != null)
                {
                    return(Visit(ifRule.normalTemplateBody()));
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        public override AnalyzerResult VisitIfElseBody([NotNull] LGTemplateParser.IfElseBodyContext context)
        {
            var result = new AnalyzerResult();

            var ifRules = context.ifElseTemplateBody().ifConditionRule();
            foreach (var ifRule in ifRules)
            {
                var expression = ifRule.ifCondition().EXPRESSION(0);
                if (expression != null)
                {
                    result.Union(AnalyzeExpression(expression.GetText()));
                }

                if (ifRule.normalTemplateBody() != null)
                {
                    result.Union(Visit(ifRule.normalTemplateBody()));
                }
            }

            return result;
        }
Exemplo n.º 4
0
        public override List <Diagnostic> VisitIfElseBody([NotNull] LGTemplateParser.IfElseBodyContext context)
        {
            var result = new List <Diagnostic>();

            var ifRules = context.ifElseTemplateBody().ifConditionRule();

            for (var idx = 0; idx < ifRules.Length; idx++)
            {
                var conditionNode = ifRules[idx].ifCondition();
                var ifExpr        = conditionNode.IF() != null;
                var elseIfExpr    = conditionNode.ELSEIF() != null;
                var elseExpr      = conditionNode.ELSE() != null;

                var node = ifExpr ? conditionNode.IF() :
                           elseIfExpr?conditionNode.ELSEIF() :
                               conditionNode.ELSE();

                if (node.GetText().Count(u => u == ' ') > 1)
                {
                    result.Add(BuildLGDiagnostic(TemplateErrors.InvalidWhitespaceInCondition, context: conditionNode));
                }

                if (idx == 0 && !ifExpr)
                {
                    result.Add(BuildLGDiagnostic(TemplateErrors.NotStartWithIfInCondition, DiagnosticSeverity.Warning, conditionNode));
                }

                if (idx > 0 && ifExpr)
                {
                    result.Add(BuildLGDiagnostic(TemplateErrors.MultipleIfInCondition, context: conditionNode));
                }

                if (idx == ifRules.Length - 1 && !elseExpr)
                {
                    result.Add(BuildLGDiagnostic(TemplateErrors.NotEndWithElseInCondition, DiagnosticSeverity.Warning, conditionNode));
                }

                if (idx > 0 && idx < ifRules.Length - 1 && !elseIfExpr)
                {
                    result.Add(BuildLGDiagnostic(TemplateErrors.InvalidMiddleInCondition, context: conditionNode));
                }

                // check rule should should with one and only expression
                if (!elseExpr)
                {
                    if (ifRules[idx].ifCondition().expression().Length != 1)
                    {
                        result.Add(BuildLGDiagnostic(TemplateErrors.InvalidExpressionInCondition, context: conditionNode));
                    }
                    else
                    {
                        var errorPrefix = "Condition '" + conditionNode.expression(0).GetText() + "': ";
                        result.AddRange(CheckExpression(conditionNode.expression(0), errorPrefix));
                    }
                }
                else
                {
                    if (ifRules[idx].ifCondition().expression().Length != 0)
                    {
                        result.Add(BuildLGDiagnostic(TemplateErrors.ExtraExpressionInCondition, context: conditionNode));
                    }
                }

                if (ifRules[idx].normalTemplateBody() != null)
                {
                    result.AddRange(Visit(ifRules[idx].normalTemplateBody()));
                }
                else
                {
                    result.Add(BuildLGDiagnostic(TemplateErrors.MissingTemplateBodyInCondition, context: conditionNode));
                }
            }

            return(result);
        }