1
1
import * as assert from 'assert' ;
2
2
3
- import { Builder } from 'llparse-builder' ;
3
+ import * as source from 'llparse-builder' ;
4
4
5
5
import { Frontend , node } from '../src/frontend' ;
6
6
import implementation from './fixtures/implementation' ;
7
7
import { Node } from './fixtures/implementation/node/base' ;
8
8
9
+ function checkNodes ( f : Frontend , root : source . node . Node ,
10
+ expected : ReadonlyArray < string > ) {
11
+ const fRoot = f . compile ( root , [ ] ) . root as Node < node . Node > ;
12
+
13
+ const out : string [ ] = [ ] ;
14
+ fRoot . build ( out ) ;
15
+
16
+ assert . deepStrictEqual ( out , expected ) ;
17
+ }
18
+
19
+ function checkResumptionTargets ( f : Frontend , expected : ReadonlyArray < string > ) {
20
+ const targets = Array . from ( f . getResumptionTargets ( ) ) . map ( ( t ) => {
21
+ return t . ref . id . name ;
22
+ } ) ;
23
+
24
+ assert . deepStrictEqual ( targets , expected ) ;
25
+ }
26
+
9
27
describe ( 'llparse-frontend' , ( ) => {
10
- let b : Builder ;
28
+ let b : source . Builder ;
11
29
let f : Frontend ;
12
30
beforeEach ( ( ) => {
13
- b = new Builder ( ) ;
31
+ b = new source . Builder ( ) ;
14
32
f = new Frontend ( 'llparse' , implementation ) ;
15
33
} ) ;
16
34
@@ -22,12 +40,7 @@ describe('llparse-frontend', () => {
22
40
root . match ( 'efg' , root ) ;
23
41
root . otherwise ( b . error ( 123 , 'hello' ) ) ;
24
42
25
- const fRoot = f . compile ( root , [ ] ) . root as Node < node . Node > ;
26
-
27
- const out : string [ ] = [ ] ;
28
- fRoot . build ( out ) ;
29
-
30
- assert . deepStrictEqual ( out , [
43
+ checkNodes ( f , root , [
31
44
'<Single name=llparse__n_root k97=llparse__n_root_1 ' +
32
45
'k101=llparse__n_root_3 otherwise-no_adv=llparse__n_error/>' ,
33
46
'<Single name=llparse__n_root_1 k98=llparse__n_root ' +
@@ -36,8 +49,16 @@ describe('llparse-frontend', () => {
36
49
'otherwise-no_adv=llparse__n_error/>' ,
37
50
'<ErrorNode name=llparse__n_error code=123 reason="hello"/>' ,
38
51
'<Sequence name=llparse__n_root_3 select="6667" ' +
52
+ 'edge=\"llparse__n_root\" ' +
39
53
'otherwise-no_adv=llparse__n_error/>' ,
40
54
] ) ;
55
+
56
+ checkResumptionTargets ( f , [
57
+ 'llparse__n_root' ,
58
+ 'llparse__n_root_1' ,
59
+ 'llparse__n_root_3' ,
60
+ 'llparse__n_root_2' ,
61
+ ] ) ;
41
62
} ) ;
42
63
43
64
it ( 'should do peephole optimization' , ( ) => {
@@ -53,13 +74,56 @@ describe('llparse-frontend', () => {
53
74
node1 . otherwise ( node2 ) ;
54
75
node2 . otherwise ( root ) ;
55
76
56
- const fRoot = f . compile ( root , [ ] ) . root as Node < node . Node > ;
77
+ checkNodes ( f , root , [
78
+ '<Empty name=llparse__n_b otherwise=llparse__n_b/>' ,
79
+ ] ) ;
57
80
58
- const out : string [ ] = [ ] ;
59
- fRoot . build ( out ) ;
81
+ checkResumptionTargets ( f , [
82
+ 'llparse__n_b' ,
83
+ ] ) ;
84
+ } ) ;
60
85
61
- assert . deepStrictEqual ( out , [
62
- '<Empty name=llparse__n_b otherwise=llparse__n_b/>' ,
86
+ it ( 'should generate proper resumption targets' , ( ) => {
87
+ b . property ( 'i64' , 'counter' ) ;
88
+
89
+ const root = b . node ( 'root' ) ;
90
+ const end = b . node ( 'end' ) ;
91
+ const store = b . invoke ( b . code . store ( 'counter' ) ) ;
92
+
93
+ root . select ( { a : 1 , b : 2 } , store ) ;
94
+ root . otherwise ( b . error ( 1 , 'okay' ) ) ;
95
+
96
+ store . otherwise ( end ) ;
97
+
98
+ end . match ( 'ohai' , root ) ;
99
+ end . match ( 'paus' , b . pause ( 1 , 'paused' ) . otherwise (
100
+ b . pause ( 2 , 'paused' ) . otherwise ( root ) ) ) ;
101
+ end . otherwise ( b . error ( 2 , 'ohai' ) ) ;
102
+
103
+ checkNodes ( f , root , [
104
+ '<Single name=llparse__n_root k97=llparse__n_invoke_store_counter ' +
105
+ 'k98=llparse__n_invoke_store_counter ' +
106
+ 'otherwise-no_adv=llparse__n_error_1/>' ,
107
+ '<Invoke name=llparse__n_invoke_store_counter ' +
108
+ 'otherwise-no_adv=llparse__n_end/>' ,
109
+ '<Single name=llparse__n_end k111=llparse__n_end_1 ' +
110
+ 'k112=llparse__n_end_2 otherwise-no_adv=llparse__n_error/>' ,
111
+ '<Sequence name=llparse__n_end_1 select="686169" ' +
112
+ 'edge="llparse__n_root" otherwise-no_adv=llparse__n_error/>' ,
113
+ '<ErrorNode name=llparse__n_error code=2 reason="ohai"/>' ,
114
+ '<Sequence name=llparse__n_end_2 select="617573" ' +
115
+ 'edge="llparse__n_pause" otherwise-no_adv=llparse__n_error/>' ,
116
+ '<Pause name=llparse__n_pause otherwise-no_adv=llparse__n_pause_1/>' ,
117
+ '<Pause name=llparse__n_pause_1 otherwise-no_adv=llparse__n_root/>' ,
118
+ '<ErrorNode name=llparse__n_error_1 code=1 reason="okay"/>' ,
119
+ ] ) ;
120
+
121
+ checkResumptionTargets ( f , [
122
+ 'llparse__n_root' ,
123
+ 'llparse__n_end' ,
124
+ 'llparse__n_end_1' ,
125
+ 'llparse__n_end_2' ,
126
+ 'llparse__n_pause_1' ,
63
127
] ) ;
64
128
} ) ;
65
129
} ) ;
0 commit comments