~sircmpwn/rc-devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch

[PATCH] parse: Allow whitespace in assignments

Details
Message ID
<20231118163327.37781-2-max@mxsr.de>
DKIM signature
missing
Download raw message
Patch: +37 -0
Examples:
a = 1
b= 2
c =3

Add tests for assignments with whitespace.
---
This fixes <https://todo.sr.ht/~sircmpwn/rc/20>.

 parse/command.ha | 16 ++++++++++++++++
 test/assign.rc   | 21 +++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/parse/command.ha b/parse/command.ha
index a696b17..c3b3678 100644
--- a/parse/command.ha
+++ b/parse/command.ha
@@ -74,12 +74,28 @@ fn parse_command(p: *parser) (ast::command | error) = {
			defer free(parts);

			if (len(parts) > 1 && len(parts[1]) != 0) {
				// Example: x=4
				lex::unlex(&p.lex, (ltok::ARGUMENT, parts[1]));
			} else {
				// Example: x= 4
				ws(p, false)?;
			};
			lex::unlex(&p.lex, (ltok::EQUAL, ""));
			lex::unlex(&p.lex, (ltok::WORD, parts[0]));
			return parse_assignment(p)?;
		};
		const had_ws = ws(p, false)?;
		if (peek(p, ltok::EQUAL)? is lex::token) {
			// Example: x = 4 or x =4
			want(p, ltok::EQUAL)?;
			ws(p, false)?;
			lex::unlex(&p.lex, (ltok::EQUAL, ""));
			lex::unlex(&p.lex, (ltok::WORD, word));
			return parse_assignment(p)?;
		} else if (had_ws) {
			// Example: x = 4 or x =4
			lex::unlex(&p.lex, (ltok::WS, ""));
		};

		lex::unlex(&p.lex, (tok.0, word));
		return parse_compound(p)?;
diff --git a/test/assign.rc b/test/assign.rc
index b5962f2..3337327 100644
--- a/test/assign.rc
+++ b/test/assign.rc
@@ -7,6 +7,27 @@ begin "Basic assignments"

	x=20
	test $x -eq 20 || fail 'expected $x to equal 20'

	x = 30
	test $x -eq 30 || fail 'expected $x to equal 30'

	x =40
	test $x -eq 40 || fail 'expected $x to equal 40'

	x= 50
	test $x -eq 50 || fail 'expected $x to equal 50'

	x="60"
	test $x -eq 60 || fail 'expected $x to equal 60'

	x = "70"
	test $x -eq 70 || fail 'expected $x to equal 70'

	x ="80"
	test $x -eq 80 || fail 'expected $x to equal 80'

	x= "90"
	test $x -eq 90 || fail 'expected $x to equal 90'
}
end

-- 
2.42.1
Reply to thread Export thread (mbox)