public override string DoWrite(ParseNode node) { var contents = node.Content; return contents.StartsWith("//") ? contents + Environment.NewLine : contents; }
protected override void FixLogic(ParseNode node) { var parentStaticDeclaration = node.Parent; var parentRule = parentStaticDeclaration.Rule; if (parentRule != RuleKind.Static) return; var getFunctionNode = node.GetParentFunctionDeclaration(); if(getFunctionNode==null) return; var siblingInstructions = parentStaticDeclaration.Parent.Children; siblingInstructions.RemoveAt(siblingInstructions.IndexOf(parentStaticDeclaration)); var functionName = GetFunctionName(getFunctionNode); var renameTable = new RenameTable(functionName); renameTable.ScanNames(parentStaticDeclaration); renameTable.RenameInTree(parentStaticDeclaration); renameTable.RenameInTree(getFunctionNode); var functionChildren = getFunctionNode.Parent.Children; var functionPosInProgram = functionChildren.IndexOf(getFunctionNode); var staticStates = parentStaticDeclaration.States.MappedNodes; functionChildren.Insert(functionPosInProgram, staticStates[1]); }
static void RenameConflict(ParseNode node) { var tokenData = node; var functionName = tokenData.Content; if (!FunctionReferenceData.HasFunction(functionName)) return; if (!FunctionReferenceData.FunctionHasRefArguments(functionName)) return; var parentNodeStates = node.Parent.States; var indexInParent = parentNodeStates.MappedNodes.IndexOf(node); var nextTokenKind = parentNodeStates.MappedNodes[indexInParent + 1].GetTokenKind(); if (nextTokenKind != TokenKind.OpenParen) return; var closeParen = parentNodeStates.GetNextMachingTokenKind( TokenKind.CloseParen, TokenKind.OpenParen, indexInParent + 1); var referenceData = FunctionReferenceData.GetReferenceData(functionName); var positionsStart = new List<int> { indexInParent + 2 }; for (var i = indexInParent + 2; i < closeParen; i++) { var nodeTokenKind = parentNodeStates[i].GetTokenKind() == TokenKind.Comma; if (nodeTokenKind) { positionsStart.Add(i + 1); } } //parsing of params seems wrong, better to skip it if (positionsStart.Count != referenceData.RefBools.Length) return; positionsStart.ReverseEachWithIndex((posStart, index) => { if (referenceData[index] == ParameterKind.None) return; parentNodeStates.Insert(posStart, TokenKind.Space.BuildTokenFromId(" ")); parentNodeStates.Insert(posStart, TokenKind.Ref.BuildTokenFromId()); }); parentNodeStates.Remap(); }
void ResolveRefConflicts(ParseNode[] functionNodes) { foreach (var functionNode in functionNodes) { ResolveConflictsInFunction(functionNode); } }
static void FixLogic(ParseNode node) { while (CanFixOpenCloseSquare(node, 0)) { //do nothing } }
protected override void FixRuleProblem(ParseNode node) { var cleanStates = node.States; var openParen = cleanStates.GeNextTokenKind(TokenKind.OpenSquared); if (openParen == 0) return; var starts = new List<int>(); var ends = new List<int>(); var firstParamStart = cleanStates.GeNextTokenKind(TokenKind.OpenParen) + 1; do { starts.Add(firstParamStart); var nextComma = cleanStates.GeNextTokenKind(TokenKind.Comma, firstParamStart); if (nextComma != 0) { ends.Add(nextComma - 1); } else { ends.Add(cleanStates.GeNextTokenKind(TokenKind.CloseParen, firstParamStart)); break; } firstParamStart = nextComma + 1; } while (true); starts.ReverseEachWithIndex((pos, index) => FixArrayInParam(pos, ends[index], cleanStates)); }
protected override void FixRuleProblem(ParseNode node) { var cleanStates = node.States; var functionName = cleanStates[1].GetTokenData().Content; var openParen = cleanStates.GeNextTokenKind(TokenKind.OpenParen); var closeParen = cleanStates.GeNextTokenKind(TokenKind.CloseParen); if (openParen + 1 == closeParen) { FunctionReferenceData.DefineFunction(functionName, 0); return; } var countCommas = cleanStates.MappedNodes.Count(paramNode => paramNode.GetTokenKind() == TokenKind.Comma); var function = FunctionReferenceData.DefineFunction(functionName, countCommas + 1); var indexParam = 0; foreach (var mappedNode in cleanStates.MappedNodes) { if (mappedNode.GetTokenKind() == TokenKind.Ref) { function[indexParam] = ParameterKind.Ref; } if (mappedNode.GetTokenKind() == TokenKind.Comma) { indexParam++; } } }
static void EvaluateDeclaration(ParseNode astNode) { var states = astNode.Parent.States; var typeIndex = states.MappedNodes.IndexOf(astNode); if (states.MappedNodes[typeIndex + 1].GetTokenKind() != TokenKind.Identifier) return; var squarePos = states.GeNextTokenKind(TokenKind.OpenSquared); if (squarePos != 0) return; var assingPos = states.GeNextTokenKind(TokenKind.Assign); if (assingPos != 0) return; var typeName = astNode.Content; var identifierPositions = states.GetAllOfToken(TokenKind.Identifier); if (identifierPositions.Count == 0) return; identifierPositions.ReverseEach(identifierPosition => { var nextTokenKind = states[identifierPosition + 1].Token; if (nextTokenKind != TokenKind.Comma && nextTokenKind != TokenKind.SemiColon) return; var openParen = states.GeNextTokenKind(TokenKind.OpenParen); if (openParen != 0) return; var insertNodes = BuildInsertNodes(typeName); states.InsertRange(identifierPosition + 1, insertNodes); }); }
static void EvaluateAssign(ParseNode node, Dictionary<string, TypeData> variableTypes) { var states = node.Parent.States; var assign = states.MappedNodes.IndexOf(node); var leftToken = states[assign - 1].GetTokenData(); var rightToken = states[assign + 1].GetTokenData(); if (leftToken.Token != TokenKind.Identifier) return; if (rightToken.Token != TokenKind.Identifier) return; var leftVariableName = leftToken.Content; var leftExpression = variableTypes.GetTypeOfName(leftVariableName); var rightVariableName = rightToken.Content; var rightExpression = variableTypes.GetTypeOfName(rightVariableName); if (leftExpression.Count == 0 || rightExpression.Count == 0) return; if (leftExpression.Equals(rightExpression)) return; var leftTypeName = leftExpression[0].Content; var rightTypeName = rightExpression[0].Content; //we don't handle arrays or other complex types if (leftExpression.TokenList.Count != 1 || rightExpression.TokenList.Count != 1) return; var conversionTypes = string.Format("{0}={1}", leftTypeName, rightTypeName); HandleConversions(conversionTypes, states, leftTypeName, assign); }
protected override void FixRuleProblem(ParseNode node) { var cleanStates = node.States; var openParen = cleanStates.MappedNodes.FirstOrDefault(searchNode => searchNode.GetTokenKind() == TokenKind.Operator && searchNode.GetTokenData().Content == "&"); if (openParen == null) return; var starts = new List<int>(); var ends = new List<int>(); var firstParamStart = cleanStates.GeNextTokenKind(TokenKind.OpenParen) + 1; do { starts.Add(firstParamStart); var nextComma = cleanStates.GeNextTokenKind(TokenKind.Comma, firstParamStart); if (nextComma != 0) { ends.Add(nextComma - 1); } else { ends.Add(cleanStates.GeNextTokenKind(TokenKind.CloseParen, firstParamStart)); break; } firstParamStart = nextComma + 1; } while (true); starts.ReverseEachWithIndex((pos, index) => FixArrayInParam(pos, ends[index], cleanStates)); }
public void Add(ParseNode parseNode) { if (Children == null) Children = new List<ParseNode>(); Children.Add(parseNode); parseNode.Parent = this; }
static void ExtractParameterData(ParseNode parent, Dictionary<string, TypeData> variableTypes) { var states = parent.States; var openParam = 1; if (states.Count == 2) return; if (states.Count == 3) { states.RemoveAt(1); return; } var pos = openParam; do { var typeData = new TypeData(); var i = pos; for (; states[i].Token != TokenKind.Identifier; i++) { typeData.AddTypeNode(states[i]); } variableTypes[states[i].Content] = typeData; pos = states.GeNextTokenKind(TokenKind.Comma, pos); if (pos == 0) return; pos++; } while (true); }
protected override void FixRuleProblem(ParseNode node) { var states = node.States[0].States; if(states.Count!=2) return; var functionNode = node.GetParentFunctionDeclaration(); var parentFunctionDeclaration = functionNode.States; if (parentFunctionDeclaration[0].Token != TokenKind.TypeName) return; var functionTypeName = parentFunctionDeclaration[0].Content; if (functionTypeName == "void") return; states.InsertRange(1, new[] { TokenKind.OpenParen.BuildTokenFromId(), TokenKind.CloseParen.BuildTokenFromId(), }); switch (functionTypeName) { case TypeNames.Int: states.Insert(2, TokenKind.Int.BuildTokenFromId("0")); break; case TypeNames.Double: states.Insert(2, TokenKind.Float.BuildTokenFromId("0.0")); break; default: throw new InvalidDataException("Invalid return type"); } states.Remap(); }
void EvaluateAssign(ParseNode node) { var states = node.Parent.States; var assign = states.MappedNodes.IndexOf(node); var leftToken = states[assign - 1].GetTokenData(); var rightToken = states[assign + 1].GetTokenData(); if (leftToken.Token != TokenKind.CloseSquared) return; if (rightToken.Token != TokenKind.Identifier) return; var pos = -1; for (var i = assign - 2; i >= 0; i--) { leftToken = states[i].GetTokenData(); if (leftToken.Token != TokenKind.OpenSquared) continue; pos = i - 1; break; } if (pos < 0) return; leftToken = states[pos].GetTokenData(); var leftVariableName = leftToken.Content; var leftExpression = GetExpressionOfName(leftVariableName); var rightVariableName = rightToken.Content; var rightExpression = GetExpressionOfName(rightVariableName); if (leftExpression == null || rightExpression == null) return; if (rightExpression.Count < 1) return; var leftTypeName = leftExpression[0].Content; var rightTypeName = rightExpression[0].Content; if (leftTypeName == rightTypeName) return; if (leftTypeName == "Unknown") return; if (rightTypeName == "Unknown") return; //we don't handle arrays or other complex types if (rightExpression.TokenList.Count != 1) return; var conversionTypes = string.Format("{0}={1}", leftTypeName, rightTypeName); switch (conversionTypes) { case "int=double": AddExplicitCastAtPosition(states, leftTypeName, assign + 1); break; //nothing to do case "color=int": case "datetime=int": case "double=int": case "int=datetime": case "double=datetime": return; default: throw new NotImplementedException(); } }
static void SearchFunctionCall(ParseNode obj) { var indexInParent = obj.PositionInParent(); var blockStates = obj.Parent.States; if (blockStates[indexInParent + 1].Token != TokenKind.OpenParen) return; var functionData = FunctionTypeData.GetFunctionData(blockStates[indexInParent].Content); if (functionData == null) return; if (functionData.ParamTypes.Length == 0) return; var closeParen = blockStates.GeNextTokenKind(TokenKind.CloseParen, indexInParent); var commas = blockStates.GetAllOfToken(TokenKind.Comma, indexInParent + 1, closeParen); if (commas.Count != functionData.ParamTypes.Length - 1) return; commas.Add(closeParen); var startRange = indexInParent + 2; for (var i = 0; i < commas.Count; i++) { var commaPos = commas[i]; var endRange = commaPos - 1; if (startRange == endRange && blockStates[startRange].Token != TokenKind.Identifier) { TryHandleParameter(functionData.ParamTypes, i, blockStates, startRange); } if (startRange == endRange && blockStates[startRange].Content == "null") { TryHandleParameterNull(functionData.ParamTypes, i, blockStates, startRange); } startRange = endRange + 2; } }
static void RenameConflicts(ParseNode[] functionNodes, Dictionary<string, string> renameTable) { foreach (var functionNode in functionNodes) { RenameConflictsInFunction(functionNode, renameTable); } }
public override void Perform(ParseNode node) { var sharpDefineVisitor = new AstTreeVisitorAllNodes(node) { CallOnMatch = FixLogic }; sharpDefineVisitor.Visit(); }
protected override void FixRuleProblem(ParseNode astNode) { var states = astNode.States; var isArrayDefined = states.Any(node => node.GetTokenKind() == TokenKind.OpenSquared); if (!isArrayDefined) return; SetRequiredParen(states); }
protected virtual void VisitAstNode(ParseNode parentNode) { for (var index = parentNode.Count - 1; index >= 0; index--) { var node = parentNode.Children[index]; VisitAstNode(node); } }
public void RenameInTree(ParseNode parent) { var astVisitor = new AstTokenVisitor(parent, TokenKind.Identifier) { CallOnMatch = OnIdentifier }; astVisitor.Visit(); }
public override void Perform(ParseNode node) { var functionNodes = node.States; foreach (var functionNode in functionNodes) { FixDeclarationsNotInFunctionBody(functionNode); } }
public override void Perform(ParseNode node) { var functionNodes = node.GetFunctionBodies(); foreach (var functionNode in functionNodes) { EvaluateFunctionReturn(functionNode); } }
public override void OnVisitMatch(ParseNode node) { var cleanStates = new CleanupAstNodeStates(node.Parent.Children); var advance = cleanStates.MappedNodes.IndexOf(node); if (advance == 0) return; cleanStates.ShiftBlock(advance); }
public override void Perform(ParseNode node) { var functionNodes = node.ExtractFunctionNodes(); foreach (var functionNode in functionNodes) { FixDeclarationsInFunctionBody(functionNode); } }
static void FixDeclarationsNotInFunctionBody(ParseNode functionBody) { var fixInFunction = new FixAssignmentInFunction(functionBody); fixInFunction.Perform(); var fixArraysAssigned = new FixAssignmentWithArraysFunction(functionBody); fixArraysAssigned.Perform(); }
public override void Perform(ParseNode ruleNode) { var visitor = new AstTreeVisitorRule(ruleNode, _rule) { CallOnMatch = FixRuleProblem }; visitor.Visit(); }
void EvaluateConflictsBetweenFunctionsAndVariables(ParseNode[] functionNodes, Dictionary<string, bool> functionsWithConflicts) { foreach (var functionNode in functionNodes) { EvaluateConflictsInFunction(functionNode, functionsWithConflicts); } }
public override void Perform(ParseNode node) { var sharpDefineVisitor = new AstTokenVisitor(node, _tokenKind) { CallOnMatch = FixLogic }; sharpDefineVisitor.Visit(); }
static void EvaluateFunctionReturn(ParseNode functionNode) { var astVisitor = new AstTokenVisitor(functionNode, TokenKind.Identifier) { CallOnMatch = SearchFunctionCall }; astVisitor.Visit(); }
protected override void FixLogic(ParseNode node) { var tokenContent = node.GetTokenData(); var content = tokenContent.Content .Replace('$', '_') .Replace('.', '_') ; tokenContent.Content = content; }