Skip to content

Commit

Permalink
#88: TS: F.nop(), F.any(), F.eos()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-zozol committed Nov 11, 2017
1 parent c297a20 commit f50797c
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 40 deletions.
2 changes: 1 addition & 1 deletion integration-ts/examples/beginner/chars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const stream = Streams.ofString('abc');
const charsParser = C.char('a')
.then(C.char('b'))
.then(C.char('c'))
.then(F.eos.drop()); // End Of Stream ; droping its value, just checking it's here
.then(F.eos().drop()); // End Of Stream ; droping its value, just checking it's here
let charsParsing = charsParser.parse(stream);
assertEquals('abc', charsParsing.value.join(''), 'Chars parsing');

2 changes: 1 addition & 1 deletion integration-ts/examples/beginner/floor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {Streams, F, C, N} from '@robusta/trash'

let stream= Streams.ofString('|4.6|');
const floorCombinator = C.char('|').drop()
.then(N.numberLiteral) // we have ['|',4.6], we keep 4.6
.then(N.numberLiteral()) // we have ['|',4.6], we keep 4.6
.then(C.char('|').drop()) // we have [4.6, '|'], we keep 4.6
.map(x =>Math.floor(x));

Expand Down
2 changes: 1 addition & 1 deletion integration-ts/examples/beginner/hello-something.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {assertEquals, assertArrayEquals, assertTrue} from '../../assert';
const helloParser = C.string("Hello")
.then(C.char(' ').rep())
.then(C.char("'")).drop()
.then(C.letter.rep()) // keeping repeated ascii letters
.then(C.letter().rep()) // keeping repeated ascii letters
.then(C.char("'").drop()); // keeping previous letters

const parsing = helloParser.parse(Streams.ofString("Hello 'World'"));
Expand Down
2 changes: 1 addition & 1 deletion integration-ts/examples/beginner/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const s = Streams.ofString(document);
// numberLitteral defines any int or float number
// We expect a number, then eos: End Of Stream
// We use drop() because we don't need the value of F.eos, we only want 12
const numberParser = N.numberLiteral.then(F.eos.drop());
const numberParser = N.numberLiteral.then(F.eos().drop());
const parsing = numberParser.parse(s);

// If the parser reached the end of stream (F.eos) without rejection, parsing is accepted
Expand Down
25 changes: 25 additions & 0 deletions integration-ts/examples/flow/nop-any-eos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {Streams, F, C,Option, N, SingleParser} from '@robusta/trash'
import {assertFalse, assertTrue} from '../../assert';

function day() {
return C.stringIn(['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY']);
}

function a(){
return C.char('a');
}

const string = '-MONDAY-';



function combinator() {
return F.any().then(day()).then(F.nop()).then(F.any()).then(F.eos());
}

let stream = Streams.ofString(string);
let parsing = combinator().parse(stream);

assertTrue(parsing.isAccepted());
assertTrue(parsing.isConsumed());

6 changes: 3 additions & 3 deletions integration-ts/examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import './beginner/chars'
import './beginner/floor'
//import './beginner/floor'
import './beginner/hello-something'
import './beginner/number'
//import './beginner/number'
import './beginner/response'
import './flow/try-with-no-or'
import './flow/not'
import './flow/nop-any-eos'
import './lazy/transmission'



