Adding relational properties to tokens? #2097
Replies: 1 comment 7 replies
-
I'm having a hard time coming up with a valid use case for this where breaking apart the lexer and parser step wouldn't already solve. My biggest objection to this is that it would slow down marked for most users (even if it is just a little bit) with no benefit. And it wouldn't benefit the people who would use it a lot more than they can already do it. If there is a scenario where something useful couldn't be done without this than the benefit might out weigh the cost of slowing down everyone else. For instance I think #2043 is beneficial because it enables things that aren't possible without rewriting the lexer and parser (arguably the only reason to use marked) and for anyone who isn't using any extensions it is just a few if statements in reduced speed. |
Beta Was this translation helpful? Give feedback.
-
Following the discussion on PR #2043 on these comments, I'm curious if there's any other interest in this idea to aid in traversing the token tree for people who want to extend or customize Marked. I see two issues:
Accessing adjacent tokens requires breaking up the translation pipeline. The
walkTokens()
function on its own does not have a way to reference adjacent siblings or parent tokens. An alternative is to have the user to break apart the processing into a separate Lexer step, manipulate the resulting token tree as they wish, then send the updated tokens into the Parser. This is doable but does not lend itself to a clean extension package that is easily publishable. UziTech has already suggested adding apostProcessing
orpreProcessing
hook to handle this, which seems like a good option.Tree traversal is still cumbersome. Even with access to the token tree via Lexer, determining which tokens are parents, grandparent, siblings, etc. might require a good deal of custom code for the user.
What are the thoughts on adding a few basic properties to each token as part of the Lexer process?
previousSibling
,nextSibling
,children
, andparent
.As each token is created, it would be trivial to tack on the
previousSibling
andparent
properties since those are readily available in the Lexing loop and we already use them. We can then amend thepreviousSibling
token to point to the current token withnextSibling
. Similarly as each child token is created, we can access it'sparent
and append the current token to achildren
array.`This would make tree traversal more intuitive and simple for extension creators. Need to check if a token is between two tokens of a particular type?
if(token.previousSibling.type === 'hr' && token.nextSibling.type === 'hr') {...}
Need to merge adjacent tokens together? Or check for the presence of a particular child token? Or grandparents?token.parent.parent
.Beta Was this translation helpful? Give feedback.
All reactions