Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consistent range syntax parsing, #791 #794

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/parser/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,11 @@ export class Tokenizer {
if (this.peek() !== '(') return
++this.p
const lhs = this.readValueOrThrow()
this.p += 2
this.skipBlank()
this.assert(this.read() === '.' && this.read() === '.', 'invalid range syntax')
const rhs = this.readValueOrThrow()
++this.p
this.skipBlank()
this.assert(this.read() === ')', 'invalid range syntax')
return new RangeToken(this.input, begin, this.p, lhs, rhs, this.file)
}

Expand Down
29 changes: 24 additions & 5 deletions src/render/expression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ describe('Expression', function () {
expect(await toPromise(create('"foo"').evaluate(ctx, false))).toBe('foo')
expect(await toPromise(create('false').evaluate(ctx, false))).toBe(false)
})
it('should eval range expression', async function () {
const ctx = new Context({ two: 2 })
expect(await toPromise(create('(2..4)').evaluate(ctx, false))).toEqual([2, 3, 4])
expect(await toPromise(create('(two..4)').evaluate(ctx, false))).toEqual([2, 3, 4])
})
it('should eval literal', async function () {
expect(await toPromise(create('2.4').evaluate(ctx, false))).toBe(2.4)
expect(await toPromise(create('"foo"').evaluate(ctx, false))).toBe('foo')
Expand Down Expand Up @@ -221,6 +216,30 @@ describe('Expression', function () {
})
})

describe('range', function () {
const ctx = new Context({ two: 2, num: { one: 1, two: 2 } })
it('should eval range expression', async function () {
expect(await toPromise(create('(2..4)').evaluate(ctx, false))).toEqual([2, 3, 4])
expect(await toPromise(create('(two..4)').evaluate(ctx, false))).toEqual([2, 3, 4])
})
it('should allow property access expression as variables', async function () {
expect(await toPromise(create('(num.one..num.two)').evaluate(ctx))).toEqual([1, 2])
expect(await toPromise(create('(num.one .. two)').evaluate(ctx))).toEqual([1, 2])
})
it('should allow blanks in range', async function () {
expect(await toPromise(create('(3 ..5)').evaluate(ctx))).toEqual([3, 4, 5])
expect(await toPromise(create('(3 .. 5)').evaluate(ctx))).toEqual([3, 4, 5])
expect(await toPromise(create('( 3 .. 5 )').evaluate(ctx))).toEqual([3, 4, 5])
})
it('should throw if .. not matched', async function () {
expect(() => create('(3.5')).toThrow('invalid range syntax')
expect(() => create('(3 5')).toThrow('invalid range syntax')
})
it('should throw if ( not patched', async function () {
expect(() => create('(3..5')).toThrow('invalid range syntax')
})
})

describe('sync', function () {
it('should eval literal', function () {
expect(toValueSync(create('2.4').evaluate(ctx, false))).toBe(2.4)
Expand Down
Loading