package shlex

// Span represents a rune-offset range in the input string.
type Span struct {
	Start int // rune offset of the first character
	End   int // rune offset after the last character
}

// Format identifies a shell's lexical rules. It is the name of one of the
// supported shells; the lexing behavior itself is internal. Unknown names
// are rejected by Split, yield an empty context in Complete, and fall
// back to the default format in Join.
type Format = string

// Supported formats.
const (
	// Default is the empty format name, resolving to Bash. Spec macros
	// and callers that omit the shell use this.
	Default Format = ""

	Bash       Format = "bash"
	Cmd        Format = "cmd"
	Elvish     Format = "elvish"
	Fish       Format = "fish"
	Hilbish    Format = "hilbish"
	Lua        Format = "lua"
	Nushell    Format = "nushell"
	Oil        Format = "oil"
	Powershell Format = "powershell"
	Tcsh       Format = "tcsh"
	Xonsh      Format = "xonsh"
	Zsh        Format = "zsh"
)

// formatImpl describes a shell's lexical rules: which runes are quotes,
// escapes, comments, and word breaks, and how operators are classified.
// It is the behavior behind a Format name and is resolved via formatImplFor.
type formatImpl interface {
	// Classifier returns a rune classifier mapping runes to runeTokenClass.
	// Called once per tokenizer; should be freshly built (may read env vars).
	Classifier() tokenClassifier

	// ClassifyOperator maps a wordbreak token's RawValue to a WordbreakType.
	// Called for WORDBREAK_TOKENs to determine redirect vs pipeline vs other.
	ClassifyOperator(raw string) WordbreakType

	// KeywordOperators returns bare-word operators (e.g. fish "and"/"or")
	// that should be treated as WORDBREAK_TOKEN despite being word characters.
	// Returns nil for shells without keyword operators.
	KeywordOperators() map[string]WordbreakType

	// NonEscapingQuoteEscapes returns true if the non-escaping quote (single
	// quote) supports limited escapes. When true, the QUOTING_STATE handler
	// peeks at the next rune on seeing the quote char or escape char:
	//   - '' (doubled quote) → one literal quote, stay in state
	//   - \' or \\ → escaped char, stay in state
	// Supported by: fish, elvish, zsh (RC_QUOTES), PowerShell, lua (the
	// doubled-quote approximation for Lua single quotes).
	NonEscapingQuoteEscapes() bool

	// NonEscapingQuoteBackslashEscapes returns true if backslash (\) is an
	// escape character inside the non-escaping quote (single quotes).
	// When true, \' and \\ inside single quotes produce the escaped char
	// and stay in QUOTING_STATE. Used by fish and lua (Lua escapes \' in
	// both quote styles).
	// Requires NonEscapingQuoteEscapes() to also be true.
	NonEscapingQuoteBackslashEscapes() bool

	// EscapeNotBareword returns false if the escape character (backslash)
	// is a literal bareword character outside quotes rather than an escape.
	// When false, the state machine does NOT enter ESCAPING_STATE from
	// IN_WORD_STATE — the escape char is treated as a regular word char.
	// Only elvish needs this (\ is a bareword char in elvish).
	EscapeNotBareword() bool

	// EscapeNotInEscapingQuote returns true if the escape character is
	// literal inside the escaping quote (double quotes) rather than acting
	// as an escape. When true, the QUOTING_ESCAPING_STATE handler treats
	// the escape rune as a regular word character instead of entering
	// ESCAPING_QUOTED_STATE. Only cmd needs this: cmd's caret (^) is
	// completely literal inside double quotes — it does not escape the
	// next character when quoted.
	EscapeNotInEscapingQuote() bool

	// EscapingQuoteEscapeChars returns the set of characters that backslash
	// can escape inside the escaping quote (double quotes). If nil, backslash
	// escapes any character. POSIX shells (bash, zsh, tcsh) return the
	// CBSDQUOTE set: \, `, $, ", and newline. Fish returns only
	// `"`, `$`, `\`, and newline (no backtick).
	EscapingQuoteEscapeChars() map[rune]bool

	// TripleQuoteSupport returns true if the shell supports triple-quoted
	// strings ('''...''' and """..."""). When true, the tokenizer peeks
	// ahead two runes on seeing a quote char to detect triple quotes and
	// enters a dedicated triple-quote state. Only xonsh needs this.
	TripleQuoteSupport() bool

	// RawPrefixSupport returns true if the shell supports raw string prefixes
	// (r'...', r"...") that suppress escape processing inside double quotes.
	// When true, the tokenizer checks whether the current word ends with a
	// raw prefix (r/R) before entering QUOTING_ESCAPING_STATE, and if so
	// treats the double quote as non-escaping instead. Only xonsh needs this.
	RawPrefixSupport() bool

	// QuoteWord quotes a single word for safe insertion into a command line.
	// Used by JoinWith. The implementation should use the shell's preferred
	// quoting style: backslash-escaping for POSIX shells, double-quote
	// wrapping for nushell/PowerShell, single-quote wrapping for shells
	// that support it, etc.
	QuoteWord(s string) string

	// IsBareWord reports whether s is a bareword for this format: it
	// stands as a single inert token in unquoted position, with no
	// wordbreak, escaping, expansion or history rules triggered,
	// including ones that act above the lexer (e.g. tcsh `=word` history
	// expansion). Identifier validity is not implied; implementations
	// sharpen the shared list (bareWordRe) where barewords also serve as
	// identifiers, e.g. Lua.
	IsBareWord(s string) bool
}

