/// <summary> /// Expands the token text /// </summary> /// <param name="table">the table used when creating the token text</param> /// <returns>the token with the expanded</returns> public ExpandedTokenData <StringSegment> Expand(TokenTextTable table) { Contract.RequiresNotNull(table); return(!IsValid ? default(ExpandedTokenData <StringSegment>) : new ExpandedTokenData <StringSegment>(Path, Line, Position, Text.ToString(table))); }
/// <summary> /// Helper method that will try to map the position in the token string to the correct original line and position in the /// file. /// </summary> /// <remarks> /// It will start with the token as line and position info. It will scan through the actual text to find the lines and /// recomputes the location. /// </remarks> /// <param name="table">The table used when creating the token.</param> /// <param name="positionInToken">The actual character position int the text of the token.</param> /// <returns>An updated token with recomputed line information</returns> public TokenTextData UpdateLineInformationForPosition(TokenTextTable table, int positionInToken) { Contract.RequiresNotNull(table); Contract.Requires(positionInToken >= 0); Contract.Requires(positionInToken <= Text.GetLength(table)); int line = Line; int pos = Position; var text = Text.ToString(table); bool seenCr = false; int lastPos = Math.Min(positionInToken, Text.GetLength(table) - 1); for (int i = 0; i < lastPos; i++) { char c = text[i]; if (c == '\n') { if (!seenCr) { // in case of \n (not \r\n) start a new line. line++; pos = 0; } } else if (c == '\r') { // in case of \r start a new line. seenCr = true; line++; pos = 0; } else { seenCr = false; pos++; } } return(new TokenTextData(line, pos, Text)); }