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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/**
* Grammar Generator
*
* Converts collected user information into Tree-sitter grammar.js files
* with paradigm-aware rule generation
*/
import { LanguageArchitecture, LanguageFeatures, LanguageSyntax } from '../commands/new.js';
export interface GrammarRule {
name: string;
definition: string;
comment?: string;
}
export interface GeneratedGrammar {
name: string;
rules: GrammarRule[];
extras: string[];
conflicts: string[][];
precedences: string[][];
word?: string;
}
/**
* Generate a complete Tree-sitter grammar from user specifications
*/
export function generateGrammar(
architecture: LanguageArchitecture,
features: LanguageFeatures,
syntax: LanguageSyntax
): GeneratedGrammar {
const rules: GrammarRule[] = [];
const extras: string[] = [];
// Start with the root rule - this varies by paradigm
rules.push(generateRootRule(architecture, features));
// Add basic token rules
rules.push(...generateTokenRules(syntax));
// Add paradigm-specific rules
rules.push(...generateParadigmRules(architecture, features, syntax));
// Add data structure rules if specified
if (features.dataStructures.length > 0) {
rules.push(...generateDataStructureRules(features.dataStructures));
}
// Add control flow rules if specified
if (features.controlFlow.length > 0) {
rules.push(...generateControlFlowRules(features.controlFlow, syntax));
}
// Set up extras (whitespace and comments)
extras.push('/\\s/', `$.${getCommentRuleName(syntax.comments.pattern)}`);
return {
name: architecture.name,
rules,
extras,
conflicts: [], // TODO: Add conflicts if needed
precedences: generatePrecedences(architecture, features),
word: 'identifier' // Most languages use identifier as word token
};
}
/**
* Generate the root rule based on language paradigm
*/
function generateRootRule(architecture: LanguageArchitecture, features: LanguageFeatures): GrammarRule {
let definition: string;
switch (architecture.paradigm) {
case 'declarative':
definition = 'repeat(choice($.rule_declaration, $.constraint, $.fact))';
break;
case 'functional':
definition = 'repeat(choice($.function_definition, $.expression, $.binding))';
break;
case 'object-oriented':
definition = 'repeat(choice($.class_definition, $.statement, $.expression))';
break;
case 'procedural':
case 'mixed':
default:
definition = 'repeat(choice($.statement, $.expression, $.declaration))';
break;
}
return {
name: 'source_file',
definition,
comment: `Root rule for ${architecture.paradigm} language`
};
}
/**
* Generate basic token rules (identifiers, numbers, strings, comments)
*/
function generateTokenRules(syntax: LanguageSyntax): GrammarRule[] {
const rules: GrammarRule[] = [];
// Identifier
rules.push({
name: 'identifier',
definition: `/${syntax.identifiers.pattern}/`,
comment: `Identifiers: ${syntax.identifiers.examples.join(', ')}`
});
// Numbers
rules.push({
name: 'number',
definition: `/${syntax.numbers.pattern}/`,
comment: `Numbers: ${syntax.numbers.examples.join(', ')}`
});
// Strings
rules.push({
name: 'string',
definition: `/${syntax.strings.pattern}/`,
comment: `Strings: ${syntax.strings.examples.join(', ')}`
});
// Comments
const commentRuleName = getCommentRuleName(syntax.comments.pattern);
rules.push({
name: commentRuleName,
definition: `/${escapeRegex(syntax.comments.pattern)}.*$/`,
comment: `Line comments starting with ${syntax.comments.pattern}`
});
return rules;
}
/**
* Generate paradigm-specific rules
*/
function generateParadigmRules(
architecture: LanguageArchitecture,
features: LanguageFeatures,
syntax: LanguageSyntax
): GrammarRule[] {
const rules: GrammarRule[] = [];
// Variable declarations (common to most paradigms)
rules.push({
name: 'variable_declaration',
definition: `seq("${syntax.variables.keyword}", $.identifier, "${syntax.variables.operator}", $.expression, "${syntax.variables.terminator}")`,
comment: `Variable declarations: ${syntax.variables.example}`
});
// Expression rule (fundamental to all paradigms)
rules.push(generateExpressionRule(architecture, features));
// Statement rule (for imperative paradigms)
if (['procedural', 'object-oriented', 'mixed'].includes(architecture.paradigm)) {
rules.push(generateStatementRule(architecture, features));
}
// Add paradigm-specific constructs
switch (architecture.paradigm) {
case 'object-oriented':
if (syntax.paradigmExamples.class) {
rules.push(generateClassRule(syntax.paradigmExamples.class));
}
break;
case 'functional':
if (syntax.paradigmExamples.function) {
rules.push(generateFunctionRule(syntax.paradigmExamples.function, features.functionTypes));
}
break;
case 'declarative':
if (syntax.paradigmExamples.rule) {
rules.push(generateDeclarativeRule(syntax.paradigmExamples.rule));
}
break;
}
return rules;
}
/**
* Generate expression rule based on paradigm
*/
function generateExpressionRule(architecture: LanguageArchitecture, features: LanguageFeatures): GrammarRule {
const choices = [
'$.identifier',
'$.number',
'$.string',
'$.parenthesized_expression'
];
// Add function calls if functions are supported
if (features.functionTypes.length > 0) {
choices.push('$.function_call');
}
// Add data structure literals
if (features.dataStructures.includes('arrays')) {
choices.push('$.array_literal');
}
if (features.dataStructures.includes('objects')) {
choices.push('$.object_literal');
}
// Add binary operations for most paradigms
if (architecture.paradigm !== 'declarative') {
choices.push('$.binary_expression');
}
return {
name: 'expression',
definition: `choice(${choices.join(', ')})`,
comment: 'Expression rule covering all expression types'
};
}
/**
* Generate statement rule for imperative paradigms
*/
function generateStatementRule(architecture: LanguageArchitecture, features: LanguageFeatures): GrammarRule {
const choices = [
'$.variable_declaration',
'$.expression_statement'
];
// Add control flow statements
if (features.controlFlow.includes('conditionals')) {
choices.push('$.if_statement');
}
if (features.controlFlow.includes('loops')) {
choices.push('$.for_statement', '$.while_statement');
}
return {
name: 'statement',
definition: `choice(${choices.join(', ')})`,
comment: 'Statement rule for imperative constructs'
};
}
/**
* Generate data structure rules
*/
function generateDataStructureRules(dataStructures: string[]): GrammarRule[] {
const rules: GrammarRule[] = [];
if (dataStructures.includes('arrays')) {
rules.push({
name: 'array_literal',
definition: 'seq("[", optional(seq($.expression, repeat(seq(",", $.expression)))), "]")',
comment: 'Array literals: [1, 2, 3]'
});
}
if (dataStructures.includes('objects')) {
rules.push({
name: 'object_literal',
definition: 'seq("{", optional(seq($.property, repeat(seq(",", $.property)))), "}")',
comment: 'Object literals: {key: value}'
});
rules.push({
name: 'property',
definition: 'seq(choice($.identifier, $.string), ":", $.expression)',
comment: 'Object property: key: value'
});
}
if (dataStructures.includes('tuples')) {
rules.push({
name: 'tuple_literal',
definition: 'seq("(", $.expression, repeat1(seq(",", $.expression)), ")")',
comment: 'Tuple literals: (a, b, c)'
});
}
return rules;
}
/**
* Generate control flow rules
*/
function generateControlFlowRules(controlFlow: string[], syntax: LanguageSyntax): GrammarRule[] {
const rules: GrammarRule[] = [];
if (controlFlow.includes('conditionals')) {
rules.push({
name: 'if_statement',
definition: 'seq("if", "(", $.expression, ")", $.block, optional(seq("else", choice($.if_statement, $.block))))',
comment: 'If-else statements'
});
rules.push({
name: 'block',
definition: 'seq("{", repeat($.statement), "}")',
comment: 'Code blocks'
});
}
if (controlFlow.includes('loops')) {
rules.push({
name: 'while_statement',
definition: 'seq("while", "(", $.expression, ")", $.block)',
comment: 'While loops'
});
rules.push({
name: 'for_statement',
definition: 'seq("for", "(", optional($.statement), ";", optional($.expression), ";", optional($.expression), ")", $.block)',
comment: 'For loops'
});
}
return rules;
}
/**
* Generate class rule from user example
*/
function generateClassRule(classExample: string): GrammarRule {
// Simple class rule - could be enhanced with more parsing
return {
name: 'class_definition',
definition: 'seq("class", $.identifier, "{", repeat($.method_definition), "}")',
comment: `Class definition based on: ${classExample}`
};
}
/**
* Generate function rule from user example
*/
function generateFunctionRule(functionExample: string, functionTypes: string[]): GrammarRule {
let definition = 'seq("function", $.identifier, "(", optional($.parameter_list), ")", $.block)';
// Add arrow functions if supported
if (functionTypes.includes('anonymous')) {
definition = `choice(${definition}, $.arrow_function)`;
}
return {
name: 'function_definition',
definition,
comment: `Function definition based on: ${functionExample}`
};
}
/**
* Generate declarative rule from user example
*/
function generateDeclarativeRule(ruleExample: string): GrammarRule {
return {
name: 'rule_declaration',
definition: 'seq("rule", $.identifier, optional(seq("when", $.expression)))',
comment: `Rule declaration based on: ${ruleExample}`
};
}
/**
* Generate precedences based on paradigm
*/
function generatePrecedences(architecture: LanguageArchitecture, features: LanguageFeatures): string[][] {
// Basic precedence for binary operations
const precedences = [
['$.binary_expression']
];
// Add function call precedence if functions are supported
if (features.functionTypes.length > 0) {
precedences.push(['$.function_call']);
}
return precedences;
}
/**
* Generate the complete grammar.js file content
*/
export function generateGrammarFile(grammar: GeneratedGrammar): string {
const lines: string[] = [];
lines.push('/**');
lines.push(` * Grammar for ${grammar.name}`);
lines.push(' * Generated by DSK (DSL Development Kit)');
lines.push(' */');
lines.push('');
lines.push('module.exports = grammar({');
lines.push(` name: '${grammar.name}',`);
lines.push('');
// Add word token if specified
if (grammar.word) {
lines.push(` word: $ => $.${grammar.word},`);
lines.push('');
}
// Add rules
lines.push(' rules: {');
grammar.rules.forEach((rule, index) => {
if (rule.comment) {
lines.push(` // ${rule.comment}`);
}
lines.push(` ${rule.name}: $ => ${rule.definition}${index < grammar.rules.length - 1 ? ',' : ''}`);
if (index < grammar.rules.length - 1) {
lines.push('');
}
});
lines.push(' }');
// Add extras
if (grammar.extras.length > 0) {
lines.push(',');
lines.push('');
lines.push(' extras: $ => [');
grammar.extras.forEach((extra, index) => {
lines.push(` ${extra}${index < grammar.extras.length - 1 ? ',' : ''}`);
});
lines.push(' ]');
}
// Add conflicts if any
if (grammar.conflicts.length > 0) {
lines.push(',');
lines.push('');
lines.push(' conflicts: $ => [');
grammar.conflicts.forEach((conflict, index) => {
lines.push(` [${conflict.join(', ')}]${index < grammar.conflicts.length - 1 ? ',' : ''}`);
});
lines.push(' ]');
}
// Add precedences if any
if (grammar.precedences.length > 0) {
lines.push(',');
lines.push('');
lines.push(' precedences: $ => [');
grammar.precedences.forEach((prec, index) => {
lines.push(` [${prec.join(', ')}]${index < grammar.precedences.length - 1 ? ',' : ''}`);
});
lines.push(' ]');
}
lines.push('});');
lines.push('');
return lines.join('\n');
}
/**
* Helper functions
*/
function getCommentRuleName(commentPattern: string): string {
switch (commentPattern) {
case '//': return 'line_comment_slash';
case '#': return 'line_comment_hash';
case ';': return 'line_comment_semicolon';
default: return 'line_comment';
}
}
function escapeRegex(pattern: string): string {
return pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
|