package shlex

// zshFormat implements Format for zsh lexing.
// Extends bash with RC_QUOTES, zsh-specific operators (>>|, ;&, ;|, &|),
// and WORDCHARS/FIGNORE for word breaks.
type zshFormat struct{}

func (zshFormat) () tokenClassifier {
	return bashFormat{}.Classifier() // zsh uses the same rune classes as bash
}

func (zshFormat) ( string) WordbreakType {
	switch  {
	case ">>|":
		return WORDBREAK_REDIRECT_OUTPUT_APPEND_FORCE
	case ";&":
		return WORDBREAK_LIST_FALLTHROUGH
	case ";|":
		return WORDBREAK_LIST_FALLTHROUGH_RETRY
	case "&|":
		return WORDBREAK_LIST_ASYNC_ERRCHECK
	default:
		return bashWordbreakType()
	}
}

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

func (zshFormat) () bool          { return true } // RC_QUOTES: '' → '
func (zshFormat) () bool { return false }
func (zshFormat) () bool                { return true }
func (zshFormat) () bool         { return false }
func (zshFormat) () map[rune]bool {
	return map[rune]bool{
		'\\': true,
		'`':  true,
		'$':  true,
		'"':  true,
		'\n': true,
	}
}
func (zshFormat) ( string) string { return bashQuoteWord() }
func (zshFormat) () bool  { return false }
func (zshFormat) () bool    { return false }

// IsLineContinuation implements lineContinuationEscaper. Zsh (like bash)
// treats backslash followed by \n or \r as a line continuation.
func (zshFormat) ( rune) bool {
	return  == '\n' ||  == '\r'
}

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

// Variable implements variableExpander. Zsh expands `$name` and `${name`;
// `$` is literal inside single quotes and when escaped. Quote stripping at
// insertion time is compsys's job (IPREFIX), not detection's.
func (zshFormat) ( Token) (Variable, bool) {
	return posixVariableRef()
}

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