BadScript 2
Loading...
Searching...
No Matches
BadScript2.Reader.BadSourceReader Class Reference

Implements the Source Code Reader. More...

Public Member Functions

 BadSourceReader (string fileName, string source, int start, int end)
 Creates a new Source Code Reader.
 
 BadSourceReader (string fileName, string source)
 Creates a new Source Code Reader.
 
void SetPosition (int index)
 Sets the Current Index of the Reader.
 
bool IsEof (int offset=0)
 Returns true if the reader is at the end of the source code.
 
BadSourcePosition MakeSourcePosition (int length)
 Creates a source position with the specified length and the current index of the reader.
 
BadSourcePosition MakeSourcePosition (int index, int length)
 Creates a source position with the specified length and index.
 
char GetCurrentChar (int offset=0)
 Returns the Current Character.
 
void MoveNext ()
 Moves the Reader to the next character in the source code.
 
bool Is (char c, int offset=0)
 Returns true if the current character matches the specified character.
 
bool Is (string s, int offset=0)
 Returns true if the string matches the specified character.
 
bool Is (params char[] chars)
 Returns true if any of the characters match the specified character.
 
bool Is (params string[] s)
 Returns true if any of the strings match the specified character.
 
bool Is (int offset, 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 (char c)
 Asserts that the current character matches the specified character.
 
BadSourcePosition Eat (params char[] c)
 Asserts that the current character matches one of the specified characters.
 
BadSourcePosition Eat (string s)
 Asserts that the current String matches the specified String.
 
BadSourcePosition Eat (params string[] s)
 Asserts that the current string matches one of the specified strings.
 
void Seek (char c)
 Skips over any character that is not equal to the specified character.
 
void Seek (string s)
 Skips over any string that is not equal to the specified string.
 
void Seek (params char[] c)
 Skips over any character that is not equal to any of the specified characters.
 
void Seek (params string[] s)
 Skips over any character that is not equal to any of the specified strings.
 
void Skip (char c)
 If the current character is equal to the specified character, it will be skipped.
 
void Skip (params char[] c)
 If the current character is equal to any of the specified characters, it will be skipped.
 

Static Public Member Functions

static BadSourceReader FromFile (string fileName)
 Creates a new Source Code Reader from a File.
 

Properties

string Preview [get]
 Preview of the Source Code.
 
string Source [get]
 The Source Code.
 
string FileName [get]
 The Filename of the Source Code.
 
int CurrentIndex [get, private set]
 The Current Index of the Reader.
 
char CurrentChar [get]
 The Current Character of the Reader.
 

Private Attributes

readonly int m_EndIndex
 
readonly string m_Source
 The Source Code.
 
readonly int m_StartIndex
 Start Index of the Source Code.
 

Detailed Description

Implements the Source Code Reader.

Definition at line 12 of file BadSourceReader.cs.

Constructor & Destructor Documentation

◆ BadSourceReader() [1/2]

BadScript2.Reader.BadSourceReader.BadSourceReader ( string  fileName,
string  source,
int  start,
int  end 
)

Creates a new Source Code Reader.

Parameters
fileNameFilename of the Source Code
sourceThe Source Code
startThe Start Index of the Source Code
endThe End Index of the Source Code
Exceptions
ArgumentOutOfRangeExceptionGets raised if the start or end index is invalid.

Definition at line 35 of file BadSourceReader.cs.

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 }
readonly string m_Source
The Source Code.
int CurrentIndex
The Current Index of the Reader.
string FileName
The Filename of the Source Code.
readonly int m_StartIndex
Start Index of the Source Code.

◆ BadSourceReader() [2/2]

BadScript2.Reader.BadSourceReader.BadSourceReader ( string  fileName,
string  source 
)

Creates a new Source Code Reader.

Parameters
fileNameThe Filename of the Source Code
sourceThe Source Code

Definition at line 60 of file BadSourceReader.cs.

60: this(fileName, source, 0, source.Length) { }

Member Function Documentation

◆ Eat() [1/4]

BadSourcePosition BadScript2.Reader.BadSourceReader.Eat ( char  c)

Asserts that the current character matches the specified character.

Parameters
cThe Character to be matched
Returns
The Source Position (Current Reader position with length 1) of the Character
Exceptions
BadSourceReaderExceptionGets raised if the character does not match the specified one

Definition at line 234 of file BadSourceReader.cs.

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 }
void MoveNext()
Moves the Reader to the next character in the source code.
BadSourcePosition MakeSourcePosition(int length)
Creates a source position with the specified length and the current index of the reader.
bool Is(char c, int offset=0)
Returns true if the current character matches the specified character.

◆ Eat() [2/4]

BadSourcePosition BadScript2.Reader.BadSourceReader.Eat ( params char[]  c)

Asserts that the current character matches one of the specified characters.

Parameters
cThe Characters to be matched
Returns
The Source Position (Current Reader position with length 1) of the Character
Exceptions
BadSourceReaderExceptionGets raised if the character does not match any of the specified ones

Definition at line 255 of file BadSourceReader.cs.

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 }

