Skip to content

Commit

Permalink
Johannes' 1st sketch for Typir (#2)
Browse files Browse the repository at this point in the history
1st prototype, applied to OX
  • Loading branch information
JohannesMeierSE authored May 14, 2024
1 parent 543bded commit 4fb70e5
Show file tree
Hide file tree
Showing 60 changed files with 7,932 additions and 2,736 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
insert_final_newline = true
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.js
75 changes: 75 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint",
"header"
],
"ignorePatterns": [
"**/{node_modules,lib,bin}"
],
"rules": {
// List of [ESLint rules](https://eslint.org/docs/rules/)
"arrow-parens": ["off", "as-needed"], // do not force arrow function parentheses
"constructor-super": "error", // checks the correct use of super() in sub-classes
"dot-notation": "error", // obj.a instead of obj['a'] when possible
"eqeqeq": "error", // ban '==', don't use 'smart' option!
"guard-for-in": "error", // needs obj.hasOwnProperty(key) checks
"new-parens": "error", // new Error() instead of new Error
"no-bitwise": "error", // bitwise operators &, | can be confused with &&, ||
"no-caller": "error", // ECMAScript deprecated arguments.caller and arguments.callee
"no-cond-assign": "error", // assignments if (a = '1') are error-prone
"no-debugger": "error", // disallow debugger; statements
"no-eval": "error", // eval is considered unsafe
"no-inner-declarations": "off", // we need to have 'namespace' functions when using TS 'export ='
"no-labels": "error", // GOTO is only used in BASIC ;)
"no-multiple-empty-lines": ["error", {"max": 3}], // two or more empty lines need to be fused to one
"no-new-wrappers": "error", // there is no reason to wrap primitve values
"no-throw-literal": "error", // only throw Error but no objects {}
"no-trailing-spaces": "error", // trim end of lines
"no-unsafe-finally": "error", // safe try/catch/finally behavior
"no-var": "error", // use const and let instead of var
"space-before-function-paren": ["error", { // space in function decl: f() vs async () => {}
"anonymous": "never",
"asyncArrow": "always",
"named": "never"
}],
"semi": [2, "always"], // Always use semicolons at end of statement
"quotes": [2, "single", { "avoidEscape": true }], // Prefer single quotes
"use-isnan": "error", // isNaN(i) Number.isNaN(i) instead of i === NaN
"header/header": [ // Use MIT/Generated file header
2,
"block",
{ "pattern": "MIT License|DO NOT EDIT MANUALLY!" }
],
// List of [@typescript-eslint rules](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#supported-rules)
"@typescript-eslint/adjacent-overload-signatures": "error", // grouping same method names
"@typescript-eslint/array-type": ["error", { // string[] instead of Array<string>
"default": "array-simple"
}],
"@typescript-eslint/ban-types": "error", // bans types like String in favor of string
"@typescript-eslint/no-inferrable-types": "off", // don't blame decls like "index: number = 0", esp. in api signatures!
"@typescript-eslint/indent": "error", // consistent indentation
//"@typescript-eslint/no-explicit-any": "error", // don't use :any type
"@typescript-eslint/no-misused-new": "error", // no constructors for interfaces or new for classes
"@typescript-eslint/no-namespace": "off", // disallow the use of custom TypeScript modules and namespaces
"@typescript-eslint/no-non-null-assertion": "off", // allow ! operator
"@typescript-eslint/parameter-properties": "error", // no property definitions in class constructors
"@typescript-eslint/no-unused-vars": ["error", { // disallow Unused Variables
"argsIgnorePattern": "^_"
}],
"@typescript-eslint/no-var-requires": "error", // use import instead of require
"@typescript-eslint/prefer-for-of": "error", // prefer for-of loop over arrays
"@typescript-eslint/prefer-namespace-keyword": "error", // prefer namespace over module in TypeScript
"@typescript-eslint/triple-slash-reference": "error", // ban /// <reference />, prefer imports
"@typescript-eslint/type-annotation-spacing": "error" // consistent space around colon ':'
}
}
14 changes: 10 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/node_modules
/out
/**/generated
.DS_Store
*/dbg
*/dbg
.vscode-test/
coverage/
dist/
lib/
out/
node_modules/
*.vsix
*.tsbuildinfo
generated/
31 changes: 26 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"${workspaceFolder}/examples"
"--extensionDevelopmentPath=${workspaceFolder}/examples/ox",
"${workspaceFolder}/examples/ox/examples"
]
},
{
Expand All @@ -24,9 +24,30 @@
],
"sourceMaps": true,
"outFiles": [
"${workspaceFolder}/out/**/*.js",
"${workspaceFolder}/node_modules/langium/**/*.js"
"${workspaceFolder}/examples/ox/out/**/*.js",
"${workspaceFolder}/examples/ox/node_modules/langium/**/*.js"
]
},
{
"name": "Vitest: Run All",
"type": "node",
"request": "launch",
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
"args": ["run", "--no-color", "--no-coverage", "--no-watch"],
"smartStep": true,
"console": "integratedTerminal",
},
{
"name": "Vitest: Run Selected File",
"type": "node",
"request": "launch",
"autoAttachChildProcesses": true,
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
"args": ["run", "${relativeFile}"],
"smartStep": true,
"console": "integratedTerminal",
}
]
}
}
54 changes: 0 additions & 54 deletions esbuild.mjs

This file was deleted.

