Skip to content

@typedblite/language

Langium grammar, generated AST, and language services for TypeQLite — the TypeQL 3.x dialect that TypeDBLite speaks.

  • Grammar (src/typeqlite.langium) — the authoritative Langium grammar definition for TypeQLite 3.x.
  • Generated AST (src/generated/ast.ts) — typed node interfaces and is* / as* type guards for every grammar production, generated by langium generate and given doc comments by scripts/document-generated.mts (the langium-generate target runs both).
  • Module (typeqlite-module.ts) — Langium DI container with all TypeQLite services wired up.
  • Synchronous validation (typeqlite-validate-sync.ts, typeqlite-validator.ts) — parse-time constraint checks that run synchronously, as the runtime requires.
  • Validation codes (typeqlite-validation-codes.ts) — stable string identifiers for every validation rule; consumed by the runtime’s DiagnosticCode to avoid a parallel vocabulary.
  • Annotation admissibility (typeqlite-annotations.ts) — table of which annotations are valid at which positions (type, sub, trait, value type).
  • Scope provider (typeqlite-scope.ts) — resolves cross-references; role names are scoped to their relation, so two relations may each relate member.
  • Formatter (typeqlite-formatter.ts) — canonical formatting for .typeqlite files.
  • Semantic tokens (typeqlite-semantic-tokens.ts) — token classifications for editor syntax highlighting.
  • Editor services — a workspace-wide schema (typeqlite-scope-computation.ts), query-label resolution (typeqlite-labels.ts), outline and hover (typeqlite-navigation.ts), completion, rename and quick fixes. The VS Code extension runs them in a language server. 🔴 The runtime uses only the parser and validateSync, so a check registered in ValidationRegistry is a runtime rule too (ADR-008 D2).
  • TextMate grammar (syntaxes/typeqlite.tmLanguage.json, imported as @typedblite/language/typeqlite.tmLanguage.json) — for editors and highlighters that use TextMate grammars, and underneath the semantic tokens in one that has both. Generated by scripts/generate-textmate.mts from the grammar’s keywords, each scoped by what it is (typeqlite-textmate.ts), with the scopes TypeDB’s TypeQL grammar uses.
Terminal window
npm install @typedblite/language

Inside this repository it is a workspace dependency of @typedblite/runtime and of the VS Code extension. Build it with npm exec nx -- run typeqlite-language:build.

Parse, then validate. This is exactly what the runtime does with every statement:

import { EmptyFileSystem } from 'langium';
import { createTypeQLiteServices, validateSync, isEntityDef, type TypeQLiteFile } from '@typedblite/language';
const { TypeQLite } = createTypeQLiteServices(EmptyFileSystem);
const result = TypeQLite.parser.LangiumParser.parse<TypeQLiteFile>(`
define
entity person, owns name @key;
attribute name, value string;
`);
const syntaxErrors = [...result.lexerErrors, ...result.parserErrors]; // []
const diagnostics = validateSync(TypeQLite, result.value); // [] — every validator rule, synchronously
const entities = result.value.clauses
.flatMap((clause) => ('definitions' in clause ? clause.definitions : []))
.filter(isEntityDef); // [person]

result.value is the AST: typed node interfaces with is* type guards for every production. It is not linked: the parser path never builds a LangiumDocument, so cross-references are not resolved. Linking, scoping and the workspace index are for language-server use, where createTypeQLiteServices is given a real file system and Langium’s document builder.

Extension Use
.typeqlite Schema and query files
.tql Alias

Language ID: typeqlite

src/generated/ is build output and is not committed. The build, test and typecheck targets regenerate it first. After editing the grammar, regenerate it yourself so that tools running from source see the change:

Terminal window
npm exec nx -- run typeqlite-language:langium-generate

Commit the grammar (src/typeqlite.langium, src/terminals.langium), never the generated files. Adding a TypeQLite construct walks through the whole path.

Langium wraps every registered validation check in an async function internally. SyncValidationRegistry overrides that wrapper to keep the check’s own synchronous result synchronous. The runtime calls validateSync, which never returns a Promise. A rule that returns a Promise is surfaced as a validation error rather than silently awaited.

TypeQLite 3.x implements the subset required by real production schemas and by TypeDB’s behavior specification (typedblite-conformance/spec/). A construct is added only when a real caller needs it. The rule is:

Find the caller first. What is here is what callers use — not what TypeDB has.

Terminal window
npm exec nx -- run typeqlite-language:test

Test files live in test/ with .spec.ts extension. The spec-backgrounds suite (test/spec-backgrounds.spec.ts) uses the vendored TypeDB behavior specification as acceptance tests for the grammar.

TypeDBLite is an independent open-source project, built on TypeDB with thanks. It is not affiliated with, sponsored by, or endorsed by TypeDB Ltd. Released under the MPL-2.0 license.