Language of your choice: library documentation
Helper class for mapping from indexes to SourcePos and back.
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.
|CharSource||A type that implements |
|Properties inherited from Loyc.Syntax.IIndexToLine|
|Gets the file name used in results returned by IndexToLine(int). More...|
|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)|
|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)|
|InternalList< int >||_lineOffsets = InternalList<int>.Empty|
|bool||_offsetsComplete = false|
|SourcePos||_startingPos = null|
|source||An immutable list of characters.|
|startingPos||Optional. The first character of |
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.
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.
Reinitializes the object (as though the constructor were called again).
Referenced by Loyc.Syntax.IndexPositionMapper< CharSource >.Reset().