Example #1
0
        public static void SetRequiredParen(CleanupAstNodeStates states)
        {
            var openSquarePos = states.MappedNodes.GeNextTokenKind(TokenKind.OpenSquared);
            var closeSquarePos = states.MappedNodes.GeNextTokenKind(TokenKind.CloseSquared);
            var commaPos = states.MappedNodes.GeNextTokenKind(TokenKind.Comma);

            if (openSquarePos + 1 == closeSquarePos || openSquarePos + 1 == commaPos)
            {
                states.Insert(openSquarePos + 1, TokenKind.Int.BuildTokenFromId("0"));
                states.Remap();
            }
            if (commaPos + 1 == closeSquarePos)
            {
                states.Insert(commaPos + 1, TokenKind.Int.BuildTokenFromId("0"));
                states.Remap();
            }

            var tokenData = states[0].GetTokenData();
            var typeToken = tokenData.Clone();


            var assignPos = states.GeNextTokenKind(TokenKind.Assign);
            if (assignPos > 0)
            {
                var openSquaredPos = states.GeNextTokenKind(TokenKind.OpenSquared);
                if (openSquaredPos > assignPos)
                    return;
            }
            var nodesToBeAdded = new List<ParseNode>
                {
                    TokenKind.Assign.BuildTokenFromId(),
                    TokenKind.Space.BuildTokenFromId(" "),
                    TokenKind.New.BuildTokenFromId(),
                    TokenKind.Space.BuildTokenFromId(" "),
                    typeToken.BuildTerminalNode(),
                };
            states.InsertRange(2, nodesToBeAdded);

            var countSquared = states.Count(node => node.GetTokenKind() == TokenKind.Comma) + 1;

            var toAdd = BuildSquaresAndCommas(countSquared);
            states.InsertRange(1, toAdd);
        }