BadScript 2
Loading...
Searching...
No Matches
BadScript2.Common.BadSourcePosition Class Reference

Describes a specific position inside a source file. More...

Public Member Functions

 BadSourcePosition (string? fileName, string source, int index, int length)
 Constructor for a Source Position.
 
string GetExcerpt (int len=10)
 Returns the excerpt of the source code.
 
string GetExcerpt (int left, int right)
 Returns the excerpt of the source code.
 
string GetPositionInfo ()
 Returns position info. Format: file://[FileName] : Line [Line].
 
BadSourcePosition Combine (BadSourcePosition other)
 Combines two Source Positions.
 

Static Public Member Functions

static BadSourcePosition Create (string fileName, string source, int index, int length)
 Creates a new Source Position.
 
static BadSourcePosition FromFile (string fileName, int index, int length)
 Creates a new Source Position.
 
static BadSourcePosition FromSource (string source, int index, int length)
 Creates a new Source Position.
 

Properties

string? FileName [get]
 The Filename of the Source Code.
 
string Source [get]
 The Source Code.
 
int Index [get]
 The Start Index of the Position.
 
int Length [get]
 The Length of the Position.
 
string Text [get]
 Returns the Position as a string.
 

Private Member Functions

 BadSourcePosition (string fileName, int index, int length)
 Constructor for a Source Position.
 

Private Attributes

string? m_PositionInfo
 
string? m_Text
 

Detailed Description

Describes a specific position inside a source file.

Definition at line 8 of file BadSourcePosition.cs.

Constructor & Destructor Documentation

◆ BadSourcePosition() [1/2]

BadScript2.Common.BadSourcePosition.BadSourcePosition ( string?  fileName,
string  source,
int  index,
int  length 
)

Constructor for a Source Position.

Parameters
fileNameThe (optional but recommended) filename
sourceThe source code.
indexThe Start Index
lengthThe Length

Definition at line 21 of file BadSourcePosition.cs.

22 {
23 FileName = fileName?.Replace('\\', '/');
24 Source = source;
25 Index = index;
26 Length = length;
27 }
int Index
The Start Index of the Position.
string? FileName
The Filename of the Source Code.
int Length
The Length of the Position.

◆ BadSourcePosition() [2/2]

BadScript2.Common.BadSourcePosition.BadSourcePosition ( string  fileName,
int  index,
int  length 
)
private

Constructor for a Source Position.

Parameters
fileNameThe filename
indexThe Start Index
lengthThe Length

Definition at line 36 of file BadSourcePosition.cs.

36 : this(
37 fileName,
38 BadFileSystem.ReadAllText(fileName),
39 index,
40 length
41 ) { }
Public interface for the filesystem abstraction of the BadScript Engine.
static string ReadAllText(this IFileSystem fileSystem, string path)

Member Function Documentation

◆ Combine()

BadSourcePosition BadScript2.Common.BadSourcePosition.Combine ( BadSourcePosition  other)

Combines two Source Positions.

Parameters
otherThe Other position
Returns
Combined Source Position
Exceptions
InvalidOperationExceptionGets raised if the filenames do not match

Definition at line 164 of file BadSourcePosition.cs.

165 {
166 if (FileName != other.FileName && Source != other.Source)
167 {
168 throw new InvalidOperationException("Cannot combine positions from different sources");
169 }
170
171 return Index < other.Index
172 ? new BadSourcePosition(FileName, Source, Index, other.Index + other.Length - Index)
173 : new BadSourcePosition(FileName, Source, other.Index, Index + Length - other.Index);
174 }
BadSourcePosition(string? fileName, string source, int index, int length)
Constructor for a Source Position.

◆ Create()

static BadSourcePosition BadScript2.Common.BadSourcePosition.Create ( string  fileName,
string  source,
int  index,
int  length 
)
static

Creates a new Source Position.

Parameters
fileNameThe (optional but recommended) filename
sourceThe source code.
indexThe Start Index
lengthThe Length
Returns
Created SourcePosition

