Dont allow variable names contain '__'

This commit is contained in:
imaqtkatt 2024-05-24 10:50:04 -03:00
parent cdbaf66da6
commit 20a20027d3
4 changed files with 29 additions and 3 deletions

View File

@ -372,7 +372,10 @@ some_var
foo/bar
```
A variable can be anything matching the regex `[A-Za-z0-9_.-/]+`.
A variable can be anything matching the regex `[A-Za-z0-9_.-/]+` but with some restrictions:
- It can not start with `//`
- It can not contain `__`
A variable is a name for some immutable expression. It is possible to rebind variables with the same name.
@ -681,7 +684,10 @@ The constructors inherit the name of their types and become functions (`Tree/Nod
### Variables
A variable can be anything matching the regex `[A-Za-z0-9_.-/]+`.
A variable can be anything matching the regex `[A-Za-z0-9_.-/]+` but with some restrictions:
- It can not start with `//`
- It can not contain `__`
A variable is a name for some immutable expression. It is possible to rebind variables with the same name.

View File

@ -821,8 +821,17 @@ pub trait ParserCommons<'a>: Parser<'a> {
}
fn parse_bend_name(&mut self) -> ParseResult<Name> {
let ini_idx = *self.index();
let name = self.take_while(|c| c.is_ascii_alphanumeric() || c == '_' || c == '.' || c == '-' || c == '/');
if name.is_empty() { self.expected("name") } else { Ok(Name::new(name.to_owned())) }
let end_idx = *self.index();
if name.contains("__") {
let msg = "Variable names are not allowed to contain \"__\".".to_string();
self.with_ctx(Err(msg), ini_idx, end_idx)
} else if name.is_empty() {
self.expected("name")
} else {
Ok(Name::new(name.to_owned()))
}
}
/// Consumes exactly the text without skipping.

View File

@ -0,0 +1,2 @@
def main:
return __this_should_fail__(*)

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_file/variable_name_double_underscore.bend
---
Errors:
In tests/golden_tests/compile_file/variable_name_double_underscore.bend :
- expected: expression
- detected:
 2 | return __this_should_fail__(*)