Move LSP semantics into typed analysis

This commit is contained in:
pavel 2026-08-27 21:28:11 +02:00
commit bac1183593
26 changed files with 1232 additions and 637 deletions

View file

@ -0,0 +1,50 @@
package lang
import (
"fmt"
"regexp"
"strconv"
)
type DiagnosticSeverity int
const (
DiagnosticError DiagnosticSeverity = 1
DiagnosticWarning DiagnosticSeverity = 2
)
type SourceSpan struct {
Start int
End int
}
type SemanticDiagnostic struct {
Code string
Message string
Severity DiagnosticSeverity
Span SourceSpan
}
func (diagnostic SemanticDiagnostic) Error() string { return diagnostic.Message }
var semanticOffsetPattern = regexp.MustCompile(`\sat\s(\d+)$`)
func diagnosticForError(code string, err error) SemanticDiagnostic {
span := SourceSpan{}
if match := semanticOffsetPattern.FindStringSubmatch(err.Error()); len(match) == 2 {
if offset, parseErr := strconv.Atoi(match[1]); parseErr == nil {
span = SourceSpan{Start: offset, End: offset + 1}
}
}
return SemanticDiagnostic{Code: code, Message: err.Error(), Severity: DiagnosticError, Span: span}
}
func undefinedDiagnostic(name string, position int) SemanticDiagnostic {
end := position + len(name)
return SemanticDiagnostic{
Code: "undefined-symbol",
Message: fmt.Sprintf("undefined identifier %s", name),
Severity: DiagnosticError,
Span: SourceSpan{Start: position, End: end},
}
}