Created
October 31, 2021 04:53
-
-
Save pfgray/5562a0feaef63919f286d90cd3307750 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as S from 'parser-ts/string' | |
import * as C from 'parser-ts/char' | |
import * as P from 'parser-ts/Parser' | |
import { pipe } from 'fp-ts/lib/function'; | |
import { run } from 'parser-ts/code-frame' | |
import { ADT } from "ts-adt"; | |
export type FolderDetails = ADT<{ | |
branch: { | |
hash: string | |
value: string | |
} | |
bare: {} | |
}> | |
export const hashAndBranch = (hash: string, value: string): FolderDetails => ({_type: 'branch', hash, value} as const) | |
export const bare: FolderDetails = {_type: 'bare'} as const | |
const input = `/home/paul/dev/chainable-components/.worktree (bare) | |
/home/paul/dev/chainable-components/foo d98443c [new_do] | |
/home/paul/dev/chainable-components/parity_with_recompose b082d42 [parity_with_recompose]`; | |
const pathP = pipe( | |
P.takeUntil<string>(s => s === ' '), | |
P.map(s => s.join('')) | |
) | |
const hashAndBranchP = pipe( | |
P.many1(C.alphanum), | |
P.map(s => s.join('')), | |
P.bindTo('hash'), | |
P.chainFirst(() => C.space), | |
P.bind('branch', () => | |
pipe(C.char('['), P.chain(() => P.takeUntil(s => s === ']')), P.map(s => s.join('')) | |
)), | |
P.chainFirst(() => C.char(']')), | |
P.map(({hash, branch}) => hashAndBranch(hash, branch)) | |
) | |
const bareP = pipe(S.string('(bare)'), P.map(() => bare)) | |
const detailsP = pipe( | |
P.either(hashAndBranchP, () => bareP) | |
) | |
const rowP = pipe( | |
pathP, | |
P.bindTo('path'), | |
P.chainFirst(() => P.many(C.char(' '))), | |
P.bind('details', () => detailsP), | |
P.chainFirst(() => P.optional(C.char('\n'))) | |
) | |
const listP = P.many1(rowP) | |
console.log(JSON.stringify( | |
run(listP, input) | |
, null, 2)) | |
// const parsePath = | |
// const parseList = | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment