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 1 commit
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
13 changes: 13 additions & 0 deletions parse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package parse

import (
"fmt"
"reflect"
"strconv"

"github.com/DE-labtory/koa/ast"
Expand Down Expand Up @@ -368,6 +369,18 @@ func parsePrefixExpression(buf TokenBuffer) (ast.Expression, error) {
if err != nil {
return nil, err
}
if exp.Operator != operatorMap[Bang] && reflect.TypeOf(exp.Right).String() == "*ast.BooleanLiteral" {
return nil, parseError{
tok.Type,
fmt.Sprintf("parsePrefixExpression() - Invalid prefix of %s", exp.Right.String()),
}
}
if exp.Operator == operatorMap[Bang] && reflect.TypeOf(exp.Right).String() == "*ast.StringLiteral" {
return nil, parseError{
tok.Type,
fmt.Sprintf("parsePrefixExpression() - Invalid prefix of %s", exp.Right.String()),
}
}

return exp, nil
}
Expand Down
6 changes: 3 additions & 3 deletions parse/parser_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,22 +782,22 @@ func TestMakePrefixExpression(t *testing.T) {
{Type: Minus, Val: "-"},
{Type: True, Val: "true"},
},
expected: "(-true)",
expectedErr: errors.New("MINUS, parsePrefixExpression() - Invalid prefix of true"),
},
{
tokens: []Token{
{Type: Bang, Val: "!"},
{Type: String, Val: "hello"},
},
expected: `(!"hello")`,
expectedErr: errors.New("BANG, parsePrefixExpression() - Invalid prefix of \"hello\""),
},
}

for i, tt := range tests {
buf := &mockTokenBuffer{tt.tokens, 0}
exp, err := makePrefixExpression(buf)

if err != nil && err != tt.expectedErr {
if err != nil && err.Error() != tt.expectedErr.Error() {
t.Errorf(`test[%d] - Wrong error returned expected="%v", got="%v"`,
i, tt.expectedErr, err)
continue
Expand Down