Skip to content

Commit 6e5576a

Browse files
committed
AnsiblePlaybook: use the revised Ypath API
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent fe011ec commit 6e5576a

File tree

1 file changed

+27
-156
lines changed

1 file changed

+27
-156
lines changed

parsers/ansibleplaybook.c

Lines changed: 27 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99
*/
1010

1111
#include "general.h" /* must always come first */
12-
#include "debug.h"
1312
#include "entry.h"
1413
#include "kind.h"
1514
#include "yaml.h"
1615
#include "parse.h"
1716
#include "subparser.h"
1817

19-
#include <stdio.h>
20-
2118
typedef enum {
2219
K_PLAY
2320
} ansiblePlaybookKind;
@@ -26,186 +23,61 @@ static kindDefinition AnsiblePlaybookKinds [] = {
2623
{ true, 'p', "play", "plays" },
2724
};
2825

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-
4426
struct sAnsiblePlaybookSubparser {
4527
yamlSubparser yaml;
46-
struct yamlBlockTypeStack *type_stack;
47-
enum ansiblePlaybookPlayDetectingState play_detection_state;
28+
int nameIndex;
4829
};
4930

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+
};
6235

63-
static void popBlockType (struct sAnsiblePlaybookSubparser *ansible,
64-
yaml_token_t *token)
36+
static void
37+
findAnsiblePlaybookTags (void)
6538
{
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);
7740
}
7841

79-
static void popAllBlockType (struct sAnsiblePlaybookSubparser *ansible,
80-
yaml_token_t *token)
42+
static void inputStart(subparser *s)
8143
{
82-
while (ansible->type_stack)
83-
popBlockType (ansible, token);
44+
((struct sAnsiblePlaybookSubparser*)s)->nameIndex = CORK_NIL;
8445
}
8546

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)
9048
{
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;
10852
}
10953

110-
static bool scalarNeq (yaml_token_t *token, unsigned int len, const char* val)
54+
static void leaveBlockCallback(yamlSubparser *s, yaml_token_t *token)
11155
{
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;
12657

127-
switch (token->type)
58+
if (token
59+
&& ansible->nameIndex != CORK_NIL
60+
&& ypathGetTypeStackDepth(s) == 2)
12861
{
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;
16466
}
16567
}
16668

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-
19969
extern parserDefinition* AnsiblePlaybookParser (void)
20070
{
20171
static struct sAnsiblePlaybookSubparser ansiblePlaybookSubparser = {
20272
.yaml = {
20373
.subparser = {
20474
.direction = SUBPARSER_BI_DIRECTION,
20575
.inputStart = inputStart,
206-
.inputEnd = inputEnd,
20776
},
208-
.newTokenNotfify = newTokenCallback
77+
.ypathTables = ypathTables,
78+
.ypathTableCount = ARRAY_SIZE (ypathTables),
79+
.leaveBlockNotify = leaveBlockCallback,
80+
.makeTagEntryNotifyViaYpath = makeTagEntryCallbackViaYpath,
20981
},
21082
};
21183
static parserDependency dependencies [] = {
@@ -221,7 +93,6 @@ extern parserDefinition* AnsiblePlaybookParser (void)
22193
def->kindTable = AnsiblePlaybookKinds;
22294
def->kindCount = ARRAY_SIZE (AnsiblePlaybookKinds);
22395
def->parser = findAnsiblePlaybookTags;
224-
def->useCork = CORK_QUEUE;
22596
return def;
22697
}
22798

0 commit comments

Comments
 (0)