lexer

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package lexer provides a hand-written, zero-copy lexer for RFC 9535 JSONPath expressions.

Tokens store byte offsets (start, end) into the source string rather than copied substrings, enabling zero-allocation access via Token.Val. String tokens additionally store a parsed value with escape sequences resolved.

Index

Constants

This section is empty.

Variables

View Source
var ErrSyntax = errors.New("jsonpath")

ErrSyntax is the sentinel error returned by Token.Err for invalid tokens.

Functions

This section is empty.

Types

type Kind

type Kind int16

Kind identifies a lexical token type.

const (
	Invalid      Kind = iota // error token; Value holds error message
	EOF                      // end of input
	Dollar                   // $
	At                       // @
	Dot                      // .
	DotDot                   // ..
	LeftBracket              // [
	RightBracket             // ]
	LeftParen                // (
	RightParen               // )
	Star                     // *
	Question                 // ?
	Comma                    // ,
	Colon                    // :
	Equal                    // ==
	NotEqual                 // !=
	Less                     // <
	LessEqual                // <=
	Greater                  // >
	GreaterEqual             // >=
	And                      // &&
	Or                       // ||
	Not                      // !
	Ident                    // identifier (member-name-shorthand / function name)
	Int                      // integer literal
	Number                   // number (float) literal
	String                   // single- or double-quoted string; Value holds parsed content
	True                     // true
	False                    // false
	Null                     // null
)

Kind values.

func (Kind) String

func (k Kind) String() string

String returns the human-readable name of k.

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer tokenizes an RFC 9535 JSONPath expression. Create with New and call Lexer.Scan repeatedly to get tokens.

func New

func New(src string) *Lexer

New creates a Lexer for src.

func (*Lexer) Scan

func (l *Lexer) Scan() Token

Scan returns the next token. After EOF is returned, subsequent calls continue returning EOF.

func (*Lexer) Source

func (l *Lexer) Source() string

Source returns the original source string.

type Token

type Token struct {
	Kind  Kind   // Kind is the token type.
	Start int    // byte offset in source (inclusive)
	End   int    // byte offset in source (exclusive)
	Value string // parsed value for String; error message for Invalid
}

Token represents a single lexical token. Use Token.Val for zero-copy access to the raw source text. For String tokens, Token.Value holds the parsed string with escape sequences resolved.

func (Token) Err

func (t Token) Err() error

Err returns a parse error for Invalid tokens and nil for all others.

func (Token) Val

func (t Token) Val(src string) string

Val returns the raw source substring — no allocation.

Jump to

Keyboard shortcuts

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