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 5 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):
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
51 changes: 46 additions & 5 deletions bashlex/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,12 @@ def p_case_command(p):
'''case_command : CASE WORD newline_list IN newline_list ESAC
| CASE WORD newline_list IN case_clause_sequence newline_list ESAC
| CASE WORD newline_list IN case_clause ESAC'''
handleNotImplemented(p, 'case command')
parts = _makeparts(p)
p[0] = ast.node(kind='compound',
redirects = [],
list=[ast.node(kind='case', parts=parts, pos=_partsspan(parts))],
pos=_partsspan(parts))
#handleNotImplemented(p, 'case command')
Copy link
Owner

Choose a reason for hiding this comment

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

left over


def p_function_def(p):
'''function_def : WORD LEFT_PAREN RIGHT_PAREN newline_list function_body
Expand Down Expand Up @@ -377,14 +382,36 @@ 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].parts.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

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

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 +420,26 @@ 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')
parts = _makeparts(p)
end = parts[len(parts)-1]
if len(p) == 3:
p[0]=p[1]
p[0].parts.append(end)
else:
p[0] = p[2]
p[0].parts.append(end)
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
60 changes: 60 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ def compoundnode(s, *parts, **kwargs):
assert not kwargs
return ast.node(kind='compound', s=s, list=list(parts), redirects=redirects)

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

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

def patternlistnode(s, *parts):
return ast.node(kind='patternlist', parts=list(parts), s=s)

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

Expand Down Expand Up @@ -1103,3 +1114,52 @@ 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 foo
;;
*)
(
echo bar
)
;;
esac
"""
self.assertASTEquals(s,
Copy link
Owner

Choose a reason for hiding this comment

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

i see what you mean about the resulting ast :) is there a more condensed way to represent it, that is still in line with how everything else is parsed?

compoundnode(s,
casenode(s,
reservedwordnode('case', 'case'),
wordnode('$1',
parameternode('1', '1')
),
reservedwordnode('in', 'in'),
patternlistnode('1) echo foo\n;;\n*)\n(\necho bar\n);;\n',
wordnode('*', '*'),
reservedwordnode(')', ')'),
compoundnode('(\necho bar\n)',
reservedwordnode('(', '('),
commandnode('echo bar',
wordnode('echo', 'echo'),
wordnode('bar', 'bar')
),
reservedwordnode(')', ')')
),
reservedwordnode(';;', ';;'),
patternlistnode('1) echo foo\n;;',
wordnode('1', '1'),
reservedwordnode(')', ')'),
commandnode('echo foo\n',
wordnode('echo', 'echo'),
wordnode('foo', 'foo')
),
reservedwordnode(';;', ';;')
)
),
reservedwordnode('esac', 'esac')
)
)
)