Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Modify to distinguish errors between current and peek #96

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ git-push-skip-local-ci: ## Run git push with skip local CI
git push
-mv ${REPO_ROOT}/.git/hooks/pre-push{.bak,}

.PHONY: git-force-push-skip-local-ci
git-force-push-skip-local-ci: ## Run git push with skip local CI
-mv ${REPO_ROOT}/.git/hooks/pre-push{,.bak}
git push -f
-mv ${REPO_ROOT}/.git/hooks/pre-push{.bak,}

.PHONY: act-check
act-check:
@if ! command -v act >/dev/null 2>&1; then \
Expand Down
42 changes: 25 additions & 17 deletions pkg/ddl/cockroachdb/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ LabelDDL:
case TOKEN_EOF:
break LabelDDL
default:
return nil, apperr.Errorf("currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
return nil, apperr.Errorf("currentToken=%#v, peekToken=%#v: %w", p.currentToken, p.peekToken, ddl.ErrUnexpectedCurrentToken)
}

p.nextToken()
Expand All @@ -107,11 +107,19 @@ func (p *Parser) parseCreateStatement() (Stmt, error) { //nolint:ireturn

switch p.currentToken.Type { //nolint:exhaustive
case TOKEN_TABLE:
return p.parseCreateTableStmt()
stmt, err := p.parseCreateTableStmt()
if err != nil {
return nil, apperr.Errorf("parseCreateTableStmt: %w", err)
}
return stmt, nil
case TOKEN_INDEX, TOKEN_UNIQUE:
return p.parseCreateIndexStmt()
stmt, err := p.parseCreateIndexStmt()
if err != nil {
return nil, apperr.Errorf("parseCreateIndexStmt: %w", err)
}
return stmt, nil
default:
return nil, apperr.Errorf("currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
return nil, apperr.Errorf("currentToken=%#v, peekToken=%#v: %w", p.currentToken, p.peekToken, ddl.ErrUnexpectedCurrentToken)
}
}

Expand Down Expand Up @@ -167,7 +175,7 @@ LabelColumns:
case isConstraint(p.currentToken.Type):
constraint, err := p.parseTableConstraint(createTableStmt.Name.Name)
if err != nil {
return nil, apperr.Errorf(errFmtPrefix+"parseConstraint: %w", err)
return nil, apperr.Errorf(errFmtPrefix+"parseTableConstraint: %w", err)
}
createTableStmt.Constraints = createTableStmt.Constraints.Append(constraint)
case p.isCurrentToken(TOKEN_COMMA):
Expand All @@ -178,10 +186,10 @@ LabelColumns:
case TOKEN_SEMICOLON, TOKEN_EOF:
break LabelColumns
default:
return nil, apperr.Errorf(errFmtPrefix+"peekToken=%#v: %w", p.peekToken, ddl.ErrUnexpectedToken)
return nil, apperr.Errorf(errFmtPrefix+"currentToken=%#v, peekToken=%#v: %w", p.currentToken, p.peekToken, ddl.ErrUnexpectedPeekToken)
}
default:
return nil, apperr.Errorf(errFmtPrefix+"currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
return nil, apperr.Errorf(errFmtPrefix+"currentToken=%#v, peekToken=%#v: %w", p.currentToken, p.peekToken, ddl.ErrUnexpectedCurrentToken)
}
}

Expand Down Expand Up @@ -313,7 +321,7 @@ func (p *Parser) parseColumn(tableName *Ident) (*Column, []Constraint, error) {
}
}
default:
return nil, nil, apperr.Errorf(errFmtPrefix+"currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
return nil, nil, apperr.Errorf(errFmtPrefix+"currentToken=%#v, peekToken=%#v: %w", p.currentToken, p.peekToken, ddl.ErrUnexpectedCurrentToken)
}

return column, constraints, nil
Expand Down Expand Up @@ -356,7 +364,7 @@ LabelDefault:
if isConstraint(p.currentToken.Type) {
break LabelDefault
}
return nil, apperr.Errorf("currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
return nil, apperr.Errorf("currentToken=%#v, peekToken=%#v: %w", p.currentToken, p.peekToken, ddl.ErrUnexpectedCurrentToken)
}

