-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(traceql): define error types with position
- Loading branch information
Showing
10 changed files
with
222 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package traceql | ||
|
||
import ( | ||
"fmt" | ||
"text/scanner" | ||
) | ||
|
||
// SyntaxError is a syntax error. | ||
type SyntaxError struct { | ||
Msg string | ||
Pos scanner.Position | ||
} | ||
|
||
// Error implements error. | ||
func (e *SyntaxError) Error() string { | ||
return fmt.Sprintf("at %s: %s", e.Pos, e.Msg) | ||
} | ||
|
||
// TypeError is a type checking error. | ||
type TypeError struct { | ||
Msg string | ||
Pos scanner.Position | ||
} | ||
|
||
// Error implements error. | ||
func (e *TypeError) Error() string { | ||
return fmt.Sprintf("at %s: %s", e.Pos, e.Msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,42 @@ | ||
package traceql | ||
|
||
import "github.com/go-faster/errors" | ||
import ( | ||
"fmt" | ||
"text/scanner" | ||
) | ||
|
||
// TypedExpr is an interface for typed expression. | ||
type TypedExpr interface { | ||
ValueType() StaticType | ||
} | ||
|
||
// CheckBinaryExpr typechecks binary expression. | ||
func CheckBinaryExpr(left TypedExpr, op BinaryOp, right TypedExpr) error { | ||
// checkBinaryExpr typechecks binary expression. | ||
func (p *parser) checkBinaryExpr( | ||
left TypedExpr, | ||
op BinaryOp, | ||
opPos scanner.Position, | ||
right TypedExpr, | ||
rightPos scanner.Position, | ||
) error { | ||
lt := left.ValueType() | ||
rt := right.ValueType() | ||
if !op.CheckType(lt) { | ||
return errors.Errorf("can't apply op %q to %q", op, lt) | ||
return &TypeError{ | ||
Msg: fmt.Sprintf("binary operator %q not defined on %q", op, lt), | ||
Pos: opPos, | ||
} | ||
} | ||
if !op.CheckType(rt) { | ||
return errors.Errorf("can't apply op %q to %q", op, rt) | ||
return &TypeError{ | ||
Msg: fmt.Sprintf("binary operator %q not defined on %q", op, rt), | ||
Pos: opPos, | ||
} | ||
} | ||
if !lt.CheckOperand(rt) { | ||
return errors.Errorf("invalid operand types: %q and %q", lt, rt) | ||
} | ||
return nil | ||
} | ||
|
||
// CheckUnaryExpr typechecks unary expression. | ||
func CheckUnaryExpr(op UnaryOp, expr TypedExpr) error { | ||
if t := expr.ValueType(); !op.CheckType(t) { | ||
return errors.Errorf("can't apply op %q to %q", op, t) | ||
return &TypeError{ | ||
Msg: fmt.Sprintf("operand types mismatch: %q and %q", lt, rt), | ||
Pos: rightPos, | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.