Exemplo n.º 1
0
 public override string DoWrite(ParseNode node)
 {
     var contents = node.Content;
     return contents.StartsWith("//")
                ? contents + Environment.NewLine
                : contents;
 }
Exemplo n.º 2
0
        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]);
        }
Exemplo n.º 3
0
 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();
 }
Exemplo n.º 4
0
 void ResolveRefConflicts(ParseNode[] functionNodes)
 {
     foreach (var functionNode in functionNodes)
     {
         ResolveConflictsInFunction(functionNode);
     }
 }
Exemplo n.º 5
0
 static void FixLogic(ParseNode node)
 {
     while (CanFixOpenCloseSquare(node, 0))
     {
         //do nothing
     }
 }
Exemplo n.º 6
0
        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);
                });
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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));
        }
Exemplo n.º 11
0
 public void Add(ParseNode parseNode)
 {
     if (Children == null)
         Children = new List<ParseNode>();
     Children.Add(parseNode);
     parseNode.Parent = this;
 }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
 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();
 }
Exemplo n.º 14
0
        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();
            }
        }
Exemplo n.º 15
0
 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);
     }
 }
Exemplo n.º 17
0
 public override void Perform(ParseNode node)
 {
     var sharpDefineVisitor = new AstTreeVisitorAllNodes(node)
         {
             CallOnMatch = FixLogic
         };
     sharpDefineVisitor.Visit();
 }
Exemplo n.º 18
0
 protected override void FixRuleProblem(ParseNode astNode)
 {
     var states = astNode.States;
     var isArrayDefined = states.Any(node => node.GetTokenKind() == TokenKind.OpenSquared);
     if (!isArrayDefined)
         return;
     SetRequiredParen(states);
 }
Exemplo n.º 19
0
 protected virtual void VisitAstNode(ParseNode parentNode)
 {
     for (var index = parentNode.Count - 1; index >= 0; index--)
     {
         var node = parentNode.Children[index];
         VisitAstNode(node);
     }
 }
Exemplo n.º 20
0
 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);
     }
 }
Exemplo n.º 22
0
 public override void Perform(ParseNode node)
 {
     var functionNodes = node.GetFunctionBodies();
     foreach (var functionNode in functionNodes)
     {
         EvaluateFunctionReturn(functionNode);
     }
 }
Exemplo n.º 23
0
 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);
     }
 }
Exemplo n.º 25
0
        static void FixDeclarationsNotInFunctionBody(ParseNode functionBody)
        {
            var fixInFunction = new FixAssignmentInFunction(functionBody);
            fixInFunction.Perform();

            var fixArraysAssigned = new FixAssignmentWithArraysFunction(functionBody);
            fixArraysAssigned.Perform();
        }
Exemplo n.º 26
0
 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);
     }
 }
Exemplo n.º 28
0
 public override void Perform(ParseNode node)
 {
     var sharpDefineVisitor = new AstTokenVisitor(node, _tokenKind)
         {
             CallOnMatch = FixLogic
         };
     sharpDefineVisitor.Visit();
 }
Exemplo n.º 29
0
 static void EvaluateFunctionReturn(ParseNode functionNode)
 {
     var astVisitor = new AstTokenVisitor(functionNode, TokenKind.Identifier)
     {
         CallOnMatch = SearchFunctionCall
     };
     astVisitor.Visit();
 }
Exemplo n.º 30
0
 protected override void FixLogic(ParseNode node)
 {
     var tokenContent = node.GetTokenData();
     var content = tokenContent.Content
         .Replace('$', '_')
         .Replace('.', '_')
         ;
     tokenContent.Content = content;
 }