Skip to content

Commit

Permalink
feat(fsm): add always(), lit(), not(), cleanup imports
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 4, 2019
1 parent 995a104 commit 992f31a
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 11 deletions.
8 changes: 8 additions & 0 deletions packages/fsm/src/always.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LitCallback, Matcher } from "./api";
import { success } from "./success";

export const always =
<T, C, R>(callback?: LitCallback<T, C, R>): Matcher<T, C, R> =>
() =>
(ctx, x) =>
success(callback && callback(ctx, x));
2 changes: 1 addition & 1 deletion packages/fsm/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type ResultBody<T> =
export type AltCallback<T, C, R> =
(ctx: C, next: ResultBody<R>, x: T[]) => ResultBody<R>;

export type RangeCallback<T, C, R> =
export type LitCallback<T, C, R> =
(ctx: C, x: T) => ResultBody<R>;

export type SeqCallback<T, C, R> =
Expand Down
7 changes: 6 additions & 1 deletion packages/fsm/src/fsm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { IObjectOf } from "@thi.ng/api";
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
import { illegalState } from "@thi.ng/errors/illegal-state";
import { Reducer, Transducer } from "@thi.ng/transducers/api";
import { reduced, unreduced, isReduced, ensureReduced } from "@thi.ng/transducers/reduced";
import {
ensureReduced,
isReduced,
reduced,
unreduced
} from "@thi.ng/transducers/reduced";
import { Match, Matcher } from "./api";

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/fsm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export * from "./api";

export * from "./alts";
export * from "./always";
export * from "./fsm";
export * from "./lit";
export * from "./not";
export * from "./range";
export * from "./repeat";
export * from "./seq";
Expand Down
26 changes: 26 additions & 0 deletions packages/fsm/src/lit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Predicate2 } from "@thi.ng/api";
import { equiv as _equiv } from "@thi.ng/equiv";
import {
Matcher,
RES_FAIL,
RES_PARTIAL,
SeqCallback
} from "./api";
import { success } from "./success";

export const lit = <T, C, R>(
match: T[],
callback?: SeqCallback<T, C, R>,
equiv: Predicate2<T> = _equiv
): Matcher<T, C, R> =>
() => {
const buf: T[] = [];
const n = match.length;
let i = 0;
return (state, x) =>
equiv((buf.push(x), x), match[i++]) ?
i === n ?
success(callback && callback(state, buf)) :
RES_PARTIAL :
RES_FAIL;
};
17 changes: 17 additions & 0 deletions packages/fsm/src/not.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Match, RES_FAIL, RES_PARTIAL, Matcher } from "./api";
import { success } from "./success";

export const not =
<T, C, R>(match: Matcher<T, C, R>, callback?): Matcher<T, C, R> =>
() => {
let m = match();
return (ctx, x) => {
const { type } = m(ctx, x);
return type === Match.FAIL ?
success(callback && callback(ctx)) :
type !== Match.PARTIAL ?
// TODO Match.FULL_NC handling?
RES_FAIL :
RES_PARTIAL;
}
};
8 changes: 4 additions & 4 deletions packages/fsm/src/range.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { alts } from "./alts";
import {
AltCallback,
LitCallback,
Matcher,
RangeCallback,
RES_FAIL
} from "./api";
import { success } from "./success";
import { str } from "./str";
import { success } from "./success";

export const range = <T extends number | string, C, R>(
min: T,
max: T,
callback?: RangeCallback<T, C, R>
callback?: LitCallback<T, C, R>
): Matcher<T, C, R> =>
() =>
(ctx, x) =>
Expand All @@ -20,7 +20,7 @@ export const range = <T extends number | string, C, R>(
RES_FAIL;

export const digit =
<C, R>(callback?: RangeCallback<string, C, R>): Matcher<string, C, R> =>
<C, R>(callback?: LitCallback<string, C, R>): Matcher<string, C, R> =>
range("0", "9", callback);

export const alpha =
Expand Down
2 changes: 1 addition & 1 deletion packages/fsm/src/repeat.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
Match,
Matcher,
SeqCallback,
RES_PARTIAL,
SeqCallback
} from "./api";
import { success } from "./success";

Expand Down
4 changes: 2 additions & 2 deletions packages/fsm/src/str.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
LitCallback,
Matcher,
RangeCallback,
RES_FAIL,
RES_PARTIAL
} from "./api";
import { success } from "./success";

export const str = <C, R>(
str: string,
callback?: RangeCallback<string, C, R>
callback?: LitCallback<string, C, R>
): Matcher<string, C, R> =>
() => {
let buf = "";
Expand Down
4 changes: 2 additions & 2 deletions packages/fsm/src/until.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Matcher, RangeCallback, RES_PARTIAL } from "./api";
import { LitCallback, Matcher, RES_PARTIAL } from "./api";
import { success } from "./success";

export const until = <C, R>(
str: string,
callback?: RangeCallback<string, C, R>
callback?: LitCallback<string, C, R>
): Matcher<string, C, R> =>
() => {
let buf = "";
Expand Down

0 comments on commit 992f31a

Please sign in to comment.