Skip to content

Commit

Permalink
Fix more issues with newlines in DSL parser
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Dec 21, 2023
1 parent fc4f366 commit d089e59
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 12 deletions.
11 changes: 6 additions & 5 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -31028,8 +31028,9 @@ const char* flecs_parse_arguments(
return NULL;
}

ecs_term_id_t *term_id = NULL;
ptr = ecs_parse_ws_eol(ptr);

ecs_term_id_t *term_id = NULL;
if (arg == 0) {
term_id = &term->src;
} else if (arg == 1) {
Expand All @@ -31047,7 +31048,7 @@ const char* flecs_parse_arguments(
return NULL;
}

ptr = ecs_parse_ws(ptr + 1);
ptr = ecs_parse_ws_eol(ptr + 1);
ptr = flecs_parse_term_flags(world, name, expr, (ptr - expr), ptr,
NULL, term_id, TOK_PAREN_CLOSE);
if (!ptr) {
Expand Down Expand Up @@ -31076,7 +31077,7 @@ const char* flecs_parse_arguments(
}

if (ptr[0] == TOK_AND) {
ptr = ecs_parse_ws(ptr + 1);
ptr = ecs_parse_ws_eol(ptr + 1);

if (term) {
term->id_flags = ECS_PAIR;
Expand Down Expand Up @@ -31141,7 +31142,7 @@ const char* flecs_parse_term(
if (!ptr) {
goto error;
}
ptr = ecs_parse_ws(ptr);
ptr = ecs_parse_ws_eol(ptr);
}

if (flecs_valid_operator_char(ptr[0])) {
Expand Down Expand Up @@ -31267,7 +31268,7 @@ const char* flecs_parse_term(
}

if (ptr[0] == TOK_PAREN_OPEN) {
ptr ++;
ptr = ecs_parse_ws_eol(ptr + 1);
if (ptr[0] == TOK_PAREN_CLOSE) {
term.src.flags = EcsIsEntity;
term.src.id = 0;
Expand Down
11 changes: 6 additions & 5 deletions src/addons/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,9 @@ const char* flecs_parse_arguments(
return NULL;
}

ecs_term_id_t *term_id = NULL;
ptr = ecs_parse_ws_eol(ptr);

ecs_term_id_t *term_id = NULL;
if (arg == 0) {
term_id = &term->src;
} else if (arg == 1) {
Expand All @@ -537,7 +538,7 @@ const char* flecs_parse_arguments(
return NULL;
}

ptr = ecs_parse_ws(ptr + 1);
ptr = ecs_parse_ws_eol(ptr + 1);
ptr = flecs_parse_term_flags(world, name, expr, (ptr - expr), ptr,
NULL, term_id, TOK_PAREN_CLOSE);
if (!ptr) {
Expand Down Expand Up @@ -566,7 +567,7 @@ const char* flecs_parse_arguments(
}

if (ptr[0] == TOK_AND) {
ptr = ecs_parse_ws(ptr + 1);
ptr = ecs_parse_ws_eol(ptr + 1);

if (term) {
term->id_flags = ECS_PAIR;
Expand Down Expand Up @@ -631,7 +632,7 @@ const char* flecs_parse_term(
if (!ptr) {
goto error;
}
ptr = ecs_parse_ws(ptr);
ptr = ecs_parse_ws_eol(ptr);
}

if (flecs_valid_operator_char(ptr[0])) {
Expand Down Expand Up @@ -757,7 +758,7 @@ const char* flecs_parse_term(
}

if (ptr[0] == TOK_PAREN_OPEN) {
ptr ++;
ptr = ecs_parse_ws_eol(ptr + 1);
if (ptr[0] == TOK_PAREN_CLOSE) {
term.src.flags = EcsIsEntity;
term.src.id = 0;
Expand Down
8 changes: 7 additions & 1 deletion test/addons/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,13 @@
"pair_3_args_this_tgt",
"pair_3_args_2_terms_this_tgt",
"pair_3_args_2_terms_this_tgt_implicit_this",
"cascade_desc"
"cascade_desc",
"newline_after_inout",
"newline_after_term_open",
"newline_after_term_src",
"newline_after_term_src_pair",
"newline_after_term_pair_comma",
"newline_after_term_pair_second"
]
}, {
"id": "Plecs",
Expand Down
142 changes: 142 additions & 0 deletions test/addons/src/Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -5738,3 +5738,145 @@ void Parser_pair_3_args_2_terms_this_tgt_implicit_this(void) {
ecs_fini(world);
}

void Parser_newline_after_inout(void) {
ecs_world_t *world = ecs_mini();

ECS_TAG(world, TagA);

ecs_filter_t f = ECS_FILTER_INIT;
test_assert(NULL != ecs_filter_init(world, &(ecs_filter_desc_t){
.storage = &f,
.expr = "[in]\nTagA($this)"
}));
test_int(filter_count(&f), 1);

ecs_term_t *terms = filter_terms(&f);
test_first(terms[0], TagA, EcsSelf|EcsIsEntity);
test_src(terms[0], EcsThis, EcsSelf|EcsUp|EcsIsVariable);
test_int(terms[0].oper, EcsAnd);
test_int(terms[0].inout, EcsIn);

ecs_filter_fini(&f);

ecs_fini(world);
}

void Parser_newline_after_term_open(void) {
ecs_world_t *world = ecs_mini();

ECS_TAG(world, TagA);

ecs_filter_t f = ECS_FILTER_INIT;
test_assert(NULL != ecs_filter_init(world, &(ecs_filter_desc_t){
.storage = &f,
.expr = "[in] TagA(\n$this)"
}));
test_int(filter_count(&f), 1);

ecs_term_t *terms = filter_terms(&f);
test_first(terms[0], TagA, EcsSelf|EcsIsEntity);
test_src(terms[0], EcsThis, EcsSelf|EcsUp|EcsIsVariable);
test_int(terms[0].oper, EcsAnd);
test_int(terms[0].inout, EcsIn);

ecs_filter_fini(&f);

ecs_fini(world);
}

void Parser_newline_after_term_src(void) {
ecs_world_t *world = ecs_mini();

ECS_TAG(world, TagA);

ecs_filter_t f = ECS_FILTER_INIT;
test_assert(NULL != ecs_filter_init(world, &(ecs_filter_desc_t){
.storage = &f,
.expr = "[in] TagA($this\n)"
}));
test_int(filter_count(&f), 1);

ecs_term_t *terms = filter_terms(&f);
test_first(terms[0], TagA, EcsSelf|EcsIsEntity);
test_src(terms[0], EcsThis, EcsSelf|EcsUp|EcsIsVariable);
test_int(terms[0].oper, EcsAnd);
test_int(terms[0].inout, EcsIn);

ecs_filter_fini(&f);

ecs_fini(world);
}

void Parser_newline_after_term_src_pair(void) {
ecs_world_t *world = ecs_mini();

ECS_TAG(world, TagA);
ECS_TAG(world, Tgt);

ecs_filter_t f = ECS_FILTER_INIT;
test_assert(NULL != ecs_filter_init(world, &(ecs_filter_desc_t){
.storage = &f,
.expr = "[in] TagA($this\n,Tgt)"
}));
test_int(filter_count(&f), 1);

ecs_term_t *terms = filter_terms(&f);
test_first(terms[0], TagA, EcsSelf|EcsIsEntity);
test_src(terms[0], EcsThis, EcsSelf|EcsUp|EcsIsVariable);
test_second(terms[0], Tgt, EcsSelf|EcsIsEntity);
test_int(terms[0].oper, EcsAnd);
test_int(terms[0].inout, EcsIn);

ecs_filter_fini(&f);

ecs_fini(world);
}

void Parser_newline_after_term_pair_comma(void) {
ecs_world_t *world = ecs_mini();

ECS_TAG(world, TagA);
ECS_TAG(world, Tgt);

ecs_filter_t f = ECS_FILTER_INIT;
test_assert(NULL != ecs_filter_init(world, &(ecs_filter_desc_t){
.storage = &f,
.expr = "[in] TagA($this,\nTgt)"
}));
test_int(filter_count(&f), 1);

ecs_term_t *terms = filter_terms(&f);
test_first(terms[0], TagA, EcsSelf|EcsIsEntity);
test_src(terms[0], EcsThis, EcsSelf|EcsUp|EcsIsVariable);
test_second(terms[0], Tgt, EcsSelf|EcsIsEntity);
test_int(terms[0].oper, EcsAnd);
test_int(terms[0].inout, EcsIn);

ecs_filter_fini(&f);

ecs_fini(world);
}

void Parser_newline_after_term_pair_second(void) {
ecs_world_t *world = ecs_mini();

ECS_TAG(world, TagA);
ECS_TAG(world, Tgt);

ecs_filter_t f = ECS_FILTER_INIT;
test_assert(NULL != ecs_filter_init(world, &(ecs_filter_desc_t){
.storage = &f,
.expr = "[in] TagA($this,Tgt\n)"
}));
test_int(filter_count(&f), 1);

ecs_term_t *terms = filter_terms(&f);
test_first(terms[0], TagA, EcsSelf|EcsIsEntity);
test_second(terms[0], Tgt, EcsSelf|EcsIsEntity);
test_int(terms[0].oper, EcsAnd);
test_int(terms[0].inout, EcsIn);

ecs_filter_fini(&f);

ecs_fini(world);
}
32 changes: 31 additions & 1 deletion test/addons/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ void Parser_pair_3_args_this_tgt(void);
void Parser_pair_3_args_2_terms_this_tgt(void);
void Parser_pair_3_args_2_terms_this_tgt_implicit_this(void);
void Parser_cascade_desc(void);
void Parser_newline_after_inout(void);
void Parser_newline_after_term_open(void);
void Parser_newline_after_term_src(void);
void Parser_newline_after_term_src_pair(void);
void Parser_newline_after_term_pair_comma(void);
void Parser_newline_after_term_pair_second(void);

// Testsuite 'Plecs'
void Plecs_null(void);
Expand Down Expand Up @@ -2751,6 +2757,30 @@ bake_test_case Parser_testcases[] = {
{
"cascade_desc",
Parser_cascade_desc
},
{
"newline_after_inout",
Parser_newline_after_inout
},
{
"newline_after_term_open",
Parser_newline_after_term_open
},
{
"newline_after_term_src",
Parser_newline_after_term_src
},
{
"newline_after_term_src_pair",
Parser_newline_after_term_src_pair
},
{
"newline_after_term_pair_comma",
Parser_newline_after_term_pair_comma
},
{
"newline_after_term_pair_second",
Parser_newline_after_term_pair_second
}
};

Expand Down Expand Up @@ -8776,7 +8806,7 @@ static bake_test_suite suites[] = {
"Parser",
NULL,
NULL,
237,
243,
Parser_testcases
},
{
Expand Down

0 comments on commit d089e59

Please sign in to comment.