Skip to content

Commit

Permalink
better panic description
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Jul 26, 2024
1 parent 9803642 commit 95cea20
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 37 deletions.
20 changes: 3 additions & 17 deletions analysis/diagnostic_kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package analysis
import (
"fmt"
"math/big"
"numscript/utils"
)

type Severity = byte
Expand All @@ -17,21 +18,6 @@ const (
Hint
)

func SeverityToString(s Severity) string {
switch s {
case ErrorSeverity:
return "Error"
case WarningSeverity:
return "Warning"
case Information:
return "Info"
case Hint:
return "Hint"
default:
panic("Invalid error type")
}
}

func SeverityToAnsiString(s Severity) string {
switch s {
case ErrorSeverity:
Expand All @@ -43,7 +29,7 @@ func SeverityToAnsiString(s Severity) string {
case Hint:
return "Hint"
default:
panic("Invalid error type")
return utils.NonExhaustiveMatchPanic[string](s)
}
}

Expand Down Expand Up @@ -163,7 +149,7 @@ func (e *BadAllotmentSum) Message() string {
return fmt.Sprintf("Allotment portions are lesser than one (Got %s). Maybe try adding a 'remaining' clause?", e.Sum.String())
}

panic("Invalid diagnostic")
panic(fmt.Sprintf("unreachable state: allotment=%s", e.Sum.String()))
}
func (*BadAllotmentSum) Severity() Severity {
return ErrorSeverity
Expand Down
5 changes: 3 additions & 2 deletions analysis/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package analysis

import (
"numscript/parser"
"numscript/utils"
)

type Hover interface{ hover() }
Expand Down Expand Up @@ -84,7 +85,7 @@ func hoverOnSentValue(sentValue parser.SentValue, position parser.Position) Hove
return hoverOnLiteral(sentValue.Monetary, position)

default:
panic("Unhandled clause")
return utils.NonExhaustiveMatchPanic[Hover](sentValue)
}
}

Expand Down Expand Up @@ -222,7 +223,7 @@ func hoverOnKeptOrDestination(inorderClause parser.KeptOrDestination, position p
return hoverOnDestination(inorderClause.Destination, position)

default:
panic("Unhandled clause")
return utils.NonExhaustiveMatchPanic[Hover](inorderClause)
}

}
Expand Down
3 changes: 2 additions & 1 deletion lsp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"numscript/analysis"
"numscript/parser"
"numscript/utils"

"github.com/sourcegraph/jsonrpc2"
)
Expand Down Expand Up @@ -102,7 +103,7 @@ func (state *State) handleHover(params HoverParams) *Hover {

msg = fmt.Sprintf("`%s%s -> %s`\n\n%s", hoverable.Node.Caller.Name, params, resolved.Return, resolved.Docs)
default:
panic("Unhandled clause")
utils.NonExhaustiveMatchPanic[any](resolved)
}

return &Hover{
Expand Down
34 changes: 17 additions & 17 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parser
import (
"math"
parser "numscript/parser/antlr"
"numscript/utils"
"strconv"
"strings"

Expand Down Expand Up @@ -146,7 +147,7 @@ func parseCapLit(capCtx parser.ICapContext) Literal {
return nil

default:
panic("Invalid ctx")
return utils.NonExhaustiveMatchPanic[Literal](capCtx.GetText())
}
}

Expand Down Expand Up @@ -215,7 +216,7 @@ func parseSource(sourceCtx parser.ISourceContext) Source {
return nil

default:
panic("unhandled context: " + sourceCtx.GetText())
return utils.NonExhaustiveMatchPanic[Source](sourceCtx.GetText())
}
}

Expand All @@ -231,7 +232,7 @@ func parseVariableAccount(variableAccountCtx parser.IVariableAccountContext) Lit
return nil

default:
panic("Unhandled clause")
return utils.NonExhaustiveMatchPanic[Literal](variableAccountCtx.GetText())
}

}
Expand Down Expand Up @@ -297,7 +298,7 @@ func parseAllotment(allotmentCtx parser.IAllotmentContext) SourceAllotmentValue
return nil

