Exemplo n.º 1
0
        /// <summary>
        /// Map variable to shorter name.
        /// </summary>
        /// <param name="astNode"></param>
        /// <param name="shortNameMap"></param>
        /// <param name="nameGenerator"></param>
        private static void ShortNameMapping(
            ProtoCore.Core core,
            AssociativeNode astNode,
            Dictionary <string, string> shortNameMap,
            ShortNameGenerator nameGenerator,
            HashSet <string> mappedVariables)
        {
            Action <IdentifierNode> func = n =>
            {
                string shortName;
                if (shortNameMap.TryGetValue(n.Value, out shortName))
                {
                    if (shortName == string.Empty)
                    {
                        shortName = nameGenerator.GetNextName();
                        while (mappedVariables.Contains(shortName))
                        {
                            shortName = nameGenerator.GetNextName();
                        }

                        shortNameMap[n.Value] = shortName;
                    }
                    n.Value = n.Name = shortName;
                }
            };

            IdentifierVisitor visitor = new IdentifierVisitor(func, core);

            astNode.Accept(visitor);
        }
Exemplo n.º 2
0
 public SqlServerParser(TransactSqlVersion transactSqlVersion)
 {
     _parser            = new TSqlParserInternal(transactSqlVersion, false);
     _tableVisitor      = CreateTableVisitor();
     _expressionVisitor = CreateExpressionVisitor();
     _identifierVisitor = CreateIdentifierVisitor();
     _actionVisitor     = CreateActionVisitor();
 }
Exemplo n.º 3
0
            public override void Visit(SelectScalarExpression node)
            {
                base.Visit(node);

                var identifier = new IdentifierVisitor();

                node.Accept(identifier);

                this.SelectNodes.Add(
                    String.Join(".", identifier.SelectNodes.Select(x => x.Value)
                                ));
            }
Exemplo n.º 4
0
        /// <summary>
        /// Remap variables.
        /// </summary>
        /// <param name="astNode"></param>
        /// <param name="renamingMap"></param>
        private static void VariableRemapping(
            ProtoCore.Core core,
            AssociativeNode astNode,
            Dictionary <string, string> renamingMap,
            Dictionary <string, string> outuptMap,
            HashSet <string> mappedVariable)
        {
            Action <IdentifierNode> func = n =>
            {
                string newIdent;
                if (renamingMap.TryGetValue(n.Value, out newIdent))
                {
                    var ident = n.Value;
                    n.Value          = n.Name = newIdent;
                    outuptMap[ident] = newIdent;
                    mappedVariable.Add(newIdent);
                }
            };

            IdentifierVisitor visitor = new IdentifierVisitor(func, core);

            astNode.Accept(visitor);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Renumber variables used in astNode.
        /// </summary>
        /// <param name="astNode"></param>
        /// <param name="numberingMap"></param>
        /// <param name="variableMap"></param>
        private static void VariableNumbering(
            ProtoCore.Core core,
            AssociativeNode astNode,
            NodeModel node,
            Dictionary <string, NumberingState> numberingMap,
            Dictionary <string, string> renamingMap,
            Dictionary <string, string> inputMap,
            Dictionary <string, string> outputMap,
            HashSet <string> mappedVariables)
        {
            Action <IdentifierNode> func = n =>
            {
                var ident = n.Value;

                // This ident is from other node's output port, it is not necessary to
                // do renumbering.
                if (inputMap.ContainsKey(ident))
                {
                    return;
                }

                NumberingState ns;
                if (numberingMap.TryGetValue(ident, out ns))
                {
                    // ident already defined somewhere else. So we continue to
                    // bump its ID until numbered variable won't conflict with
                    // all existing variables.
                    if (ns.IsNewSession)
                    {
                        ns.BumpID();
                        while (mappedVariables.Contains(ns.NumberedVariable))
                        {
                            ns.BumpID();
                        }

                        ns.IsNewSession = false;
                    }
                    // ident already defined somewhere else, but we already
                    // renumber this variable, so continue to use re-numbered
                    // one.
                }
                else
                {
                    // It is a new variable. But we need to check if some other
                    // variables are renamed to this one because of renumbering.
                    // If there is confliction, continue to bump its ID.
                    ns = new NumberingState(ident);
                    numberingMap[ident] = ns;

                    while (mappedVariables.Contains(ns.NumberedVariable))
                    {
                        ns.BumpID();
                    }
                }

                if (ns.ID != 0)
                {
                    var numberedVar = ns.NumberedVariable;
                    n.Name = n.Value = numberedVar;

                    // If this variable is numbered, and it is also output to
                    // other node, we should remap the output variable to
                    // this re-numbered variable.
                    string name = string.Format("{0}%{1}", ident, node.GUID);
                    if (renamingMap.ContainsKey(name))
                    {
                        var mappedName = renamingMap[name];
                        renamingMap[mappedName] = numberedVar;

                        // Record in output map.
                        if (outputMap.ContainsKey(mappedName))
                        {
                            outputMap[mappedName] = numberedVar;
                        }
                    }
                }

                // If this one is not the variable that going to be renamed, then
                // add to mapped variable set
                if (!renamingMap.ContainsKey(ns.NumberedVariable))
                {
                    mappedVariables.Add(ns.NumberedVariable);
                }
            };

            IdentifierVisitor visitor = new IdentifierVisitor(func, core);

            astNode.Accept(visitor);
        }
        public static IdentifierInfo TryGetIdentifierInfo([CanBeNull] this IOperation identifier)
        {
            var visitor = new IdentifierVisitor();

            return(visitor.Visit(identifier, null));
        }