[REPL] Syntax highlighting support

I’m working on the REPL for the new mojo language, which is based on LLDB’s REPL. As part of that, I’m exploring the idea of adding syntax highlighting to achieve some parity with fantastic REPLs like ipython. It seems that ipython is using the python package pygments to achive that. On the other hand, we are using plain libedit which apparently couldn’t easily support highlighting. I’ve also found in the internet other alternatives like replxx, but I don’t know if they are mature enough for widespread usage. Has anyone explored that in the past or provide some useful insights?

Thanks

On Sep 14, 2023, at 5:33 PM, walter erquinigo via LLVM Discussion Forums notifications@llvm.discoursemail.com wrote:

wallace
September 15

I’m working on the REPL for the new mojo language, which is based on LLDB’s REPL. As part of that, I’m exploring the idea of adding syntax highlighting to achieve some parity with fantastic REPLs like ipython. It seems that ipython is using the python package pygments to achive that. On the other hand, we are using plain libedit which apparently couldn’t easily support highlighting.

Not sure what you mean by this. highlighting is just adding different colors to different classes of text in the source or result output, right? lldb inserts color codes into the output to do this sort of colorization in many places today. libedit doesn’t implement inserting colors, but if there are color codes in the output, libedit does display them.

If I’m not missing something the trick is to find a syntax aware parser that you can run over your output strings and can detect the tokens you want to color with callbacks that allow you to insert color codes into the text before printing them. That sounds like a something your front-end would know how to do…

Jim

I’ve also found in the internet other alternatives like replxx, but I don’t know if they are mature enough for widespread usage. Has anyone explored that in the past or provide some useful insights?

Thanks


Visit Topic or reply to this email to respond.

To unsubscribe from these emails, click here.

Thanks for the reply.
What you said makes sense. My understanding is that, in order to have something basic, we’d need to enhance the REPL.h class with a method for that modifies the entire current multi-line expression upon every keystroke. In this modification we could use a regex based coloring scheme like TextMate grammars to avoid delays, just like most IDEs. I’d rather not try something asynchronous.
Does it sound good to you?

I recommend looking at the Tree-Sitter (GitHub - tree-sitter/tree-sitter: An incremental parsing system for programming tools) instead, since it provides better performance and user experience (and complexity) than Text-Mate-like grammars.

1 Like

I should certainly be fast enough to reparse and color the line on each keystroke, so that does seem like the most straightforward way to do it. I don’t have any experience with the various “not necessarily syntactically correct language parsers” so I can’t comment on that part.