default:
panic("Invalid allotment")
return utils.NonExhaustiveMatchPanic[SourceAllotmentValue](allotmentCtx.GetText())
}
}

Expand Down Expand Up @@ -341,7 +342,7 @@ func parseLiteral(literalCtx parser.ILiteralContext) Literal {
return nil

default:
panic("unhandled clause")
return utils.NonExhaustiveMatchPanic[Literal](literalCtx.GetText())
}
}

Expand Down Expand Up @@ -377,7 +378,7 @@ func parsePortionSource(portionCtx parser.IPortionContext) *RatioLiteral {
return nil

default:
panic("unhandled portion ctx")
return utils.NonExhaustiveMatchPanic[*RatioLiteral](portionCtx.GetText())
}
}

Expand Down Expand Up @@ -426,8 +427,7 @@ func parseDestination(destCtx parser.IDestinationContext) Destination {
return nil

default:
panic("Unhandled dest" + destCtx.GetText())

return utils.NonExhaustiveMatchPanic[Destination](destCtx.GetText())
}

}
Expand Down Expand Up @@ -458,7 +458,7 @@ func parseKeptOrDestination(clauseCtx parser.IKeptOrDestinationContext) KeptOrDe
return nil

default:
panic("Unhandled clause")
return utils.NonExhaustiveMatchPanic[KeptOrDestination](clauseCtx.GetText())
}

}
Expand All @@ -480,7 +480,7 @@ func parseDestinationAllotment(allotmentCtx parser.IAllotmentContext) Destinatio
return nil

default:
panic("unhandled portion ctx")
return utils.NonExhaustiveMatchPanic[DestinationAllotmentValue](allotmentCtx.GetText())
}
}

Expand All @@ -496,7 +496,7 @@ func parseDestinationPortion(portionCtx parser.IPortionContext) DestinationAllot
return nil

default:
panic("unhandled portion ctx")
return utils.NonExhaustiveMatchPanic[DestinationAllotmentValue](portionCtx.GetText())
}
}

Expand Down Expand Up @@ -547,7 +547,7 @@ func parseStatement(statementCtx parser.IStatementContext) Statement {
return nil

default:
panic("unhandled clause")
return utils.NonExhaustiveMatchPanic[Statement](statementCtx.GetText())
}
}

Expand All @@ -567,7 +567,7 @@ func parseSentValue(statementCtx parser.ISentValueContext) SentValue {
return nil

default:
panic("Unhandled clause")
return utils.NonExhaustiveMatchPanic[SentValue](statementCtx.GetText())
}

}
Expand All @@ -593,7 +593,7 @@ func parseVariableMonetary(sendExpr parser.IVariableMonetaryContext) Literal {
return nil

default:
panic("Unhandled clause")
return utils.NonExhaustiveMatchPanic[Literal](sendExpr.GetText())
}
}

Expand All @@ -602,7 +602,7 @@ func parseNumberLiteral(numNode antlr.TerminalNode) *NumberLiteral {

amt, err := strconv.Atoi(amtStr)
if err != nil {
panic("Invalid amt: " + amtStr)
panic("Invalid number: " + amtStr)
}

return &NumberLiteral{
Expand All @@ -623,7 +623,7 @@ func parseVariableNumber(numCtx parser.IVariableNumberContext) Literal {
return nil

default:
panic("Unhandled clause")
return utils.NonExhaustiveMatchPanic[Literal](numCtx.GetText())
}
}

Expand All @@ -642,7 +642,7 @@ func parseVariableAsset(assetCtx parser.IVariableAssetContext) Literal {
return nil

default:
panic("Unhandled clause")
return utils.NonExhaustiveMatchPanic[Literal](assetCtx.GetText())
}
}

Expand Down
7 changes: 7 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package utils

import "fmt"

func NonExhaustiveMatchPanic[T any](value any) T {
panic(fmt.Sprintf("Non exhaustive match (got '%#v')", value))
}

0 comments on commit 95cea20

Please sign in to comment.