Example #1
0
        private static void Parse(MoveInfo parentInfo, ParsingInfo parsingInfo, ScriptInfo scriptInfo)
        {
            SwitchStatement switchStatement = new SwitchStatement();
            MoveInfo        moveInfo        = new MoveInfo(parentInfo);

            // expression
            IElement tryExpGroup = moveInfo.FindNextBlack(SearchDirection.LeftToRight);

            if (!(tryExpGroup is ParenthesesGroup))
            {
                throw new SyntaxException("Could not find switch expression", parentInfo.GetErrorInfo());
            }

            ParenthesesGroup expGroup = (ParenthesesGroup)tryExpGroup;

            MoveInfo   expGroupInfo = new MoveInfo(expGroup, SearchTree.ContentBlock, 0, parentInfo);
            Expression exp          = Expression.Parse(expGroupInfo, parsingInfo, scriptInfo);

            if (exp == null || expGroupInfo.FindNextBlack(SearchDirection.LeftToRight) != null)
            {
                throw new SyntaxException("Could not parse switch expression", parentInfo.GetErrorInfo());
            }

            // scope group
            IElement tryScopeGroup = moveInfo.FindNextBlack(SearchDirection.LeftToRight);

            if (!(tryScopeGroup is ScopeGroup))
            {
                throw new SyntaxException("Could not find switch ScopeGroup", parentInfo.GetErrorInfo());
            }

            ScopeGroup scopeGroup = (ScopeGroup)tryScopeGroup;

            MoveInfo scopeGroupInfo = new MoveInfo(scopeGroup, SearchTree.ContentBlock, 0, parentInfo);
            IElement nextCase       = scopeGroupInfo.Find(SearchDirection.LeftToRight, SearchVisibility.Visible);

            if (nextCase == null)
            {
                throw new SyntaxException("Could not find any switch case", parentInfo.GetErrorInfo());
            }

            while (nextCase != null)
            {
                if (!(
                        DefaultSwitchStatement.Check(scopeGroupInfo, parsingInfo, scriptInfo, true) ||
                        CaseSwitchStatement.Check(scopeGroupInfo, parsingInfo, scriptInfo, true)
                        ))
                {
                    throw new SyntaxException("Could not parse switch case/default", parentInfo.GetErrorInfo());
                }

                nextCase = scopeGroupInfo.FindNextBlack(SearchDirection.LeftToRight);
            }

            // build
            int startIndex = parentInfo.CurrentIndex;
            int length     = (moveInfo.CurrentIndex + 1) - startIndex;

            switchStatement.AddChildren(parentInfo.CurrentElements.GetRange(startIndex, length));
            parentInfo.Replace(length, switchStatement);
        }