◆ Eat() [3/4]

BadSourcePosition BadScript2.Reader.BadSourceReader.Eat ( params string[]  s)

Asserts that the current string matches one of the specified strings.

Parameters
sThe strings to be matched
Returns
The Source Position (Current Reader position with length of the string) of the string
Exceptions
BadSourceReaderExceptionGets raised if the string does not match any of the specified ones

Definition at line 302 of file BadSourceReader.cs.

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 }
BadSourcePosition Eat(char c)
Asserts that the current character matches the specified character.

◆ Eat() [4/4]

BadSourcePosition BadScript2.Reader.BadSourceReader.Eat ( string  s)

Asserts that the current String matches the specified String.

Parameters
sThe String to be matched
Returns
The Source Position (Current Reader position with length of the string) of the Character
Exceptions
BadSourceReaderExceptionGets raised if the String does not match the specified one

Definition at line 276 of file BadSourceReader.cs.

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 }

◆ FromFile()

static BadSourceReader BadScript2.Reader.BadSourceReader.FromFile ( string  fileName)
static

Creates a new Source Code Reader from a File.

Parameters
fileNameThe File Name
Returns
BadSourceReader instance with the contents of the specified file.

Definition at line 101 of file BadSourceReader.cs.

102 {
103 return new BadSourceReader(fileName, BadFileSystem.ReadAllText(fileName));
104 }
Public interface for the filesystem abstraction of the BadScript Engine.
static string ReadAllText(this IFileSystem fileSystem, string path)
BadSourceReader(string fileName, string source, int start, int end)
Creates a new Source Code Reader.

◆ GetCurrentChar()

char BadScript2.Reader.BadSourceReader.GetCurrentChar ( int  offset = 0)

Returns the Current Character.

Parameters
offsetThe Offset from the Current Reader Position
Returns
Current Character. EOF if IsEOF(offset) equals true.

Definition at line 142 of file BadSourceReader.cs.

143 {
144 return IsEof(offset) ? '\0' : m_Source[CurrentIndex + offset];
145 }
bool IsEof(int offset=0)
Returns true if the reader is at the end of the source code.

◆ Is() [1/6]

bool BadScript2.Reader.BadSourceReader.Is ( char  c,
int  offset = 0 
)

Returns true if the current character matches the specified character.

Parameters
cThe Character to be matched.
offsetThe Offset from the Current Reader Position
Returns
True if the Character matches the current one.

Definition at line 161 of file BadSourceReader.cs.

162 {
163 return GetCurrentChar(offset) == c;
164 }
char GetCurrentChar(int offset=0)
Returns the Current Character.

◆ Is() [2/6]

bool BadScript2.Reader.BadSourceReader.Is ( int  offset,
params char[]  chars 
)

Returns true if any of the characters match the specified character.

Parameters
offsetThe Offset from the Current Reader Position
charsThe Characters to be matched.
Returns
True if any of the characters match the current one.

Definition at line 211 of file BadSourceReader.cs.

212 {
213 return chars.Any(x => Is(x, offset));
214 }

◆ Is() [3/6]

bool BadScript2.Reader.BadSourceReader.Is ( int  offset,
params string[]  s 
)

Returns true if any of the strings match the specified String.

Parameters
offsetThe Offset from the Current Reader Position
sThe strings to be matched.
Returns
True if any of the strings match the current one.

Definition at line 222 of file BadSourceReader.cs.

223 {
224 return s.Any(x => Is(x, offset));
225 }

◆ Is() [4/6]

bool BadScript2.Reader.BadSourceReader.Is ( params char[]  chars)

Returns true if any of the characters match the specified character.

Parameters
charsThe Characters to be matched.
Returns
True if any of the characters matches the current one.

Definition at line 190 of file BadSourceReader.cs.

191 {
192 return Is(0, chars);
193 }

◆ Is() [5/6]

bool BadScript2.Reader.BadSourceReader.Is ( params string[]  s)

Returns true if any of the strings match the specified character.

Parameters
sThe strings to be matched.
Returns
True if any of the strings matches the current one.

Definition at line 200 of file BadSourceReader.cs.

201 {
202 return Is(0, s);
203 }

◆ Is() [6/6]

bool BadScript2.Reader.BadSourceReader.Is ( string  s,
int  offset = 0 
)

Returns true if the string matches the specified character.

Parameters
sThe Character to be matched.
offsetThe Offset from the Current Reader Position
Returns
True if the string matches the current one.

Definition at line 172 of file BadSourceReader.cs.

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 }

◆ IsEof()

bool BadScript2.Reader.BadSourceReader.IsEof ( int  offset = 0)

Returns true if the reader is at the end of the source code.

Parameters
offsetThe Offset from the Current Reader Position
Returns
True if the Reader is at the end of the source code.

Definition at line 111 of file BadSourceReader.cs.

112 {
113 return CurrentIndex + offset >= m_EndIndex && CurrentIndex + offset >= m_StartIndex;
114 }

◆ MakeSourcePosition() [1/2]

