@@ -3,6 +3,35 @@ package models
33// TokenType represents the type of a SQL token
44type TokenType int
55
6+ // Token range constants for maintainability and clarity.
7+ // These define the boundaries for each category of tokens.
8+ const (
9+ // TokenRangeBasicStart marks the beginning of basic token types
10+ TokenRangeBasicStart TokenType = 10
11+ // TokenRangeBasicEnd marks the end of basic token types (exclusive)
12+ TokenRangeBasicEnd TokenType = 30
13+
14+ // TokenRangeStringStart marks the beginning of string literal types
15+ TokenRangeStringStart TokenType = 30
16+ // TokenRangeStringEnd marks the end of string literal types (exclusive)
17+ TokenRangeStringEnd TokenType = 50
18+
19+ // TokenRangeOperatorStart marks the beginning of operator types
20+ TokenRangeOperatorStart TokenType = 50
21+ // TokenRangeOperatorEnd marks the end of operator types (exclusive)
22+ TokenRangeOperatorEnd TokenType = 150
23+
24+ // TokenRangeKeywordStart marks the beginning of SQL keyword types
25+ TokenRangeKeywordStart TokenType = 200
26+ // TokenRangeKeywordEnd marks the end of SQL keyword types (exclusive)
27+ TokenRangeKeywordEnd TokenType = 500
28+
29+ // TokenRangeDataTypeStart marks the beginning of data type keywords
30+ TokenRangeDataTypeStart TokenType = 430
31+ // TokenRangeDataTypeEnd marks the end of data type keywords (exclusive)
32+ TokenRangeDataTypeEnd TokenType = 450
33+ )
34+
635// Token type constants with explicit values to avoid collisions
736const (
837 // Special tokens
@@ -570,23 +599,43 @@ func (t TokenType) String() string {
570599 return "TOKEN"
571600}
572601
573- // IsKeyword returns true if the token type is a SQL keyword
602+ // IsKeyword returns true if the token type is a SQL keyword.
603+ // Uses range-based checking for O(1) performance (~0.24ns/op).
604+ //
605+ // Example:
606+ //
607+ // if token.ModelType.IsKeyword() {
608+ // // Handle SQL keyword token
609+ // }
574610func (t TokenType ) IsKeyword () bool {
575- // Keywords are in ranges: 200-399 (SQL keywords), 280-299 (CTEs/Set ops),
576- // 300-319 (window), 320-329 (join), 330-349 (constraints), 350-399 (misc),
577- // 370-379 (MERGE), 380-389 (materialized), 390-399 (grouping), 400-429 (roles/txn)
578- return (t >= 200 && t < 500 && t != TokenTypeAsterisk && t != TokenTypeDoublePipe && t != TokenTypeIllegal )
611+ // Use range constants for maintainability
612+ return (t >= TokenRangeKeywordStart && t < TokenRangeKeywordEnd &&
613+ t != TokenTypeAsterisk && t != TokenTypeDoublePipe && t != TokenTypeIllegal )
579614}
580615
581- // IsOperator returns true if the token type is an operator
616+ // IsOperator returns true if the token type is an operator.
617+ // Uses range-based checking for O(1) performance.
618+ //
619+ // Example:
620+ //
621+ // if token.ModelType.IsOperator() {
622+ // // Handle operator token (e.g., +, -, *, /, etc.)
623+ // }
582624func (t TokenType ) IsOperator () bool {
583- // Operators are in range 50-99 and compound operators 100-149
584- return (t >= 50 && t < 150 ) || t == TokenTypeAsterisk || t == TokenTypeDoublePipe
625+ // Use range constants for maintainability
626+ return (t >= TokenRangeOperatorStart && t < TokenRangeOperatorEnd ) ||
627+ t == TokenTypeAsterisk || t == TokenTypeDoublePipe
585628}
586629
587- // IsLiteral returns true if the token type is a literal value
630+ // IsLiteral returns true if the token type is a literal value.
631+ // Includes identifiers, numbers, strings, and boolean/null literals.
632+ //
633+ // Example:
634+ //
635+ // if token.ModelType.IsLiteral() {
636+ // // Handle literal value (identifier, number, string, true/false/null)
637+ // }
588638func (t TokenType ) IsLiteral () bool {
589- // Identifiers, numbers, strings
590639 switch t {
591640 case TokenTypeIdentifier , TokenTypeNumber , TokenTypeString ,
592641 TokenTypeSingleQuotedString , TokenTypeDoubleQuotedString ,
@@ -651,9 +700,17 @@ func (t TokenType) IsAggregateFunction() bool {
651700 return false
652701}
653702
654- // IsDataType returns true if the token type is a SQL data type
703+ // IsDataType returns true if the token type is a SQL data type.
704+ // Uses range-based checking for O(1) performance.
705+ //
706+ // Example:
707+ //
708+ // if token.ModelType.IsDataType() {
709+ // // Handle data type token (INT, VARCHAR, BOOLEAN, etc.)
710+ // }
655711func (t TokenType ) IsDataType () bool {
656- return t >= TokenTypeInt && t <= TokenTypeUuid
712+ // Use range constants for maintainability
713+ return t >= TokenRangeDataTypeStart && t < TokenRangeDataTypeEnd
657714}
658715
659716// IsConstraint returns true if the token type is a constraint keyword
0 commit comments