lexer

package
v0.0.0-...-dcc2aae Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// keyword
	SelectString = "select"
	FromString   = "from"
	AsString     = "as"
	WhereString  = "where"
	AndString    = "and"
	OrString     = "or"
	// comparison operator
	GEString        = ">="
	LEString        = "<="
	NotEqual1String = "!="
	NotEqual2String = "<>"
)
View Source
const (
	// char
	UnderBarRune = '_'
	// comparison operator
	GTRune          = '>'
	LTRune          = '<'
	EqualRune       = '='
	ExclamationRune = '!'
	// arithmetic operator
	PlusRune     = '+'
	MinusRune    = '-'
	MultiplyRune = '*'
	DivideRune   = '/'
	ModRune      = '%'
	// separator
	CommaRune            = ','
	SemicolonRune        = ';'
	LeftParenthesisRune  = '('
	RightParenthesisRune = ')'
	SingleQuoteRune      = '\''
	// white space
	SpaceRune   = ' '
	TabRune     = '\t'
	ReturnRune  = '\r'
	NewLineRune = '\n'
)
View Source
const (
	EpsilonRune = 'ε'
)

Variables

Functions

func IsAlphabet

func IsAlphabet(c rune) bool

IsAlphabet returns if the given rune is an alphabet

func IsAlphabetOrDigit

func IsAlphabetOrDigit(c rune) bool

IsWhiteSpace returns if the given rune is either an alphabet or a digit

func IsDigit

func IsDigit(c rune) bool

IsDigit returns if the given rune is a digit

func IsWhiteSpace

func IsWhiteSpace(c rune) bool

IsWhiteSpace returns if the given rune is a white space

Types

type CharacterSet

type CharacterSet struct {
	Alphabets []rune
	Digits    []rune
}

func NewCharacterSet

func NewCharacterSet(alphabets, digits []rune) *CharacterSet

NewCharacterSet returns a new *CharacterSet

func NewCharacterSetWithDefault

func NewCharacterSetWithDefault() *CharacterSet

NewCharacterSetWithDefault returns a new *CharacterSet with default

func (*CharacterSet) GetAlphabets

func (cs *CharacterSet) GetAlphabets() []rune

GetAlphabets returns the alphabet runes

func (*CharacterSet) GetDigits

func (cs *CharacterSet) GetDigits() []rune

GetAlphabets returns the digit runes

type DFA

type DFA struct {
	CharacterSet *CharacterSet
	NFA          *NFA
	Index        int
	InitSet      *Set
}

func NewDFA

func NewDFA(cs *CharacterSet) *DFA

NewDFA returns a new *DFA

func NewDFAWithDefault

func NewDFAWithDefault() *DFA

NewDFAWithDefault returns a new *DFA with default

func (*DFA) Match

func (dfa *DFA) Match(runes []rune) *token.Token

Match matches the given runes and returns proper token

func (*DFA) Print

func (dfa *DFA) Print()

Print prints all the sets including the state in it

type Lexer

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

func NewLexer

func NewLexer(fa dependency.Lexer) *Lexer

NewLexer returns a new *Lexer

func (*Lexer) GetFiniteAutomata

func (l *Lexer) GetFiniteAutomata() dependency.Lexer

GetFiniteAutomata returns the finite automata of the lexer

func (*Lexer) Lex

func (l *Lexer) Lex(sql string) []*token.Token

Lex scans the input string and returns a token list

type NFA

type NFA struct {
	CharacterSet *CharacterSet
	Index        int
	InitState    *State
}

func NewNFA

func NewNFA(cs *CharacterSet) *NFA

NewNFA returns a new *NFA

func NewNFAWithDefault

func NewNFAWithDefault() *NFA

NewNFAWithDefault returns a new *NFA with default

func (*NFA) Match

func (nfa *NFA) Match(runes []rune) *token.Token

Match matches the given runes and returns proper token

func (*NFA) Print

func (nfa *NFA) Print()

Print prints all the states

type Set

type Set struct {
	States    []*State
	Index     int
	Next      map[rune]*Set
	IsFinal   bool
	TokenType token.Type
}

func NewSet

func NewSet(i int) *Set

NewSet returns a new *Set

func (*Set) AddNext

func (s *Set) AddNext(c rune, ns *Set)

AddNext adds the next set of given rune

func (*Set) AddState

func (s *Set) AddState(state *State)

AddState add the given state into the set,

  • if a same state already exists in the set, it will be ignored
  • if the new state is a final state and there is already a final state exists in the set, only one state of which token type is keyword stays in the set

func (*Set) Contains

func (s *Set) Contains(state *State) bool

Contains returns if the given state is in the set

func (*Set) Equal

func (s *Set) Equal(other *Set) bool

Equal returns if the twe sets contains the same states, the order of the states does not matter

func (*Set) GetFinalState

func (s *Set) GetFinalState() (int, *State)

GetFinalState returns the final state in the set

func (*Set) Print

func (s *Set) Print()

Print prints the set and all the next sets recursively

func (*Set) String

func (s *Set) String() string

String returns the string representation of the set

type State

type State struct {
	Index     int
	Next      map[rune][]*State
	IsFinal   bool
	TokenType token.Type
}

func NewState

func NewState(i int) *State

NewState returns a new *State

func (*State) AddNext

func (s *State) AddNext(c rune, ns *State)

AddNext adds the next state of the given rune

func (*State) EpsilonMove

func (s *State) EpsilonMove() []*State

EpsilonMove gets all the states that can transit to by epsilon move, it includes itself

func (*State) Print

func (s *State) Print()

Print prints the state and all the next states recursively

Jump to

Keyboard shortcuts

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