lexer

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2021 License: Apache-2.0 Imports: 5 Imported by: 0

README

Lexer

This is a reusable lexer.

To install:

go get -u github.com/adroge/lexer

This is an example of usage:

package main

import (
	"context"
	"fmt"

	"github.com/adroge/lexer"
)

func main() {
	lex := lexer.Build("start {{setting, x:y}} middle {{pi:3.14}} end text.")
	lex.Run(context.Background())

	for token := lex.NextToken(); token.Type != lexer.TokenUndefined; token = lex.NextToken() {
		fmt.Printf("token: %s\n text: \"%s\"\n\n", token.Type, token.Value)
	}
}

Another source of usage are the unit tests.

Rob Pike's Lexer from his presentation was used as inspiration.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMetaZeroLength     = errors.New("meta tag cannot be zero length")
	ErrMetaIndicatorMatch = errors.New("indicator cannot match separator")
)

Functions

func Create

func Create(input string) lexer

Create creates a new lexer. input is the string to be tokenized

func SetMeta

func SetMeta(left, right string, valueIndicator, valueSeparator rune) (err error)

SetMeta globally sets meta values to something other than the default.

err := lexer.SetMeta("<<", ">>", '=', '|')

Types

type Token

type Token struct {
	Type  TokenType
	Value string
}

func (Token) String

func (t Token) String() string

type TokenType

type TokenType int
const (
	TokenUndefined TokenType = iota
	TokenPlainText
	TokenLeftMeta
	TokenMetaIdentifier
	TokenMetaNumberValue
	TokenMetaTextValue
	TokenRightMeta
	TokenError
	TokenEof
)

func (TokenType) String

func (t TokenType) String() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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