32 changes: 32 additions & 0 deletions examples/ox/esbuild.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/******************************************************************************
* Copyright 2024 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/

//@ts-check
import * as esbuild from 'esbuild';

const watch = process.argv.includes('--watch');

const ctx = await esbuild.context({
entryPoints: ['src/extension/main.ts', 'src/language/main.ts'],
outdir: 'out',
outExtension: {
'.js': '.cjs'
},
bundle: true,
target: 'ES2017',
format: 'cjs',
loader: { '.ts': 'ts' },
external: ['vscode'],
platform: 'node',
sourcemap: true
});

if (watch) {
await ctx.watch();
} else {
await ctx.rebuild();
ctx.dispose();
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion langium-config.json → examples/ox/langium-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"grammar": "src/language/ox.langium",
"fileExtensions": [".ox"],
"textMate": {
"out": "syntaxes/ox.tmLanguage.json"
"out": "./syntaxes/ox.tmLanguage.json"
}
}],
"out": "src/language/generated"
Expand Down
File renamed without changes.
80 changes: 80 additions & 0 deletions examples/ox/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"name": "ox",
"version": "0.0.1",
"displayName": "Ox",
"description": "Please enter a brief description here",
"author": {
"name": "TypeFox",
"url": "https://www.typefox.io"
},
"license": "MIT",
"type": "module",
"engines": {
"vscode": "^1.67.0"
},
"volta": {
"node": "18.17.1",
"npm": "9.6.7"
},
"scripts": {
"clean": "shx rm -rf lib out coverage",
"build": "tsc -b tsconfig.json && node esbuild.mjs",
"watch": "concurrently -n tsc,esbuild -c blue,yellow \"tsc -b tsconfig.json --watch\" \"node esbuild.mjs --watch\"",
"build:clean": "npm run clean && npm run build",
"test": "vitest",
"test-ui": "vitest --ui",
"coverage": "vitest run --coverage",
"lint": "eslint src --ext ts",
"langium:generate": "langium generate",
"langium:watch": "langium generate --watch",
"vscode:prepublish": "npm run build && npm run lint"
},
"dependencies": {
"chalk": "~5.3.0",
"commander": "~11.0.0",
"langium": "~3.0.0",
"vscode-languageclient": "~9.0.1",
"vscode-languageserver": "~9.0.1",
"typir": "~0.0.1"
},
"devDependencies": {
"langium-cli": "~3.0.0"
},
"files": [
"bin",
"out",
"src",
"syntaxes",
"language-configuration.json"
],
"categories": [
"Programming Languages"
],
"contributes": {
"languages": [
{
"id": "ox",
"aliases": [
"Ox",
"ox"
],
"extensions": [".ox"],
"configuration": "./language-configuration.json"
}
],
"grammars": [
{
"language": "ox",
"scopeName": "source.ox",
"path": "./syntaxes/ox.tmLanguage.json"
}
]
},
"activationEvents": [
"onLanguage:ox"
],
"main": "./out/extension/main.cjs",
"bin": {
"ox-cli": "./bin/cli.js"
}
}
13 changes: 10 additions & 3 deletions src/cli/cli-util.ts → examples/ox/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import type { AstNode, LangiumDocument, LangiumServices } from 'langium';
/******************************************************************************
* Copyright 2024 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import type { AstNode, LangiumCoreServices, LangiumDocument } from 'langium';
import chalk from 'chalk';
import * as path from 'node:path';
import * as fs from 'node:fs';
import { URI } from 'langium';
import { LangiumServices } from 'langium/lsp';

export async function extractDocument(fileName: string, services: LangiumServices): Promise<LangiumDocument> {
export async function extractDocument(fileName: string, services: LangiumCoreServices): Promise<LangiumDocument> {
const extensions = services.LanguageMetaData.fileExtensions;
if (!extensions.includes(path.extname(fileName))) {
console.error(chalk.yellow(`Please choose a file with one of these extensions: ${extensions}.`));
Expand All @@ -16,7 +23,7 @@ export async function extractDocument(fileName: string, services: LangiumService
process.exit(1);
}

const document = services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI.file(path.resolve(fileName)));
const document = await services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI.file(path.resolve(fileName)));
await services.shared.workspace.DocumentBuilder.build([document], { validation: true });

const validationErrors = (document.diagnostics ?? []).filter(e => e.severity === 1);
Expand Down
6 changes: 6 additions & 0 deletions src/cli/main.ts → examples/ox/src/cli/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/******************************************************************************
* Copyright 2024 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import { Command } from 'commander';
import { OxLanguageMetaData } from '../language/generated/module.js';
import { extractDestinationAndName } from './cli-util.js';
Expand Down
6 changes: 6 additions & 0 deletions src/extension/main.ts → examples/ox/src/extension/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/******************************************************************************
* Copyright 2024 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import type { LanguageClientOptions, ServerOptions} from 'vscode-languageclient/node.js';
import * as vscode from 'vscode';
import * as path from 'node:path';
Expand Down
8 changes: 7 additions & 1 deletion src/language/main.ts → examples/ox/src/language/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { startLanguageServer } from 'langium';
/******************************************************************************
* Copyright 2024 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import { startLanguageServer } from 'langium/lsp';
import { NodeFileSystem } from 'langium/node';
import { createConnection, ProposedFeatures } from 'vscode-languageserver/node.js';
import { createOxServices } from './ox-module.js';
Expand Down
Loading

0 comments on commit 4fb70e5

Please sign in to comment.