9
9
*/
10
10
11
11
#include "general.h" /* must always come first */
12
- #include "debug.h"
13
12
#include "entry.h"
14
13
#include "kind.h"
15
14
#include "yaml.h"
16
15
#include "parse.h"
17
16
#include "subparser.h"
18
17
19
- #include <stdio.h>
20
-
21
18
typedef enum {
22
19
K_PLAY
23
20
} ansiblePlaybookKind ;
@@ -26,186 +23,61 @@ static kindDefinition AnsiblePlaybookKinds [] = {
26
23
{ true, 'p' , "play" , "plays" },
27
24
};
28
25
29
- struct yamlBlockTypeStack {
30
- yaml_token_type_t type ;
31
- int associatedCorkIndex ;
32
- struct yamlBlockTypeStack * next ;
33
- };
34
-
35
- /* - name: "THE NAME" */
36
- enum ansiblePlaybookPlayDetectingState {
37
- DSTAT_PLAY_NAME_INITIAL ,
38
- DSTAT_PLAY_NAME_KEY ,
39
- DSTAT_PLAY_NAME_KEY_SCALAR ,
40
- DSTAT_PLAY_NAME_VALUE ,
41
- };
42
-
43
-
44
26
struct sAnsiblePlaybookSubparser {
45
27
yamlSubparser yaml ;
46
- struct yamlBlockTypeStack * type_stack ;
47
- enum ansiblePlaybookPlayDetectingState play_detection_state ;
28
+ int nameIndex ;
48
29
};
49
30
50
- static void pushBlockType (struct sAnsiblePlaybookSubparser * ansible , yaml_token_type_t t )
51
- {
52
- struct yamlBlockTypeStack * s ;
53
-
54
- s = xMalloc (1 , struct yamlBlockTypeStack );
55
-
56
- s -> next = ansible -> type_stack ;
57
- ansible -> type_stack = s ;
58
-
59
- s -> type = t ;
60
- s -> associatedCorkIndex = CORK_NIL ;
61
- }
31
+ static tagYpathTable ypathTables [] = {
32
+ { "*/name" ,
33
+ YPATH_DSTAT_LAST_VALUE , K_PLAY , },
34
+ };
62
35
63
- static void popBlockType ( struct sAnsiblePlaybookSubparser * ansible ,
64
- yaml_token_t * token )
36
+ static void
37
+ findAnsiblePlaybookTags ( void )
65
38
{
66
- struct yamlBlockTypeStack * s ;
67
-
68
- s = ansible -> type_stack ;
69
- ansible -> type_stack = s -> next ;
70
-
71
- s -> next = NULL ;
72
- tagEntryInfo * tag = getEntryInCorkQueue (s -> associatedCorkIndex );
73
- if (tag && token )
74
- attachYamlPosition (tag , token , true);
75
-
76
- eFree (s );
39
+ scheduleRunningBaseparser (0 );
77
40
}
78
41
79
- static void popAllBlockType (struct sAnsiblePlaybookSubparser * ansible ,
80
- yaml_token_t * token )
42
+ static void inputStart (subparser * s )
81
43
{
82
- while (ansible -> type_stack )
83
- popBlockType (ansible , token );
44
+ ((struct sAnsiblePlaybookSubparser * )s )-> nameIndex = CORK_NIL ;
84
45
}
85
46
86
- static bool stateStackMatch (struct yamlBlockTypeStack * stack ,
87
- yaml_token_type_t * expectation ,
88
- unsigned int length ,
89
- bool partial )
47
+ static void makeTagEntryCallbackViaYpath (yamlSubparser * s , int corkIndex )
90
48
{
91
- if (length == 0 )
92
- {
93
- if (stack == NULL )
94
- return true;
95
- else if (partial )
96
- return true;
97
- else
98
- return false;
99
- }
100
-
101
- if (stack == NULL )
102
- return false;
103
-
104
- if (stack -> type == expectation [0 ])
105
- return stateStackMatch (stack -> next , expectation + 1 , length - 1 , partial );
106
- else
107
- return false;
49
+ /* a mapping in a sequence */
50
+ if (ypathGetTypeStackDepth (s ) == 2 )
51
+ ((struct sAnsiblePlaybookSubparser * )s )-> nameIndex = corkIndex ;
108
52
}
109
53
110
- static bool scalarNeq ( yaml_token_t * token , unsigned int len , const char * val )
54
+ static void leaveBlockCallback ( yamlSubparser * s , yaml_token_t * token )
111
55
{
112
- if ((token -> data .scalar .length == len )
113
- && (strncmp (val , (char * )token -> data .scalar .value , len ) == 0 ))
114
- return true;
115
- else
116
- return false;
117
- }
118
-
119
- static void ansiblePlaybookPlayStateMachine (struct sAnsiblePlaybookSubparser * ansible ,
120
- yaml_token_t * token )
121
- {
122
- yaml_token_type_t play_expect [] = {
123
- YAML_BLOCK_MAPPING_START_TOKEN ,
124
- YAML_BLOCK_SEQUENCE_START_TOKEN ,
125
- };
56
+ struct sAnsiblePlaybookSubparser * ansible = (struct sAnsiblePlaybookSubparser * )s ;
126
57
127
- switch (token -> type )
58
+ if (token
59
+ && ansible -> nameIndex != CORK_NIL
60
+ && ypathGetTypeStackDepth (s ) == 2 )
128
61
{
129
- case YAML_KEY_TOKEN :
130
- if (stateStackMatch (ansible -> type_stack ,
131
- play_expect , ARRAY_SIZE (play_expect ),
132
- false))
133
- ansible -> play_detection_state = DSTAT_PLAY_NAME_KEY ;
134
- else
135
- ansible -> play_detection_state = DSTAT_PLAY_NAME_INITIAL ;
136
- break ;
137
- case YAML_SCALAR_TOKEN :
138
- if ((ansible -> play_detection_state == DSTAT_PLAY_NAME_KEY )
139
- && scalarNeq (token , 4 , "name" ))
140
- ansible -> play_detection_state = DSTAT_PLAY_NAME_KEY_SCALAR ;
141
- else if (ansible -> play_detection_state == DSTAT_PLAY_NAME_VALUE )
142
- {
143
- tagEntryInfo tag ;
144
- initTagEntry (& tag , (char * )token -> data .scalar .value ,
145
- K_PLAY );
146
- attachYamlPosition (& tag , token , false);
147
-
148
- Assert (ansible -> type_stack -> associatedCorkIndex == CORK_NIL );
149
- ansible -> type_stack -> associatedCorkIndex = makeTagEntry (& tag );
150
- ansible -> play_detection_state = DSTAT_PLAY_NAME_INITIAL ;
151
- }
152
- else
153
- ansible -> play_detection_state = DSTAT_PLAY_NAME_INITIAL ;
154
- break ;
155
- case YAML_VALUE_TOKEN :
156
- if (ansible -> play_detection_state == DSTAT_PLAY_NAME_KEY_SCALAR )
157
- ansible -> play_detection_state = DSTAT_PLAY_NAME_VALUE ;
158
- else
159
- ansible -> play_detection_state = DSTAT_PLAY_NAME_INITIAL ;
160
- break ;
161
- default :
162
- ansible -> play_detection_state = DSTAT_PLAY_NAME_INITIAL ;
163
- break ;
62
+ tagEntryInfo * tag = getEntryInCorkQueue (ansible -> nameIndex );
63
+ if (tag )
64
+ attachYamlPosition (tag , token , true);
65
+ ansible -> nameIndex = CORK_NIL ;
164
66
}
165
67
}
166
68
167
- static void newTokenCallback (yamlSubparser * s , yaml_token_t * token )
168
- {
169
- if (token -> type == YAML_BLOCK_SEQUENCE_START_TOKEN
170
- || token -> type == YAML_BLOCK_MAPPING_START_TOKEN )
171
- pushBlockType ((struct sAnsiblePlaybookSubparser * )s , token -> type );
172
-
173
- ansiblePlaybookPlayStateMachine ((struct sAnsiblePlaybookSubparser * )s , token );
174
-
175
- if (token -> type == YAML_BLOCK_END_TOKEN )
176
- popBlockType ((struct sAnsiblePlaybookSubparser * )s , token );
177
- else if (token -> type == YAML_STREAM_END_TOKEN )
178
- popAllBlockType ((struct sAnsiblePlaybookSubparser * )s , token );
179
- }
180
-
181
- static void inputStart (subparser * s )
182
- {
183
- ((struct sAnsiblePlaybookSubparser * )s )-> play_detection_state = DSTAT_PLAY_NAME_INITIAL ;
184
- ((struct sAnsiblePlaybookSubparser * )s )-> type_stack = NULL ;
185
- }
186
-
187
- static void inputEnd (subparser * s )
188
- {
189
- popAllBlockType ((struct sAnsiblePlaybookSubparser * )s , NULL );
190
- Assert (((struct sAnsiblePlaybookSubparser * )s )-> type_stack == NULL );
191
- }
192
-
193
- static void
194
- findAnsiblePlaybookTags (void )
195
- {
196
- scheduleRunningBaseparser (0 );
197
- }
198
-
199
69
extern parserDefinition * AnsiblePlaybookParser (void )
200
70
{
201
71
static struct sAnsiblePlaybookSubparser ansiblePlaybookSubparser = {
202
72
.yaml = {
203
73
.subparser = {
204
74
.direction = SUBPARSER_BI_DIRECTION ,
205
75
.inputStart = inputStart ,
206
- .inputEnd = inputEnd ,
207
76
},
208
- .newTokenNotfify = newTokenCallback
77
+ .ypathTables = ypathTables ,
78
+ .ypathTableCount = ARRAY_SIZE (ypathTables ),
79
+ .leaveBlockNotify = leaveBlockCallback ,
80
+ .makeTagEntryNotifyViaYpath = makeTagEntryCallbackViaYpath ,
209
81
},
210
82
};
211
83
static parserDependency dependencies [] = {
@@ -221,7 +93,6 @@ extern parserDefinition* AnsiblePlaybookParser (void)
221
93
def -> kindTable = AnsiblePlaybookKinds ;
222
94
def -> kindCount = ARRAY_SIZE (AnsiblePlaybookKinds );
223
95
def -> parser = findAnsiblePlaybookTags ;
224
- def -> useCork = CORK_QUEUE ;
225
96
return def ;
226
97
}
227
98
0 commit comments