package shlex

// fishFormat implements Format for fish lexing.
// Key differences from bash:
// - \' and \\ are escapes inside single quotes (NonEscapingQuoteBackslashEscapes)
// - Keyword operators: and, or (bare words acting as pipeline delimiters)
// - `not` is a prefix keyword but not a pipeline delimiter, so not in KeywordOperators
// - No word splitting on variable expansion (doesn't affect lexing)
// - Narrower escape set in double quotes (\" \$ \\ and \+newline only)
// - Supports &&, ||, & (background), |&, &|, &>, &>>, &>?, &>>?, >&, >|, <>, >?, >>?, <?, <>&
type fishFormat struct{}

func (fishFormat) () tokenClassifier {
	 := newBaseClassifier(escapeRunes)
	// Fish operators: |, ;, <, >, &, ?
	// & is included for &&, ||, &, |&, &|, &>, &>>, &>?, >&, <>&
	// ? is part of redirect operators (>? >>? <?) — deprecated glob char
	// ( and ) are command substitution delimiters
	.addWordbreaks("|;<>&?()")
	return 
}

func (fishFormat) ( string) WordbreakType {
	switch  {
	case "|":
		return WORDBREAK_PIPE
	case "|&", "&|":
		return WORDBREAK_PIPE_WITH_STDERR
	case ">|":
		return WORDBREAK_PIPE // pipe with explicit fd (e.g. 2>|)
	case ";":
		return WORDBREAK_LIST_SEQUENTIAL
	case "&&":
		return WORDBREAK_LIST_AND
	case "||":
		return WORDBREAK_LIST_OR
	case "&":
		return WORDBREAK_LIST_ASYNC
	case ">", ">?":
		return WORDBREAK_REDIRECT_OUTPUT
	case ">>", ">>?":
		return WORDBREAK_REDIRECT_OUTPUT_APPEND
	case "&>", "&>?":
		return WORDBREAK_REDIRECT_OUTPUT_BOTH
	case "&>>", "&>>?":
		return WORDBREAK_REDIRECT_OUTPUT_BOTH_APPEND
	case "<", "<>&", "<?":
		return WORDBREAK_REDIRECT_INPUT
	case "<>":
		return WORDBREAK_REDIRECT_INPUT_OUTPUT
	case ">&":
		return WORDBREAK_REDIRECT_INPUT_DUPLICATE // fd redirection (e.g. >&2)
	default:
		return WORDBREAK_UNKNOWN
	}
}

func (fishFormat) () map[string]WordbreakType {
	return map[string]WordbreakType{
		"and": WORDBREAK_LIST_AND,
		"or":  WORDBREAK_LIST_OR,
	}
}

func (fishFormat) () bool          { return true } // ' and \\ inside single quotes
func (fishFormat) () bool { return true }
func (fishFormat) () bool                { return true }
func (fishFormat) () bool         { return false }

// EscapingQuoteEscapeChars returns the limited set of characters that
// backslash can escape inside fish double quotes: ", $, \, and newline.
// All other \X sequences are literal (both characters emitted).
func (fishFormat) () map[rune]bool {
	return fishDoubleQuoteEscapes
}

var fishDoubleQuoteEscapes = map[rune]bool{
	'"':  true,
	'$':  true,
	'\\': true,
	'\n': true,
}

func (fishFormat) ( string) string { return fishQuoteWord() }
func (fishFormat) () bool  { return false }
func (fishFormat) () bool    { return false }

// IsLineContinuation implements lineContinuationEscaper. Fish treats
// backslash followed by \n or \r as a line continuation, both inside
// and outside double quotes.
func (fishFormat) ( rune) bool {
	return  == '\n' ||  == '\r'
}

// PostProcess reclassifies ( and ) as substitution delimiters for fish
// command substitution. Fish uses bare () for command substitution
// (no $ prefix).
func (fishFormat) ( TokenSlice) TokenSlice {
	 := make(TokenSlice, 0, len())
	for ,  := range  {
		if .Type == WORDBREAK_TOKEN && .Value == "(" {
			.WordbreakType = WORDBREAK_SUBSTITUTION_OPEN
		}
		if .Type == WORDBREAK_TOKEN && .Value == ")" {
			.WordbreakType = WORDBREAK_SUBSTITUTION_CLOSE
		}
		 = append(, )
	}
	return 
}

// Variable implements variableExpander. Fish expands `$name` in barewords
// and double quotes; there is no `${` form.
func (fishFormat) ( Token) (Variable, bool) {
	return variableRef(, variableRules{
		expands:         func( LexerState) bool { return  != QUOTING_STATE },
		barewordEscapes: true,
		nameStart:       isPosixNameStart,
		nameRune:        isPosixNameRune,
	})
}

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