-
Notifications
You must be signed in to change notification settings - Fork 95
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
base: master
Are you sure you want to change the base?
Edits #78
Changes from 5 commits
13c4d39
1da2915
6343157
84bee76
660cfb9
4172edb
fa7ed97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
|
||
def p_function_def(p): | ||
'''function_def : WORD LEFT_PAREN RIGHT_PAREN newline_list function_body | ||
|
@@ -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') | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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') | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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''' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
) | ||
) | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left over