void FixArrayInParam(int pos, int endRange, CleanupAstNodeStates states) { var openParen = states.GeNextTokenKind(TokenKind.OpenSquared, pos); if (openParen == 0 || openParen > endRange) return; var closeParen = states.GeNextTokenKind(TokenKind.CloseSquared, openParen); var openNode = states[openParen]; var closeNode = states[closeParen]; states.RemoveAt(closeParen); states.RemoveAt(openParen); states.Insert(pos + 1, closeNode); states.Insert(pos + 1, openNode); states.Remap(); }
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); }
static void FindImportFunctionRanges(CleanupAstNodeStates importCleanStates, List<int> startFunctions, List<int> endFunctions) { var position = 0; do { var start = importCleanStates.GeNextTokenKind(TokenKind.TypeName, position); if (start == 0) break; var endPos = importCleanStates.GeNextTokenKind(TokenKind.SemiColon, position + 1); startFunctions.Add(start); endFunctions.Add(endPos); position = endPos; } while (true); }
static void ShiftCase(CleanupAstNodeStates cleanStates, int advancePosition) { var colon = cleanStates.GeNextTokenKind(TokenKind.Colon, advancePosition); var tokenAfterColon = cleanStates[colon + 1].Token; switch (tokenAfterColon) { case TokenKind.Continue: case TokenKind.Return: case TokenKind.Break: case TokenKind.Case: cleanStates.ShiftByNodeType(colon + 1); cleanStates.ReduceRangeOfTokensAsParent(colon + 1, colon + 1, RuleKind.CaseBlock); cleanStates.ReduceRangeOfTokensAsParent(advancePosition, colon + 1, RuleKind.Case); return; } var breakPos = cleanStates.GeNextTokenOfAny(new[] { TokenKind.Break, TokenKind.Return, TokenKind.Continue, TokenKind.Case, }, colon); if (breakPos == 0) { cleanStates.ReduceRangeOfTokensAsParent(colon + 1, cleanStates.Count - 2, RuleKind.CaseBlock); cleanStates.ReduceRangeOfTokensAsParent(advancePosition, colon + 1, RuleKind.Case); return; } var breakToken = cleanStates[breakPos].Token; switch (breakToken) { case TokenKind.Case: cleanStates.ReduceRangeOfTokensAsParent(colon + 1, breakPos - 1, RuleKind.CaseBlock); cleanStates.ReduceRangeOfTokensAsParent(advancePosition, colon + 1, RuleKind.Case); return; case TokenKind.Return: cleanStates.ShiftByNodeType(breakPos); cleanStates.ReduceRangeOfTokensAsParent(colon + 1, breakPos, RuleKind.CaseBlock); cleanStates.ReduceRangeOfTokensAsParent(advancePosition, colon + 1, RuleKind.Case); return; } cleanStates.ReduceRangeOfTokensAsParent(colon + 1, breakPos + 1, RuleKind.CaseBlock); cleanStates.ReduceRangeOfTokensAsParent(advancePosition, colon + 1, RuleKind.Case); }