lexer

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRuneStack

func NewRuneStack() runeStack

func NewStateStack

func NewStateStack() stateStack

Types

type L

type L struct {
	Input           string
	Start, Position int
	StartState      StateFunc
	Err             error
	Tokens          chan Token
	ErrorHandler    func(e string)
	Rewind          runeStack
	StateRecord     stateStack
}

func New

func New(src string, Start StateFunc) *L

New creates a returns a lexer ready to parse the given Input code.

func (*L) Backup

func (l *L) Backup() bool

Backup will take the last rune read (if any) and back up. Backups can occur more than once per call to Next, but you can never Backup past the last point a token was emitted.

func (*L) Current

func (l *L) Current() string

Current returns the value being analyzed at this moment.

func (*L) Emit

func (l *L) Emit(t TokenType)

Emit will receive a token type and push a new token with the current analyzed value into the Tokens channel.

func (*L) Error

func (l *L) Error(e string)

func (*L) Ignore

func (l *L) Ignore()

Ignore clears the Rewind stack and then sets the current beginning Position to the current Position in the Input, which effectively ignores the section of the Input being analyzed.

func (*L) IgnoreCharacter

func (l *L) IgnoreCharacter()

IgnoreCharacter removes the current character from the output

func (*L) Next

func (l *L) Next() rune

Next pulls the next rune from the Lexer and returns it, moving the Position forward in the Input.

func (*L) NextToken

func (l *L) NextToken() (*Token, bool)

NextToken returns the next token from the lexer and a value to denote whether or not the token is finished.

func (*L) Peek

func (l *L) Peek() rune

Peek performs a Next operation immediately followed by a Backup returning the peeked rune.

func (*L) PeekMany added in v1.0.2

func (l *L) PeekMany(n int) rune

PeekMany performs n Next operations immediately followed by n Backup operations returning the last peeked rune.

func (*L) RunLexer

func (l *L) RunLexer()

Start begins executing the Lexer in an asynchronous manner (using a goroutine).

func (*L) RunLexerSync

func (l *L) RunLexerSync()

func (*L) Take

func (l *L) Take(chars string) bool

Take receives a string containing all acceptable characters and will take the next rune if it matches an acceptable character

func (*L) TakeMany

func (l *L) TakeMany(chars string)

TakeMany receives a string containing all acceptable characters and will continue over each rune until it finds an unacceptable rune

func (*L) TakeManyPattern

func (l *L) TakeManyPattern(p *regexp.Regexp)

TakeManyPattern receives a regex pattern and will continue over each rune until a non-match is found

func (*L) TakePattern

func (l *L) TakePattern(p *regexp.Regexp) bool

TakePattern receives a regex pattern and will take the next rune if it matches the pattern

type StateFunc

type StateFunc func(*L) StateFunc

type Token

type Token struct {
	Type  TokenType
	Value string
	Start int
	End   int
}

func (Token) String

func (t Token) String() string

type TokenType

type TokenType int
const (
	EOFToken   TokenType = -1
	ErrorToken TokenType = 0
)

Jump to

Keyboard shortcuts

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