rhaiscript / lsp

Language server for Rhai.
Apache License 2.0
43 stars 4 forks source link

Disallow invalid expressions #17

Closed tamasfe closed 2 years ago

tamasfe commented 2 years ago

For the sake of simplicity, constructs like let a = 0 are also expressions that evaluate to ().

As a consequence currently the following is also accepted:

image

It is roughly equivalent to (this) . (const a = 2), It's not clear whether to leave it as is, or specify stricter rules.

tamasfe commented 2 years ago

I've realized that the right side of . should be a lot stricter and should allow identifiers only. How to restrict this properly is not immediately trivial to me as the . is just an operator in an expression tree where both sides can be any expression.

I'll leave this as-is for the time being as it's not a huge issue.

schungx commented 2 years ago

Yes, the RHS of . should always be an identifier.

In the parser, . and [ are split off from operators. They are actually handled as part of the parsing of a primary term, which loosely can be defined as:

(unary operator)*  identifier  ( '.' identifier |  '[' expression ']' )*

In the parser this is called an "indexing chain", although dotting is also covered.

Thus, [ and . are not parsed as operators, allowing you to catch invalid syntax early.