BadScript 2
Loading...
Searching...
No Matches
BadSourceReader.cs
Go to the documentation of this file.
2using BadScript2.IO;
3
8
12public class BadSourceReader
13{
14 private readonly int m_EndIndex;
15
19 private readonly string m_Source;
20
24 private readonly int m_StartIndex;
25
26
35 public BadSourceReader(string fileName, string source, int start, int end)
36 {
37 FileName = fileName;
38 m_Source = source;
39
40 if (start < 0 || start >= end)
41 {
42 throw new ArgumentOutOfRangeException(nameof(start));
43 }
44
45 if (end > source.Length)
46 {
47 throw new ArgumentOutOfRangeException(nameof(end));
48 }
49
50 m_StartIndex = start;
51 m_EndIndex = end;
52 CurrentIndex = start;
53 }
54
60 public BadSourceReader(string fileName, string source) : this(fileName, source, 0, source.Length) { }
61
65 public string Preview => IsEof() ? "END OF FILE" : Source.Substring(CurrentIndex);
66
70 public string Source => m_Source.Substring(m_StartIndex, m_EndIndex - m_StartIndex);
71
75 public string FileName { get; }
76
80 public int CurrentIndex { get; private set; }
81
85 public char CurrentChar => GetCurrentChar();
86
91 public void SetPosition(int index)
92 {
93 CurrentIndex = index;
94 }
95
101 public static BadSourceReader FromFile(string fileName)
102 {
103 return new BadSourceReader(fileName, BadFileSystem.ReadAllText(fileName));
104 }
105
111 public bool IsEof(int offset = 0)
112 {
113 return CurrentIndex + offset >= m_EndIndex && CurrentIndex + offset >= m_StartIndex;
114 }
115
122 {
123 return MakeSourcePosition(CurrentIndex, length);
124 }
125
132 public BadSourcePosition MakeSourcePosition(int index, int length)
133 {
134 return BadSourcePosition.Create(FileName, m_Source, index, length);
135 }
136
142 public char GetCurrentChar(int offset = 0)
143 {
144 return IsEof(offset) ? '\0' : m_Source[CurrentIndex + offset];
145 }
146
150 public void MoveNext()
151 {
152 CurrentIndex++;
153 }
154
161 public bool Is(char c, int offset = 0)
162 {
163 return GetCurrentChar(offset) == c;
164 }
165
172 public bool Is(string s, int offset = 0)
173 {
174 for (int i = 0; i < s.Length; i++)
175 {
176 if (!Is(s[i], offset + i))
177 {
178 return false;
179 }
180 }
181
182 return true;
183 }
184
190 public bool Is(params char[] chars)
191 {
192 return Is(0, chars);
193 }
194
200 public bool Is(params string[] s)
201 {
202 return Is(0, s);
203 }
204
211 public bool Is(int offset, params char[] chars)
212 {
213 return chars.Any(x => Is(x, offset));
214 }
215
222 public bool Is(int offset, params string[] s)
223 {
224 return s.Any(x => Is(x, offset));
225 }
226
227
234 public BadSourcePosition Eat(char c)
235 {
236 if (!Is(c))
237 {
238 throw new BadSourceReaderException(
239 $"Expected '{c}' but got '{(IsEof() ? "EOF" : GetCurrentChar())}'",
241 );
242 }
243
244 MoveNext();
245
246 return MakeSourcePosition(1);
247 }
248
255 public BadSourcePosition Eat(params char[] c)
256 {
257 if (!c.Any(x => Is(x)))
258 {
259 throw new BadSourceReaderException(
260 $"Expected '{string.Join("' or '", c)}' but got '{(IsEof() ? "EOF" : GetCurrentChar())}'",
262 );
263 }
264
265 MoveNext();
266
267 return MakeSourcePosition(1);
268 }
269
276 public BadSourcePosition Eat(string s)
277 {
278 int start = CurrentIndex;
279
280 for (int i = 0; i < s.Length; i++)
281 {
282 if (!Is(s[i]))
283 {
284 throw new BadSourceReaderException(
285 $"Expected '{s}' but got '{(IsEof() ? "EOF" : GetCurrentChar())}'",
286 MakeSourcePosition(start, i)
287 );
288 }
289
290 MoveNext();
291 }
292
293 return MakeSourcePosition(start, s.Length);
294 }
295
302 public BadSourcePosition Eat(params string[] s)
303 {
304 string? str = s.FirstOrDefault(x => Is(x));
305
306 if (str == null)
307 {
308 throw new BadSourceReaderException(
309 $"Expected '{string.Join("' or '", s)}' but got '{(IsEof() ? "EOF" : GetCurrentChar())}'",
311 );
312 }
313
314 return Eat(str);
315 }
316
321 public void Seek(char c)
322 {
323 while (!IsEof() && GetCurrentChar() != c)
324 {
325 MoveNext();
326 }
327 }
328
333 public void Seek(string s)
334 {
335 while (!IsEof() && !Is(s))
336 {
337 MoveNext();
338 }
339 }
340
345 public void Seek(params char[] c)
346 {
347 while (!IsEof() && !Is(c))
348 {
349 MoveNext();
350 }
351 }
352
357 public void Seek(params string[] s)
358 {
359 while (!IsEof() && !Is(s))
360 {
361 MoveNext();
362 }
363 }
364
369 public void Skip(char c)
370 {
371 if (Is(c))
372 {
373 MoveNext();
374 }
375 }
376
381 public void Skip(params char[] c)
382 {
383 bool exit = false;
384
385 while (!exit)
386 {
387 exit = true;
388
389 if (!c.Any(ch => Is(ch)))
390 {
391 continue;
392 }
393
394 MoveNext();
395 exit = false;
396 }
397 }
398}
Describes a specific position inside a source file.
static BadSourcePosition Create(string fileName, string source, int index, int length)
Creates a new Source Position.
Public interface for the filesystem abstraction of the BadScript Engine.
static string ReadAllText(this IFileSystem fileSystem, string path)
Gets Raised if the Reader encounters an Error.
Implements the Source Code Reader.
readonly string m_Source
The Source Code.
void Seek(params char[] c)
Skips over any character that is not equal to any of the specified characters.
void MoveNext()
Moves the Reader to the next character in the source code.
BadSourceReader(string fileName, string source, int start, int end)
Creates a new Source Code Reader.
bool Is(params char[] chars)
Returns true if any of the characters match the specified character.
bool Is(int offset, params string[] s)
Returns true if any of the strings match the specified String.
BadSourcePosition Eat(string s)
Asserts that the current String matches the specified String.
void SetPosition(int index)
Sets the Current Index of the Reader.
static BadSourceReader FromFile(string fileName)
Creates a new Source Code Reader from a File.
void Seek(params string[] s)
Skips over any character that is not equal to any of the specified strings.
bool Is(string s, int offset=0)
Returns true if the string matches the specified character.
void Skip(char c)
If the current character is equal to the specified character, it will be skipped.
char GetCurrentChar(int offset=0)
Returns the Current Character.
BadSourcePosition MakeSourcePosition(int length)
Creates a source position with the specified length and the current index of the reader.
void Seek(char c)
Skips over any character that is not equal to the specified character.
int CurrentIndex
The Current Index of the Reader.
void Seek(string s)
Skips over any string that is not equal to the specified string.
BadSourcePosition Eat(params char[] c)
Asserts that the current character matches one of the specified characters.
char CurrentChar
The Current Character of the Reader.
bool Is(params string[] s)
Returns true if any of the strings match the specified character.
BadSourcePosition Eat(params string[] s)
Asserts that the current string matches one of the specified strings.
void Skip(params char[] c)
If the current character is equal to any of the specified characters, it will be skipped.
BadSourceReader(string fileName, string source)
Creates a new Source Code Reader.
bool IsEof(int offset=0)
Returns true if the reader is at the end of the source code.
string FileName
The Filename of the Source Code.
BadSourcePosition Eat(char c)
Asserts that the current character matches the specified character.
BadSourcePosition MakeSourcePosition(int index, int length)
Creates a source position with the specified length and index.
string Preview
Preview of the Source Code.
bool Is(int offset, params char[] chars)
Returns true if any of the characters match the specified character.
bool Is(char c, int offset=0)
Returns true if the current character matches the specified character.
readonly int m_StartIndex
Start Index of the Source Code.
Contains Shared Data Structures and Functionality.
Contains IO Implementation for the BadScript2 Runtime.
Contains the Source Reader for the BadScript2 Language.