package syntax
import (
"github.com/hwittenborn/husk/ctypes"
"github.com/hwittenborn/husk/util"
"mvdan.cc/sh/v3/syntax"
"runtime/cgo"
)
func IsKeyword(word *ctypes.Char) bool {
return syntax.IsKeyword(ctypes.GoString(word))
}
func Quote(inputString *ctypes.Char, langVariantInt ctypes.Int) (outputString *ctypes.Char, isError bool) {
langVariant := util.GetLangVariant(langVariantInt)
quotedString, err := syntax.Quote(ctypes.GoString(inputString), langVariant)
if err == nil {
outputString = ctypes.CString(quotedString)
isError = false
} else {
outputString = ctypes.CString(err.Error())
isError = true
}
return
}
func ValidName(value *ctypes.Char) bool {
return syntax.ValidName(ctypes.GoString(value))
}
func NewParser(keepComments bool, stopAt *ctypes.Char, variantInt ctypes.Int) ctypes.UintptrT {
parser := syntax.NewParser()
syntax.KeepComments(keepComments)(parser)
syntax.Variant(util.GetLangVariant(variantInt))
if stopAt != nil {
syntax.StopAt(ctypes.GoString(stopAt))(parser)
}
return ctypes.UintptrT(cgo.NewHandle(parser))
}
func NewPos(offset, line, column ctypes.Uint) ctypes.UintptrT {
pos := syntax.NewPos(uint(offset), uint(line), uint(column))
return ctypes.UintptrT(cgo.NewHandle(pos))
}
func PosAfter(pos1, pos2 ctypes.UintptrT) bool {
pos1Handle := cgo.Handle(pos1)
pos2Handle := cgo.Handle(pos2)
goPos1 := pos1Handle.Value().(syntax.Pos)
goPos2 := pos2Handle.Value().(syntax.Pos)
return goPos1.After(goPos2)
}
func PosCol(pos ctypes.UintptrT) ctypes.Uint {
posHandle := cgo.Handle(pos)
goPos := posHandle.Value().(syntax.Pos)
return ctypes.Uint(goPos.Col())
}
func PosIsValid(pos ctypes.UintptrT) bool {
posHandle := cgo.Handle(pos)
goPos := posHandle.Value().(syntax.Pos)
return goPos.IsValid()
}
func PosLine(pos ctypes.UintptrT) ctypes.Uint {
posHandle := cgo.Handle(pos)
goPos := posHandle.Value().(syntax.Pos)
return ctypes.Uint(goPos.Line())
}
func PosOffset(pos ctypes.UintptrT) ctypes.Uint {
posHandle := cgo.Handle(pos)
goPos := posHandle.Value().(syntax.Pos)
return ctypes.Uint(goPos.Offset())
}