Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assignment variable and add test cases #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bashlex/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ def p_elif_clause(p):
for i in range(1, len(p)):
if isinstance(p[i], ast.node):
parts.append(p[i])
elif isinstance(p[i], list):
parts.extend(p[i])
else:
parts.append(ast.node(kind='reservedword', word=p[i], pos=p.lexspan(i)))
p[0] = parts
Expand Down
9 changes: 6 additions & 3 deletions bashlex/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,11 +969,14 @@ def handledollarword():
def _is_assignment(self, value, iscompassign):
c = value[0]

def legalvariablechar(x):
def legalvariablestarter(x):
return x.isalpha() or x == '_'

if not legalvariablechar(c):
return
def legalvariablechar(x):
return x.isalnum() or x == '_'

if not legalvariablestarter(c):
return False

for i, c in enumerate(value):
if c == '=':
Expand Down
24 changes: 24 additions & 0 deletions tests/test-tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ def assertTokens(self, s, tokens):
for t in tokens:
self.assertEquals(str(t.value), s[t.lexpos:t.endlexpos])

def assertNotTokens(self, s, tokens):
result = tokenize(s)

if result[-1].value == '\n':
result.pop()

self.assertNotEqual(result, tokens)

def test_empty_string(self):
self.assertEquals(len(tokenize('')), 0)

Expand Down Expand Up @@ -331,3 +339,19 @@ def test_tokenize(self):
t(tt.WORD, 'a', [0, 1]),
t(tt.WORD, "'b '", [2, 7], set([flags.word.QUOTED])),
t(tt.WORD, 'c', [8, 9])])

def test_variables(self):
s = 'a0_=b'
self.assertTokens(s, [
t(tt.ASSIGNMENT_WORD, 'a0_=b', [0, 5],
flags=set([flags.word.NOSPLIT, flags.word.ASSIGNMENT]))])

s = 'a0_+=b'
self.assertTokens(s, [
t(tt.ASSIGNMENT_WORD, 'a0_+=b', [0, 6],
flags=set([flags.word.NOSPLIT, flags.word.ASSIGNMENT]))])

s = '0var=1'
self.assertTokens(s, [
t(tt.WORD, '0var=1', [0, 6],
flags=set([]))])