Example #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);
            }
Example #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);
        }
Example #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;
        }
Example #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);
        }
 /// <summary>
 /// Exit a parse tree produced by the <c>ifElseBody</c>
 /// labeled alternative in <see cref="LGTemplateParser.body"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitIfElseBody([NotNull] LGTemplateParser.IfElseBodyContext context)
 {
 }
 /// <summary>
 /// Visit a parse tree produced by the <c>ifElseBody</c>
 /// labeled alternative in <see cref="LGTemplateParser.body"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitIfElseBody([NotNull] LGTemplateParser.IfElseBodyContext context)
 {
     return(VisitChildren(context));
 }