2 changes: 1 addition & 1 deletion integration-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "used to test npm publications",
"main": "index.js",
"dependencies": {
"@robusta/trash": "0.0.3",
"@robusta/trash": "0.0.4",
"axios": "^0.16.2"
},
"devDependencies": {
Expand Down
95 changes: 64 additions & 31 deletions masala-parser.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* Written by Nicolas Zozol
* Inspired by https://github.com/jon-hanson/parsecj
*/

export interface Unit {}

// Not needed
Expand Down Expand Up @@ -35,7 +33,8 @@ export interface Try<V, E> {
export interface List<T> {
size: number;
isEmpty: boolean;
array: Array<T>
array(): Array<T>
join(string:string): string;
}


Expand Down Expand Up @@ -75,6 +74,7 @@ export interface VoidResponse<T>extends Response<T> {

export interface Response<T> {
isAccepted(): boolean
isConsumed(): boolean
fold(accept, reject?): Response<T>;
map<Y>(f): Response<Y>;
flatMap<Y>(f): Response<Y>;
Expand All @@ -88,26 +88,39 @@ export interface ArrayParser<T> extends IParser<T> {
then<Y>(p: SingleParser<Y>): ArrayParser<T | Y>;
then(p: VoidParser): ArrayParser<T>;
map<Y> (f: (T) => Y): ArrayParser<Y>;
flatMap<Z> (f: () => Z): ArrayParser<T>;
filter(f: (T) => boolean): ArrayParser<T>
opt():ArrayParser<Option<T>>;
or<Y, P extends IParser<Y>>(p: P): ArrayParser<T>|P;
}

/* List with same values ; need bo call array() */
export interface ListParser<T> {
parse<X>(stream: Stream<X>, index?: number): Response<List<T>>;
export interface ListParser<T> extends IParser<T>{
parse<X>(stream: Stream<X>, index?: number): ListResponse<T>;
then<Y>(p: SingleParser<Y>): ArrayParser<T | Y>;
then(p: VoidParser): ListParser<T>;
map<Y> (f: (T) => Y): ListParser<Y>;
opt():ListParser<Option<T>>;
//
or<Y, P extends IParser<Y>>(p: P): ListParser<T>|P;
}

interface SingleParser<T> extends IParser<T> {
export interface SingleParser<T> extends IParser<T> {
parse<X>(stream: Stream<X>, index?: number): SingleResponse<T>;
then<Y>(p: SingleParser<Y>): ArrayParser<T | Y>;
then<Y>(p: ListParser<Y>): ListParser<T | Y>;
then(p: VoidParser): SingleParser<T>;
map<Y> (f: (T) => Y): SingleParser<Y>;

opt():SingleParser<Option<T>>;
//filter(f: () => boolean): SingleParser<T>
or<Y, P extends IParser<Y>>(p: P): SingleParser<T>|P;
}

interface VoidParser extends IParser<void> {
then<Y>(p: IParser<Y>): SingleParser<Y>;
then<Y>(p: SingleParser<Y>): SingleParser< Y>;
then<Y>(p: ListParser<Y>): ListParser<Y>;
opt():VoidParser;
or<Y, P extends IParser<Y>>(p: P): VoidParser|P;
}

/**
Expand All @@ -117,55 +130,75 @@ export interface IParser<T> {
// Needed because opt() won(t know if we have void, array or single
//then<Y>(p: IParser<Y>): IParser<any>;
map<Y> (f: (T) => Y): IParser<Y>;
flatMap<Y, P extends IParser<Y>>( builder:parserBuilder<Y,P> ):P;
drop(): VoidParser;
rep(): ListParser<T>;
thenReturns<Y>(obj:Y):SingleParser<Y>;
debug(s:string, b?:boolean);
//then<Y>(p: IParser<Y>): IParser<T|Y>;

/*flatMap<Y> (f: () => Y): IParser<Y>;
filter(f: (T) => boolean): IParser<T>
match(value: T): IParser<T>;
thenReturns<Y>(value: Y): SingleParser<Y>;
or<Y>(p: IParser<Y>): IParser<T | Y>;
opt(): IParser<T>;
rep(): ListParser<List<T>>;
occurrence(n: number): ListParser<T>;
optrep(): IParser<List<T>>;
chain<Y>(): IParser<Y>;
debug(hint?: string, details?: boolean): IParser<T>;
*/
// parse<X>(stream: Stream<X>, index?: number): Response<T>;
}

export declare class Parser<T> implements IParser <T> {
then<Y>(p: IParser<Y>): IParser<any>;
flatMap<Y>(f: () => Y): IParser<Y> ;
map<Y>(f: (T: any) => Y): IParser<Y>;
filter(f: (T: any) => boolean): IParser<T> ;
match(value: T): IParser<T> ;
drop(): VoidParser ;
thenReturns<Y>(value: Y): SingleParser<Y> ;
or<Y>(p: IParser<Y>): IParser<T | Y> ;
opt(): IParser<T> ;
rep(): ListParser<List<T>> ;
occurrence(n: number): ListParser<T> ;
optrep(): IParser<List<T>> ;
chain<Y>(): IParser<Y> ;
debug(hint?: string, details?: boolean): IParser<T>;
parse<X>(stream: Stream<X>, index?: number): Response<T> ;
}
/*
export declare class Parser<T> implements IParser <T> {
then<Y>(p: IParser<Y>): IParser<any>;
flatMap<Y>(f: () => Y): IParser<Y> ;
map<Y>(f: (T: any) => Y): IParser<Y>;
filter(f: (T: any) => boolean): IParser<T> ;
match(value: T): IParser<T> ;
drop(): VoidParser ;
thenReturns<Y>(value: Y): SingleParser<Y> ;
or<Y>(p: IParser<Y>): IParser<T | Y> ;
opt(): IParser<T> ;
rep(): ListParser<List<T>> ;
occurrence(n: number): ListParser<T> ;
optrep(): IParser<List<T>> ;
chain<Y>(): IParser<Y> ;
debug(hint?: string, details?: boolean): IParser<T>;
parse<X>(stream: Stream<X>, index?: number): Response<T> ;
}*/

export interface Response<T> {
isCompleted(): boolean
isAccepted(): boolean
fold(accept, reject?): Response<T>;
map(f): Response<T>;
flatMap(f): Response<T>;
filter(f: (value) => boolean): Response<T>;
offset: number;
}

interface CharBundle {
char(string): SingleParser<string>;
char(string:string): SingleParser<string>;
string(string:string): SingleParser<string>;
stringIn(strings:string[]): SingleParser<string>;
letter: SingleParser<string>;
}

export type parserBuilder<Y,P extends IParser<Y>> = (...rest:any[])=>P;

type extension<Y,T extends IParser<Y>> = T;

interface FlowBundle {
nop: () => void;
eos: SingleParser<Unit>;
parse<Y,P extends IParser<Y>>(P):P;
nop(): VoidParser;
try<Y,P extends IParser<Y>>(parser:P):P;
any():SingleParser<any>;
subStream(length:number):ListParser<any>
lazy<Y,P extends IParser<Y>> (builder: parserBuilder<Y,P>, args:any[]): P;
not<Y,P extends IParser<Y>>(P):SingleParser<any>;
eos(): SingleParser<Unit>;
}

interface NumberBundle {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@robusta/trash",
"description": "Masala Parser",
"license": "LGPL-2.1",
"version": "0.0.3",
"version": "0.0.4",
"keywords": [
"parser",
"javascript",
Expand Down

0 comments on commit f50797c

Please sign in to comment.