diff --git a/docs/content/error-recover/_index.md b/docs/content/error-recover/_index.md index fb59a0e9..bd9ce958 100644 --- a/docs/content/error-recover/_index.md +++ b/docs/content/error-recover/_index.md @@ -15,15 +15,11 @@ Then, the following two errors are reported: ```sql syntax error: :1:12: unexpected token: ) - - 1: SELECT (1 +) + (* 2) - ^ - - + 1| SELECT (1 +) + (* 2) + | ^ syntax error: :1:17: unexpected token: * - - 1: SELECT (1 +) + (* 2) - ^ + 1| SELECT (1 +) + (* 2) + | ^ ``` Hoever, the AST is also returned: diff --git a/error.go b/error.go index 9eac8ba5..66cd5d4a 100644 --- a/error.go +++ b/error.go @@ -36,10 +36,7 @@ func (list MultiError) Error() string { // FullError returns a full error message. func (list MultiError) FullError() string { var message bytes.Buffer - for i, err := range list { - if i > 0 { - fmt.Fprintln(&message) - } + for _, err := range list { fmt.Fprintln(&message, err.Error()) } return message.String() @@ -58,7 +55,7 @@ func (e *Error) Error() string { var message bytes.Buffer fmt.Fprintf(&message, "syntax error: %s: %s", e.Position, e.Message) if e.Position.Source != "" { - fmt.Fprintf(&message, "\n\n%s", e.Position.Source) + fmt.Fprintf(&message, "\n%s", e.Position.Source) } return message.String() } diff --git a/error_test.go b/error_test.go index 24b07a66..1da85ddb 100644 --- a/error_test.go +++ b/error_test.go @@ -19,7 +19,7 @@ func TestMultiError(t *testing.T) { Column: 0, EndLine: 0, EndColumn: 1, - Source: " 1: a b\n ^", + Source: " 1| a b\n | ^", }, } err2 := &Error{ @@ -32,7 +32,7 @@ func TestMultiError(t *testing.T) { Column: 2, EndLine: 0, EndColumn: 3, - Source: " 1: a b\n ^", + Source: " 1| a b\n | ^", }, } @@ -50,36 +50,30 @@ func TestMultiError(t *testing.T) { MultiError{err1}, heredoc.Doc(` syntax error: foo:1:1: error 1 - - 1: a b - ^ + 1| a b + | ^ `), heredoc.Doc(` syntax error: foo:1:1: error 1 - - 1: a b - ^ + 1| a b + | ^ `), }, { MultiError{err1, err2}, heredoc.Doc(` syntax error: foo:1:1: error 1 - - 1: a b - ^ + 1| a b + | ^ (and 1 other error) `), heredoc.Doc(` syntax error: foo:1:1: error 1 - - 1: a b - ^ - + 1| a b + | ^ syntax error: foo:1:3: error 2 - - 1: a b - ^ + 1| a b + | ^ `), }, } { diff --git a/testdata/result/ddl/!bad_alter_table_add_column.sql.txt b/testdata/result/ddl/!bad_alter_table_add_column.sql.txt index e653b4a5..ce6c29ac 100644 --- a/testdata/result/ddl/!bad_alter_table_add_column.sql.txt +++ b/testdata/result/ddl/!bad_alter_table_add_column.sql.txt @@ -2,9 +2,8 @@ alter table foo add column baz string(max) null --- Error syntax error: testdata/input/ddl/!bad_alter_table_add_column.sql:1:44: expected token: , but: NULL - - 1: alter table foo add column baz string(max) null - ^~~~ + 1| alter table foo add column baz string(max) null + | ^~~~ --- AST diff --git a/testdata/result/dml/!bad_insert.sql.txt b/testdata/result/dml/!bad_insert.sql.txt index 53381dfa..ddcb4be3 100644 --- a/testdata/result/dml/!bad_insert.sql.txt +++ b/testdata/result/dml/!bad_insert.sql.txt @@ -4,9 +4,8 @@ vales (1, 2, 3), (4, 5, 6) --- Error syntax error: testdata/input/dml/!bad_insert.sql:2:1: expected beginning of simple query "(", SELECT, FROM, but: "vales" - - 2: vales (1, 2, 3), - ^~~~~ + 2| vales (1, 2, 3), + | ^~~~~ --- AST diff --git a/testdata/result/expr/!bad_new_braced_constructor.sql.txt b/testdata/result/expr/!bad_new_braced_constructor.sql.txt index 6004d3dc..668ad259 100644 --- a/testdata/result/expr/!bad_new_braced_constructor.sql.txt +++ b/testdata/result/expr/!bad_new_braced_constructor.sql.txt @@ -3,9 +3,8 @@ NEW foo { bar: 1 + } --- Error syntax error: testdata/input/expr/!bad_new_braced_constructor.sql:1:20: unexpected token: } - - 1: NEW foo { bar: 1 + } - ^ + 1| NEW foo { bar: 1 + } + | ^ --- AST diff --git a/testdata/result/expr/!bad_plus.sql.txt b/testdata/result/expr/!bad_plus.sql.txt index 53433915..ccd96afc 100644 --- a/testdata/result/expr/!bad_plus.sql.txt +++ b/testdata/result/expr/!bad_plus.sql.txt @@ -3,9 +3,8 @@ --- Error syntax error: testdata/input/expr/!bad_plus.sql:2:1: unexpected token: - - 2: - ^ + 2| + | ^ --- AST diff --git a/testdata/result/expr/!bad_plus2.sql.txt b/testdata/result/expr/!bad_plus2.sql.txt index 4a607930..aa09007a 100644 --- a/testdata/result/expr/!bad_plus2.sql.txt +++ b/testdata/result/expr/!bad_plus2.sql.txt @@ -3,14 +3,11 @@ --- Error syntax error: testdata/input/expr/!bad_plus2.sql:1:5: unexpected token: ) - - 1: (1 +) + (2 +) - ^ - + 1| (1 +) + (2 +) + | ^ syntax error: testdata/input/expr/!bad_plus2.sql:1:13: unexpected token: ) - - 1: (1 +) + (2 +) - ^ + 1| (1 +) + (2 +) + | ^ --- AST diff --git a/testdata/result/expr/!bad_typed_struct.sql.txt b/testdata/result/expr/!bad_typed_struct.sql.txt index 4a1cabf8..4c821820 100644 --- a/testdata/result/expr/!bad_typed_struct.sql.txt +++ b/testdata/result/expr/!bad_typed_struct.sql.txt @@ -3,14 +3,11 @@ STRUCT<1>(2 +) --- Error syntax error: testdata/input/expr/!bad_typed_struct.sql:1:8: expected token: , ARRAY, STRUCT, but: - - 1: STRUCT<1>(2 +) - ^ - + 1| STRUCT<1>(2 +) + | ^ syntax error: testdata/input/expr/!bad_typed_struct.sql:1:14: unexpected token: ) - - 1: STRUCT<1>(2 +) - ^ + 1| STRUCT<1>(2 +) + | ^ --- AST diff --git a/testdata/result/query/!bad_hint_select.sql.txt b/testdata/result/query/!bad_hint_select.sql.txt index 9b00c469..1db96187 100644 --- a/testdata/result/query/!bad_hint_select.sql.txt +++ b/testdata/result/query/!bad_hint_select.sql.txt @@ -3,9 +3,8 @@ --- Error syntax error: testdata/input/query/!bad_hint_select.sql:1:3: expected token: {, but: SELECT - - 1: @ select 1 - ^~~~~~ + 1| @ select 1 + | ^~~~~~ --- AST diff --git a/testdata/result/query/!bad_hint_select_2.sql.txt b/testdata/result/query/!bad_hint_select_2.sql.txt index 7cee98e3..5f06efa5 100644 --- a/testdata/result/query/!bad_hint_select_2.sql.txt +++ b/testdata/result/query/!bad_hint_select_2.sql.txt @@ -2,9 +2,8 @@ @{hint = 1} select --- Error syntax error: testdata/input/query/!bad_hint_select_2.sql:1:19: unexpected token: - - 1: @{hint = 1} select - ^ + 1| @{hint = 1} select + | ^ --- AST diff --git a/testdata/result/query/!bad_select.sql.txt b/testdata/result/query/!bad_select.sql.txt index ab2c1e9f..446ffdab 100644 --- a/testdata/result/query/!bad_select.sql.txt +++ b/testdata/result/query/!bad_select.sql.txt @@ -3,9 +3,8 @@ select --- Error syntax error: testdata/input/query/!bad_select.sql:2:1: unexpected token: - - 2: - ^ + 2| + | ^ --- AST diff --git a/testdata/result/query/!bad_select_order.sql.txt b/testdata/result/query/!bad_select_order.sql.txt index 01dd9611..0e4fc2ae 100644 --- a/testdata/result/query/!bad_select_order.sql.txt +++ b/testdata/result/query/!bad_select_order.sql.txt @@ -3,9 +3,8 @@ select 1 order x asc --- Error syntax error: testdata/input/query/!bad_select_order.sql:1:16: expected token: BY, but: - - 1: select 1 order x asc - ^ + 1| select 1 order x asc + | ^ --- AST diff --git a/testdata/result/query/!bad_select_union_select.sql.txt b/testdata/result/query/!bad_select_union_select.sql.txt index a4fc6167..913e0587 100644 --- a/testdata/result/query/!bad_select_union_select.sql.txt +++ b/testdata/result/query/!bad_select_union_select.sql.txt @@ -2,14 +2,11 @@ select union all select --- Error syntax error: testdata/input/query/!bad_select_union_select.sql:1:8: unexpected token: UNION - - 1: select union all select - ^~~~~ - + 1| select union all select + | ^~~~~ syntax error: testdata/input/query/!bad_select_union_select.sql:1:24: unexpected token: - - 1: select union all select - ^ + 1| select union all select + | ^ --- AST diff --git a/testdata/result/statement/!bad_alter_table_add_column.sql.txt b/testdata/result/statement/!bad_alter_table_add_column.sql.txt index e653b4a5..ce6c29ac 100644 --- a/testdata/result/statement/!bad_alter_table_add_column.sql.txt +++ b/testdata/result/statement/!bad_alter_table_add_column.sql.txt @@ -2,9 +2,8 @@ alter table foo add column baz string(max) null --- Error syntax error: testdata/input/ddl/!bad_alter_table_add_column.sql:1:44: expected token: , but: NULL - - 1: alter table foo add column baz string(max) null - ^~~~ + 1| alter table foo add column baz string(max) null + | ^~~~ --- AST diff --git a/testdata/result/statement/!bad_hint_select.sql.txt b/testdata/result/statement/!bad_hint_select.sql.txt index 9b00c469..1db96187 100644 --- a/testdata/result/statement/!bad_hint_select.sql.txt +++ b/testdata/result/statement/!bad_hint_select.sql.txt @@ -3,9 +3,8 @@ --- Error syntax error: testdata/input/query/!bad_hint_select.sql:1:3: expected token: {, but: SELECT - - 1: @ select 1 - ^~~~~~ + 1| @ select 1 + | ^~~~~~ --- AST diff --git a/testdata/result/statement/!bad_hint_select_2.sql.txt b/testdata/result/statement/!bad_hint_select_2.sql.txt index 7cee98e3..5f06efa5 100644 --- a/testdata/result/statement/!bad_hint_select_2.sql.txt +++ b/testdata/result/statement/!bad_hint_select_2.sql.txt @@ -2,9 +2,8 @@ @{hint = 1} select --- Error syntax error: testdata/input/query/!bad_hint_select_2.sql:1:19: unexpected token: - - 1: @{hint = 1} select - ^ + 1| @{hint = 1} select + | ^ --- AST diff --git a/testdata/result/statement/!bad_insert.sql.txt b/testdata/result/statement/!bad_insert.sql.txt index 53381dfa..ddcb4be3 100644 --- a/testdata/result/statement/!bad_insert.sql.txt +++ b/testdata/result/statement/!bad_insert.sql.txt @@ -4,9 +4,8 @@ vales (1, 2, 3), (4, 5, 6) --- Error syntax error: testdata/input/dml/!bad_insert.sql:2:1: expected beginning of simple query "(", SELECT, FROM, but: "vales" - - 2: vales (1, 2, 3), - ^~~~~ + 2| vales (1, 2, 3), + | ^~~~~ --- AST diff --git a/testdata/result/statement/!bad_select.sql.txt b/testdata/result/statement/!bad_select.sql.txt index ab2c1e9f..446ffdab 100644 --- a/testdata/result/statement/!bad_select.sql.txt +++ b/testdata/result/statement/!bad_select.sql.txt @@ -3,9 +3,8 @@ select --- Error syntax error: testdata/input/query/!bad_select.sql:2:1: unexpected token: - - 2: - ^ + 2| + | ^ --- AST diff --git a/testdata/result/statement/!bad_select_order.sql.txt b/testdata/result/statement/!bad_select_order.sql.txt index 01dd9611..0e4fc2ae 100644 --- a/testdata/result/statement/!bad_select_order.sql.txt +++ b/testdata/result/statement/!bad_select_order.sql.txt @@ -3,9 +3,8 @@ select 1 order x asc --- Error syntax error: testdata/input/query/!bad_select_order.sql:1:16: expected token: BY, but: - - 1: select 1 order x asc - ^ + 1| select 1 order x asc + | ^ --- AST diff --git a/testdata/result/statement/!bad_select_union_select.sql.txt b/testdata/result/statement/!bad_select_union_select.sql.txt index a4fc6167..913e0587 100644 --- a/testdata/result/statement/!bad_select_union_select.sql.txt +++ b/testdata/result/statement/!bad_select_union_select.sql.txt @@ -2,14 +2,11 @@ select union all select --- Error syntax error: testdata/input/query/!bad_select_union_select.sql:1:8: unexpected token: UNION - - 1: select union all select - ^~~~~ - + 1| select union all select + | ^~~~~ syntax error: testdata/input/query/!bad_select_union_select.sql:1:24: unexpected token: - - 1: select union all select - ^ + 1| select union all select + | ^ --- AST diff --git a/token/file.go b/token/file.go index 4e4af7ba..098f9ff3 100644 --- a/token/file.go +++ b/token/file.go @@ -61,15 +61,15 @@ func (f *File) Position(pos, end Pos) *Position { if count < 0 { count = 0 } - fmt.Fprintf(&source, "%3d: %s\n", line+1, lineBuffer) - fmt.Fprintf(&source, " %s^%s", strings.Repeat(" ", column), strings.Repeat("~", count)) + fmt.Fprintf(&source, "%3d| %s\n", line+1, lineBuffer) + fmt.Fprintf(&source, " | %s^%s", strings.Repeat(" ", column), strings.Repeat("~", count)) case line < endLine: for l := line; l <= endLine; l++ { if l > 0 { fmt.Fprintln(&source) } lineBuffer := f.Buffer[f.lines[l] : f.lines[l+1]-1] - fmt.Fprintf(&source, "%3d: %s", l+1, lineBuffer) + fmt.Fprintf(&source, "%3d| %s", l+1, lineBuffer) } } diff --git a/token/file_test.go b/token/file_test.go index daf96745..627077d0 100644 --- a/token/file_test.go +++ b/token/file_test.go @@ -16,7 +16,7 @@ func stripMargin(s string) string { lines[i] = line } } - return strings.Join(lines, "\n") + return strings.TrimRight(strings.Join(lines, "\n"), "\n") } var file = &File{ @@ -56,8 +56,8 @@ var positionTestCases = []struct { line: 0, column: 0, endLine: 0, endColumn: 0, source: stripMargin(heredoc.Doc(` - | 1: select 1 union all - | ^ + | 1| select 1 union all + | | ^ `)), }, { @@ -65,8 +65,8 @@ var positionTestCases = []struct { line: 0, column: 0, endLine: 0, endColumn: 1, source: stripMargin(heredoc.Doc(` - | 1: select 1 union all - | ^ + | 1| select 1 union all + | | ^ `)), }, { @@ -74,8 +74,8 @@ var positionTestCases = []struct { line: 0, column: 0, endLine: 0, endColumn: 6, source: stripMargin(heredoc.Doc(` - | 1: select 1 union all - | ^~~~~~ + | 1| select 1 union all + | | ^~~~~~ `)), }, { @@ -83,8 +83,8 @@ var positionTestCases = []struct { line: 0, column: 9, endLine: 0, endColumn: 18, source: stripMargin(heredoc.Doc(` - | 1: select 1 union all - | ^~~~~~~~~ + | 1| select 1 union all + | | ^~~~~~~~~ `)), }, { @@ -92,8 +92,8 @@ var positionTestCases = []struct { line: 0, column: 18, endLine: 1, endColumn: 0, source: stripMargin(heredoc.Doc(` - | 1: select 1 union all - | 2: select 2 + | 1| select 1 union all + | 2| select 2 `)), }, } @@ -114,7 +114,7 @@ func TestPosition(t *testing.T) { if tc.endColumn != pos.EndColumn { t.Errorf("EndColumn: %d (want) != %d (got)", tc.endColumn, pos.EndColumn) } - if strings.TrimRight(tc.source, "\n") != pos.Source { + if tc.source != pos.Source { t.Errorf("Source:\n-- want --\n%s\n-- got --\n%s\n", tc.source, pos.Source) } } diff --git a/token/quote.go b/token/quote.go index 78187b2f..13ed1657 100644 --- a/token/quote.go +++ b/token/quote.go @@ -43,7 +43,7 @@ func QuoteSQLBytes(bs []byte) string { buf.WriteString("b") buf.WriteRune(quote) for _, b := range bs { - q := quoteSingleEscape(rune(b), quote, /* isString */ false) + q := quoteSingleEscape(rune(b), quote /* isString */, false) if q != "" { buf.WriteString(q) continue @@ -75,7 +75,7 @@ func QuoteSQLIdent(s string) string { func quoteSQLStringContent(s string, quote rune, buf *bytes.Buffer) { for _, r := range s { - q := quoteSingleEscape(r, quote, /* isString */ true) + q := quoteSingleEscape(r, quote /* isString */, true) if q != "" { buf.WriteString(q) continue