public void Create(CstNode parent, IToken token, string id) {
     if (token != null) {
         var count = token.TokenIndex;
         var tokenName = DetermineElementName(token, Code2XmlConstants.TokenSetElementName);
         var node = CreateTerminalNode(tokenName, token, token.Text, count, id);
         parent.AddLast(node);
         _nextTokenIndex = count + 1;
     }
 }
 public void Create(CstNode parent, IToken token, string id)
 {
     if (token != null)
     {
         var count     = token.TokenIndex;
         var tokenName = DetermineElementName(token, Code2XmlConstants.TokenSetElementName);
         var node      = CreateTerminalNode(tokenName, token, token.Text, count, id);
         parent.AddLast(node);
         _nextTokenIndex = count + 1;
     }
 }
 public CstNode FinishParsing(CstNode root) {
     var count = _stream.Count - 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);
     root.AddLast(token);
     return root;
 }
        public CstNode FinishParsing(CstNode root)
        {
            var count = _stream.Count - 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);

            root.AddLast(token);
            return(root);
        }
        public void SetLocationInformation(CstNode tree, string code) {
            var line = 1;
            var pos = 0;
            var index = 0;
            var startHiddenLocation = new CodeLocation(line, pos);
            foreach (var tokenNode in tree.AllTokenNodes()) {
                var token = tokenNode.Token;
                var tokenStr = token.Text;
                if (tokenStr == string.Empty) { // e.g. NEWLINE in Python
                    token.Range = new CodeRange(startHiddenLocation, startHiddenLocation);
                    continue;
                }
                var startHiddenIndex = index;
                var tokenChar = tokenStr[0];
                while (code[index] != tokenChar
                       || code.Substring(index, tokenStr.Length) != tokenStr) {
                    if (code[index] != '\n') {
                        pos++;
                    } else {
                        line++;
                        pos = 0;
                    }
                    index++;
                }
                var endHiddenIndex = index;
                var startTokenLocation = new CodeLocation(line, pos);
                var endIndex = index + tokenStr.Length;
                while (index < endIndex) {
                    if (code[index] != '\n') {
                        pos++;
                    } else {
                        line++;
                        pos = 0;
                    }
                    index++;
                }
                tokenNode.Hiddens.Add(
                        new CstToken(
                                Code2XmlConstants.HiddenElementName,
                                code.Substring(startHiddenIndex, endHiddenIndex - startHiddenIndex),
                                Code2XmlConstants.DefaultHiddenRuleId,
                                new CodeRange(startHiddenLocation, startTokenLocation)));
                startHiddenLocation = new CodeLocation(line, pos); // as endTokenLocation
                token.Range = new CodeRange(startTokenLocation, startHiddenLocation);
            }

            {
                var startHiddenIndex = index;
                var endIndex = code.Length;
                while (index < endIndex) {
                    if (code[index] != '\n') {
                        pos++;
                    } else {
                        line++;
                        pos = 0;
                    }
                    index++;
                }
                var endHiddenLocation = new CodeLocation(line, pos);
                var token = new CstToken(
                        Code2XmlConstants.EofTokenName, String.Empty, Code2XmlConstants.EofRuleId,
                        new CodeRange(endHiddenLocation, endHiddenLocation));
                var eofNode = new CstNode(token);
                eofNode.Hiddens.Add(
                        new CstToken(
                                Code2XmlConstants.HiddenElementName,
                                code.Substring(startHiddenIndex, index - startHiddenIndex),
                                Code2XmlConstants.DefaultHiddenRuleId,
                                new CodeRange(startHiddenLocation, endHiddenLocation)));
                tree.AddLast(eofNode);
            }
        }
 public void AddChild(CstNode parent, CstNode child, string id) {
     parent.AddLast(child);
     child.RuleId = id.Substring(child.Name.Length);
 }
 public void AddChild(CstNode parent, CstNode child, string id)
 {
     parent.AddLast(child);
     child.RuleId = id.Substring(child.Name.Length);
 }
        public void SetLocationInformation(CstNode tree, string code)
        {
            var line  = 1;
            var pos   = 0;
            var index = 0;
            var startHiddenLocation = new CodeLocation(line, pos);

            foreach (var tokenNode in tree.AllTokenNodes())
            {
                var token    = tokenNode.Token;
                var tokenStr = token.Text;
                if (tokenStr == string.Empty)   // e.g. NEWLINE in Python
                {
                    token.Range = new CodeRange(startHiddenLocation, startHiddenLocation);
                    continue;
                }
                var startHiddenIndex = index;
                var tokenChar        = tokenStr[0];
                while (code[index] != tokenChar ||
                       code.Substring(index, tokenStr.Length) != tokenStr)
                {
                    if (code[index] != '\n')
                    {
                        pos++;
                    }
                    else
                    {
                        line++;
                        pos = 0;
                    }
                    index++;
                }
                var endHiddenIndex     = index;
                var startTokenLocation = new CodeLocation(line, pos);
                var endIndex           = index + tokenStr.Length;
                while (index < endIndex)
                {
                    if (code[index] != '\n')
                    {
                        pos++;
                    }
                    else
                    {
                        line++;
                        pos = 0;
                    }
                    index++;
                }
                tokenNode.Hiddens.Add(
                    new CstToken(
                        Code2XmlConstants.HiddenElementName,
                        code.Substring(startHiddenIndex, endHiddenIndex - startHiddenIndex),
                        Code2XmlConstants.DefaultHiddenRuleId,
                        new CodeRange(startHiddenLocation, startTokenLocation)));
                startHiddenLocation = new CodeLocation(line, pos); // as endTokenLocation
                token.Range         = new CodeRange(startTokenLocation, startHiddenLocation);
            }

            {
                var startHiddenIndex = index;
                var endIndex         = code.Length;
                while (index < endIndex)
                {
                    if (code[index] != '\n')
                    {
                        pos++;
                    }
                    else
                    {
                        line++;
                        pos = 0;
                    }
                    index++;
                }
                var endHiddenLocation = new CodeLocation(line, pos);
                var token             = new CstToken(
                    Code2XmlConstants.EofTokenName, String.Empty, Code2XmlConstants.EofRuleId,
                    new CodeRange(endHiddenLocation, endHiddenLocation));
                var eofNode = new CstNode(token);
                eofNode.Hiddens.Add(
                    new CstToken(
                        Code2XmlConstants.HiddenElementName,
                        code.Substring(startHiddenIndex, index - startHiddenIndex),
                        Code2XmlConstants.DefaultHiddenRuleId,
                        new CodeRange(startHiddenLocation, endHiddenLocation)));
                tree.AddLast(eofNode);
            }
        }