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"|":returnWORDBREAK_PIPEcase"&":// Cmd: & is a command separator (like ; in POSIX)returnWORDBREAK_LIST_SEQUENTIALcase"&&":returnWORDBREAK_LIST_ANDcase"||":returnWORDBREAK_LIST_ORcase">", ">>":returnWORDBREAK_REDIRECT_OUTPUTcase"<":returnWORDBREAK_REDIRECT_INPUTcase"(", ")":// Cmd: parentheses are grouping operators for command blocksreturnWORDBREAK_UNKNOWNdefault:returnWORDBREAK_UNKNOWN }}func (cmdFormat) () map[string]WordbreakType { returnnil }func (cmdFormat) () bool { returnfalse }func (cmdFormat) () bool { returnfalse }func (cmdFormat) () bool { returntrue }func (cmdFormat) () bool { returntrue }func (cmdFormat) () map[rune]bool { returnnil }func (cmdFormat) ( string) string { returncmdQuoteWord() }func (cmdFormat) () bool { returnfalse }func (cmdFormat) () bool { returnfalse }// 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] == '>' {iflen(.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 nextif +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(, ) += 3continue } } := Token{Type: WORDBREAK_TOKEN,Value: ,RawValue: ,Span: ,State: .State,WordbreakType: , } = append(, ) += 1continue } } } = append(, ) }return}func (cmdFormat) ( string) bool {returnbareWordRe.MatchString()}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.