Skip to content

Commit 8b3a6bc

Browse files
committed
Support identifiers starting with digits in lexer
ClickHouse allows identifiers like '1alias1name1' as aliases after AS. The lexer now recognizes these as identifiers rather than treating the leading digits as a NUMBER token followed by an IDENT token. The change adds a check in readNumberOrIdent to detect when digits are directly followed by letters (excluding exponent notation like 1e5 and base prefixes like 0x, 0b, 0o).
1 parent f8d2efd commit 8b3a6bc

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

lexer/lexer.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,22 @@ func (l *Lexer) readNumberOrIdent() Item {
766766
}
767767
}
768768

769+
// Check if directly followed by letter (identifier like 1alias1name1)
770+
// Exclude exponent (e/E followed by digit/+/-) and base prefixes (0x, 0b, 0o)
771+
if unicode.IsLetter(l.ch) {
772+
val := sb.String()
773+
isExponent := (l.ch == 'e' || l.ch == 'E') && (unicode.IsDigit(l.peekChar()) || l.peekChar() == '+' || l.peekChar() == '-')
774+
isBasePrefix := val == "0" && (l.ch == 'x' || l.ch == 'X' || l.ch == 'b' || l.ch == 'B' || l.ch == 'o' || l.ch == 'O')
775+
if !isExponent && !isBasePrefix {
776+
// This is an identifier that starts with digits (e.g., 1alias1name1)
777+
for isIdentChar(l.ch) {
778+
sb.WriteRune(l.ch)
779+
l.readChar()
780+
}
781+
return Item{Token: token.IDENT, Value: sb.String(), Pos: pos}
782+
}
783+
}
784+
769785
// Not an identifier, continue as number
770786
// But we already consumed the digits, so continue from here
771787
// Handle underscore separators in numbers (only if followed by a digit)
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt2": true
4-
}
5-
}
1+
{}

0 commit comments

Comments
 (0)