2019-11-29 12:57:03 -08:00
|
|
|
/* 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;
|
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-29 00:02:00 -08:00
|
|
|
var tok strings.Builder
|
2019-11-12 12:24:45 -08:00
|
|
|
iter := &ret
|
2019-11-16 16:16:05 -08:00
|
|
|
delim := ' '
|
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
|
|
|
|
|
}
|