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.
 
int GetLine ()
 
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(fileName,
37 BadFileSystem.ReadAllText(fileName),
38 index,
39 length
40 ) { }
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 170 of file BadSourcePosition.cs.

171 {
172 if (FileName != other.FileName && Source != other.Source)
173 {
174 throw new InvalidOperationException("Cannot combine positions from different sources");
175 }
176
177 return Index < other.Index
178 ? new BadSourcePosition(FileName, Source, Index, other.Index + other.Length - Index)
179 : new BadSourcePosition(FileName, Source, other.Index, Index + Length - other.Index);
180 }
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 75 of file BadSourcePosition.cs.

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

◆ 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 87 of file BadSourcePosition.cs.

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

◆ 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 99 of file BadSourcePosition.cs.

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

◆ 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 122 of file BadSourcePosition.cs.

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

◆ 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 110 of file BadSourcePosition.cs.

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

◆ GetLine()

int BadScript2.Common.BadSourcePosition.GetLine ( )

Definition at line 130 of file BadSourcePosition.cs.

131 {
132 int line = 1;
133
134 for (int i = 0; i < Index; i++)
135 {
136 if (Source[i] == '\n')
137 {
138 line++;
139 }
140 }
141
142 return line;
143 }

◆ GetPositionInfo()

string BadScript2.Common.BadSourcePosition.GetPositionInfo ( )

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

Returns
String Representation

Definition at line 150 of file BadSourcePosition.cs.

151 {
152 if (m_PositionInfo != null)
153 {
154 return m_PositionInfo;
155 }
156
157 int line = GetLine();
158
159 m_PositionInfo = $"file://{FileName} : Line {line}";
160
161 return m_PositionInfo;
162 }

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 45 of file BadSourcePosition.cs.

45{ get; }

◆ Index

int BadScript2.Common.BadSourcePosition.Index
get

The Start Index of the Position.

Definition at line 55 of file BadSourcePosition.cs.

55{ get; }

◆ Length

int BadScript2.Common.BadSourcePosition.Length
get

The Length of the Position.

Definition at line 60 of file BadSourcePosition.cs.

60{ get; }

◆ Source

string BadScript2.Common.BadSourcePosition.Source
get

The Source Code.

Definition at line 50 of file BadSourcePosition.cs.

50{ get; }

◆ Text

string BadScript2.Common.BadSourcePosition.Text
get

Returns the Position as a string.

Definition at line 65 of file BadSourcePosition.cs.


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