package shlex

// hilbishFormat implements Format for hilbish lexing.
// Hilbish's shell runner (snail) wraps mvdan.cc/sh with the parser's
// default language variant (LangBash), so the sh mode follows the full
// bash operator grammar (|&, <<<, &>, ;;&, ...). Verified against
// golibs/snail/snail.go and mvdan.cc/sh/v3/syntax parser defaults.
//
// Known limitations (flat state machine):
//   - the default hybrid runner tries Lua first (nature/runner.lua), so
//     input may be Lua code — Lua long strings ([[...]]) and string
//     escapes are not lexed; Lua strings share ' and " with sh anyway
//   - @-modifiers (@priv, @dir=...) at line start lex as plain words
type hilbishFormat struct{}

// HILBISH_WORDBREAKS are the wordbreak characters for hilbish: the sh
// metacharacters handled by its interpreter, without bash's COMP_WORDBREAKS
// customization (@ = : are not word breaks for hilbish).
const HILBISH_WORDBREAKS = "><;|&()"

func (hilbishFormat) () tokenClassifier {
	 := newBaseClassifier(escapeRunes)
	.addWordbreaks(HILBISH_WORDBREAKS)
	return 
}

func (hilbishFormat) ( string) WordbreakType {
	return bashWordbreakType()
}

func (hilbishFormat) () map[string]WordbreakType { return nil }

func (hilbishFormat) () bool          { return false }
func (hilbishFormat) () bool { return false }
func (hilbishFormat) () bool                { return true }
func (hilbishFormat) () bool         { return false }
func (hilbishFormat) () map[rune]bool {
	return map[rune]bool{
		'\\': true,
		'`':  true,
		'$':  true,
		'"':  true,
		'\n': true,
	}
}
func (hilbishFormat) ( string) string { return hilbishQuoteWord() }
func (hilbishFormat) () bool  { return false }
func (hilbishFormat) () bool    { return false }

// IsLineContinuation implements lineContinuationEscaper. Hilbish's shell
// interpreter treats backslash followed by \n or \r as a line continuation.
func (hilbishFormat) ( rune) bool {
	return  == '\n' ||  == '\r'
}

// PostProcess reclassifies ( and ) as substitution delimiters and merges
// $ + ( into a single opener token for POSIX command substitution.
func (hilbishFormat) ( TokenSlice) TokenSlice {
	return posixSubstitutionPostProcess()
}

// Variable implements variableExpander. Hilbish's shell interpreter expands
// `$name` and `${name`; `$` is literal inside single quotes and when escaped.
func (hilbishFormat) ( Token) (Variable, bool) {
	return posixVariableRef()
}

func (hilbishFormat) ( string) bool {
	return bareWordRe.MatchString()
}