2019-11-18 22:44:24 -08:00
|
|
|
package shsh;
|
2019-11-12 12:24:45 -08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
2019-11-28 20:07:28 -08:00
|
|
|
"unicode"
|
2019-11-12 12:24:45 -08:00
|
|
|
)
|
|
|
|
|
|
2019-11-28 00:39:08 -08:00
|
|
|
type token_t int
|
|
|
|
|
const (
|
2019-11-28 00:42:47 -08:00
|
|
|
LIST token_t = iota
|
|
|
|
|
STRING token_t = iota
|
|
|
|
|
NUMBER token_t = iota
|
|
|
|
|
SYMBOL token_t = iota
|
2019-11-28 00:39:08 -08:00
|
|
|
)
|
|
|
|
|
|
2019-11-12 12:24:45 -08:00
|
|
|
type Token struct {
|
2019-11-16 16:16:05 -08:00
|
|
|
next *Token
|
2019-11-28 08:57:12 -08:00
|
|
|
tag token_t
|
2019-11-28 20:07:28 -08:00
|
|
|
position int
|
2019-11-16 16:16:05 -08:00
|
|
|
_inner interface{}
|
2019-11-12 12:24:45 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-28 00:39:08 -08:00
|
|
|
func Lex(input string) *Token {
|
2019-11-28 22:43:30 -08:00
|
|
|
if len(input) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ret *Token
|
2019-11-12 12:24:45 -08:00
|
|
|
iter := &ret
|
2019-11-16 16:16:05 -08:00
|
|
|
delim := ' '
|
2019-11-16 01:08:01 -08:00
|
|
|
var tok strings.Builder
|
2019-11-28 20:07:28 -08:00
|
|
|
is_list := false
|
|
|
|
|
is_str := false
|
2019-11-12 12:24:45 -08:00
|
|
|
|
2019-11-28 22:43:30 -08:00
|
|
|
tokenBuilder := func (pos int, tok string) {
|
2019-11-28 23:51:19 -08:00
|
|
|
if len(tok) == 0 && !is_list && !is_str {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-28 22:43:30 -08:00
|
|
|
*iter = new(Token)
|
|
|
|
|
(*iter).position = pos
|
|
|
|
|
|
|
|
|
|
if is_list {
|
|
|
|
|
(*iter)._inner = Lex(tok)
|
|
|
|
|
(*iter).tag = LIST
|
|
|
|
|
is_list = false
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
(*iter)._inner = tok
|
|
|
|
|
if is_str {
|
|
|
|
|
(*iter).tag = STRING
|
|
|
|
|
is_str = false
|
|
|
|
|
|
2019-11-28 23:51:19 -08:00
|
|
|
} else if tokenIsNumber(tok) {
|
2019-11-28 22:43:30 -08:00
|
|
|
(*iter).tag = NUMBER
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
(*iter).tag = SYMBOL
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iter = &(*iter).next
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 13:02:06 -08:00
|
|
|
for pos, char := range input {
|
2019-11-28 23:51:19 -08:00
|
|
|
if char == delim {
|
|
|
|
|
if is_str && is_list {
|
|
|
|
|
// String just ended inside list
|
|
|
|
|
is_str = false
|
|
|
|
|
delim = ')'
|
|
|
|
|
tok.WriteRune(char)
|
2019-11-28 22:43:30 -08:00
|
|
|
continue
|
2019-11-18 23:24:33 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-28 23:51:19 -08:00
|
|
|
delim = ' '
|
2019-11-28 22:43:30 -08:00
|
|
|
tokenBuilder(pos, tok.String())
|
|
|
|
|
tok.Reset()
|
2019-11-16 16:16:05 -08:00
|
|
|
|
2019-11-28 23:51:19 -08:00
|
|
|
} else {
|
|
|
|
|
if strings.ContainsRune("\"'`", char) {
|
|
|
|
|
is_str = true
|
|
|
|
|
delim = char
|
|
|
|
|
if !is_list {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if char == '(' && !is_str {
|
|
|
|
|
is_list = true
|
|
|
|
|
delim = ')'
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 01:08:01 -08:00
|
|
|
tok.WriteRune(char)
|
2019-11-12 13:09:31 -08:00
|
|
|
}
|
2019-11-28 22:43:30 -08:00
|
|
|
}
|
2019-11-12 13:09:31 -08:00
|
|
|
|
2019-11-28 22:43:30 -08:00
|
|
|
if tok.Len() > 0 {
|
2019-11-28 23:51:19 -08:00
|
|
|
if is_list || is_str {
|
|
|
|
|
// TODO: Throw hella lex error here
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-28 22:43:30 -08:00
|
|
|
tokenBuilder(len(input), tok.String())
|
2019-11-12 13:02:06 -08:00
|
|
|
}
|
2019-11-12 13:09:31 -08:00
|
|
|
|
2019-11-28 00:39:08 -08:00
|
|
|
return ret
|
2019-11-12 13:09:31 -08:00
|
|
|
}
|
2019-11-28 23:51:19 -08:00
|
|
|
|
|
|
|
|
func tokenIsNumber(arg string) bool {
|
|
|
|
|
dotCount := 0
|
|
|
|
|
|
|
|
|
|
for _, char := range arg {
|
|
|
|
|
if !unicode.IsDigit(char) {
|
|
|
|
|
if char == '.' && dotCount == 0 {
|
|
|
|
|
dotCount++
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|