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(
37 fileName,
38 BadFileSystem.ReadAllText(fileName),
39 index,
40 length
41 ) { }
42
46 public string? FileName { get; }
47
51 public string Source { get; }
52
56 public int Index { get; }
57
61 public int Length { get; }
62
66 public string Text => m_Text ??= GetExcerpt(0);
67
76 public static BadSourcePosition Create(string fileName, string source, int index, int length)
77 {
78 return new BadSourcePosition(fileName, source, index, length);
79 }
80
88 public static BadSourcePosition FromFile(string fileName, int index, int length)
89 {
90 return new BadSourcePosition(fileName, index, length);
91 }
92
100 public static BadSourcePosition FromSource(string source, int index, int length)
101 {
102 return new BadSourcePosition("<nofile>", source, index, length);
103 }
104
105
111 public string GetExcerpt(int len = 10)
112 {
113 return GetExcerpt(len, len);
114 }
115
116
123 public string GetExcerpt(int left, int right)
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 }
130
136 public string GetPositionInfo()
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 }
157
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 }
175}
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.