From e4e71cda739c9af83302af85520518b838739f91 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Thu, 16 Jan 2025 23:13:44 +0100 Subject: Whitelist valid identifier characters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of making up a list of characters that end an identifier, do the other way around since it's far less cumbersome and it prevents from silly bugs such as "var+1" being considered a single identifier. Signed-off-by: Miquel Sabaté Solà --- lib/xixanta/src/parser.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'lib/xixanta/src/parser.rs') diff --git a/lib/xixanta/src/parser.rs b/lib/xixanta/src/parser.rs index 50f2af1..e47f115 100644 --- a/lib/xixanta/src/parser.rs +++ b/lib/xixanta/src/parser.rs @@ -239,8 +239,18 @@ impl Parser { .chars() .peekable(); while let Some(c) = chars.next() { - // Check for characters that end an identifier. - if c.is_whitespace() || c == ':' || c == '(' || c == ')' || c == '=' { + // Check for the end of the identifier. For this, it's easier to + // simply list what is allowed and negate it. + if !(c.is_ascii_alphanumeric() + || c == '.' + || c == '#' + || c == '$' + || c == '%' + || c == '@' + || c == '_' + || c == '\'' + || c == '"') + { // This next match looks scarier than what it actually is. To // sum things up, the ':' character is quite troublesome, since // it can mean three things depending on the context. @@ -2160,7 +2170,7 @@ mod tests { assert!(node.right.is_none()); let literal = &node.left.clone().unwrap(); - assert_node(literal, NodeType::Literal, line, "#