996
|
1 /*
|
|
2 * BDSM2
|
|
3 * Author: Adam Sawicki
|
|
4 * http://www.regedit.risp.pl
|
|
5 * mailto:regedit@risp.pl
|
|
6 */
|
|
7 #ifndef SOURCE_H_
|
|
8 #define SOURCE_H_
|
|
9
|
|
10 class Source
|
|
11 {
|
|
12 public:
|
|
13 struct Iterator
|
|
14 {
|
|
15 size_t Index;
|
|
16 int Row;
|
|
17 int Col;
|
|
18
|
|
19 // return string in form of "index: #, row: #, col: #"
|
|
20 string ToString() const;
|
|
21 };
|
|
22
|
|
23 private:
|
|
24 const string *m_Src;
|
|
25 Iterator m_Index;
|
|
26 // Valid only if m_ErrIndexSaved
|
|
27 Iterator m_ErrIndex;
|
|
28 bool m_ErrIndexSaved;
|
|
29
|
|
30 // Read current character
|
|
31 // Cannot be end.
|
|
32 char Peek()
|
|
33 {
|
|
34 return (*m_Src)[m_Index.Index];
|
|
35 }
|
|
36 // Forward cursor one step
|
|
37 // Cannot be end.
|
|
38 void Forward();
|
|
39 // Read current character and step forward
|
|
40 // Cannot be end.
|
|
41 char Get();
|
|
42
|
|
43 public:
|
|
44 // Create view of given document as source for parsing
|
|
45 Source(const string *Src);
|
|
46
|
|
47 const Iterator & GetIterator()
|
|
48 {
|
|
49 return (m_ErrIndexSaved ? m_ErrIndex : m_Index);
|
|
50 }
|
|
51
|
|
52 // Return true of current position at the end
|
|
53 bool End()
|
|
54 {
|
|
55 return m_Index.Index >= m_Src->size();
|
|
56 }
|
|
57 // Read current character and step forward (safe)
|
|
58 // If at end, error.
|
|
59 char GetS();
|
|
60 // Skip all whitespaces and comments
|
|
61 void Skip();
|
|
62 // Freeze position for error reporting (real position will still moving)
|
|
63 void SaveErrPos() {
|
|
64 m_ErrIndex = m_Index;
|
|
65 m_ErrIndexSaved = true;
|
|
66 }
|
|
67 // Drop frozen position for error (real position will be reported in errors)
|
|
68 void DropErrPos() {
|
|
69 m_ErrIndexSaved = false;
|
|
70 }
|
|
71 // Write error and terminate process, include current (or frozen) index, row and col
|
|
72 void SrcError(const string &msg, ERR_PLACE place = ERR_GENERAL);
|
|
73 };
|
|
74
|
|
75 #endif
|