Exemplo n.º 1
0
        public override List <object> VisitSwitchCaseBody([NotNull] LGTemplateParser.SwitchCaseBodyContext context)
        {
            var switchCaseNodes   = context.switchCaseTemplateBody().switchCaseRule();
            var length            = switchCaseNodes.Length;
            var switchExprs       = switchCaseNodes[0].switchCaseStat().expression();
            var switchErrorPrefix = "Switch '" + switchExprs[0].GetText() + "': ";
            var switchExprResult  = EvalExpression(switchExprs[0].GetText(), switchExprs[0], switchCaseNodes[0].switchCaseStat().GetText(), switchErrorPrefix);
            var idx = 0;

            foreach (var switchCaseNode in switchCaseNodes)
            {
                if (idx == 0)
                {
                    idx++;
                    continue;   // skip the first node, which is switch statement
                }

                if (idx == length - 1 && switchCaseNode.switchCaseStat().DEFAULT() != null)
                {
                    var defaultBody = switchCaseNode.normalTemplateBody();
                    if (defaultBody != null)
                    {
                        return(Visit(defaultBody));
                    }
                    else
                    {
                        return(null);
                    }
                }

                var caseExprs       = switchCaseNode.switchCaseStat().expression();
                var caseErrorPrefix = "Case '" + caseExprs[0].GetText() + "': ";
                var caseExprResult  = EvalExpression(caseExprs[0].GetText(), caseExprs[0], switchCaseNode.switchCaseStat().GetText(), caseErrorPrefix);
                if (switchExprResult[0] == caseExprResult[0])
                {
                    return(Visit(switchCaseNode.normalTemplateBody()));
                }

                idx++;
            }

            return(null);
        }
Exemplo n.º 2
0
        public override AnalyzerResult VisitSwitchCaseBody([NotNull] LGTemplateParser.SwitchCaseBodyContext context)
        {
            var result = new AnalyzerResult();
            var switchCaseNodes = context.switchCaseTemplateBody().switchCaseRule();
            foreach (var iterNode in switchCaseNodes)
            {
                var expression = iterNode.switchCaseStat().EXPRESSION();
                if (expression.Length > 0)
                {
                    result.Union(AnalyzeExpression(expression[0].GetText()));
                }

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

            return result;
        }
Exemplo n.º 3
0
            public override object VisitSwitchCaseBody([NotNull] LGTemplateParser.SwitchCaseBodyContext context)
            {
                var switchCaseRules = context.switchCaseTemplateBody().switchCaseRule();
                var length          = switchCaseRules.Length;

                for (var idx = 0; idx < length; idx++)
                {
                    var switchCaseNode = switchCaseRules[idx].switchCaseStat();
                    var switchExpr     = switchCaseNode.SWITCH() != null;
                    var caseExpr       = switchCaseNode.CASE() != null;
                    var defaultExpr    = switchCaseNode.DEFAULT() != null;
                    var node           = switchExpr ? switchCaseNode.SWITCH() :
                                         caseExpr?switchCaseNode.CASE() :
                                             switchCaseNode.DEFAULT();

                    if (node.GetText().Count(u => u == ' ') > 1 ||
                        (idx == 0 && !switchExpr) ||
                        (idx > 0 && switchExpr) ||
                        (idx > 0 && idx < length - 1 && !caseExpr))
                    {
                        return(null);
                    }

                    if ((switchExpr || caseExpr) && switchCaseNode.expression().Length == 1)
                    {
                        FillInExpression(switchCaseNode.expression(0));
                    }

                    if ((caseExpr || defaultExpr) && switchCaseRules[idx].normalTemplateBody() != null)
                    {
                        Visit(switchCaseRules[idx].normalTemplateBody());
                    }
                }

                return(null);
            }
Exemplo n.º 4
0
        public override List <Diagnostic> VisitSwitchCaseBody([NotNull] LGTemplateParser.SwitchCaseBodyContext context)
        {
            var result          = new List <Diagnostic>();
            var switchCaseRules = context.switchCaseTemplateBody().switchCaseRule();
            var length          = switchCaseRules.Length;

            for (var idx = 0; idx < length; idx++)
            {
                var switchCaseNode = switchCaseRules[idx].switchCaseStat();
                var switchExpr     = switchCaseNode.SWITCH() != null;
                var caseExpr       = switchCaseNode.CASE() != null;
                var defaultExpr    = switchCaseNode.DEFAULT() != null;
                var node           = switchExpr ? switchCaseNode.SWITCH() :
                                     caseExpr?switchCaseNode.CASE() :
                                         switchCaseNode.DEFAULT();

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

                if (idx == 0 && !switchExpr)
                {
                    result.Add(BuildLGDiagnostic(TemplateErrors.NotStartWithSwitchInSwitchCase, context: switchCaseNode));
                }

                if (idx > 0 && switchExpr)
                {
                    result.Add(BuildLGDiagnostic(TemplateErrors.MultipleSwithStatementInSwitchCase, context: switchCaseNode));
                }

                if (idx > 0 && idx < length - 1 && !caseExpr)
                {
                    result.Add(BuildLGDiagnostic(TemplateErrors.InvalidStatementInMiddlerOfSwitchCase, context: switchCaseNode));
                }

                if (idx == length - 1 && (caseExpr || defaultExpr))
                {
                    if (caseExpr)
                    {
                        result.Add(BuildLGDiagnostic(TemplateErrors.NotEndWithDefaultInSwitchCase, DiagnosticSeverity.Warning, switchCaseNode));
                    }
                    else
                    {
                        if (length == 2)
                        {
                            result.Add(BuildLGDiagnostic(TemplateErrors.MissingCaseInSwitchCase, DiagnosticSeverity.Warning, switchCaseNode));
                        }
                    }
                }

                if (switchExpr || caseExpr)
                {
                    if (switchCaseNode.expression().Length != 1)
                    {
                        result.Add(BuildLGDiagnostic(TemplateErrors.InvalidExpressionInSwiathCase, context: switchCaseNode));
                    }
                    else
                    {
                        var errorPrefix = switchExpr ? "Switch" : "Case";
                        errorPrefix += " '" + switchCaseNode.expression(0).GetText() + "': ";
                        result.AddRange(CheckExpression(switchCaseNode.expression(0), errorPrefix));
                    }
                }
                else
                {
                    if (switchCaseNode.expression().Length != 0 || switchCaseNode.TEXT().Length != 0)
                    {
                        result.Add(BuildLGDiagnostic(TemplateErrors.ExtraExpressionInSwitchCase, context: switchCaseNode));
                    }
                }

                if (caseExpr || defaultExpr)
                {
                    if (switchCaseRules[idx].normalTemplateBody() != null)
                    {
                        result.AddRange(Visit(switchCaseRules[idx].normalTemplateBody()));
                    }
                    else
                    {
                        result.Add(BuildLGDiagnostic(TemplateErrors.MissingTemplateBodyInSwitchCase, context: switchCaseNode));
                    }
                }
            }

            return(result);
        }