package shlex

// tcshFormat implements Format for tcsh lexing.
// Tcsh is POSIX-family (csh heritage) with operator grammar similar to bash
// but with key differences:
//   - >& for combined stdout+stderr redirect (bash uses &>)
//   - no <<< here-string, no ;; case operators, no &> redirect
//   - = and @ are not wordbreak characters (unlike bash)
//   - >! and >>! are parser-level constructs, not lexer operators:
//     '!' is a regular word char (_PUN) in tcsh's lexer; the parser in
//     syn3() recognizes > followed by ! as noclobber override.
//
// backslash_quote and $'...' are handled by the state machine
// (backslash is already the escape char, $ is a word char before quotes).
type tcshFormat struct{}

// TCSH_WORDBREAKS are the wordbreak characters for tcsh, derived from the
// _META character class in tcsh's _cmap table (sh.char.c).
// Unlike bash, tcsh does not include = or @ as wordbreak characters.
// ! is NOT a wordbreak: it is _PUN (punctuation) in tcsh's lexer, not _META.
// The >! and >>! noclobber operators are recognized at the parser level.
const TCSH_WORDBREAKS = "><;|&()"

func (tcshFormat) () tokenClassifier {
	 := newBaseClassifier(escapeRunes)
	.addWordbreaks(TCSH_WORDBREAKS)
	return 
}

func (tcshFormat) ( string) WordbreakType {
	return tcshWordbreakType()
}

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

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

// IsLineContinuation implements lineContinuationEscaper. Tcsh treats
// backslash followed by \n or \r as a line continuation, but only outside
// quotes; inside double quotes it preserves backslash-newline (unlike
// bash, which drops it there).
func (tcshFormat) ( rune) bool {
	return  == '\n' ||  == '\r'
}

// IsLineContinuationInsideQuotes opts tcsh out of treating escape+newline
// as a line continuation inside double quotes; see
// lineContinuationInsideQuotes.
func (tcshFormat) () bool { return false }

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

// Variable implements variableExpander. Tcsh expands `$name` and
// `${name` with POSIX-ish rules; single quotes keep `$` literal.
func (tcshFormat) ( Token) (Variable, bool) {
	return posixVariableRef()
}

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