-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_scanner.cpp
83 lines (67 loc) · 2.22 KB
/
test_scanner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "scanner.hpp"
#include <cassert>
int main()
{
Scanner s;
assert(s.empty());
assert(s.lexeme().empty());
assert(s.position().line == 0);
assert(s.position().column == 0);
assert(s.peek() == EOF);
assert(s.advance() == EOF);
s.assign("Abc\neeeffD ggh \t 358:");
assert(!s.empty());
assert(!s.backup());
// Match individual characters.
assert(1 == s.match('A'));
// Backup one character.
assert(s.position().line == 1);
assert(s.position().column == 1);
assert(s.backup());
assert(s.position().line == 0);
assert(s.position().column == 0);
assert('A' == s.advance());
assert(s.position().line == 1);
assert(s.position().column == 1);
assert(1 == s.match('b'));
assert(0 == s.match('C'));
assert(1 == s.match('c'));
// The lexeme holds matched characters.
assert(s.lexeme() == "Abc");
s.lexeme().clear();
// Backup over newline.
assert('\n' == s.advance());
assert(s.position().line == 1);
assert(s.position().column == 4);
assert(s.backup());
assert(s.position().line == 1);
assert(s.position().column == 3);
assert('\n' == s.advance());
assert(s.lexeme() == "\n");
s.lexeme().clear();
// Match one character from a set.
assert(1 == s.match("D ef"));
assert(s.position().line == 2);
assert(s.position().column == 1);
assert(s.backup());
assert(s.position().line == 1);
assert(s.position().column == 4);
assert('e' == s.advance());
assert(s.position().line == 2);
assert(s.position().column == 1);
// Use the chained 'many()' call to accept one or more characters.
assert(6 == s.match("D ef").many());
// Match a sequence of characters.
assert(0 == s.match_sequence("ggH"));
assert(0 == s.match_sequence("gghI"));
assert(3 == s.match_sequence("ggh"));
assert(s.lexeme() == "eeeffD ggh");
s.lexeme().clear();
// A matched sequence can be dropped (removed from the lexeme).
assert(3 == s.match(" \t").many().drop());
assert(1 == s.match([](int c){return isdigit(c);}));
assert(2 == s.match([](int c){return isdigit(c);}).many());
assert(s.lexeme() == "358");
assert(1 == s.match(":"));
assert(s.empty());
}