package shlex

// cmdFormat implements Format for cmd.exe (with clink) lexing.
// Key differences from POSIX:
//   - Caret (^) is the escape character outside quotes, but literal inside "..."
//   - Double quotes (") are the only quote — simple toggle, no escapes inside
//   - No single quotes — ' is a literal word character
//   - & is a command separator (like ; in POSIX), not background
//   - ; is NOT a separator (literal character)
//   - ( ) are grouping operators (command blocks)
//   - REM and :: are comments (keyword/two-rune, not rune-based)
//   - % is a word character (variable expansion sigil)
//   - \ is a literal word character (Windows paths)
//   - Caret + newline is line continuation (consumed, not part of word)
//   - Numeric stream redirects: 2>, 2>&1, 1>&2 (merged in PostProcess)
//
// REM/:: keyword comments are not yet handled (deferred).
type cmdFormat struct{}

func (cmdFormat) () tokenClassifier {
	 := tokenClassifier{}
	.addRuneClass(spaceRunes, spaceRuneClass)
	// Cmd: comma is a word delimiter (like space), but not a command separator.
	// Semicolons and equals are also delimiters in cmd, but ; is kept as a
	// literal word char because it's safer for completion (e.g. set VAR=value
	// would break if = were a delimiter). Comma is always safe to split.
	.addRuneClass(",", spaceRuneClass)
	// Cmd: only " is a quote. ' is a regular word char.
	.addRuneClass(escapingQuoteRunes, escapingQuoteRuneClass) // " is the escaping quote
	// Cmd: ^ is the escape character, not \
	.addRuneClass("^", escapeRuneClass)
	// Cmd: # is not a comment (that's REM/::). Don't classify it as comment.
	// REM/:: comments need keyword detection (deferred).

	// Cmd operators: |, &, <, >, (, )
	// Note: & is a command separator (like ; in POSIX), not background
	// ; is NOT a separator in cmd (it is a literal character)
	// ( and ) are grouping operators for command blocks
	.addWordbreaks("|&<>()")
	return 
}

func (cmdFormat) ( string) WordbreakType {
	switch  {
	case "|":
		return WORDBREAK_PIPE
	case "&":
		// Cmd: & is a command separator (like ; in POSIX)
		return WORDBREAK_LIST_SEQUENTIAL
	case "&&":
		return WORDBREAK_LIST_AND
	case "||":
		return WORDBREAK_LIST_OR
	case ">", ">>":
		return WORDBREAK_REDIRECT_OUTPUT
	case "<":
		return WORDBREAK_REDIRECT_INPUT
	case "(", ")":
		// Cmd: parentheses are grouping operators for command blocks
		return WORDBREAK_UNKNOWN
	default:
		return WORDBREAK_UNKNOWN
	}
}

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

func (cmdFormat) () bool           { return false }
func (cmdFormat) () bool  { return false }
func (cmdFormat) () bool                 { return true }
func (cmdFormat) () bool          { return true }
func (cmdFormat) () map[rune]bool { return nil }
func (cmdFormat) ( string) string               { return cmdQuoteWord() }
func (cmdFormat) () bool                { return false }
func (cmdFormat) () bool                  { return false }

// IsLineContinuation implements lineContinuationEscaper. cmd.exe's caret
// followed by \n or \r is a line continuation — the sequence is consumed
// and the word continues on the next line.
func (cmdFormat) ( rune) bool {
	return  == '\n' ||  == '\r'
}

// PostProcess merges cmd.exe numeric stream-redirect operators. The
// tokenizer produces e.g. `2` as a WORD_TOKEN and `>` (or `>>`) as a
// WORDBREAK_TOKEN. This step detects adjacent word+wordbreak sequences
// like `2>`, `2>>`, `2>&1`, `1>&2` and reclassifies them as single
// WORDBREAK_TOKENs with the appropriate WordbreakType.
func (cmdFormat) ( TokenSlice) TokenSlice {
	 := make(TokenSlice, 0, len())
	for  := 0;  < len(); ++ {
		 := []

		// Look for bare WORD_TOKEN (digit 1-2) immediately followed by
		// WORDBREAK_TOKEN starting with '>' (redirect operator)
		if .Type == WORD_TOKEN && .Value == .RawValue && +1 < len() {
			 := [+1]
			if .Type == WORDBREAK_TOKEN && .adjoins() &&
				.WordbreakType.IsRedirect() && len(.RawValue) > 0 && .RawValue[0] == '>' {
				if len(.Value) == 1 && (.Value[0] == '1' || .Value[0] == '2') {
					 := .WordbreakType
					 := .RawValue + .RawValue
					 := .Value + .Value
					 := Span{Start: .Span.Start, End: .Span.End}

					// Check for &N pattern (stream merge) in the token after next
					if +2 < len() && [+2].Type == WORDBREAK_TOKEN &&
						[+2].Value == "&" && [+2].adjoins() {
						if +3 < len() && [+3].Type == WORD_TOKEN &&
							[+3].Value == [+3].RawValue &&
							[+3].adjoins([+2]) &&
							len([+3].Value) == 1 &&
							([+3].Value[0] == '1' || [+3].Value[0] == '2') {
							// 2>&1 pattern — merge all four tokens
							 += [+2].RawValue + [+3].RawValue
							 += [+2].Value + [+3].Value
							.End = [+3].Span.End
							 = WORDBREAK_REDIRECT_OUTPUT_BOTH
							 := Token{
								Type:          WORDBREAK_TOKEN,
								Value:         ,
								RawValue:      ,
								Span:          ,
								State:         [+3].State,
								WordbreakType: ,
							}
							 = append(, )
							 += 3
							continue
						}
					}

					 := Token{
						Type:          WORDBREAK_TOKEN,
						Value:         ,
						RawValue:      ,
						Span:          ,
						State:         .State,
						WordbreakType: ,
					}
					 = append(, )
					 += 1
					continue
				}
			}
		}

		 = append(, )
	}
	return 
}

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