lex

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ExprMode = Mode(iota)
	StringMode
	TemplateMode
)
View Source
const (
	TokBad = TokenKind(-2 + iota)
	TokEOF

	TokLiteral // literal character sequence (e.g. characters in string, template text outside expr)

	TokTmplExprOpen
	TokExprOpen

	TokTmplExprClose
	TokExprClose

	TokWhitespace
	TokFieldSep
	TokIdxOpen
	TokIdxClose
	TokIdent
	TokNumber
	TokStrOpen

	TokStrClose
)
View Source
const EOF = rune(-1)

Variables

This section is empty.

Functions

func RecoverSyntaxError

func RecoverSyntaxError(to *error)

RecoverSyntaxError intercepts any SyntaxError panic and stores in *to, with no effect on other panics. Example:

func Example() (err error) {
	defer RecoverSyntaxError(&err)
	// ... code that may panic with SyntaxError here ...
}

Types

type Lex

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

func NewLex

func NewLex(s string, mode Mode) *Lex

func (*Lex) Adv

func (l *Lex) Adv()

Adv moves to lex the next Token. Subsequent calls to Peek return this Token.

func (*Lex) AdvMode

func (l *Lex) AdvMode(newMode Mode)

AdvMode is as Adv, but first switches lex to a new lexer mode. It is equivalent to, but more efficient than, calling Adv then SetMode.

func (*Lex) Mode

func (l *Lex) Mode() Mode

func (*Lex) Peek

func (l *Lex) Peek() Token

Peek returns the next Token. Subsequent calls will return the sake Token until a call to Adv, SetMode, or AdvMode.

func (*Lex) RawToken

func (l *Lex) RawToken() string

func (*Lex) SetMode

func (l *Lex) SetMode(newMode Mode)

SetMode re-lexes the characters underlying the current Token in a new lexer mode, likely changing the lexed Token. Subsequent calls to Peek return this Token.

func (*Lex) SyntaxError

func (l *Lex) SyntaxError(msg string) SyntaxError

type Mode

type Mode int

type SyntaxError

type SyntaxError struct {
	Pos  int // byte position
	RPos int // rune (character) position
	Msg  string
}

func (SyntaxError) Error

func (s SyntaxError) Error() string

type Token

type Token struct {
	Kind TokenKind
	Val  any
}

type TokenKind

type TokenKind int

Jump to

Keyboard shortcuts

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