diff options
| author | elioat <{ID}+{username}@users.noreply.github.com> | 2025-08-12 15:41:43 -0400 |
|---|---|---|
| committer | elioat <{ID}+{username}@users.noreply.github.com> | 2025-08-12 15:41:43 -0400 |
| commit | c724e472b5777bd71b33ad66746fed4c4a4fe5cf (patch) | |
| tree | 1fec92c490b9ca680b5788277865629371cd3d45 /tree-sitter/dsk/dsk-cli | |
| parent | f9e33a05610b3939a1dde68d8b8f0f4207151705 (diff) | |
| download | tour-c724e472b5777bd71b33ad66746fed4c4a4fe5cf.tar.gz | |
*
Diffstat (limited to 'tree-sitter/dsk/dsk-cli')
7 files changed, 78 insertions, 13 deletions
diff --git a/tree-sitter/dsk/dsk-cli/src/commands/build.ts b/tree-sitter/dsk/dsk-cli/src/commands/build.ts index 681f435..811788f 100644 --- a/tree-sitter/dsk/dsk-cli/src/commands/build.ts +++ b/tree-sitter/dsk/dsk-cli/src/commands/build.ts @@ -55,6 +55,9 @@ async function runBuildProcess(options: { verbose?: boolean; skipC?: boolean; sk throw new Error('No grammar.js found. Are you in a DSL project directory?'); } + // Ensure CommonJS semantics locally so tree-sitter can load grammar.js (uses module.exports) + ensureCommonJsPackageJson(); + // Step 1: Generate parser with Tree-sitter console.log(chalk.blue('1️⃣ Generating parser with Tree-sitter...')); await generateParser(options.verbose); @@ -276,6 +279,27 @@ function getProjectName(): string { } /** + * Ensure the current directory is treated as CommonJS for Node resolution, + * so that tree-sitter can load `grammar.js` (which uses module.exports). + * If no local package.json exists, create a minimal one with "type": "commonjs". + */ +function ensureCommonJsPackageJson(): void { + const packageJsonPath = 'package.json'; + if (!existsSync(packageJsonPath)) { + const projectName = basename(process.cwd()); + const minimal = { + name: `${projectName}-dsl`, + private: true, + type: 'commonjs', + description: `DSL project for ${projectName} generated by DSK`, + license: 'MIT' + } as const; + writeFileSync(packageJsonPath, JSON.stringify(minimal, null, 2)); + console.log(chalk.gray(' Created local package.json with { "type": "commonjs" }')); + } +} + +/** * Generate C header file */ async function generateHeaderFile(projectName: string, includeDir: string): Promise<void> { diff --git a/tree-sitter/dsk/dsk-cli/src/commands/new.ts b/tree-sitter/dsk/dsk-cli/src/commands/new.ts index 757d4a1..1829a21 100644 --- a/tree-sitter/dsk/dsk-cli/src/commands/new.ts +++ b/tree-sitter/dsk/dsk-cli/src/commands/new.ts @@ -216,13 +216,14 @@ async function gatherSyntax(architecture: LanguageArchitecture, features: Langua const commentAnswer = await inquirer.prompt({ type: 'input', name: 'comment', - message: 'How do you write a single-line comment? (e.g., //, #, ;)', + message: 'How do you write a single-line comment? (e.g., //, #, --, ;)', default: '//' }); + const commentPrefix = String(commentAnswer.comment).trim().split(/\s+/)[0]; syntax.comments = { type: 'line_comment', - pattern: commentAnswer.comment + pattern: commentPrefix }; // Identifiers with inference @@ -241,13 +242,27 @@ async function gatherSyntax(architecture: LanguageArchitecture, features: Langua ['42', '3.14', '-17'] ); - // Strings with inference - syntax.strings = await gatherTokenWithInference( - 'string', - 'Provide an example of a string literal', - 'Provide examples of invalid strings (optional)', - ['"hello world"'] - ); + // Strings with inference (treat input as a single example, not space-split) + const stringValid = await inquirer.prompt({ + type: 'input', + name: 'examples', + message: 'Provide an example of a string literal', + default: '"hello world"' + }); + const stringInvalid = await inquirer.prompt({ + type: 'input', + name: 'examples', + message: 'Provide examples of invalid strings (optional)', + default: '' + }); + const stringValidExamples = stringValid.examples ? [String(stringValid.examples)] : ['"hello world"']; + const stringInvalidExamples = stringInvalid.examples ? [String(stringInvalid.examples)] : []; + const stringResult = inferPattern(stringValidExamples, stringInvalidExamples); + if (stringResult.pattern && stringResult.confidence > 0.7) { + syntax.strings = { pattern: stringResult.pattern.regex.source, examples: stringValidExamples }; + } else { + syntax.strings = { pattern: '"[^"]*"', examples: stringValidExamples }; + } // Variable declarations const varAnswer = await inquirer.prompt({ diff --git a/tree-sitter/dsk/dsk-cli/src/utils/inference.ts b/tree-sitter/dsk/dsk-cli/src/utils/inference.ts index a1a04f6..f49e176 100644 --- a/tree-sitter/dsk/dsk-cli/src/utils/inference.ts +++ b/tree-sitter/dsk/dsk-cli/src/utils/inference.ts @@ -33,6 +33,13 @@ export const DEFAULT_PATTERNS: TokenPattern[] = [ priority: 10 }, { + name: 'js_identifier', + description: 'JavaScript-style identifier (letters, digits, $, _, must start with letter/$/_)', + regex: /^[A-Za-z_$][A-Za-z0-9_$]*$/, + examples: ['x', 'var1', '$var', '_var', 'Var3', 'BananaFruitStand'], + priority: 11 + }, + { name: 'kebab_identifier', description: 'Kebab-case identifier (letters, digits, hyphens)', regex: /^[a-zA-Z][a-zA-Z0-9-]*[a-zA-Z0-9]$/, @@ -49,6 +56,13 @@ export const DEFAULT_PATTERNS: TokenPattern[] = [ // Numbers { + name: 'number_general', + description: 'Integer or floating point number (optional sign)', + regex: /^[+-]?(?:\d*\.\d+|\d+\.\d*|\d+)$/, + examples: ['1', '-7', '1.24', '10000', '+0.5', '2.'], + priority: 16 + }, + { name: 'integer', description: 'Integer number (optional sign, digits)', regex: /^[+-]?\d+$/, diff --git a/tree-sitter/dsk/dsk-cli/templates/default/corpus/examples.txt b/tree-sitter/dsk/dsk-cli/templates/default/corpus/examples.txt index 30d061a..94f199e 100644 --- a/tree-sitter/dsk/dsk-cli/templates/default/corpus/examples.txt +++ b/tree-sitter/dsk/dsk-cli/templates/default/corpus/examples.txt @@ -37,7 +37,7 @@ __VARIABLE_EXAMPLE__ Expressions ================================================================================ -String literal +String literal via variable declaration --- __VARIABLE_KEYWORD__ message __ASSIGNMENT_OPERATOR__ __STRING_EXAMPLE____TERMINATOR__ @@ -54,7 +54,7 @@ __VARIABLE_KEYWORD__ message __ASSIGNMENT_OPERATOR__ __STRING_EXAMPLE____TERMINA Numbers ================================================================================ -Integer +Integer via variable declaration --- __VARIABLE_KEYWORD__ count __ASSIGNMENT_OPERATOR__ __NUMBER_EXAMPLE____TERMINATOR__ @@ -71,7 +71,7 @@ __VARIABLE_KEYWORD__ count __ASSIGNMENT_OPERATOR__ __NUMBER_EXAMPLE____TERMINATO Identifiers ================================================================================ -Valid identifier +Valid identifier via variable declaration --- __VARIABLE_KEYWORD__ __IDENTIFIER_EXAMPLE__ __ASSIGNMENT_OPERATOR__ 42__TERMINATOR__ diff --git a/tree-sitter/dsk/dsk-cli/templates/default/examples/hello.__EXT__ b/tree-sitter/dsk/dsk-cli/templates/default/examples/hello.__EXT__ index 7d21ce8..07769f6 100644 --- a/tree-sitter/dsk/dsk-cli/templates/default/examples/hello.__EXT__ +++ b/tree-sitter/dsk/dsk-cli/templates/default/examples/hello.__EXT__ @@ -4,4 +4,4 @@ __PARADIGM_EXAMPLE__ __COMMENT_EXAMPLE__ More examples: __VARIABLE_EXAMPLE__ -__IDENTIFIER_EXAMPLE__ __ASSIGNMENT_OPERATOR__ __STRING_EXAMPLE____TERMINATOR__ +__VARIABLE_KEYWORD__ __IDENTIFIER_EXAMPLE__ __ASSIGNMENT_OPERATOR__ __STRING_EXAMPLE____TERMINATOR__ diff --git a/tree-sitter/dsk/dsk-cli/templates/default/package.json b/tree-sitter/dsk/dsk-cli/templates/default/package.json new file mode 100644 index 0000000..ab3d0c4 --- /dev/null +++ b/tree-sitter/dsk/dsk-cli/templates/default/package.json @@ -0,0 +1,9 @@ +{ + "name": "__DSL_NAME__-dsl", + "private": true, + "type": "commonjs", + "description": "DSL project for __DSL_NAME__ generated by DSK", + "license": "MIT" +} + + diff --git a/tree-sitter/dsk/dsk-cli/tsconfig.json b/tree-sitter/dsk/dsk-cli/tsconfig.json index edc3234..e1599f7 100644 --- a/tree-sitter/dsk/dsk-cli/tsconfig.json +++ b/tree-sitter/dsk/dsk-cli/tsconfig.json @@ -26,6 +26,9 @@ "noUnusedParameters": false, "noPropertyAccessFromIndexSignature": false }, + "include": [ + "src/**/*" + ], "exclude": [ "templates/**/*", "dist/**/*", |