glex

package
v0.0.0-...-cf82ea8 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Just a random lexer.

Index

Constants

View Source
const (
	NoMatch              = Error("No match was found in the list")
	Eof                  = Error("End of input")
	ErrInvalidUtf8String = Error("Input string isn't a valid UTF-8 sequence")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error string

Lexer errors

func (Error) Error

func (e Error) Error() string

Error interface

func (Error) String

func (e Error) String() string

Stringer interface

type Lexer

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

Hold the information to perform the lexical scanning of a given input

func NewLexer

func NewLexer(input string) (*Lexer, error)

Create a new lexer from the given input string

func NewLexerFromFile

func NewLexerFromFile(file string) (*Lexer, error)

Create a new lexer loading it's contents from the given file file contents MUST BE valid UTF-8 strings.

func (*Lexer) Eof

func (l *Lexer) Eof(err error) bool

Return true only if the error is Eof

func (*Lexer) NewRule

func (l *Lexer) NewRule(exp string, token int) error

Include a new rule into the lexer, same logic used by RuleList

func (*Lexer) Next

func (l *Lexer) Next() (Match, error)

Try to find a match for the current input and move the cursor, if the returned token is < 0, the input is consumed and the lexer tries to advance again.

If not match is found, return's an error. Later the function IsEof can be used to check if the error represent the end of input.

type Match

type Match struct {
	// text of the match
	Text string

	// token number
	Token int
}

Interface that define's a Match

type Rule

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

Represent a rule for the lexer

func NewRule

func NewRule(re string, token int) (*Rule, error)

Create a new rule based on the given regular expression.

If the re don't match the start of the string (^), it will have the (^) character inserted.

func (*Rule) Match

func (r *Rule) Match(input string) (Match, string, error)

Check if the rule can match the given string

When the input string is empty, err will hold Eof, if no match is found, err will hold NoMatch

On any other cases, this will return a Match, the rest of the string and nil.

type RuleList

type RuleList []*Rule

Hold the list of rules

func (RuleList) Match

func (l RuleList) Match(input string) (Match, string, error)

Try to match with one of the rules from the list

This method simply call's the Match method of each Rule in the list.

func (*RuleList) NewRule

func (l *RuleList) NewRule(exp string, token int) (*Rule, error)

Include the given expression/token pair into this rule list.

If there are erros in the epxression, the rule isn't included and the error is returned.

Jump to

Keyboard shortcuts

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