// formatImpls maps Format names to their behavior.
var formatImpls = map[Format]formatImpl{
	Default:    bashFormat{},
	Bash:       bashFormat{},
	Cmd:        cmdFormat{},
	Elvish:     elvishFormat{},
	Fish:       fishFormat{},
	Hilbish:    hilbishFormat{},
	Lua:        luaFormat{},
	Nushell:    nushellFormat{},
	Oil:        bashFormat{},
	Powershell: powershellFormat{},
	Tcsh:       tcshFormat{},
	Xonsh:      xonshFormat{},
	Zsh:        zshFormat{},
}

// formatImplFor returns the behavior for a Format name, reporting false for
// unknown names.
func formatImplFor( Format) (formatImpl, bool) {
	,  := formatImpls[]
	return , 
}

// escapingQuoteUnescaper is an optional interface for formats that need to
// transform escape sequences inside double quotes beyond simple
// backslash-dropping. When implemented, the ESCAPING_QUOTED_STATE handler
// calls EscapingQuoteUnescape for the rune following a backslash. If the
// rune is a recognized escape, the replacement string is used; otherwise
// both the backslash and the rune are kept literally. Formats implementing
// this interface take priority over EscapingQuoteEscapeChars.
type escapingQuoteUnescaper interface {
	EscapingQuoteUnescape(r rune) (replacement string, handled bool)
}

// postProcessor is an optional interface for formats that need to reclassify
// tokens after the main tokenization pass. Used by formats that require
// context not available in the flat state machine (e.g. elvish brace/lambda
// context for | disambiguation, nushell stream-redirect operator merging).
type postProcessor interface {
	PostProcess(tokens TokenSlice) TokenSlice
}

// lineContinuationEscaper is an optional interface for formats where the
// escape character followed by a newline (or carriage return) acts as a
// line continuation — the escape+newline sequence is consumed and discarded,
// NOT added to the word value. This applies both outside quotes (ESCAPING_STATE)
// and inside double quotes (ESCAPING_QUOTED_STATE).
//
// Supported by: bash, zsh, fish, xonsh (backslash+newline),
// PowerShell (backtick+newline), cmd (caret+newline).
type lineContinuationEscaper interface {
	// IsLineContinuation returns true if the rune following the escape
	// character should be treated as a line continuation. The parameter
	// is the rune that follows the escape character (e.g. '\n' or '\r').
	IsLineContinuation(r rune) bool
}

// lineContinuationInsideQuotes reports whether the format treats escape+newline
// as a line continuation inside double quotes. Formats that opt out (tcsh
// preserves backslash-newline inside double quotes, unlike bash) implement
// IsLineContinuationInsideQuotes() bool; the default is true.
func lineContinuationInsideQuotes( formatImpl) bool {
	if ,  := .(interface{ () bool });  {
		return .()
	}
	return true
}

// lineContinuationWhitespace is an optional interface for formats where
// a specific non-escape character followed by a newline acts as whitespace
// (a word break) rather than a concatenation. This differs from
// lineContinuationEscaper: that interface consumes escape+newline and
// concatenates the word; this interface consumes char+newline and breaks
// the word (like a space).
//
// Supported by: elvish (^+newline).
type lineContinuationWhitespace interface {
	// LineContinuationChar returns the rune that, when followed by \n or \r,
	// acts as whitespace. Returns 0 if not supported.
	LineContinuationChar() rune
	// IsLineContinuationWhitespace returns true if the rune following the
	// line-continuation character should be treated as whitespace.
	IsLineContinuationWhitespace(r rune) bool
}

// blockCommenter is an optional interface for formats that support
// multi-line block comments (e.g. PowerShell's <# ... #>). When the
// tokenizer encounters the blockCommentOpener runes at a word boundary,
// it enters a dedicated BLOCK_COMMENT_STATE that scans until the
// blockCommentCloser runes are found, spanning multiple lines.
type blockCommenter interface {
	BlockCommentOpener() string // e.g. "<#" for PowerShell
	BlockCommentCloser() string // e.g. "#>" for PowerShell
}

// stopParsingToken is an optional interface for formats that support a
// stop-parsing token (e.g. PowerShell's --%). When the tokenizer encounters
// this token as a bare word, it switches to a raw lexing mode for the
// remainder of the line (until newline or pipeline delimiter), where
// all characters except the pipeline delimiters are treated as literal
// word content.
type stopParsingToken interface {
	// StopParsingWord returns the literal token that triggers raw mode.
	// e.g. "--%" for PowerShell.
	StopParsingWord() string
}