Skip to content

Commit

Permalink
Parser: list-pattern fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Apr 20, 2024
1 parent ddc1e6f commit 7d4203b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/parser/fns/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const parseConPattern = (parser: Parser): void => {
export const parseListPattern = (parser: Parser): void => {
const mark = parser.open()
parser.expect('o-bracket')
while (!parser.eof()) {
while (!parser.eof() && !parser.at('c-bracket')) {
parsePattern(parser)
if (!parser.at('c-bracket')) {
parser.expect('comma')
Expand Down
46 changes: 41 additions & 5 deletions src/parser/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('parser', () => {
return { tree: compactParseNode(tree), errors: p.errors }
}

describe('parse use-stmt', () => {
describe('use-stmt', () => {
it('nested', () => {
const { tree, errors } = parse('use std::iter::{self, Iter, Iterator}')
expect(errors).toEqual([])
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('parser', () => {
})
})

describe('parse fn-def', () => {
describe('fn-def', () => {
it('empty', () => {
const { tree, errors } = parse('fn main() {}')
expect(errors).toEqual([])
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('parser', () => {
})
})

describe('parse var-def', () => {
describe('var-def', () => {
it('miss identifier', () => {
const { errors } = parse('let = 4')
expect(errors.length).toEqual(1)
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('parser', () => {
})
})

describe('parse if-expr', () => {
describe('if-expr', () => {
it('general', () => {
const { tree, errors } = parse('if a { b } else { c }')
expect(errors).toEqual([])
Expand Down Expand Up @@ -207,7 +207,43 @@ describe('parser', () => {
})
})
})
describe('parse comments', () => {

describe('match', () => {
it('list-pattern', () => {
const { tree, errors } = parse('match a { [] {} [b, tail] {} }')
expect(errors).toEqual([])
// biome-ignore format: compact
expect(tree).toEqual(
{ module:
[ { statement:
[ { expr:
[ { 'sub-expr':
[ { operand:
[ { 'match-expr':
[ { 'match-keyword': 'match' },
{ expr: [ { 'sub-expr': [ { operand: [ { identifier: [ { name: 'a' } ] } ] } ] } ] },
{ 'match-clauses':
[ { 'o-brace': '{' },
{ 'match-clause':
[ { patterns: [ { pattern: [ { 'pattern-expr': [ { 'list-pattern': [ { 'o-bracket': '[' }, { 'c-bracket': ']' } ] } ] } ] } ] },
{ block: [ { 'o-brace': '{' }, { 'c-brace': '}' } ] } ] },
{ 'match-clause':
[ { patterns:
[ { pattern:
[ { 'pattern-expr':
[ { 'list-pattern':
[ { 'o-bracket': '[' },
{ pattern: [ { 'pattern-expr': [ { name: 'b' } ] } ] },
{ comma: ',' },
{ pattern: [ { 'pattern-expr': [ { name: 'tail' } ] } ] },
{ 'c-bracket': ']' } ] } ] } ] } ] },
{ block: [ { 'o-brace': '{' }, { 'c-brace': '}' } ] } ] },
{ 'c-brace': '}' } ] } ] } ] } ] } ] } ] } ] }
)
})
})

describe('comments', () => {
const { tree } = parse('foo\n// comment here\nbar')
// biome-ignore format: compact
expect(tree).toEqual(
Expand Down

0 comments on commit 7d4203b

Please sign in to comment.