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

Edits #78

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
28 changes: 28 additions & 0 deletions bashlex/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ def visit(self, n):
dochild = self._visitnode(n, n.command)
if dochild is None or dochild:
self.visit(n.command)
elif k == 'case':
dochild = self._visitnode(n, n.parts)
if dochild is None or dochild:
for child in n.parts:
self.visit(child)
elif k == 'pattern_list':
dochild = self._visitnode(n, n.parts, None)
if dochild is None or dochild:
for child in n.parts:
self.visit(child)
elif k == 'case_clause_sequence':
dochild = self._visitnode(n, n.parts)
if dochild is None or dochild:
for child in n.parts:
self.visit(child)
elif k == 'pattern':
dochild = self._visitnode(n, n.parts)
if dochild is None or dochild:
for child in n.parts:
self.visit(child)
else:
raise ValueError('unknown node kind %r' % k)
self.visitnodeend(n)
Expand Down Expand Up @@ -130,6 +150,14 @@ def visitreservedword(self, n, word):
pass
def visitparameter(self, n, value):
pass
def visitcase(self, n, parts):
pass
def visitcase_clause_sequence(self, n, parts, sequence):
pass
def visitpattern(self, n, parts):
pass
def visitpattern_list(self, n, parts, action):
pass
def visittilde(self, n, value):
pass
def visitredirect(self, n, input, type, output, heredoc):
Expand Down
42 changes: 38 additions & 4 deletions bashlex/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,38 @@ def p_elif_clause(p):
def p_case_clause(p):
'''case_clause : pattern_list
| case_clause_sequence pattern_list'''
handleNotImplemented(p, 'case clause')

Copy link
Owner

Choose a reason for hiding this comment

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

unnecessary new line here

if len(p) == 2:
p[0]=p[1]
else:
p[0] = p[2]
p[0].append(p[1])

def p_pattern_list(p):
'''pattern_list : newline_list pattern RIGHT_PAREN compound_list
| newline_list pattern RIGHT_PAREN newline_list
| newline_list LEFT_PAREN pattern RIGHT_PAREN compound_list
| newline_list LEFT_PAREN pattern RIGHT_PAREN newline_list'''
handleNotImplemented(p, 'pattern list')

Copy link
Owner

Choose a reason for hiding this comment

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

same

parserobj = p.context
Copy link
Owner

Choose a reason for hiding this comment

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

seems unused

Copy link
Author

Choose a reason for hiding this comment

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

Taken care of

parts = []
action = None
if p.slice[2].type == "pattern":
patterns = p[2]
parts.extend(patterns)
rparen = ast.node(kind='reservedword', word=p[3], pos = p.lexspan(3))
parts.append(rparen)
else:
lparen = ast.node(kind='reservedword', word=p[2], pos=p.lexspan(2))
patterns = p[3]
rparen = ast.node(kind='reservedword', word=p[4], pos=p.lexspan(4))
parts.extend([lparen, patterns, rparen])
if p.slice[-1].type == "compound_list":
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 comment that explains this part

# for some reason, p[-1] does not give the "true" last element, do not know why
Copy link
Owner

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

Thanks!

action = p[len(p)-1]
parts.append(action)
p[0] = ast.node(kind="pattern_list",
parts=parts, pos = _partsspan(parts))

def p_case_clause_sequence(p):
'''case_clause_sequence : pattern_list SEMI_SEMI
Expand All @@ -393,12 +417,22 @@ def p_case_clause_sequence(p):
| case_clause_sequence pattern_list SEMI_AND
| pattern_list SEMI_SEMI_AND
| case_clause_sequence pattern_list SEMI_SEMI_AND'''
handleNotImplemented(p, 'case clause')
if len(p) == 3:
p[0]=p[1]
else:
p[0] = p[2]
p[0].parts.append(p[1])

def p_pattern(p):
'''pattern : WORD
| pattern BAR WORD'''
handleNotImplemented(p, 'pattern')

Copy link
Owner

Choose a reason for hiding this comment

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

newline

parserobj = p.context
if len(p) == 2:
p[0] = [_expandword(parserobj, p.slice[1])]
else:
p[0] = p[1]
p[0].append(_expandword(parserobj, p.slice[3]))

def p_list(p):
'''list : newline_list list0'''
Expand Down
29 changes: 29 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ def compoundnode(s, *parts, **kwargs):
assert not kwargs
return ast.node(kind='compound', s=s, list=list(parts), redirects=redirects)

def casenode(parts, s):
return(ast.node(kind='compound',
redirects=[],
list = [ast.ndoe(kind='case', parts=list(parts), s=s)])

def caseclausesequence(s, parts):
return ast.node(kind='caseclausesequence', parts=list(parts), s=s)

def procsubnode(s, command):
return ast.node(kind='processsubstitution', s=s, command=command)

Expand Down Expand Up @@ -1103,3 +1111,24 @@ def test_parameter_braces(self):
])
),
)
def test_cases(self):
idank marked this conversation as resolved.
Show resolved Hide resolved
return
# s = """
# case "$1" in
# 1) echo 1;;
# 2)
# # comment
# (
# echo 2
# echo 3
# )
# ;;
# 3) echo 3;;
# esac
# """
# self.assertASTEquals(s,
# casenode(s, parts = [
# reservedwordnode('case', 'case'),
# ])
# )