BadSourcePosition BadScript2.Reader.BadSourceReader.MakeSourcePosition ( int  index,
int  length 
)

Creates a source position with the specified length and index.

Parameters
indexThe Start index
lengthThe Length of the Position
Returns
A new BadSourcePosition Instance.

Definition at line 132 of file BadSourceReader.cs.

133 {
134 return BadSourcePosition.Create(FileName, m_Source, index, length);
135 }
Describes a specific position inside a source file.
static BadSourcePosition Create(string fileName, string source, int index, int length)
Creates a new Source Position.

◆ MakeSourcePosition() [2/2]

BadSourcePosition BadScript2.Reader.BadSourceReader.MakeSourcePosition ( int  length)

Creates a source position with the specified length and the current index of the reader.

Parameters
lengthThe Length of the Position
Returns
A new BadSourcePosition Instance.

Definition at line 121 of file BadSourceReader.cs.

122 {
123 return MakeSourcePosition(CurrentIndex, length);
124 }

◆ MoveNext()

void BadScript2.Reader.BadSourceReader.MoveNext ( )

Moves the Reader to the next character in the source code.

Definition at line 150 of file BadSourceReader.cs.

151 {
152 CurrentIndex++;
153 }

◆ Seek() [1/4]

void BadScript2.Reader.BadSourceReader.Seek ( char  c)

Skips over any character that is not equal to the specified character.

Parameters
cThe character to be matched.

Definition at line 321 of file BadSourceReader.cs.

322 {
323 while (!IsEof() && GetCurrentChar() != c)
324 {
325 MoveNext();
326 }
327 }

◆ Seek() [2/4]

void BadScript2.Reader.BadSourceReader.Seek ( params char[]  c)

Skips over any character that is not equal to any of the specified characters.

Parameters
cThe characters to be matched.

Definition at line 345 of file BadSourceReader.cs.

346 {
347 while (!IsEof() && !Is(c))
348 {
349 MoveNext();
350 }
351 }

◆ Seek() [3/4]

void BadScript2.Reader.BadSourceReader.Seek ( params string[]  s)

Skips over any character that is not equal to any of the specified strings.

Parameters
sThe strings to be matched.

Definition at line 357 of file BadSourceReader.cs.

358 {
359 while (!IsEof() && !Is(s))
360 {
361 MoveNext();
362 }
363 }

◆ Seek() [4/4]

void BadScript2.Reader.BadSourceReader.Seek ( string  s)

Skips over any string that is not equal to the specified string.

Parameters
sThe string to be matched.

Definition at line 333 of file BadSourceReader.cs.

334 {
335 while (!IsEof() && !Is(s))
336 {
337 MoveNext();
338 }
339 }

◆ SetPosition()

void BadScript2.Reader.BadSourceReader.SetPosition ( int  index)

Sets the Current Index of the Reader.

Parameters
indexThe new Index

Definition at line 91 of file BadSourceReader.cs.

92 {
93 CurrentIndex = index;
94 }

◆ Skip() [1/2]

void BadScript2.Reader.BadSourceReader.Skip ( char  c)

If the current character is equal to the specified character, it will be skipped.

Parameters
cThe character to be matched.

Definition at line 369 of file BadSourceReader.cs.

370 {
371 if (Is(c))
372 {
373 MoveNext();
374 }
375 }

◆ Skip() [2/2]

void BadScript2.Reader.BadSourceReader.Skip ( params char[]  c)

If the current character is equal to any of the specified characters, it will be skipped.

Parameters
cThe characters to be matched.

Definition at line 381 of file BadSourceReader.cs.

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 }

Member Data Documentation

◆ m_EndIndex

readonly int BadScript2.Reader.BadSourceReader.m_EndIndex
private

Definition at line 14 of file BadSourceReader.cs.

◆ m_Source

readonly string BadScript2.Reader.BadSourceReader.m_Source
private

The Source Code.

Definition at line 19 of file BadSourceReader.cs.

◆ m_StartIndex

readonly int BadScript2.Reader.BadSourceReader.m_StartIndex
private

Start Index of the Source Code.

Definition at line 24 of file BadSourceReader.cs.

Property Documentation

◆ CurrentChar

char BadScript2.Reader.BadSourceReader.CurrentChar
get

The Current Character of the Reader.

Definition at line 85 of file BadSourceReader.cs.

◆ CurrentIndex

int BadScript2.Reader.BadSourceReader.CurrentIndex
getprivate set

The Current Index of the Reader.

Definition at line 80 of file BadSourceReader.cs.

80{ get; private set; }

◆ FileName

string BadScript2.Reader.BadSourceReader.FileName
get

The Filename of the Source Code.

Definition at line 75 of file BadSourceReader.cs.

75{ get; }

◆ Preview

string BadScript2.Reader.BadSourceReader.Preview
get

Preview of the Source Code.

Definition at line 65 of file BadSourceReader.cs.

◆ Source

string BadScript2.Reader.BadSourceReader.Source
get

The Source Code.

Definition at line 70 of file BadSourceReader.cs.


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