Documentation
¶
Index ¶
- Constants
- func DetectCommonSyntaxErrors(source string, line int, errorMsg string) string
- func DetectMissingBracket(source string, line, column int) string
- func DetectUnclosedString(source string, line int) string
- func ExtractSourceSnippet(source string, line int) string
- func FormatCodeSnippetWithFix(source string, line, column int, fixedLine string) string
- func FormatError(err error) string
- func FormatSuggestions(results []SuggestionResult, multipleAllowed bool) string
- func GetFunctionSuggestion(funcName string, availableFuncs []string) string
- func GetRouteSuggestion(routePath string, availableRoutes []string) string
- func GetRuntimeSuggestion(errorType string, context map[string]interface{}) string
- func GetSuggestionForDivisionByZero() string
- func GetSuggestionForSQLInjection() string
- func GetSuggestionForTypeMismatch(expected, actual string) string
- func GetSuggestionForUndefinedVariable(varName string, availableVars []string) string
- func GetTypeMismatchSuggestion(expected, actual, context string) string
- func GetTypeSuggestion(typeName string, availableTypes []string) string
- func GetVariableSuggestion(varName string, availableVars []string) string
- func IsValidIdentifier(s string) bool
- func SuggestMissingSemicolon(source string, line int) bool
- func SuggestValidIdentifier(s string) string
- func WithFileName(err error, fileName string) error
- func WithLineInfo(err error, line, col int, source string) error
- func WithSuggestion(err error, suggestion string) error
- func Wrap(err error, line, col int, source, fileName, suggestion string) error
- type CompileError
- func NewCompileError(message string, line, column int, sourceSnippet, suggestion string) *CompileError
- func NewParseError(message string, line, column int, sourceSnippet, suggestion string) *CompileError
- func NewTypeError(message string, line, column int, sourceSnippet, suggestion string) *CompileError
- func (e *CompileError) Error() string
- func (e *CompileError) FormatError(useColors bool) string
- func (e *CompileError) WithContext(context string) *CompileError
- func (e *CompileError) WithFixedLine(fixedLine string) *CompileError
- func (e *CompileError) WithTypes(expected, actual string) *CompileError
- type RuntimeError
- func (e *RuntimeError) Error() string
- func (e *RuntimeError) FormatError(useColors bool) string
- func (e *RuntimeError) WithExpression(expr string) *RuntimeError
- func (e *RuntimeError) WithRoute(route string) *RuntimeError
- func (e *RuntimeError) WithScope(scope map[string]interface{}) *RuntimeError
- func (e *RuntimeError) WithStackFrame(function, location string, line int) *RuntimeError
- func (e *RuntimeError) WithSuggestion(suggestion string) *RuntimeError
- type StackFrame
- type SuggestionConfig
- type SuggestionResult
- type SyntaxPattern
Examples ¶
- CompileError (EnhancedTypeError)
- CompileError (EnhancedVariableSuggestion)
- CompileError (ParseError)
- CompileError (SyntaxErrorWithFix)
- CompileError (TypeError)
- DetectMissingBracket
- DetectUnclosedString
- GetFunctionSuggestion
- GetRouteSuggestion
- GetTypeSuggestion
- RuntimeError (DivisionByZero)
- RuntimeError (EnhancedFullContext)
- RuntimeError (UndefinedVariable)
Constants ¶
const ( Reset = "\033[0m" Red = "\033[31m" Green = "\033[32m" Yellow = "\033[33m" Blue = "\033[34m" Purple = "\033[35m" Cyan = "\033[36m" Gray = "\033[90m" Bold = "\033[1m" )
ANSI color codes for terminal output
Variables ¶
This section is empty.
Functions ¶
func DetectCommonSyntaxErrors ¶
DetectCommonSyntaxErrors detects common syntax error patterns
func DetectMissingBracket ¶
DetectMissingBracket detects missing brackets, braces, or parentheses
Example ¶
Example: Missing bracket detection
source := `@ GET /items GET -> array {
$ items = [1, 2, 3
return items
}`
suggestion := DetectMissingBracket(source, 2, 24)
err := NewParseError(
"Unexpected token at end of line",
2,
24,
ExtractSourceSnippet(source, 2),
suggestion,
)
fmt.Println(err.FormatError(false))
// Output will suggest adding closing bracket
func DetectUnclosedString ¶
DetectUnclosedString detects unclosed string literals
Example ¶
Example: Unclosed string detection
source := `@ GET /message GET -> string {
$ msg = "Hello world
return msg
}`
suggestion := DetectUnclosedString(source, 2)
err := NewParseError(
"Unexpected end of line in string literal",
2,
24,
ExtractSourceSnippet(source, 2),
suggestion,
)
fmt.Println(err.FormatError(false))
// Output will detect unclosed string
func ExtractSourceSnippet ¶
ExtractSourceSnippet extracts a source code snippet around a specific line
func FormatCodeSnippetWithFix ¶
FormatCodeSnippetWithFix formats source code with a suggested fix
func FormatError ¶
FormatError formats any error with colors and context This is the main public interface for error formatting
func FormatSuggestions ¶
func FormatSuggestions(results []SuggestionResult, multipleAllowed bool) string
FormatSuggestions formats suggestion results into a human-readable string
func GetFunctionSuggestion ¶
GetFunctionSuggestion suggests function names
Example ¶
Example: Function name suggestion
availableFuncs := []string{"println", "print", "printf", "log"}
suggestion := GetFunctionSuggestion("prnt", availableFuncs)
source := `@ GET /test GET -> string {
prnt("Hello")
return "ok"
}`
snippet := ExtractSourceSnippet(source, 2)
err := NewCompileError(
"Undefined function 'prnt'",
2,
5,
snippet,
suggestion,
)
fmt.Println(err.FormatError(false))
func GetRouteSuggestion ¶
GetRouteSuggestion suggests route paths
Example ¶
Example: Route path suggestion
availableRoutes := []string{"/users", "/users/:id", "/products", "/orders"}
suggestion := GetRouteSuggestion("/usr", availableRoutes)
err := NewCompileError(
"Route '/usr' is not defined",
1,
1,
"@ forward /usr",
suggestion,
)
fmt.Println(err.FormatError(false))
func GetRuntimeSuggestion ¶
GetRuntimeSuggestion provides context-aware runtime error suggestions
func GetSuggestionForDivisionByZero ¶
func GetSuggestionForDivisionByZero() string
GetSuggestionForDivisionByZero suggests fixes for division by zero
func GetSuggestionForSQLInjection ¶
func GetSuggestionForSQLInjection() string
GetSuggestionForSQLInjection suggests fixes for SQL injection vulnerabilities
func GetSuggestionForTypeMismatch ¶
GetSuggestionForTypeMismatch suggests fixes for type mismatches
func GetSuggestionForUndefinedVariable ¶
GetSuggestionForUndefinedVariable suggests common fixes for undefined variables
func GetTypeMismatchSuggestion ¶
GetTypeMismatchSuggestion provides enhanced type mismatch suggestions
func GetTypeSuggestion ¶
GetTypeSuggestion suggests type names
Example ¶
Example: Type name suggestion
customTypes := []string{"User", "Product", "Order"}
suggestion := GetTypeSuggestion("Usr", customTypes)
source := `@ GET /data GET -> Usr {
$ user: Usr = getUser()
return user
}`
snippet := ExtractSourceSnippet(source, 2)
err := NewTypeError(
"Unknown type 'Usr'",
2,
13,
snippet,
suggestion,
)
fmt.Println(err.FormatError(false))
func GetVariableSuggestion ¶
GetVariableSuggestion suggests variable names with enhanced fuzzy matching
func IsValidIdentifier ¶
IsValidIdentifier checks if a string is a valid identifier
func SuggestMissingSemicolon ¶
SuggestMissingSemicolon checks if a semicolon might be missing (if language requires it)
func SuggestValidIdentifier ¶
SuggestValidIdentifier suggests corrections to make an invalid identifier valid
func WithFileName ¶
WithFileName adds a filename to an error
func WithLineInfo ¶
WithLineInfo wraps an error with line and column information
func WithSuggestion ¶
WithSuggestion wraps an error with a helpful suggestion
Types ¶
type CompileError ¶
type CompileError struct {
Message string
Line int
Column int
SourceSnippet string
Suggestion string
FileName string
ErrorType string
FixedLine string // Suggested fix for the error line
ExpectedType string // For type errors
ActualType string // For type errors
Context string // Additional context (e.g., "in function foo", "in route /users")
}
CompileError represents a compilation error with context
Example (EnhancedTypeError) ¶
Example: Type error with expected vs actual
source := `@ GET /calculate POST -> int {
$ result: int = "not a number"
return result
}`
snippet := ExtractSourceSnippet(source, 2)
suggestion := GetTypeMismatchSuggestion("int", "string", "variable assignment")
err := NewTypeError(
"Type mismatch in assignment",
2,
21,
snippet,
suggestion,
).WithTypes("int", "string").WithContext("in route /calculate POST")
fmt.Println(err.FormatError(false))
// Output will show expected vs actual types clearly
Example (EnhancedVariableSuggestion) ¶
Example: Enhanced variable suggestion
source := `@ GET /users GET -> array {
$ userList = getUsers()
$ totl = userList.length
return userList
}`
snippet := ExtractSourceSnippet(source, 3)
availableVars := []string{"userList", "total", "count"}
suggestion := GetVariableSuggestion("totl", availableVars)
err := NewCompileError(
"Undefined variable 'totl'",
3,
7,
snippet,
suggestion,
).WithContext("in route /users GET")
fmt.Println(err.FormatError(false))
// Output will show enhanced error with "Did you mean 'total'?" suggestion
Example (ParseError) ¶
source := `@ GET /users GET {
$ users = [1, 2, 3
return users
}`
err := NewParseError(
"Missing closing bracket in array literal",
2,
24,
ExtractSourceSnippet(source, 2),
"Add a closing bracket ']' to complete the array definition",
)
// Print without colors for consistent output
output := err.FormatError(false)
fmt.Print(output)
Example (SyntaxErrorWithFix) ¶
Example: Syntax error with suggested fix
source := `@ GET /data GET -> object {
$ data = {name: "test", age 25}
return data
}`
snippet := ExtractSourceSnippet(source, 2)
err := NewParseError(
"Missing colon in object literal",
2,
36,
snippet,
"Object properties should be in the format 'key: value'",
).WithFixedLine(` $ data = {name: "test", age: 25}`)
fmt.Println(err.FormatError(false))
// Output will show the error and suggested fix inline
Example (TypeError) ¶
source := `$ age: int = "twenty-five"` err := NewTypeError( "Type mismatch: cannot assign 'string' to variable of type 'int'", 1, 14, ExtractSourceSnippet(source, 1), "Convert the string to an integer (e.g., 25) or change the type annotation to 'str'", ) output := err.FormatError(false) fmt.Print(output)
func NewCompileError ¶
func NewCompileError(message string, line, column int, sourceSnippet, suggestion string) *CompileError
NewCompileError creates a new compile error with context
func NewParseError ¶
func NewParseError(message string, line, column int, sourceSnippet, suggestion string) *CompileError
NewParseError creates a parse-specific error
func NewTypeError ¶
func NewTypeError(message string, line, column int, sourceSnippet, suggestion string) *CompileError
NewTypeError creates a type-checking error
func (*CompileError) Error ¶
func (e *CompileError) Error() string
Error implements the error interface
func (*CompileError) FormatError ¶
func (e *CompileError) FormatError(useColors bool) string
FormatError formats the error with optional color support
func (*CompileError) WithContext ¶
func (e *CompileError) WithContext(context string) *CompileError
WithContext adds contextual information to the error
func (*CompileError) WithFixedLine ¶
func (e *CompileError) WithFixedLine(fixedLine string) *CompileError
WithFixedLine adds a suggested fix line to the error
func (*CompileError) WithTypes ¶
func (e *CompileError) WithTypes(expected, actual string) *CompileError
WithTypes adds type information for type errors
type RuntimeError ¶
type RuntimeError struct {
Message string
Route string
Expression string
StackTrace []StackFrame
Suggestion string
ErrorType string
Scope map[string]interface{}
}
RuntimeError represents a runtime error with execution context
Example (DivisionByZero) ¶
err := NewRuntimeError("Division by zero error").
WithRoute("/calculate POST").
WithExpression("result = total / count").
WithScope(map[string]interface{}{
"total": 100,
"count": 0,
}).
WithSuggestion("Add a check to ensure 'count' is not zero before performing division").
WithStackFrame("calculateAverage", "/calculate POST", 12).
WithStackFrame("main", "main.glyph", 5)
output := err.FormatError(false)
fmt.Print(output)
Example (EnhancedFullContext) ¶
Example: Runtime error with full context
err := NewRuntimeError("Division by zero").
WithRoute("/calculate POST").
WithExpression("result = numerator / denominator").
WithSuggestion(GetRuntimeSuggestion("division_by_zero", nil)).
WithScope(map[string]interface{}{
"numerator": 10,
"denominator": 0,
}).
WithStackFrame("calculateResult", "/calculate POST", 5).
WithStackFrame("handleRequest", "main.glyph", 12)
fmt.Println(err.FormatError(false))
// Output will show full runtime context with variables and stack trace
Example (UndefinedVariable) ¶
err := NewRuntimeError("Variable 'usrName' is not defined").
WithRoute("/profile GET").
WithExpression("return { name: usrName, id: userId }").
WithScope(map[string]interface{}{
"userId": 123,
"userName": "john_doe",
}).
WithSuggestion("Did you mean 'userName'? Or define the variable before using it: $ usrName = value")
output := err.FormatError(false)
fmt.Print(output)
func NewRuntimeError ¶
func NewRuntimeError(message string) *RuntimeError
NewRuntimeError creates a new runtime error
func (*RuntimeError) Error ¶
func (e *RuntimeError) Error() string
Error implements the error interface
func (*RuntimeError) FormatError ¶
func (e *RuntimeError) FormatError(useColors bool) string
FormatError formats the runtime error with optional color support
func (*RuntimeError) WithExpression ¶
func (e *RuntimeError) WithExpression(expr string) *RuntimeError
WithExpression adds expression context to a runtime error
func (*RuntimeError) WithRoute ¶
func (e *RuntimeError) WithRoute(route string) *RuntimeError
WithRoute adds route context to a runtime error
func (*RuntimeError) WithScope ¶
func (e *RuntimeError) WithScope(scope map[string]interface{}) *RuntimeError
WithScope adds variable scope to a runtime error
func (*RuntimeError) WithStackFrame ¶
func (e *RuntimeError) WithStackFrame(function, location string, line int) *RuntimeError
WithStackFrame adds a stack frame to a runtime error
func (*RuntimeError) WithSuggestion ¶
func (e *RuntimeError) WithSuggestion(suggestion string) *RuntimeError
WithSuggestion adds a suggestion to a runtime error
type StackFrame ¶
StackFrame represents a single frame in the call stack
type SuggestionConfig ¶
type SuggestionConfig struct {
MaxSuggestions int
MaxDistance int
MinSimilarityScore float64
ShowMultipleSuggestions bool
}
SuggestionConfig controls suggestion behavior
func DefaultSuggestionConfig ¶
func DefaultSuggestionConfig() *SuggestionConfig
DefaultSuggestionConfig returns the default configuration
type SuggestionResult ¶
SuggestionResult contains a suggestion with its confidence score
func FindBestSuggestions ¶
func FindBestSuggestions(target string, candidates []string, config *SuggestionConfig) []SuggestionResult
FindBestSuggestions finds the best matching suggestions for a given name
type SyntaxPattern ¶
SyntaxPattern represents a common syntax error pattern