SHS/token.go
2019-11-29 12:57:03 -08:00

140 lines
3.1 KiB
Go

/* SHS: Syntactically Homogeneous Shell
* Copyright (C) 2019 Aidan Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package shs;
import (
"strings"
"unicode"
)
type token_t int
const (
LIST token_t = iota
STRING token_t = iota
NUMBER token_t = iota
SYMBOL token_t = iota
)
type Token struct {
next *Token
tag token_t
position int
_inner interface{}
}
func Lex(input string) *Token {
if len(input) == 0 {
return nil
}
var ret *Token
var tok strings.Builder
iter := &ret
delim := ' '
is_list := false
is_str := false
tokenBuilder := func (pos int, tok string) {
if len(tok) == 0 && !is_list && !is_str {
return
}
*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
} else if tokenIsNumber(tok) {
(*iter).tag = NUMBER
} else {
(*iter).tag = SYMBOL
}
}
iter = &(*iter).next
}
for pos, char := range input {
if char == delim {
if is_str && is_list {
// String just ended inside list
is_str = false
delim = ')'
tok.WriteRune(char)
continue
}
delim = ' '
tokenBuilder(pos, tok.String())
tok.Reset()
} else {
if strings.ContainsRune("\"'`", char) {
is_str = true
delim = char
if !is_list {
continue
}
} else if char == '(' && !is_str {
is_list = true
delim = ')'
continue
}
tok.WriteRune(char)
}
}
if tok.Len() > 0 {
if is_list || is_str {
// TODO: Throw hella lex error here
return ret
}
tokenBuilder(len(input), tok.String())
}
return ret
}
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
}