Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yaml: revise Ypath API #3842

Merged
merged 6 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main/subparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ extern subparser* getSubparserRunningBaseparser (void);
extern void chooseExclusiveSubparser (subparser *s, void *data);

extern subparser *getLanguageSubparser (langType sublang, bool including_none_crafted_parser);
extern langType getSubparserLanguage (subparser *s);

/* Interface for Subparsers */
#define RUN_DEFAULT_SUBPARSERS -1
Expand Down
1 change: 0 additions & 1 deletion main/subparser_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* FUNCTION PROTOTYPES
*/
extern subparser *getFirstSubparser(struct slaveControlBlock *controlBlock);
extern langType getSubparserLanguage (subparser *s);

/* A base parser doesn't have to call the following three functions.
The main part calls them internally. */
Expand Down
183 changes: 27 additions & 156 deletions parsers/ansibleplaybook.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
*/

#include "general.h" /* must always come first */
#include "debug.h"
#include "entry.h"
#include "kind.h"
#include "yaml.h"
#include "parse.h"
#include "subparser.h"

#include <stdio.h>

typedef enum {
K_PLAY
} ansiblePlaybookKind;
Expand All @@ -26,186 +23,61 @@ static kindDefinition AnsiblePlaybookKinds [] = {
{ true, 'p', "play", "plays" },
};

struct yamlBlockTypeStack {
yaml_token_type_t type;
int associatedCorkIndex;
struct yamlBlockTypeStack *next;
};

/* - name: "THE NAME" */
enum ansiblePlaybookPlayDetectingState {
DSTAT_PLAY_NAME_INITIAL,
DSTAT_PLAY_NAME_KEY,
DSTAT_PLAY_NAME_KEY_SCALAR,
DSTAT_PLAY_NAME_VALUE,
};


struct sAnsiblePlaybookSubparser {
yamlSubparser yaml;
struct yamlBlockTypeStack *type_stack;
enum ansiblePlaybookPlayDetectingState play_detection_state;
int nameIndex;
};

static void pushBlockType (struct sAnsiblePlaybookSubparser *ansible, yaml_token_type_t t)
{
struct yamlBlockTypeStack *s;

s = xMalloc (1, struct yamlBlockTypeStack);

s->next = ansible->type_stack;
ansible->type_stack = s;

s->type = t;
s->associatedCorkIndex = CORK_NIL;
}
static tagYpathTable ypathTables [] = {
{ "*/name",
YPATH_DSTAT_LAST_VALUE, K_PLAY, },
};

static void popBlockType (struct sAnsiblePlaybookSubparser *ansible,
yaml_token_t *token)
static void
findAnsiblePlaybookTags (void)
{
struct yamlBlockTypeStack *s;

s = ansible->type_stack;
ansible->type_stack = s->next;

s->next = NULL;
tagEntryInfo *tag = getEntryInCorkQueue (s->associatedCorkIndex);
if (tag && token)
attachYamlPosition (tag, token, true);

eFree (s);
scheduleRunningBaseparser (0);
}

static void popAllBlockType (struct sAnsiblePlaybookSubparser *ansible,
yaml_token_t *token)
static void inputStart(subparser *s)
{
while (ansible->type_stack)
popBlockType (ansible, token);
((struct sAnsiblePlaybookSubparser*)s)->nameIndex = CORK_NIL;
}

static bool stateStackMatch (struct yamlBlockTypeStack *stack,
yaml_token_type_t *expectation,
unsigned int length,
bool partial)
static void makeTagEntryCallbackViaYpath(yamlSubparser *s, int corkIndex)
{
if (length == 0)
{
if (stack == NULL)
return true;
else if (partial)
return true;
else
return false;
}

if (stack == NULL)
return false;

if (stack->type == expectation[0])
return stateStackMatch (stack->next, expectation + 1, length - 1, partial);
else
return false;
/* a mapping in a sequence */
if (ypathGetTypeStackDepth(s) == 2)
((struct sAnsiblePlaybookSubparser *)s)->nameIndex = corkIndex;
}

static bool scalarNeq (yaml_token_t *token, unsigned int len, const char* val)
static void leaveBlockCallback(yamlSubparser *s, yaml_token_t *token)
{
if ((token->data.scalar.length == len)
&& (strncmp (val, (char *)token->data.scalar.value, len) == 0))
return true;
else
return false;
}

