-
Notifications
You must be signed in to change notification settings - Fork 9
/
generator.ts
199 lines (180 loc) · 6.55 KB
/
generator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {
makeGenerator,
makeEveryOtherGenerator,
NodeGenerators,
} from '../ast/index.js';
const generators: NodeGenerators = {
program: (node) => generate(node.wsStart) + generate(node.program),
preprocessor: (node) => generate(node.line) + generate(node._),
keyword: (node) => generate(node.token) + generate(node.whitespace),
precision: (node) =>
generate(node.prefix) + generate(node.qualifier) + generate(node.specifier),
// Statements
expression_statement: (node) =>
generate(node.expression) + generate(node.semi),
if_statement: (node) =>
generate(node.if) +
generate(node.lp) +
generate(node.condition) +
generate(node.rp) +
generate(node.body) +
generate(node.else),
switch_statement: (node) =>
generate(node.switch) +
generate(node.lp) +
generate(node.expression) +
generate(node.rp) +
generate(node.lb) +
generate(node.cases) +
generate(node.rb),
break_statement: (node) => generate(node.break) + generate(node.semi),
do_statement: (node) =>
generate(node.do) +
generate(node.body) +
generate(node.while) +
generate(node.lp) +
generate(node.expression) +
generate(node.rp) +
generate(node.semi),
continue_statement: (node) => generate(node.continue) + generate(node.semi),
return_statement: (node) =>
generate(node.return) + generate(node.expression) + generate(node.semi),
discard_statement: (node) => generate(node.discard) + generate(node.semi),
while_statement: (node) =>
generate(node.while) +
generate(node.lp) +
generate(node.condition) +
generate(node.rp) +
generate(node.body),
for_statement: (node) =>
generate(node.for) +
generate(node.lp) +
generate(node.init) +
generate(node.initSemi) +
generate(node.condition) +
generate(node.conditionSemi) +
generate(node.operation) +
generate(node.rp) +
generate(node.body),
condition_expression: (node) =>
generate(node.specified_type) +
generate(node.identifier) +
generate(node.operator) +
generate(node.initializer),
declaration_statement: (node) =>
generate(node.declaration) + generate(node.semi),
fully_specified_type: (node) =>
generate(node.qualifiers) + generate(node.specifier),
layout_qualifier: (node) =>
generate(node.layout) +
generate(node.lp) +
generateWithEveryOther(node.qualifiers, node.commas) +
generate(node.rp),
layout_qualifier_id: (node) =>
generate(node.identifier) +
generate(node.operator) +
generate(node.expression),
switch_case: (node) =>
generate(node.case) +
generate(node.test) +
generate(node.colon) +
generate(node.statements),
default_case: (node) =>
generate(node.default) + generate(node.colon) + generate(node.statements),
declaration: (node) =>
generate(node.identifier) +
generate(node.quantifier) +
generate(node.equal) +
generate(node.initializer),
declarator_list: (node) =>
generate(node.specified_type) +
generateWithEveryOther(node.declarations, node.commas),
type_specifier: (node) =>
generate(node.specifier) + generate(node.quantifier),
array_specifier: (node) =>
generate(node.lb) + generate(node.expression) + generate(node.rb),
identifier: (node) => node.identifier + generate(node.whitespace),
type_name: (node) => node.identifier + generate(node.whitespace),
function_header: (node) =>
generate(node.returnType) + generate(node.name) + generate(node.lp),
function_prototype: (node) =>
generate(node.header.returnType) +
generate(node.header.name) +
generate(node.header.lp) +
(node.parameters
? generateWithEveryOther(node.parameters, node.commas)
: '') +
generate(node.rp),
parameter_declaration: (node) =>
generate(node.qualifier) +
generate(node.specifier) +
generate(node.identifier) +
generate(node.quantifier),
compound_statement: (node) =>
generate(node.lb) + generate(node.statements) + generate(node.rb),
function: (node) => generate(node['prototype']) + generate(node.body),
function_call: (node) =>
generate(node.identifier) +
generate(node.lp) +
generate(node.args) +
generate(node.rp),
postfix: (node) => generate(node.expression) + generate(node.postfix),
quantifier: (node) =>
generate(node.lb) + generate(node.expression) + generate(node.rb),
quantified_identifier: (node) =>
generate(node.identifier) + generate(node.quantifier),
field_selection: (node) => generate(node.dot) + generate(node.selection),
subroutine_qualifier: (node) =>
generate(node.subroutine) +
generate(node.lp) +
generate(node.type_names) +
generate(node.commas) +
generate(node.rp),
assignment: (node) =>
generate(node.left) + generate(node.operator) + generate(node.right),
ternary: (node) =>
generate(node.expression) +
generate(node.question) +
generate(node.left) +
generate(node.colon) +
generate(node.right),
binary: (node) =>
generate(node.left) + generate(node.operator) + generate(node.right),
group: (node) =>
generate(node.lp) + generate(node.expression) + generate(node.rp),
unary: (node) => generate(node.operator) + generate(node.expression),
float_constant: (node) => generate(node.token) + generate(node.whitespace),
double_constant: (node) => generate(node.token) + generate(node.whitespace),
int_constant: (node) => generate(node.token) + generate(node.whitespace),
uint_constant: (node) => generate(node.token) + generate(node.whitespace),
bool_constant: (node) => generate(node.token) + generate(node.whitespace),
literal: (node) => generate(node.literal) + generate(node.whitespace),
struct: (node) =>
generate(node.struct) +
generate(node.typeName) +
generate(node.lb) +
generate(node.declarations) +
generate(node.rb),
struct_declaration: (node) =>
generate(node.declaration) + generate(node.semi),
interface_declarator: (node) =>
generate(node.qualifiers) +
generate(node.interface_type) +
generate(node.lp) +
generate(node.declarations) +
generate(node.rp) +
generate(node.identifier),
struct_declarator: (node) =>
generate(node.specified_type) +
generateWithEveryOther(node.declarations, node.commas),
initializer_list: (node) =>
generate(node.lb) +
generateWithEveryOther(node.initializers, node.commas) +
generate(node.rb),
qualifier_declarator: (node) =>
generate(node.qualifiers) +
generateWithEveryOther(node.declarations, node.commas),
};
const generate = makeGenerator(generators);
const generateWithEveryOther = makeEveryOtherGenerator(generate);
export default generate;