-
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
370 additions
and
223 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,66 @@ | ||
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 | ||
fmt.Fprintf(&b, "%s (", err) | ||
if src.Filename != "" { | ||
fmt.Fprintf(&b, "%s: ", src.Filename) | ||
} | ||
line := src.LineOfPos(set.Contents, err.Pos) | ||
fmt.Fprintf(&b, "line %d, column %d):\n%s\n", start.Line, start.Column, line) | ||
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.