Live data from Hacker News

Show HN: A Rust macro that parses Java-like syntax and runs it as a Rust program

gitlab.com

11–20 of 35 posts

Re: Show HN: A Rust macro that parses Java-like syntax and runs it as a Rust program

#11

Cool. On the other hand, projects like these suggest to me that Macro systems like Rust's are a bit too powerful. Not that I know how to make it just enough less powerful.

In my experience Rust macros are painful enough to write to deter people from abusing them too much. Whether that's a bug or a feature, I'll let you decide.

Re: Show HN: A Rust macro that parses Java-like syntax and runs it as a Rust program

#12
post #7

How well do LSP implementations cope with complex macros like this?

I think that I actually managed to get the language server to take 100% CPU once, I think it was stuck recursing. Don't want to dig deeper because I had to REISUB my computer, heh. But other than that one very rare time it works pretty well. Can't always show you the most useful things, because sometimes it depends on invocation and line number but it's doing well with syntax errors.

Re: Show HN: A Rust macro that parses Java-like syntax and runs it as a Rust program

#13

There are a few things that are annoying about writing macros* (in particular when I last checked there was no way to break the hygiene which is something that is occasionally useful for eg making lots of functions that follow a similar template) in Rust but in general I think they strike a really nice balance for a language with complex syntax: 1. Macros only happen in certain points and don’t need to be known to be…

I gotta say what I really like about macros are that ( and )s are matched. If you say:

  while ($($cond:tt)*) {
You'll actually match both

  while (true) {
and

  while (5 * (2 + 3)) {
. It counts the parenthesis :D

Re: Show HN: A Rust macro that parses Java-like syntax and runs it as a Rust program

#14

There are a few things that are annoying about writing macros* (in particular when I last checked there was no way to break the hygiene which is something that is occasionally useful for eg making lots of functions that follow a similar template) in Rust but in general I think they strike a really nice balance for a language with complex syntax: 1. Macros only happen in certain points and don’t need to be known to be…

I gotta say what I really like about macros are that ( and )s are matched. If you say: while ($($cond:tt)*) { You'll actually match both while (true) { and while (5 * (2 + 3)) { . It counts the parenthesis :D

Even C preprocessor macros count parenthesis.

   MAC((here, have one argument), and then this second one)

Re: Show HN: A Rust macro that parses Java-like syntax and runs it as a Rust program

#15

There are a few things that are annoying about writing macros* (in particular when I last checked there was no way to break the hygiene which is something that is occasionally useful for eg making lots of functions that follow a similar template) in Rust but in general I think they strike a really nice balance for a language with complex syntax: 1. Macros only happen in certain points and don’t need to be known to be…

I gotta say what I really like about macros are that ( and )s are matched. If you say: while ($($cond:tt)*) { You'll actually match both while (true) { and while (5 * (2 + 3)) { . It counts the parenthesis :D

But this is a requirement of the “you must be able to parse source code including macros without having to know how to expand macros” rule. Otherwise how would you know how to parse the following:

  foo!((x))
As

  foo! ( { ( x ) } )
Or

  foo! ( { ( x } ) )
With {} representing the argument to the macro.

Re: Show HN: A Rust macro that parses Java-like syntax and runs it as a Rust program

#16

Cool. On the other hand, projects like these suggest to me that Macro systems like Rust's are a bit too powerful. Not that I know how to make it just enough less powerful.

Rust's macro system is actually quite limited (I don't know if it's Turing-complete, but the artificial recursion limit is low enough that even if it is, it's not going to be all that abusable); macros are mostly just good for getting rid of repetitive boilerplate, and community norms (and implementation papercuts) discourage people from reaching for macros willy-nilly.

Re: Show HN: A Rust macro that parses Java-like syntax and runs it as a Rust program

#17
post #7

How well do LSP implementations cope with complex macros like this?

In short they don’t, neither does the compiler.

I wrote a rather large macro to auto-generate a Java-Ops parser. The I spent the next 4 days re-writing it so I didn’t have to give the Rust compiler 4GiB of stack just to compile.

Deeply recursive structures (boardering on 1mil+ recursion depth gets rough)

Re: Show HN: A Rust macro that parses Java-like syntax and runs it as a Rust program

#19

Would it be possible to write a compiler from Java to Rust? C# to rust? The goal then would be to get fast(er) execution times.

Rust is a good base for many languages, but in these cases it would probably still imply implementing the JVM/the CLR, rather than "Java" or "C#".

There are probably research experiments that have involved ahead of time compilation, but if they had succeeded then I expect most people would have used them.

GraalVM looks very promising.

Re: Show HN: A Rust macro that parses Java-like syntax and runs it as a Rust program

#20

There are a few things that are annoying about writing macros* (in particular when I last checked there was no way to break the hygiene which is something that is occasionally useful for eg making lots of functions that follow a similar template) in Rust but in general I think they strike a really nice balance for a language with complex syntax: 1. Macros only happen in certain points and don’t need to be known to be…

You might be interested in the work that Carl Mäsak is doing with 007 (http://masak.github.io/007/). It is the playground for developing future versions of macros in Perl 6.
Post reply on HN