Syntax highlighting variables #3929
Replies: 3 comments 8 replies
-
Hi @stevecopley! This is a lovely idea (that we have also thought of) but it is technically not really viable, since we use the Ace syntax highlighter which operates entirely separate from our parser. We can't really change the highlighter dynamically after each parse. If you want to take a stab at it, go ahead, but I think it will be very tricky. |
Beta Was this translation helpful? Give feedback.
-
I made a draft PR that implements the before mentioned solution - #3988, the only difference is that I changed the green color to yellow. Since we already use green for numbers. Let me know what you guys think! |
Beta Was this translation helpful? Give feedback.
-
Sadly we didn't manage to quite get over Ace limitation when it comes to highlighting variables. Luckily, we don't use Ace anymore!! Now we use CodeMirror which has support for a parser called Lezer. This allows us to generate full fledged abstract syntax trees on the fly while the user writes their program. This could allow us to highlight variables, although it would need a bit of work. Lezer uses a LR parser while Lark, the tool we use to actually transpile Hedy to Python, uses Eearley. This means that Lezer doesn't like the ambiguities we use, specially in the first few levels, therefore we can't have variable information directly at parsing time, since the trees we generate look like this:
As you can see, variables are actually Text nodes, because we don't have a clear way of distinguishing between the two. But I think we can go through the tree once we have it, and check if a Text node is a variable by iterating through |
Beta Was this translation helpful? Give feedback.
-
Can't find any prior mention of this in the discussions, so...
To aid understanding the concept of user-created variables, it would be helpful to subtly highlight variables within print statements, so that they are visually different to the surrounding text. Students could then see that their variables are placed within the text.
Beta Was this translation helpful? Give feedback.
All reactions