private static Tuple <string, bool> GetSurroundingPaths(
            CstNode node, string path, ICollection <CstNode> surroundingNodes,
            FeatureExtractor extractor, ISet <string> paths,
            IDictionary <CstNode, string> ancestors)
        {
            var token    = "";
            var temporal = false;
            var count    = 0;

            if (node.HasToken)
            {
                token    = extractor.GetToken(node);
                temporal = IsTemporalVariable(token);
            }
            else
            {
                foreach (var child in node.Children())
                {
                    count++;
                    if (surroundingNodes.Contains(child))
                    {
                        //var newPath = path + (ancestors.Contains(child) ? "/" : ">")
                        //              + child.Name + child.RuleId;
                        var newPath = (ancestors.ContainsKey(child)
                                ? ancestors[child]
                                : path + ">" + child.Name + child.RuleId);
                        var ret = GetSurroundingPaths(
                            child, newPath, surroundingNodes, extractor, paths, ancestors);
                        temporal = ret.Item2;
                        if (token != null && ret.Item1 != null)
                        {
                            token += ret.Item1;
                            continue;
                        }
                    }
                    token = null;
                }
            }
            if (temporal && count <= 1)
            {
                return(Tuple.Create <string, bool>(null, true));
            }
            paths.Add(path);
            if (token != null)
            {
                paths.Add(path + "'" + token); // need node.Name + node.RuleId ?
            }
            return(Tuple.Create(token, false));
        }
        private static string GetFeatureVector(
            CstNode node, string path, IDictionary <string, BigInteger> featureString2Bit,
            FeatureExtractor extractor, IDictionary <CstNode, string> ancestors,
            ref BigInteger vector)
        {
            BigInteger bit;
            var        token = "";

            if (node.HasToken)
            {
                token = extractor.GetToken(node);
            }
            else
            {
                foreach (var child in node.Children())
                {
                    //var newPath = path + (ancestors.Contains(child) ? "/" : ">")
                    //              + child.Name + child.RuleId;
                    var newPath = (ancestors.ContainsKey(child)
                            ? ancestors[child]
                            : path + ">" + child.Name + child.RuleId);
                    if (featureString2Bit.TryGetValue(newPath, out bit))
                    {
                        vector |= bit;
                        var ret = GetFeatureVector(
                            child, newPath, featureString2Bit, extractor, ancestors, ref vector);
                        if (token != null && ret != null)
                        {
                            token += ret;
                            continue;
                        }
                    }
                    token = null;
                }
            }
            if (token != null)
            {
                if (featureString2Bit.TryGetValue(path + "'" + token, out bit))
                {
                    vector |= bit;
                }
            }
            return(token);
        }
        public CstNode FinishParsing()
        {
            var root  = _dummyRoot.Children().First();
            var count = _stream.Size - 1; // Avoid writing "<EOF>"

            while (count > 0 && _stream.Get(count - 1).Type < 0)
            {
                count--;
            }
            var antlrToken = _stream.Get(count);
            var token      = CreateTerminalNode(
                Code2XmlConstants.EofTokenName, antlrToken, String.Empty, count,
                Code2XmlConstants.EofRuleId);

            // TODO: Deals with a bug in ANTLR 4 temporally
            if (token.Hiddens.Count > 0)
            {
                var location = token.Hiddens.Last().Range.EndLocation;
                token.Token.Range = new CodeRange(location, location);
            }
            root.AddLast(token);
            return(root);
        }
 private static string GetFeatureVector(
         CstNode node, string path, IDictionary<string, BigInteger> featureString2Bit,
         FeatureExtractor extractor, IDictionary<CstNode, string> ancestors,
         ref BigInteger vector) {
     BigInteger bit;
     var token = "";
     if (node.HasToken) {
         token = extractor.GetToken(node);
     } else {
         foreach (var child in node.Children()) {
             //var newPath = path + (ancestors.Contains(child) ? "/" : ">")
             //              + child.Name + child.RuleId;
             var newPath = (ancestors.ContainsKey(child)
                     ? ancestors[child]
                     : path + ">" + child.Name + child.RuleId);
             if (featureString2Bit.TryGetValue(newPath, out bit)) {
                 vector |= bit;
                 var ret = GetFeatureVector(
                         child, newPath, featureString2Bit, extractor, ancestors, ref vector);
                 if (token != null && ret != null) {
                     token += ret;
                     continue;
                 }
             }
             token = null;
         }
     }
     if (token != null) {
         if (featureString2Bit.TryGetValue(path + "'" + token, out bit)) {
             vector |= bit;
         }
     }
     return token;
 }
 private static Tuple<string, bool> GetSurroundingPaths(
         CstNode node, string path, ICollection<CstNode> surroundingNodes,
         FeatureExtractor extractor, ISet<string> paths,
         IDictionary<CstNode, string> ancestors) {
     var token = "";
     var temporal = false;
     var count = 0;
     if (node.HasToken) {
         token = extractor.GetToken(node);
         temporal = IsTemporalVariable(token);
     } else {
         foreach (var child in node.Children()) {
             count++;
             if (surroundingNodes.Contains(child)) {
                 //var newPath = path + (ancestors.Contains(child) ? "/" : ">")
                 //              + child.Name + child.RuleId;
                 var newPath = (ancestors.ContainsKey(child)
                         ? ancestors[child]
                         : path + ">" + child.Name + child.RuleId);
                 var ret = GetSurroundingPaths(
                         child, newPath, surroundingNodes, extractor, paths, ancestors);
                 temporal = ret.Item2;
                 if (token != null && ret.Item1 != null) {
                     token += ret.Item1;
                     continue;
                 }
             }
             token = null;
         }
     }
     if (temporal && count <= 1) {
         return Tuple.Create<string, bool>(null, true);
     }
     paths.Add(path);
     if (token != null) {
         paths.Add(path + "'" + token); // need node.Name + node.RuleId ?
     }
     return Tuple.Create(token, false);
 }