Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Aug 11, 2022
2 parents b5ddc85 + 7aacd86 commit 39dae47
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
### Bugfixes
-->
## 0.7.1 (2022/08/11)

### Bugfixes
- (#12) Inference of type in parser.map() fails caused by T.alt not returning a Parser type.

## 0.7.0 (2022/08/11)

Expand Down
2 changes: 1 addition & 1 deletion examples/json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"typescript": "4.7.4"
},
"dependencies": {
"terrario": "0.4.0"
"terrario": "0.7.0"
}
}
24 changes: 11 additions & 13 deletions examples/json/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as T from 'terrario';

const spaces = T.regexp(/[ \t\r\n]*/);
const spaces = T.str(/[ \t\r\n]*/);

const lang = T.createLanguage({
value: r => T.alt([
Expand All @@ -17,7 +17,7 @@ const lang = T.createLanguage({
bool: r => T.alt([
T.str('true'),
T.str('false'),
]).map((value: 'true' | 'false') => {
]).map(value => {
return (value === 'true');
}),

Expand All @@ -31,20 +31,18 @@ const lang = T.createLanguage({
], 1),

number: r => T.alt([
T.regexp(/[+-]?[0-9]+\.[0-9]+/),
T.regexp(/[+-]?[0-9]+/),
]).map((value: string) => {
return parseFloat(value);
}),
T.str(/[+-]?[0-9]+\.[0-9]+/),
T.str(/[+-]?[0-9]+/),
]).map(value => Number(value)),

object: r => {
const entry = T.seq([
r.string,
r.string as T.Parser<string>,
spaces,
T.str(':'),
spaces,
r.value,
]).map((value: unknown[]) => {
r.value as T.Parser<unknown>,
]).map((value) => {
return { key: value[0], value: value[4] };
});
const separator = T.seq([
Expand All @@ -58,7 +56,7 @@ const lang = T.createLanguage({
T.sep(entry, separator, 1).option(),
spaces,
T.str('}'),
], 2).map((value: { key: string, value: unknown }[] | null) => {
], 2).map(value => {
if (value == null) {
return {};
}
Expand All @@ -79,10 +77,10 @@ const lang = T.createLanguage({
return T.seq([
T.str('['),
spaces,
T.sep(r.value, separator, 1).option(),
T.sep(r.value as T.Parser<unknown>, separator, 1).option(),
spaces,
T.str(']'),
], 2).map((value: unknown[] | null) => {
], 2).map((value) => {
return (value != null ? value : []);
});
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "terrario",
"version": "0.7.0",
"version": "0.7.1",
"description": "A simple parser combinator library with TypeScript.",
"main": "./built/index.js",
"types": "./built/index.d.ts",
Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export type Failure = {
export type Result<T> = Success<T> | Failure;
export type ParserHandler<T> = (input: string, index: number, state: any) => Result<T>;

type ParserResult<T> = T extends Parser<infer R> ? R : never;
type ParserResults<T> = T extends [infer Head, ...infer Tail] ? [ParserResult<Head>, ...ParserResults<Tail>] : [];

export function success<T>(index: number, value: T): Success<T> {
return {
success: true,
Expand Down Expand Up @@ -148,16 +151,13 @@ function strWithRegExp(pattern: RegExp): Parser<string> {
});
}

type SeqResultItem<T> = T extends Parser<infer R> ? R : never;
type SeqResult<T> = T extends [infer Head, ...infer Tail] ? [SeqResultItem<Head>, ...SeqResult<Tail>] : [];

export function seq<T extends Parser<any>[]>(parsers: [...T]): Parser<SeqResult<[...T]>>
export function seq<T extends Parser<any>[]>(parsers: [...T]): Parser<ParserResults<[...T]>>
export function seq<T extends Parser<any>[], U extends number>(parsers: [...T], select: U): T[U]
export function seq(parsers: Parser<any>[], select?: number) {
return (select == null) ? seqAll(parsers) : seqSelect(parsers, select);
}

function seqAll<T extends Parser<any>[]>(parsers: [...T]): Parser<SeqResult<[...T]>> {
function seqAll<T extends Parser<any>[]>(parsers: [...T]): Parser<ParserResults<[...T]>> {
return new Parser((input, index, state) => {
let result;
let latestIndex = index;
Expand All @@ -170,19 +170,19 @@ function seqAll<T extends Parser<any>[]>(parsers: [...T]): Parser<SeqResult<[...
latestIndex = result.index;
accum.push(result.value);
}
return success(latestIndex, (accum as SeqResult<[...T]>));
return success(latestIndex, (accum as ParserResults<[...T]>));
});
}

function seqSelect<T extends Parser<any>[], U extends number>(parsers: [...T], select: U): T[U] {
return seqAll(parsers).map(values => values[select]);
}

export function alt<T extends Parser<unknown>[]>(parsers: T): T[number] {
export function alt<T extends Parser<unknown>[]>(parsers: [...T]): Parser<ParserResults<T>[number]> {
return new Parser((input, index, state) => {
let result;
for (let i = 0; i < parsers.length; i++) {
result = parsers[i].handler(input, index, state);
result = parsers[i].handler(input, index, state) as Result<ParserResults<T>[number]>;
if (result.success) {
return result;
}
Expand Down

0 comments on commit 39dae47

Please sign in to comment.