示例#1
0
        public static FuncDefParam Parse(MoveInfo parentInfo, ParsingInfo parsingInfo, ScriptInfo scriptInfo)
        {
            FuncDefParam param = new FuncDefParam();

            MoveInfo moveInfo = new MoveInfo(parentInfo);

            MoveInfo wordMoveInfo = moveInfo;
            IElement wordTry      = moveInfo.Find(SearchDirection.LeftToRight, SearchVisibility.Visible);
            int      startIndex   = moveInfo.CurrentIndex;

            if (wordTry == null)
            {
                throw new SyntaxException("Could not parse FuncDef param", parentInfo.GetErrorInfo());
            }

            if (wordTry is SQBracketGroup)
            {
                param._group   = (SQBracketGroup)wordTry;
                param.Optional = true;

                MoveInfo bracketInfo = new MoveInfo((SQBracketGroup)wordTry, SearchTree.ContentBlock, 0, moveInfo);
                wordTry      = bracketInfo.Find(SearchDirection.LeftToRight, SearchVisibility.Visible);
                wordMoveInfo = bracketInfo;
            }

            if (wordTry == null || !wordTry.IsTT(TokenType.Word))
            {
                throw new SyntaxException("Could not parse FuncDef param", parentInfo.GetErrorInfo());
            }

            VarName.Parse(wordMoveInfo, parsingInfo, scriptInfo);
            param.VarName = (VarName)wordMoveInfo.Current;

            param.Name = wordTry.ToString();
            param.AddChildren(parentInfo.CurrentElements.GetRange(startIndex, moveInfo.CurrentIndex - startIndex + 1));
            parentInfo.Replace((moveInfo.CurrentIndex + 1) - startIndex, param);
            return(param);
        }
示例#2
0
        private static void Parse(MoveInfo parentInfo, ParsingInfo parsingInfo, ScriptInfo scriptInfo)
        {
            ForEachStatement foreachStatement = new ForEachStatement();
            MoveInfo         moveInfo         = new MoveInfo(parentInfo);

            foreachStatement._foreachKeyword = (Token)moveInfo.Current;

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

            if (expGroupTry == null || !(expGroupTry is ParenthesesGroup))
            {
                throw new SyntaxException("Could not find foreach expression", parentInfo.GetErrorInfo());
            }

            ParenthesesGroup expGroup     = (ParenthesesGroup)expGroupTry;
            MoveInfo         expGroupInfo = new MoveInfo(expGroup, SearchTree.ContentBlock, 0, parentInfo);

            foreachStatement._foreachGroup = expGroup;

            // var define
            IElement tryVar = expGroupInfo.Find(SearchDirection.LeftToRight, SearchVisibility.Visible);

            if (tryVar == null || !tryVar.IsTT(TokenType.Word))
            {
                throw new SyntaxException("Could not parse foreach var", parentInfo.GetErrorInfo());
            }

            VarName.Parse(expGroupInfo, parsingInfo, scriptInfo);
            VarName varName = (VarName)expGroupInfo.Current;

            LocalVarInfo localVar = parsingInfo.CurrentFunc.LocalVars.Find(a => a.Name.EqualCode(tryVar.ToString()));     // there is already var with this name...

            if (localVar == null)
            {
                parsingInfo.CurrentFunc.LocalVars.Add(new LocalVarInfo(scriptInfo.SF, tryVar.ToString(), tryVar.CharIndex, tryVar.CharLength, null, varName));
            }

            foreachStatement._currentVar = varName;

            // in keyword
            IElement tryIn = expGroupInfo.FindNextBlack(SearchDirection.LeftToRight);

            if (tryIn == null || !tryIn.IsTT(TokenType.Word) || !tryIn.ToString().EqualCode("in"))
            {
                throw new SyntaxException("Could not find foreach 'in'", parentInfo.GetErrorInfo());
            }

            // array
            IElement   tryArray    = expGroupInfo.FindNextBlack(SearchDirection.LeftToRight);
            Expression tryArrayExp = Expression.Parse(expGroupInfo, parsingInfo, scriptInfo);

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

            foreachStatement._array = tryArrayExp;

            // statement
            moveInfo.FindNextBlack(SearchDirection.LeftToRight);
            if (!Statement.Check(moveInfo, parsingInfo, scriptInfo))
            {
                throw new SyntaxException("Could not parse foreach statement", parentInfo.GetErrorInfo());
            }

            foreachStatement._statement = (Statement)moveInfo.Current;

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

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