2023-04-08 09:14:14 +02:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
|
2023-04-14 10:59:37 +02:00
|
|
|
"git.defalsify.org/vise/cache"
|
|
|
|
"git.defalsify.org/vise/state"
|
2023-04-08 09:14:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
inputRegexStr = "^[a-zA-Z0-9].*$"
|
|
|
|
inputRegex = regexp.MustCompile(inputRegexStr)
|
2023-04-16 13:15:57 +02:00
|
|
|
ctrlRegexStr = "^[><_^.]$"
|
2023-04-10 08:54:52 +02:00
|
|
|
ctrlRegex = regexp.MustCompile(ctrlRegexStr)
|
2023-04-08 09:54:55 +02:00
|
|
|
symRegexStr = "^[a-zA-Z0-9][a-zA-Z0-9_]+$"
|
2023-04-10 08:54:52 +02:00
|
|
|
symRegex = regexp.MustCompile(symRegexStr)
|
2023-04-08 09:14:14 +02:00
|
|
|
|
2023-04-08 09:54:55 +02:00
|
|
|
)
|
2023-04-08 09:14:14 +02:00
|
|
|
|
2023-04-08 09:54:55 +02:00
|
|
|
// CheckInput validates the given byte string as client input.
|
2023-04-08 10:31:32 +02:00
|
|
|
func ValidInput(input []byte) error {
|
2023-04-08 09:14:14 +02:00
|
|
|
if !inputRegex.Match(input) {
|
2023-04-08 09:54:55 +02:00
|
|
|
return fmt.Errorf("Input '%s' does not match input format /%s/", input, inputRegexStr)
|
2023-04-08 09:14:14 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-08 09:54:55 +02:00
|
|
|
// control characters for relative navigation.
|
2023-04-08 10:31:32 +02:00
|
|
|
func validControl(input []byte) error {
|
2023-04-08 09:54:55 +02:00
|
|
|
if !ctrlRegex.Match(input) {
|
|
|
|
return fmt.Errorf("Input '%s' does not match 'control' format /%s/", input, ctrlRegexStr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckSym validates the given byte string as a node symbol.
|
2023-04-08 10:31:32 +02:00
|
|
|
func ValidSym(input []byte) error {
|
2023-04-08 09:54:55 +02:00
|
|
|
if !symRegex.Match(input) {
|
|
|
|
return fmt.Errorf("Input '%s' does not match 'sym' format /%s/", input, symRegexStr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-08 10:31:32 +02:00
|
|
|
// false if target is not valid
|
|
|
|
func valid(target []byte) bool {
|
|
|
|
var ok bool
|
|
|
|
if len(target) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2023-04-08 09:54:55 +02:00
|
|
|
|
2023-04-08 10:31:32 +02:00
|
|
|
err := ValidSym(target)
|
2023-04-08 09:54:55 +02:00
|
|
|
if err == nil {
|
2023-04-08 10:31:32 +02:00
|
|
|
ok = true
|
2023-04-08 09:54:55 +02:00
|
|
|
}
|
|
|
|
|
2023-04-08 10:31:32 +02:00
|
|
|
if !ok {
|
|
|
|
err = validControl(target)
|
2023-04-08 09:54:55 +02:00
|
|
|
if err == nil {
|
2023-04-08 10:31:32 +02:00
|
|
|
ok = true
|
2023-04-08 09:14:14 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-08 10:31:32 +02:00
|
|
|
return ok
|
|
|
|
}
|
2023-04-08 09:14:14 +02:00
|
|
|
|
2023-04-08 10:31:32 +02:00
|
|
|
// CheckTarget tests whether the navigation state transition is available in the current state.
|
|
|
|
//
|
|
|
|
// Fails if target is formally invalid, or if navigation is unavailable.
|
|
|
|
func CheckTarget(target []byte, st *state.State) (bool, error) {
|
|
|
|
ok := valid(target)
|
|
|
|
if !ok {
|
|
|
|
return false, fmt.Errorf("invalid target: %x", target)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch target[0] {
|
|
|
|
case '_':
|
|
|
|
topOk, err := st.Top()
|
2023-04-16 13:15:57 +02:00
|
|
|
if err != nil {
|
2023-04-08 10:31:32 +02:00
|
|
|
return false, err
|
2023-04-08 09:54:55 +02:00
|
|
|
}
|
2023-04-08 10:31:32 +02:00
|
|
|
return topOk, nil
|
|
|
|
case '<':
|
|
|
|
_, prevOk := st.Sides()
|
|
|
|
return prevOk, nil
|
|
|
|
case '>':
|
|
|
|
nextOk, _ := st.Sides()
|
|
|
|
return nextOk, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// route parsed target symbol to navigation state change method,
|
2023-04-10 10:05:48 +02:00
|
|
|
func applyTarget(target []byte, st *state.State, ca cache.Memory, ctx context.Context) (string, uint16, error) {
|
2023-04-08 10:31:32 +02:00
|
|
|
var err error
|
|
|
|
sym, idx := st.Where()
|
|
|
|
|
|
|
|
ok := valid(target)
|
|
|
|
if !ok {
|
|
|
|
return sym, idx, fmt.Errorf("invalid input: %x", target)
|
2023-04-08 09:14:14 +02:00
|
|
|
}
|
|
|
|
|
2023-04-08 09:54:55 +02:00
|
|
|
switch target[0] {
|
|
|
|
case '_':
|
|
|
|
sym, err = st.Up()
|
|
|
|
if err != nil {
|
|
|
|
return sym, idx, err
|
|
|
|
}
|
2023-04-10 16:26:18 +02:00
|
|
|
err = ca.Pop()
|
|
|
|
if err != nil {
|
|
|
|
return sym, idx, err
|
|
|
|
}
|
|
|
|
|
2023-04-08 09:54:55 +02:00
|
|
|
case '>':
|
|
|
|
idx, err = st.Next()
|
|
|
|
if err != nil {
|
|
|
|
return sym, idx, err
|
|
|
|
}
|
|
|
|
case '<':
|
|
|
|
idx, err = st.Previous()
|
|
|
|
if err != nil {
|
|
|
|
return sym, idx, err
|
|
|
|
}
|
2023-04-10 10:05:48 +02:00
|
|
|
case '^':
|
2023-04-10 16:26:18 +02:00
|
|
|
notTop := true
|
|
|
|
for notTop {
|
|
|
|
notTop, err := st.Top()
|
|
|
|
if notTop {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
sym, err = st.Up()
|
|
|
|
if err != nil {
|
|
|
|
return sym, idx, err
|
|
|
|
}
|
|
|
|
err = ca.Pop()
|
|
|
|
if err != nil {
|
|
|
|
return sym, idx, err
|
|
|
|
}
|
2023-04-10 10:05:48 +02:00
|
|
|
}
|
2023-04-16 13:15:57 +02:00
|
|
|
case '.':
|
|
|
|
st.Same()
|
|
|
|
location, idx := st.Where()
|
|
|
|
return location, idx, nil
|
2023-04-08 09:54:55 +02:00
|
|
|
default:
|
|
|
|
sym = string(target)
|
2023-04-10 16:26:18 +02:00
|
|
|
err := st.Down(sym)
|
|
|
|
if err != nil {
|
|
|
|
return sym, idx, err
|
|
|
|
}
|
|
|
|
err = ca.Push()
|
|
|
|
if err != nil {
|
|
|
|
return sym, idx, err
|
|
|
|
}
|
2023-04-08 09:54:55 +02:00
|
|
|
idx = 0
|
2023-04-08 09:14:14 +02:00
|
|
|
}
|
2023-04-08 09:54:55 +02:00
|
|
|
return sym, idx, nil
|
2023-04-08 09:14:14 +02:00
|
|
|
}
|