Skip to content

Commit

Permalink
Fix issue with parsing newlines in query strings
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Dec 21, 2023
1 parent b71cfb2 commit fc4f366
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 19 deletions.
20 changes: 12 additions & 8 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4779,7 +4779,7 @@ ecs_table_t *flecs_traverse_from_expr(
const char *ptr = expr;
if (ptr) {
ecs_term_t term = {0};
while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term, NULL))){
while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term, NULL, false))){
if (!ecs_term_is_initialized(&term)) {
break;
}
Expand Down Expand Up @@ -4842,7 +4842,7 @@ void flecs_defer_from_expr(
const char *ptr = expr;
if (ptr) {
ecs_term_t term = {0};
while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term, NULL))) {
while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term, NULL, false))) {
if (!ecs_term_is_initialized(&term)) {
break;
}
Expand Down Expand Up @@ -11318,7 +11318,7 @@ ecs_filter_t* ecs_filter_init(
}

while (ptr[0] &&
(ptr = ecs_parse_term(world, name, expr, ptr, &term, extra_args)))
(ptr = ecs_parse_term(world, name, expr, ptr, &term, extra_args, true)))
{
if (!ecs_term_is_initialized(&term)) {
break;
Expand Down Expand Up @@ -30869,7 +30869,6 @@ const char* flecs_parse_annotation(
ptr = ecs_parse_ws(ptr);

if (ptr[0] != TOK_BRACKET_CLOSE) {
printf("errr\n");
ecs_parser_error(name, sig, column, "expected ]");
return NULL;
}
Expand Down Expand Up @@ -31472,7 +31471,8 @@ char* ecs_parse_term(
const char *expr,
const char *ptr,
ecs_term_t *term,
ecs_term_id_t *extra_args)
ecs_term_id_t *extra_args,
bool allow_newline)
{
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_check(ptr != NULL, ECS_INVALID_PARAMETER, NULL);
Expand Down Expand Up @@ -31623,7 +31623,11 @@ char* ecs_parse_term(
term->id_flags = 0;
}

ptr = ecs_parse_ws(ptr);
if (allow_newline) {
ptr = ecs_parse_ws_eol(ptr);
} else {
ptr = ecs_parse_ws(ptr);
}

return ECS_CONST_CAST(char*, ptr);
error:
Expand Down Expand Up @@ -33346,7 +33350,7 @@ const char *plecs_parse_plecs_term(
decl_id = state->last_predicate;
}

ptr = ecs_parse_term(world, name, expr, ptr, &term, NULL);
ptr = ecs_parse_term(world, name, expr, ptr, &term, NULL, false);
if (!ptr) {
return NULL;
}
Expand Down Expand Up @@ -55679,7 +55683,7 @@ int ecs_meta_set_string(
ecs_id_t id = 0;
#ifdef FLECS_PARSER
ecs_term_t term = {0};
if (ecs_parse_term(cursor->world, NULL, value, value, &term, NULL)) {
if (ecs_parse_term(cursor->world, NULL, value, value, &term, NULL, false)) {
if (ecs_term_finalize(cursor->world, &term)) {
ecs_term_fini(&term);
goto error;
Expand Down
3 changes: 2 additions & 1 deletion flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15395,7 +15395,8 @@ char* ecs_parse_term(
const char *expr,
const char *ptr,
ecs_term_t *term_out,
ecs_term_id_t *extra_args);
ecs_term_id_t *extra_args,
bool allow_newline);

#ifdef __cplusplus
}
Expand Down
3 changes: 2 additions & 1 deletion include/flecs/addons/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ char* ecs_parse_term(
const char *expr,
const char *ptr,
ecs_term_t *term_out,
ecs_term_id_t *extra_args);
ecs_term_id_t *extra_args,
bool allow_newline);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion src/addons/meta/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ int ecs_meta_set_string(
ecs_id_t id = 0;
#ifdef FLECS_PARSER
ecs_term_t term = {0};
if (ecs_parse_term(cursor->world, NULL, value, value, &term, NULL)) {
if (ecs_parse_term(cursor->world, NULL, value, value, &term, NULL, false)) {
if (ecs_term_finalize(cursor->world, &term)) {
ecs_term_fini(&term);
goto error;
Expand Down
10 changes: 7 additions & 3 deletions src/addons/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ const char* flecs_parse_annotation(
ptr = ecs_parse_ws(ptr);

if (ptr[0] != TOK_BRACKET_CLOSE) {
printf("errr\n");
ecs_parser_error(name, sig, column, "expected ]");
return NULL;
}
Expand Down Expand Up @@ -962,7 +961,8 @@ char* ecs_parse_term(
const char *expr,
const char *ptr,
ecs_term_t *term,
ecs_term_id_t *extra_args)
ecs_term_id_t *extra_args,
bool allow_newline)
{
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_check(ptr != NULL, ECS_INVALID_PARAMETER, NULL);
Expand Down Expand Up @@ -1113,7 +1113,11 @@ char* ecs_parse_term(
term->id_flags = 0;
}

ptr = ecs_parse_ws(ptr);
if (allow_newline) {
ptr = ecs_parse_ws_eol(ptr);
} else {
ptr = ecs_parse_ws(ptr);
}

return ECS_CONST_CAST(char*, ptr);
error:
Expand Down
2 changes: 1 addition & 1 deletion src/addons/plecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,7 @@ const char *plecs_parse_plecs_term(
decl_id = state->last_predicate;
}

ptr = ecs_parse_term(world, name, expr, ptr, &term, NULL);
ptr = ecs_parse_term(world, name, expr, ptr, &term, NULL, false);
if (!ptr) {
return NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ ecs_table_t *flecs_traverse_from_expr(
const char *ptr = expr;
if (ptr) {
ecs_term_t term = {0};
while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term, NULL))){
while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term, NULL, false))){
if (!ecs_term_is_initialized(&term)) {
break;
}
Expand Down Expand Up @@ -1439,7 +1439,7 @@ void flecs_defer_from_expr(
const char *ptr = expr;
if (ptr) {
ecs_term_t term = {0};
while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term, NULL))) {
while (ptr[0] && (ptr = ecs_parse_term(world, name, expr, ptr, &term, NULL, false))) {
if (!ecs_term_is_initialized(&term)) {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ ecs_filter_t* ecs_filter_init(
}

while (ptr[0] &&
(ptr = ecs_parse_term(world, name, expr, ptr, &term, extra_args)))
(ptr = ecs_parse_term(world, name, expr, ptr, &term, extra_args, true)))
{
if (!ecs_term_is_initialized(&term)) {
break;
Expand Down
2 changes: 2 additions & 0 deletions test/addons/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@
"query_scope_unbalanced",
"query_not_scope",
"query_empty_scope",
"query_scope_newline_after_open",
"query_scope_newline_after_close",
"override_tag",
"override_pair",
"pair_3_args",
Expand Down
79 changes: 79 additions & 0 deletions test/addons/src/Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -5314,6 +5314,84 @@ void Parser_query_empty_scope(void) {
ecs_fini(world);
}

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

ECS_TAG(world, TagA);
ECS_TAG(world, TagB);

ecs_filter_t f = ECS_FILTER_INIT;
test_assert(NULL != ecs_filter_init(world, &(ecs_filter_desc_t){
.storage = &f,
.expr = "TagA, {\nTagB}"
}));
test_int(filter_count(&f), 4);

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, EcsInOutDefault);

test_first(terms[1], EcsScopeOpen, EcsSelf|EcsIsEntity);
test_src(terms[1], 0, EcsIsEntity);
test_int(terms[1].oper, EcsAnd);
test_int(terms[1].inout, EcsInOutNone);

test_first(terms[2], TagB, EcsSelf|EcsIsEntity);
test_src(terms[2], EcsThis, EcsSelf|EcsUp|EcsIsVariable);
test_int(terms[2].oper, EcsAnd);
test_int(terms[2].inout, EcsInOutDefault);

test_first(terms[3], EcsScopeClose, EcsSelf|EcsIsEntity);
test_src(terms[3], 0, EcsIsEntity);
test_int(terms[3].oper, EcsAnd);
test_int(terms[3].inout, EcsInOutNone);

ecs_filter_fini(&f);

ecs_fini(world);
}

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

ECS_TAG(world, TagA);
ECS_TAG(world, TagB);

ecs_filter_t f = ECS_FILTER_INIT;
test_assert(NULL != ecs_filter_init(world, &(ecs_filter_desc_t){
.storage = &f,
.expr = "TagA, {TagB\n}"
}));
test_int(filter_count(&f), 4);

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, EcsInOutDefault);

test_first(terms[1], EcsScopeOpen, EcsSelf|EcsIsEntity);
test_src(terms[1], 0, EcsIsEntity);
test_int(terms[1].oper, EcsAnd);
test_int(terms[1].inout, EcsInOutNone);

test_first(terms[2], TagB, EcsSelf|EcsIsEntity);
test_src(terms[2], EcsThis, EcsSelf|EcsUp|EcsIsVariable);
test_int(terms[2].oper, EcsAnd);
test_int(terms[2].inout, EcsInOutDefault);

test_first(terms[3], EcsScopeClose, EcsSelf|EcsIsEntity);
test_src(terms[3], 0, EcsIsEntity);
test_int(terms[3].oper, EcsAnd);
test_int(terms[3].inout, EcsInOutNone);

ecs_filter_fini(&f);

ecs_fini(world);
}

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

Expand Down Expand Up @@ -5659,3 +5737,4 @@ void Parser_pair_3_args_2_terms_this_tgt_implicit_this(void) {

ecs_fini(world);
}

12 changes: 11 additions & 1 deletion test/addons/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ void Parser_query_nested_scope_spaces(void);
void Parser_query_scope_unbalanced(void);
void Parser_query_not_scope(void);
void Parser_query_empty_scope(void);
void Parser_query_scope_newline_after_open(void);
void Parser_query_scope_newline_after_close(void);
void Parser_override_tag(void);
void Parser_override_pair(void);
void Parser_pair_3_args(void);
Expand Down Expand Up @@ -2698,6 +2700,14 @@ bake_test_case Parser_testcases[] = {
"query_empty_scope",
Parser_query_empty_scope
},
{
"query_scope_newline_after_open",
Parser_query_scope_newline_after_open
},
{
"query_scope_newline_after_close",
Parser_query_scope_newline_after_close
},
{
"override_tag",
Parser_override_tag
Expand Down Expand Up @@ -8766,7 +8776,7 @@ static bake_test_suite suites[] = {
"Parser",
NULL,
NULL,
235,
237,
Parser_testcases
},
{
Expand Down

0 comments on commit fc4f366

Please sign in to comment.