token

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package token defines the lexical tokens of the SQLite SQL dialect.

Kind mirrors the TK_* token codes SQLite's tokenizer produces (src/tokenize.c) and the grammar consumes (src/parse.y). Names match the SQLite spelling so grammar attributions in the parser read directly against parse.y; where SQLite's name differs from the keyword (TK_COLUMNKW for COLUMN, TK_AUTOINCR for AUTOINCREMENT) the SQLite name is kept.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanFallback

func CanFallback(k Kind) bool

CanFallback reports whether k is in SQLite's %fallback ID set.

func IsID

func IsID(k Kind) bool

IsID reports whether k satisfies the "id" class (ID|INDEXED).

func IsIDJ

func IsIDJ(k Kind) bool

IsIDJ reports whether k satisfies the "idj" class (ID|INDEXED|JOIN_KW).

func IsIDS

func IsIDS(k Kind) bool

IsIDS reports whether k satisfies the "ids" class (ID|STRING).

func IsName

func IsName(k Kind) bool

IsName reports whether k satisfies the "nm" production (idj|STRING), the name production used for tables, columns, indexes and the like.

func IsPlainID

func IsPlainID(k Kind) bool

IsPlainID reports whether k is TK_ID, or a keyword that becomes one through %fallback. It is not one of parse.y's token classes: it is the bare ID terminal, which a few rules use directly.

generated ::= LP expr RP ID.

Types

type Kind

type Kind int

Kind identifies the lexical class of a token.

const (
	EOF Kind = iota // end of input; also the synthetic tokens SQLite appends

	// Tokenizer-only kinds. SPACE and COMMENT never reach the parser.
	SPACE
	COMMENT
	ILLEGAL // "unrecognized token"

	// Literals and names.
	ID
	STRING
	INTEGER
	FLOAT
	BLOB
	VARIABLE
	QNUMBER // numeric literal containing '_' digit separators

	// Punctuation and operators.
	LP     // (
	RP     // )
	SEMI   // ;
	COMMA  // ,
	DOT    // .
	PLUS   // +
	MINUS  // -
	STAR   // *
	SLASH  // /
	REM    // %
	EQ     // = or ==
	NE     // != or <>
	LT     // <
	LE     // <=
	GT     // >
	GE     // >=
	LSHIFT // <<
	RSHIFT // >>
	BITAND // &
	BITOR  // |
	BITNOT // ~
	CONCAT // ||
	PTR    // -> or ->>

	// Keywords, in the order of SQLite's keyword table.
	ABORT
	ACTION
	ADD
	AFTER
	ALL
	ALTER
	ALWAYS
	ANALYZE
	AND
	AS
	ASC
	ATTACH
	AUTOINCR
	BEFORE
	BEGIN
	BETWEEN
	BY
	CASCADE
	CASE
	CAST
	CHECK
	COLLATE
	COLUMNKW
	COMMIT
	CONFLICT
	CONSTRAINT
	CREATE
	CURRENT
	CTIME_KW // CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP
	DATABASE
	DEFAULT
	DEFERRED
	DEFERRABLE
	DELETE
	DESC
	DETACH
	DISTINCT
	DO
	DROP
	END
	EACH
	ELSE
	ESCAPE
	EXCEPT
	EXCLUSIVE
	EXCLUDE
	EXISTS
	EXPLAIN
	FAIL
	FILTER
	FIRST
	FOLLOWING
	FOR
	FOREIGN
	FROM
	GENERATED
	GROUP
	GROUPS
	HAVING
	IF
	IGNORE
	IMMEDIATE
	IN
	INDEX
	INDEXED
	INITIALLY
	INSERT
	INSTEAD
	INTERSECT
	INTO
	IS
	ISNULL
	JOIN
	JOIN_KW // CROSS, FULL, INNER, LEFT, NATURAL, OUTER, RIGHT
	KEY
	LAST
	LIKE_KW // GLOB, LIKE, REGEXP
	LIMIT
	MATCH
	MATERIALIZED
	NO
	NOT
	NOTHING
	NOTNULL
	NULL
	NULLS
	OF
	OFFSET
	ON
	OR
	ORDER
	OTHERS
	OVER
	PARTITION
	PLAN
	PRAGMA
	PRECEDING
	PRIMARY
	QUERY
	RAISE
	RANGE
	RECURSIVE
	REFERENCES
	REINDEX
	RELEASE
	RENAME
	REPLACE
	RESTRICT
	RETURNING
	ROLLBACK
	ROW
	ROWS
	SAVEPOINT
	SELECT
	SET
	TABLE
	TEMP
	THEN
	TIES
	TO
	TRANSACTION
	TRIGGER
	UNBOUNDED
	UNION
	UNIQUE
	UPDATE
	USING
	VACUUM
	VALUES
	VIEW
	VIRTUAL
	WHEN
	WHERE
	WINDOW
	WITH
	WITHOUT

	// KindCount is one past the last kind, so a table indexed by Kind can
	// be sized without a map.
	KindCount
)

The token kinds. The ordering carries no meaning (unlike SQLite's generated codes, which are ordered to shrink the LALR tables).

func Lookup

func Lookup(name string) Kind

Lookup returns the keyword kind for an identifier spelling, or ID if it is not a keyword. Matching is ASCII case-insensitive, as in keywordCode().

This runs once per identifier token, so it neither allocates nor copies: the candidates are found from the length and first letter alone, and each is compared against the input a byte at a time, upper-casing as it goes.

func (Kind) String

func (k Kind) String() string

String returns the SQLite TK_* name of the kind, without the prefix.

type Token

type Token struct {
	Kind Kind `json:"kind"`
	Pos  int  `json:"pos"`
	End  int  `json:"end"`
}

Token is one lexical token: a kind and the half-open byte range [Pos, End) of the input it was lexed from.

The spelling is deliberately not a field. It is exactly src[Pos:End], so storing it would duplicate what the offsets already say -- and it would put a pointer in the struct. That is not a small thing here: a token slice is the largest single allocation a parse makes, and one holding pointers costs a write barrier per token to build and has to be walked by the garbage collector afterwards. Deriving the text instead makes Token 24 bytes rather than 40 and the slice invisible to the collector, which is worth about 2.7x on the cost of producing one.

func (Token) Text

func (t Token) Text(src string) string

Text returns the token's spelling, given the input it was lexed from.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL