scanner

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2021 License: BSD-3-Clause Imports: 7 Imported by: 1

Documentation

Index

Constants

View Source
const (
	EOF       = scanner.EOF
	Ident     = scanner.Ident
	Int       = scanner.Int
	Float     = scanner.Float
	Char      = scanner.Char
	String    = scanner.String
	RawString = scanner.RawString
	Comment   = scanner.Comment
)

EOF is identical to text/scanner.EOF. Token types are replicated here for practical reasons.

Variables

View Source
var AnyToken []int = nil

AnyToken is a helper flag: expect any token from the scanner.

Functions

func Lexeme

func Lexeme(token interface{}) string

Lexeme is a helper function to receive a string from a token.

func MakeToken

func MakeToken(name string, id int) lex.Action

MakeToken is a pre-defined action which wraps a scanned match into a token.

func Skip

func Skip(*lex.Scanner, *machines.Match) (interface{}, error)

Skip is a pre-defined action which ignores the scanned match.

Types

type DefaultTokenizer

type DefaultTokenizer struct {
	scanner.Scanner

	Error func(error) // error handler
	// contains filtered or unexported fields
}

DefaultTokenizer is a default implementation, backed by scanner.Scanner. Create one with GoTokenizer.

func GoTokenizer

func GoTokenizer(sourceID string, input io.Reader, opts ...Option) *DefaultTokenizer

GoTokenizer creates a scanner/tokenizer accepting tokens similar to the Go language.

func (*DefaultTokenizer) NextToken

func (t *DefaultTokenizer) NextToken(exp []int) (int, interface{}, uint64, uint64)

NextToken is part of the Tokenizer interface.

func (*DefaultTokenizer) SetErrorHandler

func (t *DefaultTokenizer) SetErrorHandler(h func(error))

SetErrorHandler sets an error handler for the scanner.

type LMAdapter

type LMAdapter struct {
	Lexer *lex.Lexer
}

LMAdapter is a lexmachine adapter to use lexmachine as a scanner.

func NewLMAdapter

func NewLMAdapter(init func(*lex.Lexer), literals []string, keywords []string, tokenIds map[string]int) (*LMAdapter, error)

NewLMAdapter creates a new lexmachine adapter. It receives a list of literals ('[', ';', …), a list of keywords ("if", "for", …) and a map for translating token strings to their values.

NewLMAdapter will return an error if compiling the DFA failed.

func (*LMAdapter) Scanner

func (lm *LMAdapter) Scanner(input string) (*LMScanner, error)

Scanner creates a scanner for a given input. The scanner will implement the Tokenizer interface.

type LMScanner

type LMScanner struct {
	Error func(error)
	// contains filtered or unexported fields
}

LMScanner is a scanner type for lexmachine scanners, implementing the Tokenizer interface.

func (*LMScanner) NextToken

func (lms *LMScanner) NextToken(expected []int) (int, interface{}, uint64, uint64)

NextToken is part of the Tokenizer interface.

Warning: The current implementation will ignore the 'expected'-argument.

func (*LMScanner) SetErrorHandler

func (lms *LMScanner) SetErrorHandler(h func(error))

SetErrorHandler sets an error handler for the scanner.

type Option

type Option func(p *DefaultTokenizer)

Option configures a default tokenier.

func SkipComments

func SkipComments(b bool) Option

SkipComments set or clears mode-flag SkipComments.

func UnifyStrings

func UnifyStrings(b bool) Option

UnifyStrings sets or clears option UnifyStrings: treat raw strings and single chars as strings.

type Tokenizer

type Tokenizer interface {
	NextToken(expected []int) (tokval int, token interface{}, start, len uint64)
	SetErrorHandler(func(error))
}

Tokenizer is a scanner interface.

Jump to

Keyboard shortcuts

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