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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-20 20:59:52 -07:00
|
|
|
package ast
|
2019-11-12 12:24:45 -08:00
|
|
|
|
|
|
|
|
import (
|
2019-11-28 20:07:28 -08:00
|
|
|
"unicode"
|
2019-11-12 12:24:45 -08:00
|
|
|
)
|
|
|
|
|
|
2020-06-17 00:28:16 -07:00
|
|
|
type Token_t int
|
2019-11-28 00:39:08 -08:00
|
|
|
const (
|
2020-06-17 00:28:16 -07: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 {
|
2020-06-17 00:28:16 -07:00
|
|
|
Next *Token
|
|
|
|
|
Tag Token_t
|
|
|
|
|
Position int
|
|
|
|
|
Inner interface{}
|
2019-11-12 12:24:45 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-29 19:02:30 -08:00
|
|
|
const string_delims string = "\"'`"
|
|
|
|
|
|
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-28 20:07:28 -08:00
|
|
|
is_str := false
|
2019-11-29 19:02:30 -08:00
|
|
|
is_list := 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)
|
2020-06-17 00:28:16 -07:00
|
|
|
(*iter).Position = pos
|
2019-11-28 22:43:30 -08:00
|
|
|
|
|
|
|
|
if is_list {
|
2020-06-17 00:28:16 -07:00
|
|
|
(*iter).Inner = Lex(tok)
|
|
|
|
|
(*iter).Tag = LIST
|
2019-11-28 22:43:30 -08:00
|
|
|
is_list = false
|
|
|
|
|
|
|
|
|
|
} else {
|
2020-06-17 00:28:16 -07:00
|
|
|
(*iter).Inner = tok
|
2019-11-28 22:43:30 -08:00
|
|
|
if is_str {
|
2020-06-17 00:28:16 -07:00
|
|
|
(*iter).Tag = STRING
|
2019-11-28 22:43:30 -08:00
|
|
|
is_str = false
|
|
|
|
|
|
2019-11-28 23:51:19 -08:00
|
|
|
} else if tokenIsNumber(tok) {
|
2020-06-17 00:28:16 -07:00
|
|
|
(*iter).Tag = NUMBER
|
2019-11-28 22:43:30 -08:00
|
|
|
|
|
|
|
|
} else {
|
2020-06-17 00:28:16 -07:00
|
|
|
(*iter).Tag = SYMBOL
|
2019-11-28 22:43:30 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 00:28:16 -07:00
|
|
|
iter = &(*iter).Next
|
2019-11-28 22:43:30 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-29 19:02:30 -08:00
|
|
|
// returns -1 on unmatched string delim
|
|
|
|
|
matchStrEnd := func(start int, delim byte) int {
|
|
|
|
|
for i := start; i < len(input); i++ {
|
|
|
|
|
if input[i] == delim {
|
|
|
|
|
return i
|
2019-11-18 23:24:33 -08:00
|
|
|
}
|
2019-11-29 19:02:30 -08:00
|
|
|
}
|
2019-11-18 23:24:33 -08:00
|
|
|
|
2019-11-29 19:02:30 -08:00
|
|
|
return -1
|
|
|
|
|
}
|
2019-11-16 16:16:05 -08:00
|
|
|
|
2019-11-29 19:02:30 -08:00
|
|
|
// returns -1 on unmatched string delim
|
|
|
|
|
// returns -2 on unmatched list delim
|
|
|
|
|
matchListEnd := func(start int) int {
|
|
|
|
|
depth := 0
|
|
|
|
|
|
|
|
|
|
for i := start; i < len(input); i++ {
|
|
|
|
|
switch input[i] {
|
|
|
|
|
case '"','\'','`':
|
|
|
|
|
i = matchStrEnd(i + 1, input[i])
|
|
|
|
|
if i == -1 {
|
|
|
|
|
return -1
|
2019-11-28 23:51:19 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-29 19:02:30 -08:00
|
|
|
case '(':
|
|
|
|
|
depth++
|
2019-11-28 23:51:19 -08:00
|
|
|
|
2019-11-29 19:02:30 -08:00
|
|
|
case ')':
|
|
|
|
|
if depth == 0 {
|
|
|
|
|
return i
|
|
|
|
|
} else {
|
|
|
|
|
depth -= 1
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-12 13:09:31 -08:00
|
|
|
}
|
2019-11-29 19:02:30 -08:00
|
|
|
|
|
|
|
|
return -2
|
2019-11-28 22:43:30 -08:00
|
|
|
}
|
2019-11-12 13:09:31 -08:00
|
|
|
|
2019-11-29 19:02:30 -08:00
|
|
|
needs_alloc := false
|
|
|
|
|
start_pos := 0
|
|
|
|
|
for i := 0; i < len(input); i++ {
|
|
|
|
|
switch input[i] {
|
|
|
|
|
case '(':
|
|
|
|
|
start_pos = i + 1
|
|
|
|
|
i = matchListEnd(start_pos)
|
|
|
|
|
is_list = true
|
|
|
|
|
needs_alloc = true
|
|
|
|
|
|
|
|
|
|
case '"','\'','`':
|
|
|
|
|
start_pos = i + 1
|
|
|
|
|
i = matchStrEnd(start_pos, input[i])
|
|
|
|
|
is_str = true
|
|
|
|
|
needs_alloc = true
|
|
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
|
if i == start_pos {
|
|
|
|
|
start_pos += 1
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
needs_alloc = true
|
2019-11-28 23:51:19 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-29 19:02:30 -08:00
|
|
|
if needs_alloc {
|
|
|
|
|
needs_alloc = false
|
|
|
|
|
if (i < 0) {
|
|
|
|
|
// TODO: Maybe not overload this.
|
|
|
|
|
start_pos = i
|
|
|
|
|
goto error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tokenBuilder(start_pos, input[start_pos:i])
|
|
|
|
|
start_pos = i+1
|
|
|
|
|
}
|
2019-11-12 13:02:06 -08:00
|
|
|
}
|
2019-11-12 13:09:31 -08:00
|
|
|
|
2019-11-29 19:02:30 -08:00
|
|
|
if start_pos < len(input) {
|
|
|
|
|
tokenBuilder(start_pos, input[start_pos:])
|
|
|
|
|
}
|
2019-11-28 00:39:08 -08:00
|
|
|
return ret
|
2019-11-29 19:02:30 -08:00
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
// TODO: Hook into error module
|
|
|
|
|
// TODO: Finalize and GC alloced tokens
|
|
|
|
|
if start_pos == -1 {
|
2020-06-21 01:30:54 -07:00
|
|
|
log.Log(log.ERR,
|
|
|
|
|
"Unmatched string delimiter in input. discarding.",
|
|
|
|
|
"lex")
|
2019-11-29 19:02:30 -08:00
|
|
|
} else if start_pos == -2 {
|
2020-06-21 01:30:54 -07:00
|
|
|
log.Log(log.ERR,
|
|
|
|
|
"Unmatched list delimiter in input. discarding.",
|
|
|
|
|
"lex")
|
2019-11-29 19:02:30 -08:00
|
|
|
} else {
|
2020-06-21 01:30:54 -07:00
|
|
|
log.Log(log.ERR,
|
|
|
|
|
"Unknown error in input. discarding.",
|
|
|
|
|
"lex")
|
2019-11-29 19:02:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
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
|
|
|
|
|
}
|