diff options
| -rw-r--r-- | src/index.ts | 91 | ||||
| -rw-r--r-- | src/parser.ts | 94 | ||||
| -rw-r--r-- | tests/basic.spec.ts | 10 |
3 files changed, 144 insertions, 51 deletions
diff --git a/src/index.ts b/src/index.ts index a9b3409..3dc572b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,55 +1,48 @@ -import { flow, identity, pipe } from 'fp-ts/function' -import { Either, left, right, map, chain, mapLeft, fold, orElse } from 'fp-ts/Either' +import { constant, flow, identity, pipe } from 'fp-ts/function' +import {mapTo, symbol} from './parser' -type char = string +export const start = mapTo(symbol('^'), constant({ tag: 'Start' } as Expr)) +export const end = mapTo(symbol('$'), constant({ tag: 'End' } as Expr)) +export const anyItem = mapTo(symbol(','), constant({ tag: 'AnyItem' } as Expr)) +export const nextItem = mapTo(symbol(','), constant({ tag: 'NextItem' } as Expr)) +// export const optional = mapTo(symbol('?'), constant({ tag: 'Optional' } as Expr)) +// export const zeroOrMore = mapTo(symbol('*'), constant({ tag: 'ZeroOrMore' } as Expr)) +// export const oneOrMore = mapTo(symbol('+'), constant({ tag: 'OneOrMore' } as Expr)) -type ParserResult<T> = [T, string] -type ParserError = [string, string] -type Parser<T> = (input: string) => Either<ParserError, ParserResult<T>> +type Expr = + | { tag: 'Start' } + | { tag: 'End' } + | { tag: 'Optional' } + | { tag: 'OneOrMore' } + | { tag: 'ZeroOrMore' } + | { tag: 'NextItem' } + | { tag: 'AnyItem' } + | { tag: 'Or' } + | { tag: 'String' } + | { tag: 'Number' } + | { tag: 'Bool' } + | { tag: 'Truthy' } + | { tag: 'Falsey' } -export const many0 = <T>(parser: Parser<T>): Parser<Array<T>> => flow( - parser, - chain(([a, nextInput]) => - pipe( - nextInput, - many0(parser), - map(([ls, inp]): ParserResult<T[]> => [[a, ...ls], inp]), - ) - ), - orElse(([_, inp]) => right([[] as T[], inp])) -) +/* -export const many1 = <T>(parser: Parser<T>): Parser<Array<T>> => flow( - many0(parser), - chain(([res, inp]) => - res.length > 0 ? right([res, inp]) : left([`many1 failed to parse at ${inp}`, inp])) -) +^ $ => start and end of list +? => optional +. => any item +* => 0 or more instances ++ => 1 or more instances +{3,6} => 3 to 6 instances +\s => string +\n => number +\b => boolean +\T => truthy +\F => falsey +(> 5) => number greater than +(< 5) => number less than +, => followed by +[name x] => apply x on property `name` +| => or +/x/ => match regular expression (string values in list) -export const satify_char = (f: (c: char) => boolean): Parser<char> => (input: string) => { - const c = input.charAt(0) - if (f(c)) return right([c, input.slice(1)]) - return left([`Expected to satisfy ${f}, got "${c}"`, input]) -}; - -export const digit = satify_char(c => /^[0-9]$/g.test(c)) - -export const integer: Parser<number> = flow( - many1(digit), - map(([ds, input]) => [parseInt(ds.join(''), 10), input]) -) - -export const or = <T>(parsers: Parser<T>[]): Parser<T> => { - const ppp = ([p, ...ps]: Parser<T>[]) => flow( - p, - orElse(([_, inp]) => or(ps)(inp)) - ) - - return parsers.length > 0 ? ppp(parsers) : (inp: string) => left(['unable to match', inp]) -} - -export const space = satify_char(c => c === ' ') -export const newline = satify_char(c => c === '\n') -export const tab = satify_char(c => c === '\t') - -export const whitespace = or([space, newline, tab]) +*/ diff --git a/src/parser.ts b/src/parser.ts new file mode 100644 index 0000000..076e0c5 --- /dev/null +++ b/src/parser.ts @@ -0,0 +1,94 @@ +import { flow, pipe } from 'fp-ts/function' +import { Either, left, right, map, chain, orElse } from 'fp-ts/Either' + +export type char = string + +export type ParserResult<T> = [T, string] +export type ParserError = [string, string] +export type Parser<T> = (input: string) => Either<ParserError, ParserResult<T>> + +export const constP = <T>(v: T): Parser<T> => (inp: string) => right([v, inp]) + +export const many0 = <T>(parser: Parser<T>): Parser<Array<T>> => flow( + parser, + chain(([a, nextInput]) => + pipe( + nextInput, + many0(parser), + map(([ls, inp]): ParserResult<T[]> => [[a, ...ls], inp]), + ) + ), + orElse(([_, inp]) => right([[] as T[], inp])) +) + +export const many1 = <T>(parser: Parser<T>): Parser<Array<T>> => flow( + many0(parser), + chain(([res, inp]) => + res.length > 0 ? right([res, inp]) : left([`many1 failed to parse at ${inp}`, inp])) +) + +export const prefixed = <T>(a: Parser<any>, b: Parser<T>): Parser<T> => flow( + a, + chain(([_, inp]) => b(inp)) +) + +export const suffixed = <T>(a: Parser<T>, b: Parser<any>): Parser<T> => flow( + a, + chain(([out, inp]) => pipe(inp, + b, + chain(([_, inp2]) => right([out, inp2])) + )) +) + +export const delimited = <T>(p: Parser<any>, a: Parser<T>, s: Parser<any>): Parser<T> => + suffixed(prefixed(p, a), s) + +export const satifyChar = (f: (c: char) => boolean): Parser<char> => (input: string) => { + const c = input.charAt(0) + if (f(c)) return right([c, input.slice(1)]) + return left([`Expected to satisfy ${f}, got "${c}"`, input]) +} + +export const digit = satifyChar(c => /^[0-9]$/g.test(c)) + +export const integer: Parser<number> = flow( + many1(digit), + map(([ds, input]) => [parseInt(ds.join(''), 10), input]) +) + +export const or = <T>(parsers: Parser<T>[]): Parser<T> => { + const ppp = ([p, ...ps]: Parser<T>[]) => flow( + p, + orElse(([_, inp]) => or(ps)(inp)) + ) + + return parsers.length > 0 ? ppp(parsers) : (inp: string) => left(['unable to match', inp]) +} + +export const matchChar = (ch: char): Parser<char> => satifyChar(c => c === ch) + +export const space = matchChar(' ') +export const newline = matchChar('\n') +export const tab = matchChar('\t') + +export const whitespace = or([space, newline, tab]) +export const whitespaces0 = many0(whitespace) + +export const matchString = (s: string): Parser<string> => + s === '' ? constP('') : flow( + matchChar(s.charAt(0)), + chain(([c, inp]) => pipe( + inp, + matchString(s.slice(1)), + map(([s, inp]) => [c + s, inp]), + )) + ) + +export const symbol = (s: string): Parser<string> => + delimited(whitespaces0, matchString(s), whitespaces0) + +export const mapTo = <I, R>(p: Parser<I>, f: (p: I) => R): Parser<R> => flow( + p, + map(([v, inp]) => [f(v), inp]) +) + diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts index 26a2edb..e650cf7 100644 --- a/tests/basic.spec.ts +++ b/tests/basic.spec.ts @@ -1,10 +1,16 @@ -import {right} from 'fp-ts/Either' -import { many0, digit, integer, whitespace } from '../src' +import {left, right} from 'fp-ts/Either' +import { many0, digit, integer, whitespace, suffixed, prefixed, whitespaces0, delimited, symbol } from '../src/parser' describe('Foobar', () => { it ('should do shit', () => { expect(integer('12901')).toEqual(right([12901, ''])) expect(integer('12901asas')).toEqual(right([12901, 'asas'])) + expect(whitespace(' ')).toEqual(right([' ', ''])) + expect(whitespace('\n')).toEqual(right(['\n', ''])) + expect(whitespace('a')).toEqual(left(['unable to match', 'a'])) + + expect(delimited(whitespaces0, integer, whitespaces0)(' 20 ')).toEqual(right([20, ''])) + expect(delimited(whitespaces0, integer, whitespaces0)(' 2 0 ')).toEqual(right([2, '0 '])) }) }) |
