コード例 #1
0
        public override List <string> VisitIfElseBody([NotNull] LGFileParser.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);
        }
コード例 #2
0
        public override AnalyzerResult VisitIfElseBody([NotNull] LGFileParser.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);
        }
コード例 #3
0
        public override List <Diagnostic> VisitIfElseBody([NotNull] LGFileParser.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(LGErrors.InvalidWhitespaceInCondition, context: conditionNode));
                }

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

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

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

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

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

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

            return(result);
        }
コード例 #4
0
            public override List <Diagnostic> VisitIfElseBody([NotNull] LGFileParser.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($"At most 1 whitespace is allowed between IF/ELSEIF/ELSE and :", context: conditionNode));
                    }

                    if (idx == 0 && !ifExpr)
                    {
                        result.Add(BuildLGDiagnostic($"condition is not start with if", DiagnosticSeverity.Warning, conditionNode));
                    }

                    if (idx > 0 && ifExpr)
                    {
                        result.Add(BuildLGDiagnostic($"condition can't have more than one if", context: conditionNode));
                    }

                    if (idx == ifRules.Length - 1 && !elseExpr)
                    {
                        result.Add(BuildLGDiagnostic($"condition is not end with else", DiagnosticSeverity.Warning, conditionNode));
                    }

                    if (idx > 0 && idx < ifRules.Length - 1 && !elseIfExpr)
                    {
                        result.Add(BuildLGDiagnostic($"only elseif is allowed in middle of condition", context: conditionNode));
                    }

                    // check rule should should with one and only expression
                    if (!elseExpr)
                    {
                        if (ifRules[idx].ifCondition().EXPRESSION().Length != 1)
                        {
                            result.Add(BuildLGDiagnostic($"if and elseif should followed by one valid expression", context: conditionNode));
                        }
                        else
                        {
                            result.AddRange(CheckExpression(ifRules[idx].ifCondition().EXPRESSION(0).GetText(), conditionNode));
                        }
                    }
                    else
                    {
                        if (ifRules[idx].ifCondition().EXPRESSION().Length != 0)
                        {
                            result.Add(BuildLGDiagnostic($"else should not followed by any expression", context: conditionNode));
                        }
                    }

                    if (ifRules[idx].normalTemplateBody() != null)
                    {
                        result.AddRange(Visit(ifRules[idx].normalTemplateBody()));
                    }
                    else
                    {
                        result.Add(BuildLGDiagnostic($"no normal template body in condition block", context: conditionNode));
                    }
                }

                return(result);
            }