Enhanced C#
Language of your choice: library documentation
Properties | Public Member Functions | Protected Member Functions | Protected fields | List of all members
Loyc.Syntax.IndexPositionMapper< CharSource > Class Template Reference

Helper class for mapping from indexes to SourcePos and back. More...

Source file:
Inheritance diagram for Loyc.Syntax.IndexPositionMapper< CharSource >:
Loyc.Syntax.IIndexPositionMapper Loyc.Syntax.IIndexToLine Loyc.Syntax.SourceFile< CharSource > Loyc.Syntax.Lexing.LexerSourceFile< CharSource >


Helper class for mapping from indexes to SourcePos and back.

Synonym for IndexPositionMapper<IListSource<char>>.

This class's job is to keep track of the locations of line breaks in order to map from indices to SourcePos objects or vice versa. Converting indexes to SourcePos is commonly needed for error reporting; lexers, parsers and code analyzers typically use indexes (simple integers) internally, but must convert to SourcePos in order to communicate with the end user. Occasionally one may wish to convert in the reverse direction also (SourcePos to index).

Line breaks themselves are classified as being at the end of each line. So if the file is "Bob\nJoe", IndexToLine(3).Line == 1, not 2.

The outputs are immutable and this class assumes the input file never changes. However, this class is not entirly multi-thread-safe until the entire input file or string has been scanned, since the list of line breaks is built on-demand, without locking.

Template Parameters
CharSourceA type that implements IListSource(Char). Originally this class did not have any type parameters and dealt with IListSource(Char), but it was made generic so that one could wrap value types such as UString without boxing.
Type Constraints
CharSource :IListSource<char> 


string FileName [get]
int LineCount [get]
- Properties inherited from Loyc.Syntax.IIndexToLine
string FileName [get]
 Gets the file name used in results returned by IndexToLine(int). More...

Public Member Functions

 IndexPositionMapper (CharSource source, SourcePos startingPos=null)
 Initializes CharIndexPositionMapper. More...
 IndexPositionMapper (CharSource source, string fileName)
SourcePos IndexToLine (int index)
 Returns the position in a source file of the specified index. More...
int LineToIndex (int lineNo)
 Returns the index in a source file of the beginning of the specified line, where the first line is number 1, not 0. More...
int LineToIndex (LineAndCol pos)
 IndexPositionMapper (IListSource< char > source, SourcePos startingPos=null)
 IndexPositionMapper (IListSource< char > source, string fileName)

Protected Member Functions

void Reset (CharSource source, string fileName)
 Reinitializes the object (as though the constructor were called again). More...
void Reset (CharSource source, SourcePos startingPos=null)
SourcePos NewSourcePos (int Line, int PosInLine)
void ReadUntilAfter (int toIndex)
bool AdvanceAfterNextNewline (ref int index)

Protected fields

CharSource _source
InternalList< int > _lineOffsets = InternalList<int>.Empty
bool _offsetsComplete = false
SourcePos _startingPos = null

Constructor & Destructor Documentation

Loyc.Syntax.IndexPositionMapper< CharSource >.IndexPositionMapper ( CharSource  source,
SourcePos  startingPos = null 

Initializes CharIndexPositionMapper.

sourceAn immutable list of characters.
startingPosOptional. The first character of source will be considered to have the file name and line number specified by this object. If this is null, IndexToLine() will return a blank file name ("").

Member Function Documentation

SourcePos Loyc.Syntax.IndexPositionMapper< CharSource >.IndexToLine ( int  index)

Returns the position in a source file of the specified index.

If index is negative, this should return a SourcePos where Line and PosInLine are zero (signifying an unknown location). If index is beyond the end of the file, this should retun the final position in the file.

Implements Loyc.Syntax.IIndexToLine.

Referenced by Loyc.Syntax.Lexing.TokenListAsLexer.IndexToLine(), Loyc.Syntax.Lexing.BaseLexer< CharSrc >.IndexToLine(), and Loyc.Syntax.Lexing.TokensToTree.NextToken().

int Loyc.Syntax.IndexPositionMapper< CharSource >.LineToIndex ( int  lineNo)

Returns the index in a source file of the beginning of the specified line, where the first line is number 1, not 0.

If lineNo is zero, this method should return -1 (signifying an unknown location). If lineNo is larger than the largest line number, this method should return the index of end-of-file.

Implements Loyc.Syntax.IIndexPositionMapper.

void Loyc.Syntax.IndexPositionMapper< CharSource >.Reset ( CharSource  source,
string  fileName 

Reinitializes the object (as though the constructor were called again).

References Loyc.Syntax.IndexPositionMapper< CharSource >.Reset().

Referenced by Loyc.Syntax.IndexPositionMapper< CharSource >.Reset().