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

Refactor parsePrefixExpression, block some expressions #189 #191

Merged
merged 4 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 28 additions & 6 deletions parse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,16 +367,38 @@ func parseInfixExpression(buf TokenBuffer, left ast.Expression) (ast.Expression,
func parsePrefixExpression(buf TokenBuffer) (ast.Expression, error) {
var err error
Copy link
Member

Choose a reason for hiding this comment

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

I think this err is not needed

tok := buf.Read()

exp := &ast.PrefixExpression{
Operator: operatorMap[tok.Type],
}

exp.Right, err = parseExpression(buf, PREFIX)
op := operatorMap[tok.Type]
right, err := parseExpression(buf, PREFIX)
if err != nil {
return nil, err
}
switch op {
case ast.Bang:
switch right.(type) {
case *ast.StringLiteral:
return nil, parseError{
tok.Type,
fmt.Sprintf("parsePrefixExpression() - Invalid prefix of %s", right.String()),
tok.Line,
tok.Column,
}
}
case ast.Minus:
switch right.(type) {
case *ast.BooleanLiteral:
return nil, parseError{
tok.Type,
fmt.Sprintf("parsePrefixExpression() - Invalid prefix of %s", right.String()),
tok.Line,
tok.Column,
}
}
}

exp := &ast.PrefixExpression{
Operator: op,
Right: right,
}
return exp, nil
}

Expand Down
4 changes: 2 additions & 2 deletions parse/parser_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ func TestMakePrefixExpression(t *testing.T) {
},
0,
},
expected: "(-true)",
expectedErr: errors.New("[line 0, column 0] parsePrefixExpression() - Invalid prefix of true"),
},
{
buf: &mockTokenBuffer{
Expand All @@ -1018,7 +1018,7 @@ func TestMakePrefixExpression(t *testing.T) {
},
0,
},
expected: `(!"hello")`,
expectedErr: errors.New("[line 0, column 0] parsePrefixExpression() - Invalid prefix of \"hello\""),
},
}

Expand Down