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.IHasFileName Loyc.Syntax.SourceFile< CharSource > Loyc.Syntax.Lexing.LexerSourceFile< CharSource >

Remarks

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> 

Properties

string? FileName [get]
 
int LineCount [get]
 
- Properties inherited from Loyc.Syntax.IHasFileName
string FileName [get]
 

Public Member Functions

 IndexPositionMapper (CharSource source, ILineColumnFile startingPos=null)
 Initializes CharIndexPositionMapper. More...
 
 IndexPositionMapper (CharSource source, string fileName)
 
LineColumnFile 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 (ILineAndColumn 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, ILineColumnFile startingPos=null)
 
LineColumnFile 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
 
ILineColumnFile _startingPos = null
 

Constructor & Destructor Documentation

◆ IndexPositionMapper()

Loyc.Syntax.IndexPositionMapper< CharSource >.IndexPositionMapper ( CharSource  source,
ILineColumnFile  startingPos = null 
)
inline

Initializes CharIndexPositionMapper.

Parameters
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 ("").

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

Member Function Documentation

◆ IndexToLine()

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

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(), and Loyc.Syntax.Lexing.BaseLexer< CharSrc >.IndexToLine().

◆ LineToIndex()

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

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.

◆ Reset()

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