The only two AST nodes that track any semblance of location right now are ast.Workspace and ast.Stack. This is simply because, using the standard JSON and YAML parsers, we aren't given any information about the resulting unmarshaled node locations. To fix that, we'll need to crack open the parsers and get our hands dirty. In the meantime, we can crudely implement diag.Diagable on ast.Workspace and ast.Stack, however, to simply return their diag.Documents.
39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
package diag
|
|
|
|
// ID is a unique diagnostics identifier.
|
|
type ID int
|
|
|
|
// Diag is an instance of an error or warning generated by the compiler.
|
|
type Diag struct {
|
|
ID ID // a unique identifier for this diagnostic.
|
|
Message string // a human-friendly message for this diagnostic.
|
|
Doc *Document // the document in which this diagnostic occurred.
|
|
Loc *Location // the document location at which this diagnostic occurred.
|
|
}
|
|
|
|
// Diagable can be used to determine a diagnostic's position.
|
|
type Diagable interface {
|
|
Where() (*Document, *Location)
|
|
}
|
|
|
|
// At adds a position to an existing diagnostic, retaining its ID and message.
|
|
func (diag *Diag) At(d Diagable) *Diag {
|
|
doc, loc := d.Where()
|
|
return &Diag{
|
|
ID: diag.ID,
|
|
Message: diag.Message,
|
|
Doc: doc,
|
|
Loc: loc,
|
|
}
|
|
}
|
|
|
|
// WithFile adds a file to an existing diagnostic, retaining its ID and message.
|
|
func (diag *Diag) AtFile(file string) *Diag {
|
|
return &Diag{
|
|
ID: diag.ID,
|
|
Message: diag.Message,
|
|
Doc: NewDocument(file),
|
|
}
|
|
}
|