Skip to content

Commit adab8b9

Browse files
authored
Allow underscores in names of functions/constants/etc (#10)
Also ran cargo update
1 parent 67f6e09 commit adab8b9

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

Cargo.lock

Lines changed: 20 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/src/parser/implementations.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use nom::{
77
error::context,
88
multi::{many1, separated_list0},
99
sequence::{delimited, preceded, separated_pair, terminated, tuple},
10+
InputTakeAtPosition,
1011
};
1112

1213
use crate::{ast::*, parser::*};
@@ -49,11 +50,17 @@ impl<'i> Parser<'i> for Identifier<'i> {
4950
impl<'i> Identifier<'i> {
5051
/// Like `Identifier::parse` except it doesn't check if the identifier is a reserved keyword.
5152
fn parse_maybe_reserved(i: Input<'i>) -> Result<Self> {
53+
/// These characters aren't allowed in identifiers.
54+
fn not_allowed_in_identifier<T: nom_unicode::IsChar>(item: T) -> bool {
55+
let i = item.as_char();
56+
!i.is_alphanumeric() && i != '_'
57+
}
58+
5259
let parser = preceded(
53-
// Identifiers cannot start with a number
60+
// Identifiers must start with an alphabetic character.
5461
nom_unicode::complete::alpha1,
55-
// But after the first char, they can include numbers.
56-
nom_unicode::complete::alphanumeric0,
62+
// But after the first char, they can include numbers and underscores.
63+
|i: Input<'i>| i.split_at_position_complete(not_allowed_in_identifier),
5764
);
5865
map(recognize(parser), Self)(i)
5966
}
@@ -536,6 +543,15 @@ in y"#,
536543
assert_parse(ref_to_tests)
537544
}
538545

546+
#[test]
547+
fn test_valid_variables() {
548+
let tests = vec![(
549+
Identifier::from_span("n_hello", 0, 1),
550+
Input::new("n_hello"),
551+
)];
552+
assert_parse::<Identifier>(tests)
553+
}
554+
539555
#[test]
540556
fn test_invalid_variables() {
541557
let invalid_binding_names = [
@@ -546,8 +562,6 @@ in y"#,
546562
"let",
547563
"in",
548564
"0000000aassdfasdfasdfasdf013423452342134234234234",
549-
// TODO: fix this, it should be valid.
550-
"n_hello",
551565
];
552566
for identifier in invalid_binding_names {
553567
let i = format!("{identifier} = 100");

0 commit comments

Comments
 (0)