Skip to content

Commit

Permalink
update apis
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Aug 19, 2023
1 parent 13442dd commit f40ba71
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 110 deletions.
2 changes: 1 addition & 1 deletion examples/calculator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { token as T } from 'terrario';
import { tokenParsers as T } from 'terrario';
import { Operator, buildPrattParser } from './pratt.js';

type Expr = { kind: 'number', value: number } | Operator<Expr>;
Expand Down
2 changes: 1 addition & 1 deletion examples/calculator/src/pratt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { token as T } from 'terrario';
import { tokenParsers as T } from 'terrario';

type OperatorInfo = {
kind: 'prefix' | 'postfix',
Expand Down
40 changes: 20 additions & 20 deletions examples/json/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { string as T } from 'terrario';
import * as T from 'terrario';

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

type JsonValue = null | boolean | string | number | Record<string, unknown> | unknown[];

Expand Down Expand Up @@ -31,47 +31,47 @@ const lang = T.language<Json>({
r.number,
]),

null: r => T.str('null').map(() => null),
null: r => T.token('null').map(() => null),

bool: r => T.alt([
T.str('true'),
T.str('false'),
T.token('true'),
T.token('false'),
]).map(value => (value === 'true')),

string: r => T.seq([
T.str('"'),
T.char.many({ notMatch: T.alt([T.str('"'), T.cr, T.lf]) }).text(),
T.str('"'),
T.token('"'),
T.any.many({ notMatch: T.alt([T.token('"'), T.cr, T.lf]) }).span(),
T.token('"'),
], 1),

number: r => T.alt([
T.seq([
T.str(/[+-]/).option(),
T.str(/[0-9]/).many(1),
T.token(/[+-]/).option(),
T.token(/[0-9]/).many(1),
T.seq([
T.str('.'),
T.str(/[0-9]/).many(1),
T.token('.'),
T.token(/[0-9]/).many(1),
]).option(),
]).text(),
]).span(),
]).map(value => Number(value)),

object: r => {
const entry = T.seq([
r.string,
spaces,
T.str(':'),
T.token(':'),
spaces,
r.value,
]).map(value => {
return { key: value[0], value: value[4] };
});
const separator = T.seq([
spaces,
T.str(','),
T.token(','),
spaces,
]);
return T.seq([
T.str('{'),
T.token('{'),
spaces,
T.seq([
entry,
Expand All @@ -80,7 +80,7 @@ const lang = T.language<Json>({
], 1).many(),
]).map(x => [x[0], ...x[1]]).option(),
spaces,
T.str('}'),
T.token('}'),
], 2).map(value => {
if (value == null) {
return {};
Expand All @@ -96,11 +96,11 @@ const lang = T.language<Json>({
array: r => {
const separator = T.seq([
spaces,
T.str(','),
T.token(','),
spaces,
]);
return T.seq([
T.str('['),
T.token('['),
spaces,
T.seq([
r.value,
Expand All @@ -110,7 +110,7 @@ const lang = T.language<Json>({
], 1).many(),
]).map(x => [x[0], ...x[1]]).option(),
spaces,
T.str(']'),
T.token(']'),
], 2).map(value => {
return (value != null ? value : []);
});
Expand Down
78 changes: 75 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,79 @@
import * as string from './string-parser.js';
import * as token from './token-parser.js';
import {
Failure,
Language,
LanguageSource,
LazyContext,
LazyParserOpts,
ParserContext,
ParserHandler,
Result,
ResultType,
ResultTypes,
StrictParserOpts,
Success,
Parser,
alt,
any,
cr,
crlf,
eof,
failure,
language,
lazy,
lf,
lineBegin,
lineEnd,
match,
newline,
notMatch,
parser,
seq,
sof,
succeeded,
success,
token,
where,
} from './string-parsers.js';

export {
string,
Failure,
Language,
LanguageSource,
LazyContext,
LazyParserOpts,
ParserContext,
ParserHandler,
Result,
ResultType,
ResultTypes,
StrictParserOpts,
Success,
Parser,
alt,
any,
cr,
crlf,
eof,
failure,
language,
lazy,
lf,
lineBegin,
lineEnd,
match,
newline,
notMatch,
parser,
seq,
sof,
succeeded,
success,
token,
where,
};

import * as tokenParsers from './token-parsers.js';

export {
tokenParsers,
};
Loading

0 comments on commit f40ba71

Please sign in to comment.