p.nextToken()
Expand Down Expand Up @@ -398,7 +406,7 @@ LabelExpr:
}
idents = append(idents, NewRawIdent(value))
case TOKEN_EOF:
return nil, apperr.Errorf("currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
return nil, apperr.Errorf("currentToken=%#v, peekToken=%#v: %w", p.currentToken, p.peekToken, ddl.ErrUnexpectedCurrentToken)
default:
if isReservedValue(p.currentToken.Type) {
idents = append(idents, NewRawIdent(p.currentToken.Type.String()))
Expand Down Expand Up @@ -492,7 +500,7 @@ LabelConstraints:
case TOKEN_IDENT, TOKEN_COMMA, TOKEN_CLOSE_PAREN:
break LabelConstraints
default:
return nil, apperr.Errorf("currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
return nil, apperr.Errorf("currentToken=%#v, peekToken=%#v: %w", p.currentToken, p.peekToken, ddl.ErrUnexpectedCurrentToken)
}

p.nextToken()
Expand All @@ -507,7 +515,7 @@ func (p *Parser) parseTableConstraint(tableName *Ident) (Constraint, error) { //
if p.isCurrentToken(TOKEN_CONSTRAINT) {
p.nextToken() // current = constraint_name
if p.currentToken.Type != TOKEN_IDENT {
return nil, apperr.Errorf("currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
return nil, apperr.Errorf("currentToken=%#v, peekToken=%#v: %w", p.currentToken, p.peekToken, ddl.ErrUnexpectedCurrentToken)
}
constraintName = NewRawIdent(p.currentToken.Literal.Str)
p.nextToken() // current = PRIMARY or CHECK //diff:ignore-line-postgres-cockroach
Expand Down Expand Up @@ -622,7 +630,7 @@ func (p *Parser) parseTableConstraint(tableName *Ident) (Constraint, error) { //
c.Columns = idents
return c, nil
default:
return nil, apperr.Errorf("currentToken=%s: %w", p.currentToken.Type, ddl.ErrUnexpectedToken)
return nil, apperr.Errorf("currentToken=%#v, peekToken=%#v: %w", p.currentToken, p.peekToken, ddl.ErrUnexpectedCurrentToken)
}
}

Expand Down Expand Up @@ -708,7 +716,7 @@ LabelIdents:
p.nextToken()
break LabelIdents
default:
return nil, apperr.Errorf("currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
return nil, apperr.Errorf("currentToken=%#v, peekToken=%#v: %w", p.currentToken, p.peekToken, ddl.ErrUnexpectedCurrentToken)
}
p.nextToken()
}
Expand All @@ -729,7 +737,7 @@ LabelIdents:
case TOKEN_CLOSE_PAREN:
break LabelIdents
case TOKEN_EOF, TOKEN_ILLEGAL:
return nil, apperr.Errorf("currentToken=%#v: %w", p.currentToken, ddl.ErrUnexpectedToken)
return nil, apperr.Errorf("currentToken=%#v, peekToken=%#v: %w", p.currentToken, p.peekToken, ddl.ErrUnexpectedCurrentToken)
default:
idents = append(idents, NewRawIdent(p.currentToken.Literal.Str))
}
Expand Down Expand Up @@ -807,7 +815,7 @@ func (p *Parser) checkCurrentToken(expectedTypes ...TokenType) error {
return nil
}
}
return apperr.Errorf("currentToken: expected=%s, but got=%#v: %w", stringz.JoinStringers(",", expectedTypes...), p.currentToken, ddl.ErrUnexpectedToken)
return apperr.Errorf("currentToken=%#v, peekToken=%#v: expected=%v, but got=%v: %w", p.currentToken, p.peekToken, stringz.JoinStringers(",", expectedTypes...), p.currentToken.Type, ddl.ErrUnexpectedCurrentToken)
}

func (p *Parser) isPeekToken(expectedTypes ...TokenType) bool {
Expand All @@ -825,5 +833,5 @@ func (p *Parser) checkPeekToken(expectedTypes ...TokenType) error {
return nil
}
}
return apperr.Errorf("peekToken: expected=%s, but got=%#v: %w", stringz.JoinStringers(",", expectedTypes...), p.peekToken, ddl.ErrUnexpectedToken)
return apperr.Errorf("currentToken=%#v, peekToken=%#v: expected=%v, but got=%v: %w", p.currentToken, p.peekToken, stringz.JoinStringers(",", expectedTypes...), p.peekToken.Type, ddl.ErrUnexpectedPeekToken)
}
Loading
Loading