Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Langium binding and more #15

Merged
merged 15 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:

jobs:
build:
name: monaco-languageclient
name: typir-build
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
Expand All @@ -33,10 +33,10 @@ jobs:
run: |
npm run build

- name: Lint
shell: bash
run: |
npm run lint
# - name: Lint
# shell: bash
# run: |
# npm run lint

- name: Test
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion examples/lox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dependencies": {
"commander": "~12.1.0",
"langium": "~3.2.0",
"typir": "~0.0.1",
"typir-langium": "~0.0.1",
"vscode-languageclient": "~9.0.1",
"vscode-languageserver": "~9.0.1"
},
Expand Down
12 changes: 9 additions & 3 deletions examples/lox/src/language/lox-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { DefaultSharedCoreModuleContext, LangiumCoreServices, LangiumSharedCoreS
import { LoxGeneratedModule, LoxGeneratedSharedModule } from './generated/module.js';
import { LoxScopeProvider } from './lox-scope.js';
import { LoxValidationRegistry, LoxValidator } from './lox-validator.js';
import { DefaultSharedModuleContext, LangiumSharedServices, createDefaultSharedModule } from 'langium/lsp';
import { DefaultSharedModuleContext, LangiumServices, LangiumSharedServices, createDefaultSharedModule } from 'langium/lsp';
import { createLangiumModuleForTypirBinding, initializeLangiumTypirServices, LangiumServicesForTypirBinding } from 'typir-langium';
import { createLoxTypirModule } from './type-system/lox-type-checking.js';
import { registerValidationChecks } from 'langium/grammar';

/**
* Declaration of custom services - add your own service classes here.
Expand All @@ -23,7 +26,7 @@ export type LoxAddedServices = {
* Union of Langium default services and your custom services - use this as constructor parameter
* of custom service classes.
*/
export type LoxServices = LangiumCoreServices & LoxAddedServices
export type LoxServices = LangiumServices & LoxAddedServices & LangiumServicesForTypirBinding

/**
* Dependency injection module that overrides Langium default services and contributes the
Expand Down Expand Up @@ -66,8 +69,11 @@ export function createLoxServices(context: DefaultSharedModuleContext): {
const Lox = inject(
createDefaultCoreModule({ shared }),
LoxGeneratedModule,
LoxModule
createLangiumModuleForTypirBinding(shared),
LoxModule,
createLoxTypirModule(shared),
);
shared.ServiceRegistry.register(Lox);
initializeLangiumTypirServices(Lox);
return { shared, Lox };
}
26 changes: 5 additions & 21 deletions examples/lox/src/language/lox-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import { AstNode, AstUtils, ValidationAcceptor, ValidationChecks, ValidationRegistry } from 'langium';
import { BinaryExpression, Class, ExpressionBlock, FunctionDeclaration, isReturnStatement, LoxAstType, LoxProgram, MethodMember, TypeReference, UnaryExpression, VariableDeclaration } from './generated/ast.js';
import { AstNode, ValidationAcceptor, ValidationChecks, ValidationRegistry } from 'langium';
import { BinaryExpression, LoxAstType, VariableDeclaration } from './generated/ast.js';
import type { LoxServices } from './lox-module.js';
import { isAssignable } from './type-system/assignment.js';
import { isVoidType, TypeDescription, typeToString } from './type-system/descriptions.js';
import { TypeDescription } from './type-system/descriptions.js';
import { inferType } from './type-system/infer.js';
import { isLegalOperation } from './type-system/operator.js';
import { createTypir } from './type-system/lox-type-checking.js';

/**
* Registry for validation checks.
Expand All @@ -23,31 +21,17 @@ export class LoxValidationRegistry extends ValidationRegistry {
const checks: ValidationChecks<LoxAstType> = {
BinaryExpression: validator.checkBinaryOperationAllowed,
VariableDeclaration: validator.checkVariableDeclaration,
LoxProgram: validator.checkTypingProblemsWithTypir,
};
this.register(checks, validator);
}
}

/**
* Implementation of custom validations.
* Implementation of custom validations on the syntactic level (which can be checked without using Typir).
* Validations on type level are done by Typir.
*/
export class LoxValidator {

checkTypingProblemsWithTypir(node: LoxProgram, accept: ValidationAcceptor) {
// executes all checks, which are directly derived from the current Typir configuration,
// i.e. arguments fit to parameters for function calls (including operands for operators)
const typir = createTypir(node);
AstUtils.streamAllContents(node).forEach(node => {
// print all found problems for each AST node
const typeProblems = typir.validation.collector.validate(node);
for (const problem of typeProblems) {
const message = typir.printer.printValidationProblem(problem);
accept(problem.severity, message, { node, property: problem.domainProperty, index: problem.domainIndex });
}
});
}

checkVariableDeclaration(decl: VariableDeclaration, accept: ValidationAcceptor): void {
if (!decl.type && !decl.value) {
accept('error', 'Variables require a type hint or an assignment at creation', {
Expand Down
Loading