From ce1028ae06277755089e09f015c99183c792b65c Mon Sep 17 00:00:00 2001 From: Alex Tokarev <aleksator@gmail.com> Date: Fri, 7 Jun 2019 19:58:58 +0000 Subject: [PATCH] Allow variable names to start with an underscore --- examples/unicode.out | 2 +- examples/variables.ion | 5 +++++ examples/variables.out | 1 + src/lib/parser/statement/parse.rs | 2 +- src/lib/shell/assignments.rs | 4 ++-- 5 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 examples/variables.ion create mode 100644 examples/variables.out diff --git a/examples/unicode.out b/examples/unicode.out index 78310995..5831cf27 100644 --- a/examples/unicode.out +++ b/examples/unicode.out @@ -3,5 +3,5 @@ ӌ ion: assignment error: invalid variable name Variable names may only be (unicode) alphanumeric or `_` -The first character must be alphabetic +The first character must be alphabetic or `_` diff --git a/examples/variables.ion b/examples/variables.ion new file mode 100644 index 00000000..d0db286c --- /dev/null +++ b/examples/variables.ion @@ -0,0 +1,5 @@ +let alpha_numeric_name0 = hello +let _name_with_1_leading_underscore = leading +let __2 = underscores +let ___ = ! +echo $alpha_numeric_name0 $_name_with_1_leading_underscore $__2 $___ \ No newline at end of file diff --git a/examples/variables.out b/examples/variables.out new file mode 100644 index 00000000..8fff7bb5 --- /dev/null +++ b/examples/variables.out @@ -0,0 +1 @@ +hello leading underscores ! diff --git a/src/lib/parser/statement/parse.rs b/src/lib/parser/statement/parse.rs index f53642cf..88db1c16 100644 --- a/src/lib/parser/statement/parse.rs +++ b/src/lib/parser/statement/parse.rs @@ -16,7 +16,7 @@ use std::char; pub fn is_valid_name(name: &str) -> bool { let mut chars = name.chars(); - chars.next().map_or(false, char::is_alphabetic) + chars.next().map_or(false, |b| char::is_alphabetic(b) || b == '_') && chars.all(|b| b.is_alphanumeric() || b == '_') } diff --git a/src/lib/shell/assignments.rs b/src/lib/shell/assignments.rs index f762e1f5..2107dcab 100644 --- a/src/lib/shell/assignments.rs +++ b/src/lib/shell/assignments.rs @@ -99,7 +99,7 @@ impl<'b> Shell<'b> { } } - /// Collect all updates to perform on variables for a given assignement action + /// Collect all updates to perform on variables for a given assignment action pub(crate) fn calculate<'a>( &mut self, actions: AssignmentActions<'a>, @@ -115,7 +115,7 @@ impl<'b> Shell<'b> { if !is_valid_name(key.name) { return Err("invalid variable name\nVariable names may only be (unicode) \ - alphanumeric or `_`\nThe first character must be alphabetic" + alphanumeric or `_`\nThe first character must be alphabetic or `_`" .to_string()); } -- GitLab