BadScript 2
Loading...
Searching...
No Matches
BadSourcePosition.cs
Go to the documentation of this file.
1using BadScript2.IO;
2
3namespace BadScript2.Common;
4
8public class BadSourcePosition
9{
10 private string? m_PositionInfo;
11
12 private string? m_Text;
13
21 public BadSourcePosition(string? fileName, string source, int index, int length)
22 {
23 FileName = fileName?.Replace('\\', '/');
24 Source = source;
25 Index = index;
26 Length = length;
27 }
28
29
36 private BadSourcePosition(string fileName, int index, int length) : this(fileName,
37 BadFileSystem.ReadAllText(fileName),
38 index,
39 length
40 ) { }
41
45 public string? FileName { get; }
46
50 public string Source { get; }
51
55 public int Index { get; }
56
60 public int Length { get; }
61
65 public string Text => m_Text ??= GetExcerpt(0);
66
75 public static BadSourcePosition Create(string fileName, string source, int index, int length)
76 {
77 return new BadSourcePosition(fileName, source, index, length);
78 }
79
87 public static BadSourcePosition FromFile(string fileName, int index, int length)
88 {
89 return new BadSourcePosition(fileName, index, length);
90 }
91
99 public static BadSourcePosition FromSource(string source, int index, int length)
100 {
101 return new BadSourcePosition("<nofile>", source, index, length);
102 }
103
104
110 public string GetExcerpt(int len = 10)
111 {
112 return GetExcerpt(len, len);
113 }
114
115
122 public string GetExcerpt(int left, int right)
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 }
129
130 public int GetLine()
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 }
144
150 public string GetPositionInfo()
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 }
163
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 }
181}
Describes a specific position inside a source file.
string GetPositionInfo()
Returns position info. Format: file://[FileName] : Line [Line].
static BadSourcePosition Create(string fileName, string source, int index, int length)
Creates a new Source Position.
int Index
The Start Index of the Position.
string GetExcerpt(int left, int right)
Returns the excerpt of the source code.
BadSourcePosition(string? fileName, string source, int index, int length)
Constructor for a Source Position.
BadSourcePosition(string fileName, int index, int length)
Constructor for a Source Position.
string GetExcerpt(int len=10)
Returns the excerpt of the source code.
static BadSourcePosition FromFile(string fileName, int index, int length)
Creates a new Source Position.
string? FileName
The Filename of the Source Code.
static BadSourcePosition FromSource(string source, int index, int length)
Creates a new Source Position.
BadSourcePosition Combine(BadSourcePosition other)
Combines two Source Positions.
string Text
Returns the Position as a string.
int Length
The Length of the Position.
Public interface for the filesystem abstraction of the BadScript Engine.
Contains Shared Data Structures and Functionality.
Contains IO Implementation for the BadScript2 Runtime.