MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / tryParseRedirect

Function tryParseRedirect

source code/utils/bash/bashParser.ts:1625–1861  ·  view source on GitHub ↗

* Parse a redirect operator + destination(s). * @param greedy When true, file_redirect consumes repeat1($._literal) per * grammar's prec.left — `cmd >f a b c` attaches `a b c` to the redirect. * When false (preRedirect context), takes only 1 destination because * command's dynamic preceden

(P: ParseState, greedy = false)

Source from the content-addressed store, hash-verified

1623 * command's dynamic precedence beats redirected_statement's prec(-1).
1624 */
1625function tryParseRedirect(P: ParseState, greedy = false): TsNode | null {
1626 const save = saveLex(P.L)
1627 skipBlanks(P.L)
1628 // File descriptor prefix?
1629 let fd: TsNode | null = null
1630 if (isDigit(peek(P.L))) {
1631 const startB = P.L.b
1632 let j = P.L.i
1633 while (j < P.L.len && isDigit(P.L.src[j]!)) j++
1634 const after = j < P.L.len ? P.L.src[j]! : ''
1635 if (after === '>' || after === '<') {
1636 while (P.L.i < j) advance(P.L)
1637 fd = mk(P, 'file_descriptor', startB, P.L.b, [])
1638 }
1639 }
1640 const t = nextToken(P.L, 'arg')
1641 if (t.type !== 'OP') {
1642 restoreLex(P.L, save)
1643 return null
1644 }
1645 const v = t.value
1646 if (v === '<<<') {
1647 const op = leaf(P, '<<<', t)
1648 skipBlanks(P.L)
1649 const target = parseWord(P, 'arg')
1650 const end = target ? target.endIndex : op.endIndex
1651 const kids = target ? [op, target] : [op]
1652 return mk(
1653 P,
1654 'herestring_redirect',
1655 fd ? fd.startIndex : op.startIndex,
1656 end,
1657 fd ? [fd, ...kids] : kids,
1658 )
1659 }
1660 if (v === '<<' || v === '<<-') {
1661 const op = leaf(P, v, t)
1662 // Heredoc start — delimiter word (may be quoted)
1663 skipBlanks(P.L)
1664 const dStart = P.L.b
1665 let quoted = false
1666 let delim = ''
1667 const dc = peek(P.L)
1668 if (dc === "'" || dc === '"') {
1669 quoted = true
1670 advance(P.L)
1671 while (P.L.i < P.L.len && peek(P.L) !== dc) {
1672 delim += peek(P.L)
1673 advance(P.L)
1674 }
1675 if (P.L.i < P.L.len) advance(P.L)
1676 } else if (dc === '\\') {
1677 // Backslash-escaped delimiter: \X — exactly one escaped char, body is
1678 // quoted (literal). Covers <<\EOF <<\' <<\\ etc.
1679 quoted = true
1680 advance(P.L)
1681 if (P.L.i < P.L.len && peek(P.L) !== '\n') {
1682 delim += peek(P.L)

Callers 2

parseSimpleCommandFunction · 0.85
maybeRedirectFunction · 0.85

Calls 15

saveLexFunction · 0.85
skipBlanksFunction · 0.85
isDigitFunction · 0.85
advanceFunction · 0.85
mkFunction · 0.85
nextTokenFunction · 0.85
restoreLexFunction · 0.85
leafFunction · 0.85
parseWordFunction · 0.85
isIdentCharFunction · 0.85
isHeredocDelimCharFunction · 0.85
isRedirectLiteralStartFunction · 0.85

Tested by

no test coverage detected