Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser): add parser combinators #78

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,39 @@ type res5 = Pipe<
- [x] `Extends<A, B>`
- [x] `Equals<A, B>`
- [x] `DoesNotExtend<A, B>`
- [ ] Parser
- [x] Parse
- [x] ToString
- [x] Literal
- [x] NotLiteral
- [x] Optional
- [x] Many
- [x] Many1
- [x] Sequence
- [x] EndOfInput
- [x] Map
- [x] MapError
- [x] Skip
- [x] Choice
- [x] Or
- [x] Not
- [x] Whitespace
- [x] Whitespaces
- [x] Trim
- [x] TrimLeft
- [x] TrimRight
- [x] Any
- [x] CharRange
- [x] Alpha
- [x] AlphaNum
- [x] Digit
- [x] Digits
- [x] Word
- [x] SepBy
- [x] Between
- [x] PrefixBy
- [x] SufixBy
- [x] SepByLiteral
- [x] BetweenLiterals
- [x] PrefixByLiteral
- [x] SufixByLiteral
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { Tuples } from "./internals/tuples/Tuples";
import { Unions } from "./internals/unions/Unions";
import { Booleans } from "./internals/booleans/Booleans";
import { Match } from "./internals/match/Match";
import { Parser } from "./internals/parser/Parser";

export {
_,
Expand Down Expand Up @@ -62,11 +63,13 @@ export {
Numbers,
Tuples,
Functions,
Parser,
Booleans as B,
Objects as O,
Unions as U,
Strings as S,
Numbers as N,
Tuples as T,
Functions as F,
Parser as P,
};
14 changes: 14 additions & 0 deletions src/internals/objects/Objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ export namespace Objects {
return: Impl.FromEntries<Extract<this["arg0"], [PropertyKey, any]>>;
}

/**
* Create an object from an array like `[key1, value1, key2, value2, ...]`.
* @param arr - array to convert to an object
* @returns an object
*
* @example
* ```ts
* type T0 = Call<Objects.FromArray, ["a", 1, "b", true]>; // { a: 1; b: true }
* ```
*/
export interface FromArray extends Fn {
return: Impl.FromArray<this["arg0"]>;
}

/**
* Turn an object into a union of entries
* @param obj - The object to transform to entries
Expand Down
7 changes: 7 additions & 0 deletions src/internals/objects/impl/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export type FromEntries<entries extends [PropertyKey, any]> = {
[entry in entries as entry[0]]: entry[1];
};

export type FromArray<
arr extends unknown[],
Acc extends Record<PropertyKey, unknown> = {}
> = arr extends [infer key extends PropertyKey, infer value, ...infer rest]
? FromArray<rest, Prettify<Acc & { [K in key]: value }>>
: Acc;

export type Entries<T> = Keys<T> extends infer keys extends keyof T
? {
[K in keys]: [K, T[K]];
Expand Down
Loading