fast_lexer.unit_test.anubis
5.05 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
read tools/unit_test.anubis
define String
format
(
AtEndOfInput a
) =
if a is
{
not_at_end_of_input then "not_at_end_of_input",
at_end_of_input then "at_end_of_input"
}.
define FastLexerOutput
test_fast_lexer
(
List(FastLexerState) lexer,
ByteArray input,
FastLexerLastAccepted last_accepted,
Int position,
Int start,
Word16 state
) =
if make_fast_lexer(lexer) is
{
unknown_state(n) then print("fast lexer unknown state: "+to_decimal(n)+"\n"); should_not_happen(ignored(0,0)),
too_many_states then print("too many states.\n"); should_not_happen(ignored(0,0)),
ok(fl) then with result = fl(input,last_accepted,position,start,state),
print("Result = ");
if result is
{
rejected(w,eoi,s,e) then
print("rejected("+to_decimal(w)+","
+format(eoi)+","
+abs_to_decimal(s)+","
+abs_to_decimal(e)
+")\n"),
accepted(w,eoi,s,e) then
print("accepted("+to_decimal(w)+","
+format(eoi)+","
+abs_to_decimal(s)+","
+abs_to_decimal(e)
+")\n"),
ignored(w,s) then
print("ignored("+to_decimal(w)+","
+abs_to_decimal(s)
+")\n")
};
result
}.
A lexer accepting "g+abu", "g+abuzo", and ignoring blanks
define List(FastLexerState)
lexer_1
=
[
/* state 0 */ rejecting([transition('g',1),transition(' ',7)]),
/* state 1 */ rejecting([transition('a',2),transition('g',1)]),
/* state 2 */ rejecting([transition('b',3)]),
/* state 3 */ rejecting([transition('u',4)]),
/* state 4 */ accepting([transition('z',5)]),
/* state 5 */ rejecting([transition('o',6)]),
/* state 6 */ accepting([ ]),
/* state 7 */ ignoring([ ])
].
define One
fast_lexer_test
(
UnitTestContext ut
) =
/* starting in state 0, position 0 */
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array("gab"),none,0,0,0),
rejected(3,at_end_of_input,0,3), "1");
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array("gabu"),none,0,0,0),
accepted(4,at_end_of_input,0,4), "2");
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array("ggggabu"),none,0,0,0),
accepted(4,at_end_of_input,0,7), "2bis");
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array("gabuz"),none,0,0,0),
accepted(5,at_end_of_input,0,4), "3");
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array("gmbuz"),none,0,0,0),
rejected(1,not_at_end_of_input,0,2),"4");
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array("gabuzo"),none,0,0,0),
accepted(6,at_end_of_input,0,6), "5");
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array("gabuzobof"),none,0,0,0),
accepted(6,not_at_end_of_input,0,6), "6");
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array("gggggabuzobof"),none,0,0,0),
accepted(6,not_at_end_of_input,0,10), "6bis");
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array(" gggabuzobof"),none,0,0,0),
accepted(6,not_at_end_of_input,2,10), "6ter");
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array(" "),none,0,0,0),
ignored(7,3), "7");
unique.
/* restarting from some other state (with or without an already accepted position) */
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array("bu"),none,bol,neol,0,2),
accepted(4,2,at_end_of_input), "7");
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array("gabuzfff"),last(4,4),bol,neol,5,5),
accepted(4,4,not_at_end_of_input), "8");
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array("gabuzomeu"),last(4,4),bol,neol,5,5),
accepted(6,6,not_at_end_of_input), "9");
/* testing bol and eol */
assertIsSame(ut,
test_fast_lexer(lexer_1,to_byte_array("meu"),none,bol,neol,0,0),
accepted(6,6,not_at_end_of_input), "9");
unique.
public define UnitTestSuite
make_fast_lexer_test_suite
=
ut_suite("predefined.fast_lexer",
[
ut_fixture("fast lexer", fast_lexer_test)
]).
global define One
fast_lexer_tests
(List(String) args) =
execute_tests([make_fast_lexer_test_suite],args).