2020-10-08 23:59:03 +00:00
|
|
|
package treeops
|
2020-09-17 11:58:01 +00:00
|
|
|
|
|
|
|
import (
|
2020-09-17 12:12:56 +00:00
|
|
|
"strconv"
|
2020-09-17 11:58:01 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
lex "github.com/timtadh/lexmachine"
|
|
|
|
"github.com/timtadh/lexmachine/machines"
|
|
|
|
)
|
|
|
|
|
2020-09-24 03:28:47 +00:00
|
|
|
var Literals []string // The tokens representing literal strings
|
|
|
|
var Keywords []string // The keyword tokens
|
|
|
|
var Tokens []string // All of the tokens (including literals and keywords)
|
|
|
|
var TokenIds map[string]int // A map from the token names to their int ids
|
|
|
|
|
|
|
|
var bracketLiterals []string
|
2020-09-17 11:58:01 +00:00
|
|
|
|
|
|
|
func initTokens() {
|
2020-09-24 03:28:47 +00:00
|
|
|
bracketLiterals = []string{"(", ")"}
|
2020-09-24 00:52:45 +00:00
|
|
|
Literals = []string{ // these need a traverse operator infront
|
2020-09-17 11:58:01 +00:00
|
|
|
"[+]",
|
|
|
|
"[*]",
|
|
|
|
"**",
|
|
|
|
}
|
|
|
|
Tokens = []string{
|
2020-09-20 12:40:09 +00:00
|
|
|
"OR_OPERATOR",
|
|
|
|
"AND_OPERATOR",
|
|
|
|
"EQUALS_OPERATOR",
|
2020-09-24 00:52:45 +00:00
|
|
|
"EQUALS_SELF_OPERATOR",
|
|
|
|
"TRAVERSE_OPERATOR",
|
2020-09-18 07:08:33 +00:00
|
|
|
"PATH_KEY", // apples
|
2020-09-24 00:52:45 +00:00
|
|
|
"ARRAY_INDEX", // 123
|
2020-09-17 11:58:01 +00:00
|
|
|
}
|
2020-09-24 03:28:47 +00:00
|
|
|
Tokens = append(Tokens, bracketLiterals...)
|
2020-09-17 11:58:01 +00:00
|
|
|
Tokens = append(Tokens, Literals...)
|
|
|
|
TokenIds = make(map[string]int)
|
|
|
|
for i, tok := range Tokens {
|
|
|
|
TokenIds[tok] = i
|
|
|
|
}
|
2020-09-20 12:40:09 +00:00
|
|
|
|
|
|
|
initMaps()
|
2020-09-17 11:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func skip(*lex.Scanner, *machines.Match) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func token(name string) lex.Action {
|
|
|
|
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
|
|
|
|
return s.Token(TokenIds[name], string(m.Bytes), m), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 12:12:56 +00:00
|
|
|
func unwrap(value string) string {
|
|
|
|
return value[1 : len(value)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrappedToken(name string) lex.Action {
|
|
|
|
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
|
|
|
|
return s.Token(TokenIds[name], unwrap(string(m.Bytes)), m), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func numberToken(name string, wrapped bool) lex.Action {
|
|
|
|
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
|
|
|
|
var numberString = string(m.Bytes)
|
|
|
|
if wrapped {
|
|
|
|
numberString = unwrap(numberString)
|
|
|
|
}
|
|
|
|
var number, errParsingInt = strconv.ParseInt(numberString, 10, 64) // nolint
|
|
|
|
if errParsingInt != nil {
|
|
|
|
return nil, errParsingInt
|
|
|
|
}
|
|
|
|
return s.Token(TokenIds[name], number, m), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 11:58:01 +00:00
|
|
|
// Creates the lexer object and compiles the NFA.
|
|
|
|
func initLexer() (*lex.Lexer, error) {
|
|
|
|
lexer := lex.NewLexer()
|
2020-09-24 03:28:47 +00:00
|
|
|
for _, lit := range bracketLiterals {
|
2020-09-17 11:58:01 +00:00
|
|
|
r := "\\" + strings.Join(strings.Split(lit, ""), "\\")
|
|
|
|
lexer.Add([]byte(r), token(lit))
|
|
|
|
}
|
2020-09-24 03:28:47 +00:00
|
|
|
for _, lit := range Literals {
|
2020-09-24 00:52:45 +00:00
|
|
|
r := "\\" + strings.Join(strings.Split(lit, ""), "\\")
|
|
|
|
lexer.Add([]byte(r), token(lit))
|
|
|
|
}
|
2020-09-20 12:40:09 +00:00
|
|
|
lexer.Add([]byte(`([Oo][Rr])`), token("OR_OPERATOR"))
|
|
|
|
lexer.Add([]byte(`([Aa][Nn][Dd])`), token("AND_OPERATOR"))
|
2020-09-24 00:52:45 +00:00
|
|
|
lexer.Add([]byte(`\.\s*==\s*`), token("EQUALS_SELF_OPERATOR"))
|
|
|
|
lexer.Add([]byte(`\s*==\s*`), token("EQUALS_OPERATOR"))
|
2020-09-17 12:12:56 +00:00
|
|
|
lexer.Add([]byte(`\[-?[0-9]+\]`), numberToken("ARRAY_INDEX", true))
|
|
|
|
lexer.Add([]byte(`-?[0-9]+`), numberToken("ARRAY_INDEX", false))
|
2020-09-17 11:58:01 +00:00
|
|
|
lexer.Add([]byte("( |\t|\n|\r)+"), skip)
|
2020-09-18 07:08:33 +00:00
|
|
|
lexer.Add([]byte(`"[^ "]+"`), wrappedToken("PATH_KEY"))
|
|
|
|
lexer.Add([]byte(`[^ \.\[\(\)=]+`), token("PATH_KEY"))
|
2020-09-24 00:52:45 +00:00
|
|
|
lexer.Add([]byte(`\.`), token("TRAVERSE_OPERATOR"))
|
2020-09-17 11:58:01 +00:00
|
|
|
err := lexer.Compile()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return lexer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type PathTokeniser interface {
|
|
|
|
Tokenise(path string) ([]*lex.Token, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type pathTokeniser struct {
|
|
|
|
lexer *lex.Lexer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPathTokeniser() PathTokeniser {
|
|
|
|
initTokens()
|
|
|
|
var lexer, err = initLexer()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &pathTokeniser{lexer}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pathTokeniser) Tokenise(path string) ([]*lex.Token, error) {
|
|
|
|
scanner, err := p.lexer.Scanner([]byte(path))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var tokens []*lex.Token
|
|
|
|
for tok, err, eof := scanner.Next(); !eof; tok, err, eof = scanner.Next() {
|
|
|
|
|
|
|
|
if tok != nil {
|
|
|
|
token := tok.(*lex.Token)
|
|
|
|
log.Debugf("Processing %v - %v", token.Value, Tokens[token.Type])
|
|
|
|
tokens = append(tokens, token)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 00:52:45 +00:00
|
|
|
var postProcessedTokens []*lex.Token = make([]*lex.Token, 0)
|
|
|
|
|
|
|
|
for index, token := range tokens {
|
2020-09-24 03:28:47 +00:00
|
|
|
for _, literalTokenDef := range append(Literals, "ARRAY_INDEX", "(") {
|
2020-09-24 00:52:45 +00:00
|
|
|
if index > 0 && token.Type == TokenIds[literalTokenDef] && tokens[index-1].Type != TokenIds["TRAVERSE_OPERATOR"] {
|
|
|
|
postProcessedTokens = append(postProcessedTokens, &lex.Token{Type: TokenIds["TRAVERSE_OPERATOR"], Value: "."})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
postProcessedTokens = append(postProcessedTokens, token)
|
2020-09-24 03:28:47 +00:00
|
|
|
for _, literalTokenDef := range append(Literals, "ARRAY_INDEX", ")") {
|
2020-09-24 00:52:45 +00:00
|
|
|
if index != len(tokens)-1 && token.Type == TokenIds[literalTokenDef] && tokens[index+1].Type != TokenIds["TRAVERSE_OPERATOR"] {
|
|
|
|
postProcessedTokens = append(postProcessedTokens, &lex.Token{Type: TokenIds["TRAVERSE_OPERATOR"], Value: "."})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 11:58:01 +00:00
|
|
|
|
2020-09-24 00:52:45 +00:00
|
|
|
return postProcessedTokens, nil
|
2020-09-17 11:58:01 +00:00
|
|
|
}
|