Skip to content

Commit

Permalink
Fix error with strings containing equal operator
Browse files Browse the repository at this point in the history
As with e27a159 ("Allow semicolons inside of strings"), the parser
was too naive and regarded any '=' operator as part of an assignment,
despite that it could be art of string literal.

Luckily the fix was already done inside of the parsing of assignments,
we just needed to move it up.

Signed-off-by: Miquel Sabaté Solà <[email protected]>
  • Loading branch information
mssola committed Jan 15, 2025
1 parent e27a159 commit e7d815c
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions lib/xixanta/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,10 @@ impl Parser {
match INSTRUCTIONS.get(&id.value) {
Some(_) => Ok(self.parse_instruction(line, id)?),
None => {
if line.contains('=') {
// Skip whitespaces and check if we have a '=' sign. If that's
// the case, then it's an assignment.
self.skip_whitespace(line);
if line.chars().nth(self.offset).unwrap_or_default() == '=' {
Ok(self.parse_assignment(line, id)?)
} else {
self.parse_other(line, id)
Expand Down Expand Up @@ -538,12 +541,6 @@ impl Parser {
return Err(self.parser_error(&msg));
}

// Skip whitespaces and make sure that we have a '=' sign.
self.skip_whitespace(line);
if line.chars().nth(self.offset).unwrap_or_default() != '=' {
return Err(self.parser_error(format!("unknown instruction '{}'", id.value).as_str()));
}

// Skip the '=' sign and any possible whitespaces.
self.next();
self.skip_whitespace(line);
Expand Down Expand Up @@ -1621,17 +1618,17 @@ mod tests {
#[test]
fn parse_string() {
let mut parser = Parser::default();
let line = ".asciiz \"a: b, c; d\" ; Comment";
let line = ".asciiz \"=a: b, c; d\" ; Comment";

assert!(parser.parse(line.as_bytes(), SourceInfo::default()).is_ok());

let stmt = parser.nodes.last().unwrap().last().unwrap();
let inner = stmt.args.as_ref().unwrap().first().unwrap();
assert_eq!(inner.node_type, NodeType::Value);
assert_eq!(inner.value.value, "\"a: b, c; d\"");
assert_eq!(inner.value.value, "\"=a: b, c; d\"");
assert_eq!(
line.get(inner.value.start..inner.value.end).unwrap(),
"\"a: b, c; d\""
"\"=a: b, c; d\""
);
}

Expand Down

0 comments on commit e7d815c

Please sign in to comment.