person <NAME> {
age: <INTEGER>;
rank: <INTEGER>;
}
<NAME>: 任意の名前,
半角小文字アルファベットのみ許容
<INTEGER>: 任意の整数
前提
OS: Arch Linux
Docker: Docker version 20.10.16, build aa7e414fdc
使用する Docker イメージ: node:18
作業用コンテナ起動
docker run -it--rm-v"$(pwd):/work"--workdir /work node:18 bash
Langium
のジェネレーターをインストール
npm i -g yo generator-langium
Langium のプロジェクトを作成
# su - node$ cd /work$ yo langium? ==========================================================================We're constantly looking for ways to make yo better! May we anonymously report usage statistics to improve the tool over time? More info: https://github.com/yeoman/insight & http://yeoman.io========================================================================== Yes┌─────┐ ─┐┌───┐ │ ╶─╮ ┌─╮ ╭─╮ ╷ ╷ ╷ ┌─┬─╮│ ,´ │ ╭─┤ │ │ │ │ │ │ │ │ │ ││╱ ╰─ ╰─┘ ╵ ╵ ╰─┤ ╵ ╰─╯ ╵ ╵ ╵` ╶─╯Welcome to Langium! This tool generates a VS Code extension with a "Hello World" language to get started quickly. The extension name is an identifier used in the extension marketplace or package registry.? Your extension name: firststepThe language name is used to identify your language in VS Code. Please provide a name to be shown in the UI. CamelCase and kebab-case variants will be created and used in different parts of the extension and language server.? Your language name: firststepSource files of your language are identified by their file name extension. You can specify multiple file extensions separated by commas.? File extensions: .firststep create firststep/langium-config.json create firststep/langium-quickstart.md create firststep/language-configuration.json create firststep/package.json create firststep/tsconfig.json create firststep/bin/cli create firststep/src/extension.ts create firststep/src/cli/cli-util.ts create firststep/src/cli/generator.ts create firststep/src/cli/index.ts create firststep/src/language-server/firststep-module.ts create firststep/src/language-server/firststep-validator.ts create firststep/src/language-server/firststep.langium create firststep/src/language-server/main.ts create firststep/.vscode/extensions.json create firststep/.vscode/launch.json create firststep/.eslintrc.json create firststep/.vscodeignoreadded 173 packages, and audited 174 packages in 23s27 packages are looking for funding run `npm fund` for detailsfound 0 vulnerabilitiesnpm notice npm notice New minor version of npm available! 8.9.0 -> 8.11.0npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.11.0npm notice Run npm install -g npm@8.11.0 to update!npm notice > firststep@0.0.1 langium:generate> langium generateReading config from langium-config.jsonsrc/language-server/firststep.langium:14:10 - This rule is declared but never referenced.src/language-server/firststep.langium:15:10 - This rule is declared but never referenced.Writing generated files to /work/firststep/src/language-server/generatedWriting textmate grammar to /work/firststep/syntaxes/firststep.tmLanguage.jsonLangium generator finished successfully in 218ms> firststep@0.0.1 build> tsc -b tsconfig.jsonNo change to package.json was detected. No package manager install will be executed.
diff --git a/firststep/src/cli/index.ts b/firststep/src/cli/index.tsindex 4322246..e0a4230 100644--- a/firststep/src/cli/index.ts+++ b/firststep/src/cli/index.ts@@ -1,9 +1,16 @@-//import colors from 'colors';+import colors from 'colors'; import { Command } from 'commander';-//import { Model } from '../language-server/generated/ast';-//import { FirststepLanguageMetaData } from '../language-server/generated/module';-//import { createFirststepServices } from '../language-server/firststep-module';-//import { extractAstNode } from './cli-util';+import { Model } from '../language-server/generated/ast';+import { FirststepLanguageMetaData } from '../language-server/generated/module';+import { createFirststepServices } from '../language-server/firststep-module';+import { extractAstNode } from './cli-util';++export const testAction = async (fileName: string, opts: GenerateOptions): Promise<void> => {+ const services = createFirststepServices().Firststep;+ await extractAstNode<Model>(fileName, services);+ console.log(colors.green(`文法チェック OK`));+ // ※ extractAstNode から呼ばれる `extractDocument` 内で、 `process.exit(1)` されるので、 try-catch しない+}; export type GenerateOptions = { destination?: string;@@ -16,7 +23,12 @@ export default function(): void { // eslint-disable-next-line @typescript-eslint/no-var-requires .version(require('../../package.json').version);- // const fileExtensions = FirststepLanguageMetaData.fileExtensions.join(', ');+ const fileExtensions = FirststepLanguageMetaData.fileExtensions.join(', ');+ program+ .command('test')+ .argument('<file>', `source file (possible file extensions: ${fileExtensions})`)+ .description('文法チェック')+ .action(testAction); program.parse(process.argv); }
動作確認
パーサーの動作確認
test.firststep ファイルを作って、先程実装した
test サブコマンドで確認する。
$ node bin/cli test ./test-ok.firststep文法チェック OK$ node bin/cli test ./test-ng.firststepThere are validation errors:line 1: Expecting keyword '{' but found `000`. [000]
[mikoto@letsarch ~]$ docker run -it--rm hello-worldHello from Docker!This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps:1. The Docker client contacted the Docker daemon.2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)3. The Docker daemon created a new container from that image which runs theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID:https://hub.docker.com/For more examples and ideas, visit:https://docs.docker.com/get-started/