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 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); } }