From af8d6e3e0bba8ff73a77b24dc05f342726fa7961 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Thu, 23 May 2024 10:34:48 -0300 Subject: [PATCH] Dont allow top level names start with '//' --- src/fun/parser.rs | 3 +++ src/imp/parser.rs | 2 +- .../compile_file/top_level_name_slashslash.bend | 8 ++++++++ .../compile_file__top_level_name_slashslash.bend.snap | 8 ++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/golden_tests/compile_file/top_level_name_slashslash.bend create mode 100644 tests/snapshots/compile_file__top_level_name_slashslash.bend.snap diff --git a/src/fun/parser.rs b/src/fun/parser.rs index 08eae435..80722051 100644 --- a/src/fun/parser.rs +++ b/src/fun/parser.rs @@ -812,6 +812,9 @@ pub trait ParserCommons<'a>: Parser<'a> { if nam.contains("__") { let msg = "Top-level names are not allowed to contain \"__\".".to_string(); self.with_ctx(Err(msg), ini_idx, end_idx) + } else if nam.starts_with("//") { + let msg = "Top-level names are not allowed to start with \"//\".".to_string(); + self.with_ctx(Err(msg), ini_idx, end_idx) } else { Ok(nam) } diff --git a/src/imp/parser.rs b/src/imp/parser.rs index 6c31b6b7..90cb797c 100644 --- a/src/imp/parser.rs +++ b/src/imp/parser.rs @@ -912,7 +912,7 @@ impl<'a> PyParser<'a> { } self.skip_trivia_inline(); - let name = self.parse_bend_name()?; + let name = self.parse_top_level_name()?; self.skip_trivia_inline(); let params = if self.starts_with("(") { self.list_like(|p| p.parse_bend_name(), "(", ")", ",", true, 0)? diff --git a/tests/golden_tests/compile_file/top_level_name_slashslash.bend b/tests/golden_tests/compile_file/top_level_name_slashslash.bend new file mode 100644 index 00000000..221e9fa0 --- /dev/null +++ b/tests/golden_tests/compile_file/top_level_name_slashslash.bend @@ -0,0 +1,8 @@ +def random//constant(): + return 3.14 + +def //thisshouldfail(): + return random//constant() + +def main: + return //thisshouldfail() diff --git a/tests/snapshots/compile_file__top_level_name_slashslash.bend.snap b/tests/snapshots/compile_file__top_level_name_slashslash.bend.snap new file mode 100644 index 00000000..058840a0 --- /dev/null +++ b/tests/snapshots/compile_file__top_level_name_slashslash.bend.snap @@ -0,0 +1,8 @@ +--- +source: tests/golden_tests.rs +input_file: tests/golden_tests/compile_file/top_level_name_slashslash.bend +--- +Errors: +In tests/golden_tests/compile_file/top_level_name_slashslash.bend : +Top-level names are not allowed to start with "//". + 4 | def //thisshouldfail():