rhaiscript / rhai

Rhai - An embedded scripting language for Rust.
https://crates.io/crates/rhai
Apache License 2.0
3.63k stars 174 forks source link

Customizing "let", "fn" syntax #860

Closed VinnyVicious closed 3 months ago

VinnyVicious commented 3 months ago

With Rhai, is it possible to rename the keyword fn to something like def? What about the let keyword? Would it be possible to remove it all together and have no keyword for variable definition? How flexible is the parsing of Rhai?

schungx commented 3 months ago

Well, you can use a token mapper to remap these tokens into let and fn... or remap let into a parse error...

The tokenizer is closed, the syntax is closed, but you can do certain manipulation within the gaps...

https://rhai.rs/book/engine/token-mapper.html

You can, of course, disable certain keywords altogether and they'll parse as errors. That's standard feature. Check out https://rhai.rs/book/engine/disable-keywords.html

VinnyVicious commented 3 months ago

Removing let though would not allow variables to be declared, though. I just want it to be foo = 5 instead of let foo = 5. Or maybe $foo = 5

schungx commented 3 months ago

Removing let though would not allow variables to be declared, though. I just want it to be foo = 5 instead of let foo = 5. Or maybe $foo = 5

As I mentioned, it is not possible to alter Rhai's syntax, meaning that you can't do these.

For $foo you may try a custom syntax... but it really is not recommended for you to alter Rhai's syntax to accommodate another language. Perhaps you should fork Rhai and directly modify its syntax to suit yours...

VinnyVicious commented 3 months ago

Thank you for the clarification, that does make sense.