static void ansiblePlaybookPlayStateMachine (struct sAnsiblePlaybookSubparser *ansible,
yaml_token_t *token)
{
yaml_token_type_t play_expect[] = {
YAML_BLOCK_MAPPING_START_TOKEN,
YAML_BLOCK_SEQUENCE_START_TOKEN,
};
struct sAnsiblePlaybookSubparser *ansible = (struct sAnsiblePlaybookSubparser *)s;

switch (token->type)
if (token
&& ansible->nameIndex != CORK_NIL
&& ypathGetTypeStackDepth(s) == 2)
{
case YAML_KEY_TOKEN:
if (stateStackMatch (ansible->type_stack,
play_expect, ARRAY_SIZE (play_expect),
false))
ansible->play_detection_state = DSTAT_PLAY_NAME_KEY;
else
ansible->play_detection_state = DSTAT_PLAY_NAME_INITIAL;
break;
case YAML_SCALAR_TOKEN:
if ((ansible->play_detection_state == DSTAT_PLAY_NAME_KEY)
&& scalarNeq (token, 4, "name"))
ansible->play_detection_state = DSTAT_PLAY_NAME_KEY_SCALAR;
else if (ansible->play_detection_state == DSTAT_PLAY_NAME_VALUE)
{
tagEntryInfo tag;
initTagEntry (&tag, (char *)token->data.scalar.value,
K_PLAY);
attachYamlPosition (&tag, token, false);

Assert (ansible->type_stack->associatedCorkIndex == CORK_NIL);
ansible->type_stack->associatedCorkIndex = makeTagEntry (&tag);
ansible->play_detection_state = DSTAT_PLAY_NAME_INITIAL;
}
else
ansible->play_detection_state = DSTAT_PLAY_NAME_INITIAL;
break;
case YAML_VALUE_TOKEN:
if (ansible->play_detection_state == DSTAT_PLAY_NAME_KEY_SCALAR)
ansible->play_detection_state = DSTAT_PLAY_NAME_VALUE;
else
ansible->play_detection_state = DSTAT_PLAY_NAME_INITIAL;
break;
default:
ansible->play_detection_state = DSTAT_PLAY_NAME_INITIAL;
break;
tagEntryInfo *tag = getEntryInCorkQueue (ansible->nameIndex);
if (tag)
attachYamlPosition (tag, token, true);
ansible->nameIndex = CORK_NIL;
}
}

static void newTokenCallback (yamlSubparser *s, yaml_token_t *token)
{
if (token->type == YAML_BLOCK_SEQUENCE_START_TOKEN
|| token->type == YAML_BLOCK_MAPPING_START_TOKEN)
pushBlockType ((struct sAnsiblePlaybookSubparser *)s, token->type);

ansiblePlaybookPlayStateMachine ((struct sAnsiblePlaybookSubparser *)s, token);

if (token->type == YAML_BLOCK_END_TOKEN)
popBlockType ((struct sAnsiblePlaybookSubparser *)s, token);
else if (token->type == YAML_STREAM_END_TOKEN)
popAllBlockType ((struct sAnsiblePlaybookSubparser *)s, token);
}

static void inputStart(subparser *s)
{
((struct sAnsiblePlaybookSubparser*)s)->play_detection_state = DSTAT_PLAY_NAME_INITIAL;
((struct sAnsiblePlaybookSubparser*)s)->type_stack = NULL;
}

static void inputEnd(subparser *s)
{
popAllBlockType ((struct sAnsiblePlaybookSubparser *)s, NULL);
Assert (((struct sAnsiblePlaybookSubparser*)s)->type_stack == NULL);
}

static void
findAnsiblePlaybookTags (void)
{
scheduleRunningBaseparser (0);
}

extern parserDefinition* AnsiblePlaybookParser (void)
{
static struct sAnsiblePlaybookSubparser ansiblePlaybookSubparser = {
.yaml = {
.subparser = {
.direction = SUBPARSER_BI_DIRECTION,
.inputStart = inputStart,
.inputEnd = inputEnd,
},
.newTokenNotfify = newTokenCallback
.ypathTables = ypathTables,
.ypathTableCount = ARRAY_SIZE (ypathTables),
.leaveBlockNotify = leaveBlockCallback,
.makeTagEntryNotifyViaYpath = makeTagEntryCallbackViaYpath,
},
};
static parserDependency dependencies [] = {
Expand All @@ -221,7 +93,6 @@ extern parserDefinition* AnsiblePlaybookParser (void)
def->kindTable = AnsiblePlaybookKinds;
def->kindCount = ARRAY_SIZE (AnsiblePlaybookKinds);
def->parser = findAnsiblePlaybookTags;
def->useCork = CORK_QUEUE;
return def;
}

Expand Down
Loading