Skip to content

Commit 746f0b7

Browse files
authored
Improve statement end position handling to prevent null pointer exceptions (#162)
1 parent 0254798 commit 746f0b7

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

parser/parse_system.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ func (p *Parser) parseGrantPrivilegeStmt(pos Pos) (*GrantPrivilegeStmt, error) {
981981
return nil, err
982982
}
983983
if len(options) != 0 {
984-
statementEnd = p.last().End
984+
statementEnd = p.End()
985985
}
986986

987987
return &GrantPrivilegeStmt{

parser/parser_column.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ func (p *Parser) tryParseCompressionCodecs(pos Pos) (*CompressionCodec, error) {
10821082
}
10831083
}
10841084

1085-
rightParenPos := p.last().End
1085+
rightParenPos := p.End()
10861086
if err := p.expectTokenKind(TokenKindRParen); err != nil {
10871087
return nil, err
10881088
}

parser/parser_common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ func (p *Parser) last() *Token {
2727
return p.lexer.lastToken
2828
}
2929

30+
func (p *Parser) End() Pos {
31+
if p.last() == nil {
32+
return Pos(p.lexer.current + 1)
33+
}
34+
return p.last().End
35+
}
36+
3037
func (p *Parser) Pos() Pos {
3138
last := p.last()
3239
if last == nil {

parser/parser_query.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (p *Parser) parseTopClause(pos Pos) (*TopClause, error) {
5757

5858
withTies := false
5959
if p.tryConsumeKeywords(KeywordWith) {
60-
topEnd = p.last().End
60+
topEnd = p.End()
6161
if err := p.expectKeyword(KeywordTies); err != nil {
6262
return nil, err
6363
}
@@ -196,7 +196,7 @@ func (p *Parser) parseJoinTableExpr(_ Pos) (Expr, error) {
196196

197197
hasFinal := p.matchKeyword(KeywordFinal)
198198
if hasFinal {
199-
statementEnd = p.last().End
199+
statementEnd = p.End()
200200
_ = p.lexer.consumeToken()
201201
}
202202

@@ -587,7 +587,7 @@ func (p *Parser) parseWindowFrameClause(pos Pos) (*WindowFrameClause, error) {
587587
case p.matchKeyword(KeywordCurrent):
588588
currentPos := p.Pos()
589589
_ = p.lexer.consumeToken()
590-
rowEnd := p.last().End
590+
rowEnd := p.End()
591591
if err := p.expectKeyword(KeywordRow); err != nil {
592592
return nil, err
593593
}
@@ -622,7 +622,7 @@ func (p *Parser) parseWindowFrameClause(pos Pos) (*WindowFrameClause, error) {
622622
switch {
623623
case p.matchKeyword(KeywordPreceding), p.matchKeyword(KeywordFollowing):
624624
direction = p.last().String
625-
unboundedEnd = p.last().End
625+
unboundedEnd = p.End()
626626
_ = p.lexer.consumeToken()
627627
default:
628628
return nil, fmt.Errorf("expected PRECEDING or FOLLOWING, got %s", p.lastTokenKind())

parser/parser_table.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ func (p *Parser) parseTableSchemaClause(pos Pos) (*TableSchemaClause, error) {
344344
}
345345
return &TableSchemaClause{
346346
SchemaPos: pos,
347-
SchemaEnd: p.last().End,
347+
SchemaEnd: p.End(),
348348
TableFunction: &TableFunctionExpr{
349349
Name: ident,
350350
Args: argsExpr,
@@ -353,7 +353,7 @@ func (p *Parser) parseTableSchemaClause(pos Pos) (*TableSchemaClause, error) {
353353
default:
354354
return &TableSchemaClause{
355355
SchemaPos: pos,
356-
SchemaEnd: p.last().End,
356+
SchemaEnd: p.End(),
357357
AliasTable: &TableIdentifier{
358358
Table: ident,
359359
},
@@ -1322,7 +1322,7 @@ func (p *Parser) parseInsertStmt(pos Pos) (*InsertStmt, error) {
13221322
default:
13231323
// do nothing
13241324
}
1325-
1325+
13261326
if err != nil {
13271327
return nil, err
13281328
}

0 commit comments

Comments
 (0)