Skip to content

Fix assignment, variable name can be alpha numeric#46

Closed
sereysethy wants to merge 0 commit intoidank:masterfrom
sereysethy:master
Closed

Fix assignment, variable name can be alpha numeric#46
sereysethy wants to merge 0 commit intoidank:masterfrom
sereysethy:master

Conversation

@sereysethy
Copy link

No description provided.

@idank
Copy link
Owner

idank commented Jun 26, 2019

Thanks for the change.

This function is based on http://git.savannah.gnu.org/cgit/bash.git/tree/general.c?id=df2c55de9c87c2ee8904280d26e80f5c48dd6434#n263

Can you make sure the change you're making is correct with respect to how Bash handles this?

Also, can you please add a test?

Thanks!

@sereysethy
Copy link
Author

I verified in the bash source code, here it is the comment that I found:

/* Variable names in assignment statements may contain only letters,
	 digits, and `_'. */

So variable name can be letters, digits and _. I also added a test case, the test passes. Please check.

@idank
Copy link
Owner

idank commented Jul 1, 2019

I think this will also accept variable names that start with a digit, no?

@sereysethy
Copy link
Author

No, you cannot have variables starting with a digit, this will have a conflict with the reserved parameters (positional parameters) passed to a script or a function ${N} where N is digit.

@idank
Copy link
Owner

idank commented Jul 1, 2019

Doesn't your change allow that?

Copy link
Owner

@idank idank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please squash the commits? I think one commit is enough here.

t(tt.WORD, "'b '", [2, 7], set([flags.word.QUOTED])),
t(tt.WORD, 'c', [8, 9])])

def test_variables(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test for the invalid case where you a variable name starts with a digit

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to write an invalid case?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to parse something like 0var=1

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate more? Parse in the test case?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please add a test case for the problematic assignment which shows that parsing fails. If you're having trouble, I can do it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a failed case, but I am not sure if it is a correct way to do it.

c = value[0]

def legalvariablestarter(x):
return x.isalpha()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing or x == '_' in here since _x is a legal variable name. please add a test.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants