diff options
| author | Akshay Nair <phenax5@gmail.com> | 2022-01-06 21:59:34 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2022-01-06 22:00:38 +0530 |
| commit | 3d88de49d99d86c70204d6870296b20177c2af21 (patch) | |
| tree | f8bbf0580035f0792d5568082ec11db02af5668b | |
| parent | 0cc4e48c848aef5751573628e4af6e194b272d1d (diff) | |
| download | elxr-3d88de49d99d86c70204d6870296b20177c2af21.tar.gz elxr-3d88de49d99d86c70204d6870296b20177c2af21.zip | |
chore: adds prettier
| -rw-r--r-- | .prettierrc | 6 | ||||
| -rw-r--r-- | esbuild.js | 2 | ||||
| -rw-r--r-- | package.json | 2 | ||||
| -rw-r--r-- | src/index.d.ts | 7 | ||||
| -rw-r--r-- | src/index.ts | 8 | ||||
| -rw-r--r-- | src/parser.ts | 135 | ||||
| -rw-r--r-- | tests/basic.spec.d.ts | 0 | ||||
| -rw-r--r-- | tests/basic.spec.ts | 25 | ||||
| -rw-r--r-- | yarn.lock | 5 |
9 files changed, 118 insertions, 72 deletions
diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..f491fe1 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "semi": false, + "trailingComma": "es5", + "singleQuote": true, + "printWidth": 80 +} @@ -6,7 +6,7 @@ esbuild outdir: 'lib', bundle: true, sourcemap: true, - minify: false, + minify: true, splitting: false, format: 'esm', target: ['esnext'] diff --git a/package.json b/package.json index 9d62f1f..c27633e 100644 --- a/package.json +++ b/package.json @@ -10,12 +10,14 @@ "scripts": { "tsc": "tsc --emitDeclarationOnly --outDir lib", "test": "jest", + "format": "prettier --write ./src/**/*.ts", "build": "rimraf lib && node ./esbuild.js && npm run tsc" }, "devDependencies": { "@types/jest": "^27.4.0", "esbuild": "^0.14.10", "jest": "^27.4.7", + "prettier": "^2.5.1", "rimraf": "^3.0.2", "ts-jest": "^27.1.2", "typescript": "^4.5.4" diff --git a/src/index.d.ts b/src/index.d.ts deleted file mode 100644 index fdaab41..0000000 --- a/src/index.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Either } from 'fp-ts/Either'; -declare type ParserResult<T> = [T, string]; -declare type ParserError = [string, string]; -declare type Parser<T> = (input: string) => Either<ParserError, ParserResult<T>>; -export declare const p_digit: Parser<string>; -export declare const many0: <T>(p: Parser<T>) => Parser<T[]>; -export {}; diff --git a/src/index.ts b/src/index.ts index 3dc572b..d236473 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,13 @@ import { constant, flow, identity, pipe } from 'fp-ts/function' -import {mapTo, symbol} from './parser' +import { mapTo, symbol } from './parser' 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 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)) @@ -45,4 +48,3 @@ type Expr = /x/ => match regular expression (string values in list) */ - diff --git a/src/parser.ts b/src/parser.ts index 076e0c5..f8dcd44 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -7,49 +7,67 @@ 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 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 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 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 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 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 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 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 digit = satifyChar((c) => /^[0-9]$/g.test(c)) export const integer: Parser<number> = flow( many1(digit), @@ -57,15 +75,18 @@ export const integer: Parser<number> = flow( ) export const or = <T>(parsers: Parser<T>[]): Parser<T> => { - const ppp = ([p, ...ps]: Parser<T>[]) => flow( - p, - orElse(([_, inp]) => or(ps)(inp)) - ) + 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]) + 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 matchChar = (ch: char): Parser<char> => satifyChar((c) => c === ch) export const space = matchChar(' ') export const newline = matchChar('\n') @@ -75,20 +96,24 @@ 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]), - )) - ) + 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]) -) - +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.d.ts b/tests/basic.spec.d.ts deleted file mode 100644 index e69de29..0000000 --- a/tests/basic.spec.d.ts +++ /dev/null diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts index e650cf7..eeeb121 100644 --- a/tests/basic.spec.ts +++ b/tests/basic.spec.ts @@ -1,16 +1,29 @@ -import {left, right} from 'fp-ts/Either' -import { many0, digit, integer, whitespace, suffixed, prefixed, whitespaces0, delimited, symbol } from '../src/parser' +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', () => { + 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 '])) + expect(delimited(whitespaces0, integer, whitespaces0)(' 20 ')).toEqual( + right([20, '']) + ) + expect(delimited(whitespaces0, integer, whitespaces0)(' 2 0 ')).toEqual( + right([2, '0 ']) + ) }) }) - @@ -2155,6 +2155,11 @@ prelude-ls@~1.1.2: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54" integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ= +prettier@^2.5.1: + version "2.5.1" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.5.1.tgz#fff75fa9d519c54cf0fce328c1017d94546bc56a" + integrity sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg== + pretty-format@^27.0.0, pretty-format@^27.4.6: version "27.4.6" resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-27.4.6.tgz#1b784d2f53c68db31797b2348fa39b49e31846b7" |
