@@ -7,6 +7,8 @@ import * as nunjucks from 'nunjucks';
7
7
import { injectable , inject } from 'inversify' ;
8
8
import { TYPES } from '@vulcan/core/containers' ;
9
9
10
+ const FINIAL_BUILDER_NAME = 'FINAL_BUILDER' ;
11
+
10
12
// TODO: temporary interface
11
13
export interface QueryBuilder {
12
14
count ( ) : QueryBuilder ;
@@ -29,37 +31,68 @@ export class ReqExtension implements NunjucksTagExtension {
29
31
30
32
public parse (
31
33
parser : nunjucks . parser . Parser ,
32
- nodes : typeof nunjucks . nodes
34
+ nodes : typeof nunjucks . nodes ,
35
+ lexer : typeof nunjucks . lexer
33
36
) : NunjucksTagExtensionParseResult {
34
- // get the tag token
35
- const token = parser . nextToken ( ) ;
36
-
37
- const args = parser . parseSignature ( null , true ) ;
38
- parser . advanceAfterBlockEnd ( token . value ) ;
39
-
40
- const requestQuery = parser . parseUntilBlocks ( 'endreq' ) ;
41
- parser . advanceAfterBlockEnd ( ) ;
37
+ // {% req var (main) %} body {% endreq %}
38
+ // consume req tag
39
+ const reqToken = parser . nextToken ( ) ;
40
+ // variable
41
+ let nextToken = parser . peekToken ( ) ;
42
+ if ( nextToken . type === lexer . TOKEN_BLOCK_END ) {
43
+ // {% req %}
44
+ parser . fail ( `Expected a variable` , nextToken . lineno , nextToken . colno ) ;
45
+ }
46
+ if ( nextToken . type !== lexer . TOKEN_SYMBOL ) {
47
+ parser . fail (
48
+ `Expected a symbol, but got ${ nextToken . type } ` ,
49
+ nextToken . lineno ,
50
+ nextToken . colno
51
+ ) ;
52
+ }
53
+ const variable = parser . parseExpression ( ) ;
42
54
43
- const variable = args . children [ 0 ] ;
44
- if ( ! variable ) {
45
- parser . fail ( `Expected a variable` , token . lineno , token . colno ) ;
55
+ // main denotation
56
+ nextToken = parser . peekToken ( ) ;
57
+ let mainBuilder = false ;
58
+ if ( nextToken . type !== lexer . TOKEN_BLOCK_END ) {
59
+ if ( nextToken . type !== lexer . TOKEN_SYMBOL || nextToken . value !== 'main' ) {
60
+ parser . fail (
61
+ `Expected a symbol "main"` ,
62
+ nextToken . lineno ,
63
+ nextToken . colno
64
+ ) ;
65
+ }
66
+ mainBuilder = true ;
67
+ // Consume this token (main)
68
+ parser . nextToken ( ) ;
46
69
}
47
- if ( ! ( variable instanceof nodes . Symbol ) ) {
70
+
71
+ const endToken = parser . nextToken ( ) ;
72
+ if ( endToken . type !== lexer . TOKEN_BLOCK_END ) {
48
73
parser . fail (
49
- `Expected a symbol , but got ${ variable . typename } ` ,
50
- variable . lineno ,
51
- variable . colno
74
+ `Expected a block end , but got ${ endToken . type } ` ,
75
+ endToken . lineno ,
76
+ endToken . colno
52
77
) ;
53
78
}
54
79
55
- const variableName = new nodes . Literal (
56
- variable . colno ,
57
- variable . lineno ,
58
- ( variable as nunjucks . nodes . Symbol ) . value
59
- ) ;
80
+ const requestQuery = parser . parseUntilBlocks ( 'endreq' ) ;
81
+ parser . advanceAfterBlockEnd ( ) ;
60
82
61
- const argsNodeToPass = new nodes . NodeList ( args . lineno , args . colno ) ;
62
- argsNodeToPass . addChild ( variableName ) ;
83
+ const argsNodeToPass = new nodes . NodeList ( reqToken . lineno , reqToken . colno ) ;
84
+ // variable name
85
+ argsNodeToPass . addChild (
86
+ new nodes . Literal (
87
+ variable . colno ,
88
+ variable . lineno ,
89
+ ( variable as nunjucks . nodes . Symbol ) . value
90
+ )
91
+ ) ;
92
+ // is main builder
93
+ argsNodeToPass . addChild (
94
+ new nodes . Literal ( variable . colno , variable . lineno , String ( mainBuilder ) )
95
+ ) ;
63
96
64
97
return {
65
98
argsNodeList : argsNodeToPass ,
@@ -69,12 +102,16 @@ export class ReqExtension implements NunjucksTagExtension {
69
102
70
103
public async run ( { context, args } : NunjucksTagExtensionRunOptions ) {
71
104
const name : string = args [ 0 ] ;
72
- const requestQuery : ( ) => string = args [ 1 ] ;
105
+ const requestQuery : ( ) => string = args [ 2 ] ;
73
106
const query = requestQuery ( )
74
107
. split ( / \r ? \n / )
75
108
. filter ( ( line ) => line . trim ( ) . length > 0 )
76
109
. join ( '\n' ) ;
77
110
const builder = await this . executor . createBuilder ( query ) ;
78
111
context . setVariable ( name , builder ) ;
112
+
113
+ if ( Boolean ( args [ 1 ] ) ) {
114
+ context . setVariable ( FINIAL_BUILDER_NAME , builder ) ;
115
+ }
79
116
}
80
117
}
0 commit comments