Definition at line 76 of file BadSourcePosition.cs.

77 {
78 return new BadSourcePosition(fileName, source, index, length);
79 }

◆ FromFile()

static BadSourcePosition BadScript2.Common.BadSourcePosition.FromFile ( string  fileName,
int  index,
int  length 
)
static

Creates a new Source Position.

Parameters
fileNameThe filename
indexThe Start Index
lengthThe Length
Returns
Created SourcePosition

Definition at line 88 of file BadSourcePosition.cs.

89 {
90 return new BadSourcePosition(fileName, index, length);
91 }

◆ FromSource()

static BadSourcePosition BadScript2.Common.BadSourcePosition.FromSource ( string  source,
int  index,
int  length 
)
static

Creates a new Source Position.

Parameters
sourceThe source code.
indexThe Start Index
lengthThe Length
Returns
Created SourcePosition

Definition at line 100 of file BadSourcePosition.cs.

101 {
102 return new BadSourcePosition("<nofile>", source, index, length);
103 }

◆ GetExcerpt() [1/2]

string BadScript2.Common.BadSourcePosition.GetExcerpt ( int  left,
int  right 
)

Returns the excerpt of the source code.

Parameters
leftThe additional Characters before the excerpt
rightThe additional Characters after the excerpt
Returns
String Excerpt

Definition at line 123 of file BadSourcePosition.cs.

124 {
125 int start = Math.Max(0, Index - left);
126 int end = Math.Min(Source.Length, Index + Length + right);
127
128 return Source.Substring(start, end - start);
129 }

◆ GetExcerpt() [2/2]

string BadScript2.Common.BadSourcePosition.GetExcerpt ( int  len = 10)

Returns the excerpt of the source code.

Parameters
lenThe additional Characters before and after the excerpt
Returns
String Excerpt

Definition at line 111 of file BadSourcePosition.cs.

112 {
113 return GetExcerpt(len, len);
114 }
string GetExcerpt(int len=10)
Returns the excerpt of the source code.

◆ GetPositionInfo()

string BadScript2.Common.BadSourcePosition.GetPositionInfo ( )

Returns position info. Format: file://[FileName] : Line [Line].

Returns
String Representation

Definition at line 136 of file BadSourcePosition.cs.

137 {
138 if (m_PositionInfo != null)
139 {
140 return m_PositionInfo;
141 }
142
143 int line = 1;
144
145 for (int i = 0; i < Index; i++)
146 {
147 if (Source[i] == '\n')
148 {
149 line++;
150 }
151 }
152
153 m_PositionInfo = $"file://{FileName} : Line {line}";
154
155 return m_PositionInfo;
156 }

Member Data Documentation

◆ m_PositionInfo

string? BadScript2.Common.BadSourcePosition.m_PositionInfo
private

Definition at line 10 of file BadSourcePosition.cs.

◆ m_Text

string? BadScript2.Common.BadSourcePosition.m_Text
private

Definition at line 12 of file BadSourcePosition.cs.

Property Documentation

◆ FileName

string? BadScript2.Common.BadSourcePosition.FileName
get

The Filename of the Source Code.

Definition at line 46 of file BadSourcePosition.cs.

46{ get; }

◆ Index

int BadScript2.Common.BadSourcePosition.Index
get

The Start Index of the Position.

Definition at line 56 of file BadSourcePosition.cs.

56{ get; }

◆ Length

int BadScript2.Common.BadSourcePosition.Length
get

The Length of the Position.

Definition at line 61 of file BadSourcePosition.cs.

61{ get; }

◆ Source

string BadScript2.Common.BadSourcePosition.Source
get

The Source Code.

Definition at line 51 of file BadSourcePosition.cs.

51{ get; }

◆ Text

string BadScript2.Common.BadSourcePosition.Text
get

Returns the Position as a string.

Definition at line 66 of file BadSourcePosition.cs.


The documentation for this class was generated from the following file: