Skip to content

Commit

Permalink
Fix erroneous attachment of comments to the right of tokens
Browse files Browse the repository at this point in the history
In order for a comment to attach to the right it must begin
at the same line as the token ends.

Signed-off-by: Pantelis Antoniou <[email protected]>
  • Loading branch information
pantoniou committed Nov 8, 2021
1 parent f332aaf commit c1fbdc6
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/lib/fy-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ int fy_scan_comment(struct fy_parser *fyp, struct fy_atom *handle, bool single_l
int fy_attach_comments_if_any(struct fy_parser *fyp, struct fy_token *fyt)
{
struct fy_atom *handle;
struct fy_mark fym;
int c, rc;

if (!fyp || !fyt)
Expand All @@ -930,7 +931,8 @@ int fy_attach_comments_if_any(struct fy_parser *fyp, struct fy_token *fyt)
(handle = fy_token_comment_handle(fyt, fycp_top, true)) != NULL) {
assert (!fy_atom_is_set(handle));
*handle = fyp->last_comment;
memset(&fyp->last_comment, 0, sizeof(fyp->last_comment));
/* erase last comment */
fy_atom_reset(&fyp->last_comment);
}

/* right hand comment */
Expand All @@ -940,7 +942,13 @@ int fy_attach_comments_if_any(struct fy_parser *fyp, struct fy_token *fyt)
fy_advance(fyp, c);

if (c == '#') {
handle = fy_token_comment_handle(fyt, fycp_right, true);
fy_get_mark(fyp, &fym);

/* it's a right comment only if it's on the same line */
if (fym.line == fyt->handle.end_mark.line)
handle = fy_token_comment_handle(fyt, fycp_right, true);
else
handle = &fyp->last_comment; /* otherwise, last comment */

rc = fy_scan_comment(fyp, handle, false);
fyp_error_check(fyp, !rc, err_out_rc,
Expand Down Expand Up @@ -976,11 +984,6 @@ int fy_scan_to_next_token(struct fy_parser *fyp)
goto done;
}

if ((fyp->cfg.flags & FYPCF_PARSE_COMMENTS) && fy_atom_is_set(&fyp->last_comment)) {
fy_input_unref(fyp->last_comment.fyi);
fyp->last_comment.fyi = NULL;
}

for (;;) {

tabs_allowed = fyp->flow_level || !fyp->simple_key_allowed;
Expand Down

0 comments on commit c1fbdc6

Please sign in to comment.