diff options
| author | Akshay Nair <phenax5@gmail.com> | 2022-01-13 21:20:23 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2022-01-13 21:20:23 +0530 |
| commit | 016821207f1f2294dd09935a4c1d14d78ff3aed1 (patch) | |
| tree | f5f852cb94be77ceb5a97a0b6d62cb323090b00b /tests | |
| parent | e26086fef3cf142cffd145836a4c7c12cfeb9656 (diff) | |
| download | elxr-016821207f1f2294dd09935a4c1d14d78ff3aed1.tar.gz elxr-016821207f1f2294dd09935a4c1d14d78ff3aed1.zip | |
refactor(parser): reimplements alt parser
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/basic.spec.ts | 2 | ||||
| -rw-r--r-- | tests/eval.spec.ts | 33 | ||||
| -rw-r--r-- | tests/parser.spec.ts | 93 |
3 files changed, 60 insertions, 68 deletions
diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts index 2d7af37..9336841 100644 --- a/tests/basic.spec.ts +++ b/tests/basic.spec.ts @@ -17,7 +17,7 @@ describe('Basic tests', () => { // jlog(matchAll(/([age \n])+/, [ {}, { age: 1 }, { age: 2 }, { age: 0 }, '' ])) // jlog(matchAll(/\n+/, [ '', 1, 2, 0, '6', 5, '' ])) - //jlog(matchAll(/ -2.05 /, [ 2, -2.05, 5, -2.05, 2.05, 0.05, 'wow', '-2.05', '-2' ])) + jlog(matchAll(/ -2.05|2|true|\s|\T /, [ 2, -2.05, 5, -2.05, 2.05, 0.05, 'wow', '-2.05', '-2' ])) }) describe('matchAll', () => { diff --git a/tests/eval.spec.ts b/tests/eval.spec.ts index f4999b6..ab91948 100644 --- a/tests/eval.spec.ts +++ b/tests/eval.spec.ts @@ -10,13 +10,11 @@ describe('Eval', () => { const liexp: ListExpr = [ none, - [ - Expr.OneOrMore({ - expr: Expr.Group({ - exprs: [Expr.AnyNumber(), Expr.Truthy()], - }), + Expr.OneOrMore({ + expr: Expr.Group({ + exprs: [Expr.AnyNumber(), Expr.Truthy()], }), - ], + }), none, ] @@ -26,10 +24,10 @@ describe('Eval', () => { it('basic evaluation', () => { const list = [0, 1, '2', 3, [4], 5] - const liexp: ListExpr = [none, [Expr.AnyNumber(), Expr.Truthy()], none] + const liexp: ListExpr = [none, Expr.Group({ exprs: [Expr.AnyNumber(), Expr.Truthy()] }), none] expect(find(liexp, list)).toEqual([1, 3, 5]) - const liexp2: ListExpr = [none, [Expr.Falsey(), Expr.Truthy()], none] + const liexp2: ListExpr = [none, Expr.Group({ exprs: [Expr.Falsey(), Expr.Truthy()] }), none] expect(find(liexp2, list)).toEqual([]) }) @@ -38,10 +36,10 @@ describe('Eval', () => { const liexp: ListExpr = [ none, - [ + Expr.Group({ exprs: [ Expr.AnyItem(), Expr.Group({ exprs: [Expr.AnyNumber(), Expr.Truthy()] }), - ], + ] }), none, ] expect(find(liexp, list)).toEqual([1, 3, 5]) @@ -58,7 +56,7 @@ describe('Eval', () => { const liexp: ListExpr = [ none, - [ + Expr.Group({ exprs: [ Expr.PropertyMatch({ name: 'name', expr: Expr.Group({ exprs: [Expr.AnyString(), Expr.Truthy()] }), @@ -67,21 +65,10 @@ describe('Eval', () => { name: 'age', expr: Expr.Group({ exprs: [Expr.AnyNumber()] }), }), - ], + ] }), none, ] expect(find(liexp, list)).toEqual([{ name: 'gello', age: 20 }]) }) - - xit('with groups', () => { - const list = [0, 1, 1, 1, '2', 3, 4, [4], 5, ''] - - const liexp: ListExpr = [ - none, - [Expr.OneOrMore({ expr: Expr.AnyNumber() })], - none, - ] - expect(find(liexp, list)).toEqual([1, 3, 5]) - }) }) diff --git a/tests/parser.spec.ts b/tests/parser.spec.ts index bd71509..510e5bb 100644 --- a/tests/parser.spec.ts +++ b/tests/parser.spec.ts @@ -4,16 +4,27 @@ import { whitespace, whitespaces0, delimited, + sepBy1, + symbol, } from '../src/parser/utils' import { parser } from '../src/parser' import { none, some } from 'fp-ts/Option' import { Expr, Literal } from '../src/types' import { jlog } from '../src/utils' -const wrap = (l: any) => right([[none, l, none], '']) +const wrap = (e: Expr) => + right([[none, e, none], '']) +const grouped = (l: Expr[]) => + wrap(Expr.Group({ exprs: l })) +const groupedAlt = (l: Expr[]) => + wrap(Expr.Or({ exprs: l })) describe('Parser', () => { it('should do shit', () => { + // jlog(parser(/ -2.05|\n2|true|\s|\T /.source)) + }) + + it('should do shit', () => { expect(digits('12901')).toEqual(right(['12901', ''])) expect(digits('12901asas')).toEqual(right(['12901', 'asas'])) expect(whitespace(' ')).toEqual(right([' ', ''])) @@ -28,18 +39,19 @@ describe('Parser', () => { ) }) - it('should maybeshut', () => { - expect(parser(/^ .\s(\n)\b \T $/.source)).toEqual( + it('should compount expressions', () => { + expect(parser(/^ .\s(\n\b) \T $/.source)).toEqual( right([ [ some(Expr.Start()), - [ - Expr.AnyItem(), - Expr.AnyString(), - Expr.Group({ exprs: [Expr.AnyNumber()] }), - Expr.AnyBool(), - Expr.Truthy(), - ], + Expr.Group({ + exprs: [ + Expr.AnyItem(), + Expr.AnyString(), + Expr.Group({ exprs: [Expr.AnyNumber(), Expr.AnyBool()] }), + Expr.Truthy(), + ], + }), some(Expr.End()), ], '', @@ -50,48 +62,41 @@ describe('Parser', () => { right([ [ some(Expr.Start()), - [ - Expr.ZeroOrMore({ expr: Expr.AnyString() }), - Expr.Optional({ expr: Expr.Truthy() }), - Expr.OneOrMore({ expr: Expr.AnyNumber() }), - ], + Expr.Group({ + exprs: [ + Expr.ZeroOrMore({ expr: Expr.AnyString() }), + Expr.Optional({ expr: Expr.Truthy() }), + Expr.OneOrMore({ expr: Expr.AnyNumber() }), + ], + }), some(Expr.End()), ], '', ]), ) + }) + it('should or expressions', () => { expect(parser(/ \s|\b\T|\n /.source)).toEqual( - right([ - [ - none, - [ - Expr.Or({ - left: Expr.AnyString(), - right: [ - Expr.AnyBool(), - Expr.Or({ - left: Expr.Truthy(), - right: [Expr.AnyNumber()], - }), - ], - }), - ], - none, - ], - '', + groupedAlt([ + Expr.AnyString(), + Expr.Group({ exprs: [Expr.AnyBool(), Expr.Truthy()] }), + Expr.AnyNumber(), ]), ) }) it('object proprtyu', () => { expect(parser(/ [name \s\T] [age \n] /.source)).toEqual( - wrap([ + grouped([ Expr.PropertyMatch({ name: 'name', expr: Expr.Group({ exprs: [Expr.AnyString(), Expr.Truthy()] }), }), - Expr.PropertyMatch({ name: 'age', expr: Expr.Group({ exprs: [Expr.AnyNumber()] }) }), + Expr.PropertyMatch({ + name: 'age', + expr: Expr.AnyNumber(), + }), ]), ) }) @@ -99,35 +104,35 @@ describe('Parser', () => { it('literals', () => { // unsigned numbers expect(parser(/ 0.105 /.source)).toEqual( - wrap([Expr.Literal(Literal.Number(0.105))]), + wrap(Expr.Literal(Literal.Number(0.105))), ) expect(parser(/ 9 /.source)).toEqual( - wrap([Expr.Literal(Literal.Number(9))]), + wrap(Expr.Literal(Literal.Number(9))), ) expect(parser(/ 23 /.source)).toEqual( - wrap([Expr.Literal(Literal.Number(23))]), + wrap(Expr.Literal(Literal.Number(23))), ) // signed numbers expect(parser(/ +23.025 /.source)).toEqual( - wrap([Expr.Literal(Literal.Number(23.025))]), + wrap(Expr.Literal(Literal.Number(23.025))), ) expect(parser(/ -23.025 /.source)).toEqual( - wrap([Expr.Literal(Literal.Number(-23.025))]), + wrap(Expr.Literal(Literal.Number(-23.025))), ) expect(parser(/ +23 /.source)).toEqual( - wrap([Expr.Literal(Literal.Number(23))]), + wrap(Expr.Literal(Literal.Number(23))), ) expect(parser(/ -23 /.source)).toEqual( - wrap([Expr.Literal(Literal.Number(-23))]), + wrap(Expr.Literal(Literal.Number(-23))), ) // boolean expect(parser(/ true /.source)).toEqual( - wrap([Expr.Literal(Literal.Boolean(true))]), + wrap(Expr.Literal(Literal.Boolean(true))), ) expect(parser(/ false /.source)).toEqual( - wrap([Expr.Literal(Literal.Boolean(false))]), + wrap(Expr.Literal(Literal.Boolean(false))), ) }) }) |
