-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces ast.Errors- an error type that should be used whenever a generated error refers to a place in the query source. This commit switches the parser package to use ast.Error but a follow up commit will use ast.Errors for the semantic package as well.
- Loading branch information
Showing
22 changed files
with
373 additions
and
224 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
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,69 @@ | ||
package clierrors | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/brimdata/zed/compiler/ast" | ||
"github.com/brimdata/zed/compiler/parser" | ||
"go.uber.org/multierr" | ||
) | ||
|
||
func Format(set *parser.SourceSet, err error) error { | ||
if err == nil { | ||
return err | ||
} | ||
var errs []error | ||
for _, err := range multierr.Errors(err) { | ||
if asterr, ok := err.(*ast.Error); ok { | ||
err = formatASTError(set, asterr) | ||
} | ||
errs = append(errs, err) | ||
} | ||
return errors.Join(errs...) | ||
} | ||
|
||
func formatASTError(set *parser.SourceSet, err *ast.Error) error { | ||
src := set.SourceOf(err.Pos) | ||
start := src.Position(err.Pos) | ||
end := src.Position(err.End) | ||
var b strings.Builder | ||
b.WriteString(err.Error()) | ||
b.WriteString(" (") | ||
if src.Filename != "" { | ||
fmt.Fprintf(&b, "%s: ", src.Filename) | ||
} | ||
fmt.Fprintf(&b, "line %d, ", start.Line) | ||
fmt.Fprintf(&b, "column %d):\n", start.Column) | ||
line := src.LineOfPos(set.Contents, err.Pos) | ||
b.WriteString(line + "\n") | ||
if end.IsValid() { | ||
formatSpanError(&b, line, start, end) | ||
} else { | ||
formatPointError(&b, start) | ||
} | ||
return errors.New(b.String()) | ||
} | ||
|
||
func formatSpanError(b *strings.Builder, line string, start, end parser.Position) { | ||
col := start.Column - 1 | ||
b.WriteString(strings.Repeat(" ", col)) | ||
n := len(line) - col | ||
if start.Line == end.Line { | ||
n = end.Column - col | ||
} | ||
b.WriteString(strings.Repeat("~", n)) | ||
} | ||
|
||
func formatPointError(b *strings.Builder, start parser.Position) { | ||
col := start.Column - 1 | ||
for k := 0; k < col; k++ { | ||
if k >= col-4 && k != col-1 { | ||
b.WriteByte('=') | ||
} else { | ||
b.WriteByte(' ') | ||
} | ||
} | ||
b.WriteString("^ ===") | ||
} |
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
Oops, something went wrong.