Skip to content

Commit

Permalink
Error on extra alphabetic character at the end of identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
arnodb committed Nov 8, 2023
1 parent ff946df commit 17a72a2
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions datapet_lang/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use nom::{
branch::alt,
bytes::complete::{escaped, is_not, tag, take_while1},
character::complete::{alpha1, alphanumeric1, anychar, char, multispace0, multispace1},
character::complete::{
alpha1, alphanumeric1, anychar, char, multispace0, multispace1, satisfy,
},
combinator::{cut, eof, opt, peek, recognize},
error::{ErrorKind, ParseError},
multi::{many0, many0_count, many1, many_till, separated_list0, separated_list1},
Expand Down Expand Up @@ -309,10 +311,16 @@ fn simple_path(input: &str) -> SpannedResult<&str, &str> {
}

fn identifier(input: &str) -> SpannedResult<&str, &str> {
recognize(pair(
recognize(tuple((
alt((alpha1, tag("_"))),
many0_count(alt((alphanumeric1, tag("_")))),
))
peek(alt((
recognize(satisfy(|c| {
!c.is_alphabetic() && !c.is_numeric() && c != '_'
})),
recognize(eof),
))),
)))
.parse(input)
.map_err(|err| {
err.map(|_: SpannedError<&str>| SpannedError {
Expand Down Expand Up @@ -451,12 +459,12 @@ mod tests {
#[test]
fn test_identifier_extra_alphabetic_char() {
let input = "café";
let (tail, ident) = assert_matches!(
let (kind, span) = assert_matches!(
identifier(input),
Ok((tail, ident)) => (tail, ident)
Err(nom::Err::Error(SpannedError { kind, span })) => (kind, span)
);
assert_span_at_distance!(input, tail, "é", 3, "tail");
assert_eq!(ident, "caf");
assert_eq!(kind, SpannedErrorKind::Identifier);
assert_span_at_distance!(input, span, "café", 0);
}

#[test]
Expand Down

0 comments on commit 17a72a2

Please sign in to comment.