@@ -2,7 +2,7 @@ import {toAST} from '@ohm-js/miniohm-js/toAST.js';
22import test from 'ava' ;
33import * as ohm from 'ohm-js' ;
44
5- import { matchWithInput , wasmMatcherForGrammar } from './_helpers.js' ;
5+ import { wasmMatcherForGrammar } from './_helpers.js' ;
66
77const arithmetic = ohm . grammar ( `
88 Arithmetic {
@@ -26,23 +26,154 @@ const arithmetic = ohm.grammar(`
2626// eslint-disable-next-line ava/no-skip-test
2727test ( 'toAST basic' , async t => {
2828 const m = await wasmMatcherForGrammar ( arithmetic ) ;
29- const match = str => ( m . setInput ( str ) , m . match ( str ) ) ;
29+ m . setInput ( '10 + 20' ) ;
30+ let matchResult = m . match ( ) ;
31+ let ast = toAST ( matchResult , {
32+ AddExp_plus : {
33+ expr1 : 0 ,
34+ expr2 : 2 ,
35+ } ,
36+ } ) ;
37+ let expected = {
38+ expr1 : '10' ,
39+ expr2 : '20' ,
40+ type : 'AddExp_plus' ,
41+ } ;
42+ t . deepEqual ( ast , expected , 'proper AST with mapped properties' ) ;
3043
31- const r = match ( '10 + 20' ) ;
32- t . true ( r . succeeded ( ) ) ;
33- const ast = toAST ( r , {
44+ ast = toAST ( matchResult , {
3445 AddExp_plus : {
3546 expr1 : 0 ,
47+ op : 1 ,
3648 expr2 : 2 ,
3749 } ,
3850 } ) ;
39- t . deepEqual (
40- ast ,
41- {
42- expr1 : '10' ,
43- expr2 : '20' ,
51+ expected = {
52+ expr1 : '10' ,
53+ op : '+' ,
54+ expr2 : '20' ,
55+ type : 'AddExp_plus' ,
56+ } ;
57+ t . deepEqual ( ast , expected , 'proper AST with explicitly mapped property' ) ;
58+
59+ ast = toAST ( matchResult , {
60+ AddExp_plus : {
61+ 0 : 0 ,
62+ } ,
63+ } ) ;
64+ expected = {
65+ 0 : '10' ,
66+ type : 'AddExp_plus' ,
67+ } ;
68+ t . deepEqual ( ast , expected , 'proper AST with explicitly removed property' ) ;
69+
70+ ast = toAST ( matchResult , {
71+ AddExp_plus : {
72+ 0 : 0 ,
73+ type : undefined ,
74+ } ,
75+ } ) ;
76+ expected = {
77+ 0 : '10' ,
78+ } ;
79+ t . deepEqual ( ast , expected , 'proper AST with explicitly removed type' ) ;
80+
81+ ast = toAST ( matchResult , {
82+ AddExp_plus : {
83+ expr1 : 0 ,
84+ op : 'plus' ,
85+ expr2 : 2 ,
86+ } ,
87+ } ) ;
88+ expected = {
89+ expr1 : '10' ,
90+ op : 'plus' ,
91+ expr2 : '20' ,
92+ type : 'AddExp_plus' ,
93+ } ;
94+ t . deepEqual ( ast , expected , 'proper AST with static property' ) ;
95+
96+ ast = toAST ( matchResult , {
97+ AddExp_plus : {
98+ expr1 : Object ( 0 ) ,
99+ op : 'plus' ,
100+ expr2 : Object ( 2 ) ,
101+ } ,
102+ } ) ;
103+ expected = {
104+ expr1 : 0 ,
105+ op : 'plus' ,
106+ expr2 : 2 ,
107+ type : 'AddExp_plus' ,
108+ } ;
109+ t . deepEqual ( ast , expected , 'proper AST with boxed number property' ) ;
110+
111+ // ast = toAST(matchResult, {
112+ // AddExp_plus: {
113+ // expr1: 0,
114+ // expr2: 2,
115+ // str(children) {
116+ // return children
117+ // .map(function(child) {
118+ // return child.toAST(this.args.mapping);
119+ // }, this)
120+ // .join('');
121+ // },
122+ // },
123+ // });
124+ // expected = {
125+ // expr1: '10',
126+ // expr2: '20',
127+ // str: '10+20',
128+ // type: 'AddExp_plus',
129+ // };
130+ // t.deepEqual(ast, expected, 'proper AST with computed property');
131+
132+ m . setInput ( '10 + 20 - 30' ) ;
133+ matchResult = m . match ( ) ;
134+ ast = toAST ( matchResult , {
135+ AddExp_plus : 2 ,
136+ } ) ;
137+ expected = {
138+ 0 : '20' , // child 2 of AddExp_plus
139+ 2 : '30' ,
140+ type : 'AddExp_minus' ,
141+ } ;
142+ t . deepEqual ( ast , expected , 'proper AST with forwarded child node' ) ;
143+
144+ // const myToAST = node =>
145+ // toAST(node, {
146+ // AddExp_plus(expr1, _, expr2) {
147+ // expr1 = expr1.toAST(this.args.mapping);
148+ // expr2 = expr2.toAST(this.args.mapping);
149+ // return 'plus(' + expr1 + ', ' + expr2 + ')';
150+ // },
151+ // });
152+ // ast = myToAST(matchResult);
153+ // expected = {
154+ // 0: 'plus(10, 20)', // child 2 of AddExp_plus
155+ // 2: '30',
156+ // type: 'AddExp_minus',
157+ // };
158+ // t.deepEqual(ast, expected, 'proper AST with computed node/operation extension');
159+
160+ ast = toAST ( matchResult , {
161+ Exp : {
162+ type : 'Exp' ,
163+ 0 : 0 ,
164+ } ,
165+ } ) ;
166+ expected = {
167+ 0 : {
168+ 0 : {
169+ 0 : '10' ,
170+ 2 : '20' ,
44171 type : 'AddExp_plus' ,
45172 } ,
46- 'proper AST with mapped properties' ,
47- ) ;
173+ 2 : '30' ,
174+ type : 'AddExp_minus' ,
175+ } ,
176+ type : 'Exp' ,
177+ } ;
178+ t . deepEqual ( ast , expected , 'proper AST with explicity reintroduced node' ) ;
48179} ) ;
0 commit comments