-
Notifications
You must be signed in to change notification settings - Fork 2
comment tracking
to track comments that belong to a node, we should keep track the latest node that can have attached comments, the comments that are currently waiting to be attached to a node. the actions the parser can perform on the comment buffer are:
- suck: returns the current comment buffer, and sets the comment buffer to []; this is for the leading comments
- spew: attaches the current comment buffer to the latest accepting node, and sets the comment buffer to []; this is for the trailing comments
comments are always attached to the most simple, adjacent node; different tokens at different parse contexts can either trigger a suck or a spew. The one thing that is worth noting in the whole process is, no nodes sets its own trailing comments. Finally, an example:
/* while */ while ( /* test */ false ) /* b */ { /* leading-expr */ n() /* trailing-expr */; /* trailing-stmt */ } /* trailing-while */
// the process:
/* while */ // -----------------> combuf: [/* while */]
while // -----------------> suck; while's leading: [/* while */], combuf: []
( // -----------------> no action
/* test */ // -----------------> combuf: [/* test */]-b
false // -----------------> suck; Literal's leading: [/* test */], combuf: []
) // -----------------> spew combuf
/* b */ // -----------------> combuf: [/* b */]
{ // -----------------> suck; b's leading: [/* b */], combuf: []
/* leading-expr */ // --------------> combuf: [/* leading-expr */]
n // -----------------> suck; Identifier's leading: [/* leading-expr */], combuf: []; LAN: n
( // -----------------> spew combuf
) // -----------------> spew combuf; LAN: n()
/* trailing-expr */ // -------------> combuf: [/* trailing-expr */]
; // -----------------> spew combuf; n()'s trailing: [/* trailing-expr */], LAN: n();
/* trailing stmt */ // -------------> combuf [/* trailing stmt */]
} // -----------------> spew combuf; n();'s trailing: [/* trailing stmt */], LAN: while (false) { n(); }
/* trailing-while */ // ------------> combuf: [/* trailing while */]
<:eof:> // ----------------> spew combuf; while (false) { n(); }'s trailing: [/* trailing-while */]
this is not the ideal way though, and comments in things like while /* this while */ (false) { /* this body */ }
will not be tracked even though they should, in some way; using specific comment array names like preTestComments
for above case, as an example, might be a possible solution, i.e:
({ type: 'WhileStatment',
test: { type: 'Literal', value: false},
body: { type: 'BlockStatement', body: [], innerBodyComments: [/* this body */], leadingComments: [], trailingComments: [] },
preTestComments: [/* this while */],
trailingComments: [],
leadingComments: []
})