Skip to content

Commit ffb6cfe

Browse files
Josh Duttonudoprog
authored andcommitted
Allow underscores at the start of a variable
1 parent 2f0e7af commit ffb6cfe

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

crates/rune/src/ast/ident.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ fn ast_parse() {
66

77
rt::<ast::Ident>("foo");
88
rt::<ast::Ident>("a42");
9+
rt::<ast::Ident>("_ignored");
910
}
1011

1112
/// An identifier, like `foo` or `Hello`.

crates/rune/src/parse/lexer.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,17 @@ impl<'a> Lexer<'a> {
143143
self.iter.next();
144144
}
145145

146-
let (ident, span) = self.iter.source_from(start);
147-
let kind = ast::Kind::from_keyword(ident)
148-
.unwrap_or(ast::Kind::Ident(ast::LitSource::Text(self.source_id)));
149-
Ok(Some(ast::Token { kind, span }))
146+
match self.iter.source_from(start) {
147+
("_", span) => Ok(Some(ast::Token {
148+
span,
149+
kind: ast::Kind::Underscore,
150+
})),
151+
(ident, span) => {
152+
let kind = ast::Kind::from_keyword(ident)
153+
.unwrap_or(ast::Kind::Ident(ast::LitSource::Text(self.source_id)));
154+
Ok(Some(ast::Token { kind, span }))
155+
}
156+
}
150157
}
151158

152159
/// Consume a number literal.
@@ -781,7 +788,6 @@ impl<'a> Lexer<'a> {
781788
}
782789
'[' => ast::Kind::Open(ast::Delimiter::Bracket),
783790
']' => ast::Kind::Close(ast::Delimiter::Bracket),
784-
'_' => ast::Kind::Underscore,
785791
',' => ast::Kind::Comma,
786792
':' => ast::Kind::Colon,
787793
'#' => ast::Kind::Pound,
@@ -803,7 +809,7 @@ impl<'a> Lexer<'a> {
803809
'@' => ast::Kind::At,
804810
'$' => ast::Kind::Dollar,
805811
'~' => ast::Kind::Tilde,
806-
'a'..='z' | 'A'..='Z' => {
812+
'_' | 'a'..='z' | 'A'..='Z' => {
807813
return self.next_ident(start);
808814
}
809815
'0'..='9' => {

crates/rune/src/tests/vm_assign_exprs.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ fn test_basic_assign() {
99
assert_eq!(out, 42);
1010
}
1111

12+
#[test]
13+
fn test_assign_underscore() {
14+
let out: i64 = rune! {
15+
pub fn main() { let _a = 0; _a = 42; _a }
16+
};
17+
18+
assert_eq!(out, 42);
19+
}
20+
21+
#[test]
22+
fn test_assign_underscores() {
23+
let out: i64 = rune! {
24+
pub fn main() { let ___ = 0; ___ = 42; ___ }
25+
};
26+
27+
assert_eq!(out, 42);
28+
}
29+
1230
#[test]
1331
fn test_assign_anon_object() {
1432
let out: i64 = rune! {

0 commit comments

Comments
 (0)