BGC Tools
Public Member Functions | Data Fields | Private Attributes
LightJson.Serialization.TextScanner Class Reference

Represents a text scanner that reads one character at a time. More...

Collaboration diagram for LightJson.Serialization.TextScanner:
Collaboration graph
[legend]

Public Member Functions

 TextScanner (TextReader reader)
 Initializes a new instance of TextScanner. More...
 
char Peek ()
 Reads the next character in the stream without changing the current position. More...
 
char Read ()
 Reads the next character in the stream, advancing the text position. More...
 
void SkipWhitespace ()
 Advances the scanner to next non-whitespace character. More...
 
void Assert (char next)
 Verifies that the given character matches the next character in the stream. If the characters do not match, an exception will be thrown. More...
 
void Assert (string next)
 Verifies that the given string matches the next characters in the stream. If the strings do not match, an exception will be thrown. More...
 

Data Fields

TextPosition Position => position
 The position of the scanner within the text. More...
 
bool CanRead => reader.Peek() != -1
 Indicates whether there are still characters to be read. More...
 

Private Attributes

TextReader reader
 
TextPosition position
 

Detailed Description

Represents a text scanner that reads one character at a time.

Definition at line 12 of file TextScanner.cs.

Constructor & Destructor Documentation

◆ TextScanner()

LightJson.Serialization.TextScanner.TextScanner ( TextReader  reader)
inline

Initializes a new instance of TextScanner.

Parameters
readerThe TextReader to read the text.

Definition at line 27 of file TextScanner.cs.

28  {
29  if (reader == null)
30  {
31  throw new ArgumentNullException(nameof(reader));
32  }
33 
34  this.reader = reader;
35  }

Member Function Documentation

◆ Assert() [1/2]

void LightJson.Serialization.TextScanner.Assert ( char  next)
inline

Verifies that the given character matches the next character in the stream. If the characters do not match, an exception will be thrown.

Parameters
nextThe expected character.

Definition at line 105 of file TextScanner.cs.

106  {
107  if (Peek() == next)
108  {
109  Read();
110  }
111  else
112  {
113  throw new JsonParseException(
114  message: $"Parser expected '{next}', found '{Peek()}'",
115  type: ErrorType.InvalidOrUnexpectedCharacter,
116  position: position);
117  }
118  }
char Peek()
Reads the next character in the stream without changing the current position.
Definition: TextScanner.cs:40
char Read()
Reads the next character in the stream, advancing the text position.
Definition: TextScanner.cs:57

◆ Assert() [2/2]

void LightJson.Serialization.TextScanner.Assert ( string  next)
inline

Verifies that the given string matches the next characters in the stream. If the strings do not match, an exception will be thrown.

Parameters
nextThe expected string.

Definition at line 125 of file TextScanner.cs.

References LightJson.Serialization.JsonParseException.Type.

126  {
127  try
128  {
129  for (int i = 0; i < next.Length; i += 1)
130  {
131  Assert(next[i]);
132  }
133  }
134  catch (JsonParseException e) when (e.Type == ErrorType.InvalidOrUnexpectedCharacter)
135  {
136  throw new JsonParseException(
137  message: $"Parser expected '{next}'",
138  type: ErrorType.InvalidOrUnexpectedCharacter,
139  position: position);
140  }
141  }
void Assert(char next)
Verifies that the given character matches the next character in the stream. If the characters do not ...
Definition: TextScanner.cs:105

◆ Peek()

char LightJson.Serialization.TextScanner.Peek ( )
inline

Reads the next character in the stream without changing the current position.

Definition at line 40 of file TextScanner.cs.

41  {
42  int next = reader.Peek();
43 
44  if (next == -1)
45  {
46  throw new JsonParseException(
47  type: ErrorType.IncompleteMessage,
49  }
50 
51  return (char)next;
52  }

◆ Read()

char LightJson.Serialization.TextScanner.Read ( )
inline

Reads the next character in the stream, advancing the text position.

Definition at line 57 of file TextScanner.cs.

References LightJson.Serialization.TextPosition.column, and LightJson.Serialization.TextPosition.line.

58  {
59  int next = reader.Read();
60 
61  if (next == -1)
62  {
63  throw new JsonParseException(
64  type: ErrorType.IncompleteMessage,
66  }
67 
68  switch (next)
69  {
70  case '\r':
71  // Normalize '\r\n' line encoding to '\n'.
72  if (reader.Peek() == '\n')
73  {
74  reader.Read();
75  }
76  goto case '\n';
77 
78  case '\n':
79  position.line += 1;
80  position.column = 0;
81  return '\n';
82 
83  default:
84  position.column += 1;
85  return (char)next;
86  }
87  }
long line
The line position, 0-based.
Definition: TextPosition.cs:14
long column
The column position, 0-based.
Definition: TextPosition.cs:11

◆ SkipWhitespace()

void LightJson.Serialization.TextScanner.SkipWhitespace ( )
inline

Advances the scanner to next non-whitespace character.

Definition at line 92 of file TextScanner.cs.

93  {
94  while (char.IsWhiteSpace(Peek()))
95  {
96  Read();
97  }
98  }
char Peek()
Reads the next character in the stream without changing the current position.
Definition: TextScanner.cs:40
char Read()
Reads the next character in the stream, advancing the text position.
Definition: TextScanner.cs:57

Field Documentation

◆ CanRead

bool LightJson.Serialization.TextScanner.CanRead => reader.Peek() != -1

Indicates whether there are still characters to be read.

Definition at line 21 of file TextScanner.cs.

◆ position

TextPosition LightJson.Serialization.TextScanner.position
private

Definition at line 15 of file TextScanner.cs.

◆ Position

TextPosition LightJson.Serialization.TextScanner.Position => position

The position of the scanner within the text.

Definition at line 18 of file TextScanner.cs.

◆ reader

TextReader LightJson.Serialization.TextScanner.reader
private

Definition at line 14 of file TextScanner.cs.


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