1- import { test } from 'vitest'
1+ import { test , describe , beforeAll } from 'vitest'
22import path from 'node:path'
33import { parseFile } from 'abnf'
44import peggy from 'peggy'
55
6- async function generateParser ( startRule = 'maml' ) {
6+ const allowedStartRules = [ 'maml' ]
7+
8+ async function generateParser ( ) {
79 const __dirname = new URL ( '.' , import . meta. url ) . pathname
810 const rules = await parseFile ( path . join ( __dirname , 'maml.abnf' ) )
911 const text = rules . toFormat ( )
10- const doParse = peggy . generate ( text , { startRule : 'maml' } ) ?. parse
12+ const doParse = peggy . generate ( text , { allowedStartRules } ) ?. parse
1113 if ( ! doParse ) throw new Error ( 'Parser generation failed' )
12- return ( input ) => {
14+ return ( input , startRule = 'maml' ) => {
1315 try {
14- doParse ( input , { grammarSource : 'input' } )
16+ return doParse ( input , { startRule , grammarSource : 'input' } )
1517 } catch ( e ) {
1618 if ( typeof e . format === 'function' ) {
1719 throw new SyntaxError ( e . format ( [
18- { source : 'input' , text : input } ,
20+ { source : 'input' , text : input } ,
1921 ] ) )
2022 } else {
2123 throw e
@@ -24,75 +26,77 @@ async function generateParser(startRule = 'maml') {
2426 }
2527}
2628
27- test ( 'simple' , async ( ) => {
28- const parse = await generateParser ( )
29- parse ( `null` )
30- parse ( `true` )
31- parse ( `false` )
29+ let parse
30+
31+ beforeAll ( async ( ) => {
32+ parse = await generateParser ( )
3233} )
3334
34- test ( 'object' , async ( ) => {
35- const parse = await generateParser ( )
36- parse ( `{"a":1,"b":2}` )
37- parse ( ` { "a" : 1 , "b" : 2 } ` )
38- parse ( `
35+ describe ( 'MAML v0.1' , ( ) => {
36+ test ( 'simple' , ( ) => {
37+ parse ( `null` )
38+ parse ( `true` )
39+ parse ( `false` )
40+ } )
41+
42+ test ( 'object' , ( ) => {
43+ parse ( `{"a":1,"b":2}` )
44+ parse ( ` { "a" : 1 , "b" : 2 } ` )
45+ parse ( `
3946 {
4047 a: 1
4148 b: 2
4249 }
4350 ` )
44- parse ( `
51+ parse ( `
4552 {
4653 a: 1,
4754 b: 2,
4855 }
4956 ` )
50- } )
57+ } )
5158
52- test ( 'object with comments' , async ( ) => {
53- const parse = await generateParser ( )
54- parse ( `
59+ test ( 'object with comments' , ( ) => {
60+ parse ( `
5561 # before
5662 {}
5763 # after
5864 ` )
59- parse ( `
65+ parse ( `
6066 { # before
6167 a: 1 # after
6268 b: 2 # after
6369 # again
6470 } # after
6571 ` )
66- } )
72+ } )
6773
68- test ( 'array' , async ( ) => {
69- const parse = await generateParser ( )
70- parse ( `[]` )
71- parse ( `[1,2,3]` )
72- parse ( `
74+ test ( 'array' , ( ) => {
75+ parse ( `[]` )
76+ parse ( `[1,2,3]` )
77+ parse ( `
7378 [
7479 1
7580 2
7681 3
7782 ]
7883 ` )
79- parse ( `
84+ parse ( `
8085 [
8186 1,
8287 2,
8388 3,
8489 ]
8590 ` )
86- } )
91+ } )
8792
88- test ( 'array with comments' , async ( ) => {
89- const parse = await generateParser ( )
90- parse ( `
93+ test ( 'array with comments' , ( ) => {
94+ parse ( `
9195 # before
9296 []
9397 # after
9498 ` )
95- parse ( `
99+ parse ( `
96100 [ # before
97101 1 # after
98102 2 # after
@@ -101,14 +105,13 @@ test('array with comments', async () => {
101105 # again
102106 ] # after
103107 ` )
104- } )
108+ } )
105109
106- test ( 'string' , async ( ) => {
107- const parse = await generateParser ( )
108- parse ( `""` )
109- parse ( `"a"` )
110+ test ( 'string' , ( ) => {
111+ parse ( `""` )
112+ parse ( `"a"` )
110113
111- // Testing multiline string is not possible,
112- // as peggy parser does not support lookaheads.
114+ // Testing multiline string is not possible,
115+ // as peggy parser does not support lookaheads.
116+ } )
113117} )
114-
0 commit comments