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

Function parsePipeline

source code/utils/bash/bashParser.ts:940–994  ·  view source on GitHub ↗

* Parse commands joined by | or |&. Flat children with operator leaves. * tree-sitter quirk: `a | b 2>nul | c` hoists the redirect on `b` to wrap * the preceding pipeline fragment — pipeline(redirected_statement( * pipeline(a,|,b), 2>nul), |, c).

(P: ParseState)

Source from the content-addressed store, hash-verified

938 * pipeline(a,|,b), 2>nul), |, c).
939 */
940function parsePipeline(P: ParseState): TsNode | null {
941 let first = parseCommand(P)
942 if (!first) return null
943 const parts: TsNode[] = [first]
944 while (true) {
945 const save = saveLex(P.L)
946 const t = nextToken(P.L, 'cmd')
947 if (t.type === 'OP' && (t.value === '|' || t.value === '|&')) {
948 const op = leaf(P, t.value, t)
949 skipNewlines(P)
950 const next = parseCommand(P)
951 if (!next) {
952 parts.push(op)
953 break
954 }
955 // Hoist trailing redirect on `next` to wrap current pipeline fragment
956 if (
957 next.type === 'redirected_statement' &&
958 next.children.length >= 2 &&
959 parts.length >= 1
960 ) {
961 const inner = next.children[0]!
962 const redirs = next.children.slice(1)
963 // Wrap existing parts + op + inner as a pipeline
964 const pipeKids = [...parts, op, inner]
965 const pipeNode = mk(
966 P,
967 'pipeline',
968 pipeKids[0]!.startIndex,
969 inner.endIndex,
970 pipeKids,
971 )
972 const lastR = redirs[redirs.length - 1]!
973 const wrapped = mk(
974 P,
975 'redirected_statement',
976 pipeNode.startIndex,
977 lastR.endIndex,
978 [pipeNode, ...redirs],
979 )
980 parts.length = 0
981 parts.push(wrapped)
982 first = wrapped
983 continue
984 }
985 parts.push(op, next)
986 } else {
987 restoreLex(P.L, save)
988 break
989 }
990 }
991 if (parts.length === 1) return parts[0]!
992 const last = parts[parts.length - 1]!
993 return mk(P, 'pipeline', parts[0]!.startIndex, last.endIndex, parts)
994}
995
996/** Parse a single command: simple, compound, or control structure. */
997function parseCommand(P: ParseState): TsNode | null {

Callers 1

parseAndOrFunction · 0.85

Calls 7

saveLexFunction · 0.85
nextTokenFunction · 0.85
leafFunction · 0.85
skipNewlinesFunction · 0.85
mkFunction · 0.85
restoreLexFunction · 0.85
parseCommandFunction · 0.70

Tested by

no test coverage detected